How to install PHP 8.1 + Nginx in Ubuntu 20 with SSL via Letsencrypt

PHP is a popular programming language used for web development. Nginx is a high-performance web server that can be used as a reverse proxy, load balancer, and HTTP cache. SSL (Secure Sockets Layer) is a security protocol that provides secure communication over the internet. In this blog, we will go through the steps to install PHP 8.1 with Nginx in Ubuntu 20 and secure it with SSL via Letsencrypt.


Before we start, we need to have the following prerequisites:

Ubuntu 20 installed on your server
Root access to the server
A domain name pointing to your server’s IP address

Step 1: Add PPA for PHP 8.1

The first step is to add the PPA for PHP 8.1 to the Ubuntu package repository. To do this, run the following command:

Step 2: Update package repository

After adding the PPA, update the package repository using the following command:

Step 3: Install PHP 8.1

Now, install PHP 8.1 along with the required PHP modules using the following command:

Copy code
sudo apt install php8.1 php8.1-fpm php8.1-mbstring php8.1-mysql php8.1-xml php8.1-zip

Step 4: Configure PHP-FPM

Next, we need to configure PHP-FPM to work with Nginx. Edit the PHP-FPM configuration file using the following command:

Copy code
sudo nano /etc/php/8.1/fpm/pool.d/www.conf
Find the following lines and uncomment them:

Copy code
listen.owner = www-data = www-data
listen.mode = 0660

Step 5: Install nginx

sudo apt install nginx 

Step 5: Configure Nginx

Now, we need to configure Nginx to work with PHP-FPM. Create a new Nginx configuration file for your domain using the following command:

sudo nano /etc/nginx/sites-available/
server {
    listen 80;
    root /var/www/atiq;

    index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;

    location ~ /\.ht {
        deny all;


sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
sudo nginx -t

In Ubuntu, Snapd is already installed. So you don’t need to do anything here. But for other OS, you need to first install SNAPD.

Then run the following command to install “core”

sudo snap install core; sudo snap refresh core

Next, install the certbot using the following command.

sudo snap install --classic certbot

Once you have installed the certbot then it’s time to issue the SSL certificate by running the following command.

sudo certbot --nginx

Leave a Comment

Your email address will not be published. Required fields are marked *