How to install Nginx, PHP, LetsEncrypt SSL on Ubuntu cheat sheet

Image without description

Here are quick&dirty notes on how to install Nginx web server, PHP and LetsEncrypt SSL certificate on Ubuntu server. Tested with the following versions: nginx 1.20.2, php 8.1.7, ubuntu 20.04.

By default Nginx is not capable of processing dynamic web pages with PHP so it requires the use of specific additional plugins. This is resolved by adding the php-fpm (FastCGI Process Manager) package. First of all, we get rid of the old php versions:

sudo apt-get purge php7.*

In the preparation phase, we will update the Ubuntu system packages and install some dependencies.

sudo apt-get update && sudo apt-get install ca-certificates apt-transport-https software-properties-common

Let's start by installing the Nginx server, adding PHP support and finally the SSL certificate in 4 main steps.

1. Install Nginx

sudo apt-get install nginx -y

Check the running process with the command

sudo systemctl status nginx

Important. Do not forget to configure your Ubuntu server's firewall and close unused open ports.

2. Add Ondrej PHP PPA repository and install PHP 8.1

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update && sudo apt-get install php8.1-fpm

Check the running process with the command

sudo systemctl status php8.1-fpm

3. Add PHP support to Nginx

Open the file "/etc/nginx/sites-available/default" in a text editor

  • Add this (replace the entire file with this)
server {
  # Example PHP Nginx FPM config file
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/html;

  # Add index.php to setup Nginx, PHP & PHP-FPM config
  index index.php index.html index.htm index.nginx-debian.html;

  server_name _;

  location / {
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # Nginx php-fpm sock config:
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    # Nginx php-cgi config :
    # Nginx PHP fastcgi_pass 127.0.0.1:9000;
  }

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
  location ~ /\.ht {
    deny all;
  }
} # End of PHP FPM Nginx config example

Server and php setup done. Check first

sudo nginx -t

Then restart Nginx (and php-fpm) using this command

sudo systemctl restart nginx

Check the functionality/features of PHP with a browser. Create an info page and browse it

sudo chmod -R 777 /var/www/html

echo "<?php phpinfo(); ?>" >> /var/www/html/info.php

4. Secure Nginx with LetsEncrypt SSL free certificate

sudo apt install python3-certbot-nginx -y

Get a certificate and redirect from http to https

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@example.com -d www.example.com

Tadaa! Applausi

Ready. Should be up&rocking. If you encounter any problems please comment through Disqus below. It will help make this cheat sheet better and updated. However, the guide has not been updated for obsolete versions.

// viHannes, 26 jun 2022 in Helsinki, Finland