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 24 installed on your server
Root access to the server
A domain name pointing to your server’s IP address
Table of Contents
Step 1: Add PPA for PHP 8.1/8.4
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/8.2/8.3/8.4
Now, install PHP 8.1 along with the required PHP modules using the following command:
sudo apt install php8.4 php8.4-fpm php8.4-mbstring php8.4-mysql php8.4-xml php8.4-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.4/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 php84.atiqur.xyz;
root /var/www/atiq;
index 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.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/atiqur /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 OSs, you need to install SNAPD first.
Then run the following command to install “core”
sudo apt install snapd
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
It will ask for your email address for the first time. Please provide your email address and select “Yes” for all the options.
This will set up the SSL certificate for you using Letsencrypt.
This SSL certificate is valid for 90 days only. To renew the SSL certificate automatically set up a corn job which is given below.
sudo crontab -e
0 3 * * * sudo certbot renew >/dev/null 2>&1
The command above will run this renewal command every morning at 3 AM.
I also created a full video so you can see how I can run those commands.
Conclusion
We hope this article has given you the tips and tools to successfully set up SSL with PHP 8.4 and Nginxin AWS. If you have any questions about what we discussed or simply want to say hello, feel free to leave a comment below.