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.

Prerequisites:

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:

sudo add-apt-repository ppa:ondrej/php

Step 2: Update the package repository

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

sudo apt update -y

Step 3: Install PHP 8.1

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

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

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:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf


Find the following lines and uncomment them:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Step 5: Install nginx

sudo apt install nginx  -y

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/atiqur
server {
    listen 80;
    server_name fpm.atiqur.xyz;
    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/yourdomain.com /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

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply