NGINX is a popular web server known for its speed and reliability, used by many top websites. It handles lots of connections efficiently, making it a great choice for busy websites. To run websites with PHP, you need to set up PHP-FPM. NGINX works with PHP-FPM to process PHP files and display them to users, helping websites load faster and handle more traffic.

This tutorial will help you install and configure NGINX and PHP-FPM on Ubuntu 24.04 systems. It also covers creating virtual hosts and securing them with Let’s Encrypt SSL.

Setup NGINX with PHP-FPM on Ubuntu 24.04: Step-by-Step

This guide assumes you’re using Ubuntu 24.04. You’ll need to use the command line (terminal) and have an account with administrator privileges (sudo). A basic understanding of web servers is helpful.

Step 1: Update Your System

Keeping your computer’s software up-to-date is important for security and performance. Here’s how to do it on Ubuntu:

  1. Open the terminal application (usually found in the Applications menu).
  2. Type the following command and press Enter:
    sudo apt update
    

    This command tells your computer to download information about available updates.

  3. Once the update information is downloaded, type this command and press Enter:
    sudo apt upgrade
    

    This command installs the updates.

Step 2: Install NGINX Server

Now, let’s install the NGINX web server. Follow these steps:

  1. In the terminal, type the following command and press Enter:
    sudo apt install nginx
    

    This command installs NGINX on your system.

  2. Once the installation is complete, start the NGINX service by typing:
    sudo systemctl start nginx
    
  3. Enable NGINX to start on boot with the following command:
    sudo systemctl enable nginx
    
  4. Check the status of NGINX to ensure it’s running:
    sudo systemctl status nginx
    

Next, you’ll need to install and configure PHP-FPM to work with NGINX. This will allow NGINX to process PHP files.

Step 3: Install PHP-FPM

Follow these steps to install PHP-FPM:

  1. In the terminal, type the following command and press Enter:
    sudo apt install php-fpm
    
  2. Once the installation is complete, start the PHP-FPM service by typing:
    sudo systemctl start php-fpm
    
  3. Enable PHP-FPM to start on boot with the following command:
    sudo systemctl enable php-fpm
    
  4. Check the status of PHP-FPM to ensure it’s running:
    sudo systemctl status php-fpm
    

Step 4: Configure NGINX to Use PHP-FPM

Let’s create a new host file for your website and configure it to use PHP-FPM:

  1. Navigate to the NGINX sites-available directory:
    cd /etc/nginx/sites-available/
    
  2. Create a new configuration file for your website. Replace `example.com` with your domain name:
    sudo nano /etc/nginx/sites-available/example.com
    
  3. Add the following configuration to the file. Make sure to replace `example.com` with your actual domain name:
    
    server {
      listen 80;
      server_name example.com www.example.com;
    
      root /var/www/example.com/html;
      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/php7.4-fpm.sock;
      }
    
      location ~ /.ht {
        deny all;
      }
    }
    
    
  4. Save and close the file by pressing Ctrl X, then Y, and Enter.
  5. Create the document root directory for your website:
    sudo mkdir -p /var/www/example.com/html
    
  6. Assign ownership of the directory to the current user:
    sudo chown -R $USER:$USER /var/www/example.com/html
    
  7. Ensure the permissions are correct:
    sudo chmod -R 755 /var/www/example.com
    
  8. Create a simple PHP file to test your configuration:
    nano /var/www/example.com/html/index.php
    

    Add the following line to the file:

    
    
    
    

    Save and close the file.

  9. Enable your new website configuration by creating a symbolic link in the sites-enabled directory:
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    
  10. Test the NGINX configuration for syntax errors:
    sudo nginx -t
    
  11. If the test is successful, reload NGINX to apply the changes:
    sudo systemctl reload nginx
    
  12. Open your web browser and navigate to http://example.com. You should see the PHP info page, which means your NGINX server is correctly configured to use PHP-FPM.

Step 5: Secure Your Website with Let’s Encrypt SSL

To secure your website with HTTPS, you can use Let’s Encrypt SSL. Follow these steps:

  1. Install Certbot, the Let’s Encrypt client:
    sudo apt install certbot python3-certbot-nginx
    
  2. Run Certbot to obtain and install your SSL certificate:
    sudo certbot --nginx
    

    Follow the prompts to complete the installation.

  3. Verify Certbot auto-renewal by running a dry run:
    sudo certbot renew --dry-run
    

Your NGINX server is now set up with PHP-FPM and secured with Let’s Encrypt SSL. This setup ensures your website loads quickly and securely, handling more visitors efficiently.

Conclusion

By following this tutorial, you’ve successfully installed and configured NGINX and PHP-FPM on your Ubuntu 24.04 system. This setup allows your server to handle PHP files efficiently, improving your website’s speed and performance. Additionally, securing your site with Let’s Encrypt SSL ensures that your visitors’ data is protected, building trust and reliability. With your NGINX server now optimized and secure, your website is ready to handle increased traffic and deliver a fast, secure browsing experience to users.