The article provides a comprehensive guide to setting up Nginx with PHP support and securing it with SSL certificates using Certbot on an Ubuntu 24.04 server. This installation will be using:
PHP - 8.3.6
NGINX - 1.24.0
LINUX - 24.04
Nginx + PHP
Via your linux terminal, you will use the command line to update the system packages. You will use apt to do this, here is the code to do so in your terminal:
apt update
Nginx and PHP-FPM offer a robust and efficient solution for running PHP applications. Their combined strengths create a high-performing, secure, and scalable web server environment, after updating your package list you can install PHP-FPM with this line of code:
apt install php-fpm
Once PHP-FPM and Nginx are installed you now need to configure Nginx to use PHP-FPM. You will need to create the Nginx configuration file for your domain, you can do this by using vim or nano to edit the desired path which will also create the file when something is saved within the file:
vim /etc/nginx/sites-available/yourdomain.com
Once you have created this configuration file with vim or nano, you will then paste the following configuration into the file, replacing yourdomain.com with your actual domain name:
listen 80;
server_name yourdomain.com;
root /var/www/html/yourdomain.com; # Update with your website root directory
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Update with your PHP-FPM version
}
location ~ /\.ht {
deny all;
}
}
This Nginx configuration instructs the server to handle web traffic for a specific domain, serving static content directly and directing PHP requests to a separate PHP-FPM process for efficient execution. Also make sure the following php version is correct, if 8.3 is incorrect or you are unsure, you will need update and change the location of “fastcgi_pass unix:” After this you will then need to create a symbolic link to enable the Nginx configuration for your domain:
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
You can then test the Nginx Configuration to check for errors:
nginx -t
If everything was successful you can apply the changes with:
systemctl restart nginx
Optionally, you can create a test PHP file (‘info.php’) in the website root directory to verify PHP is functional, add the following code:
<?php
phpinfo();
?>
Access yourdomain.com/info.php or localhost/info.php, if setup correctly you should see the PHP information page.
SSL + Certbot
Via your linux terminal, you will use the command line to update the system packages. You will use apt to do this, here is the code to do so in your terminal:
apt update
After doing so you will need to install Certbot and the Certbot Nginx plugin, Certbot gets and sets up Let’s Encrypt SSL certificates for your Nginx web server which is why it is needed:
apt install certbot python3-certbot-nginx -y
After this you can run Certbot with Nginx to obtain and configure the SSL/TLS certificates from Let’s Encrypt for your domain. An SSL encrypts data between a client and server, ensuring secure and private communication.This will involve entering your email, agreeing to terms, and selecting the domain name:
certbot --nginx
Make sure to test the Nginx configuration after the previous process just to double check:
nginx -t
Once done with this you can restart Nginx to apply the SSL configuration changes and you now have a SSL certificate.
systemctl restart nginx
Summary
The article provides a straightforward guide to set up Nginx with PHP support and secure it using SSL certificates with Certbot on an Ubuntu 24.04 server. It covers updating packages, installing PHP-FPM and Nginx, configuring Nginx for PHP, obtaining SSL certificates with Certbot, and restarting Nginx for the changes to take effect. This process ensures a secure and functional Nginx server capable of handling PHP and SSL/TLS encryption.