Photo by Clker-Free-Vector-Images

Introduction

In this tutorial, we will guide you through the process of installing and configuring Nginx with PHP-FPM on Ubuntu 22.04. Nginx is a high-performance web server that is known for its speed and scalability. PHP-FPM, on the other hand, is a FastCGI Process Manager that allows Nginx to process PHP files efficiently. By combining these two technologies, you can create a powerful web server setup that can handle high traffic and deliver excellent performance.

Prerequisites

Before we begin, make sure you have the following:

  • A fresh installation of Ubuntu 22.04.
  • User privileges: either root access or a non-root user with sudo privileges.

Step 1: Update the System

The first step is to update the system to ensure that you have the latest packages and security patches. Open the terminal and run the following commands:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Next, we will install Nginx using the following command:

sudo apt install nginx -y

Once the installation is complete, start and enable the Nginx service:

sudo systemctl start nginx

sudo systemctl enable nginx

Step 3: Install PHP-FPM

Now it’s time to install PHP-FPM and the necessary PHP extensions. Run the following commands:

sudo apt install php php-fpm php-mysql php-zip php-gd php-mbstring php-curl php-xml -y

Once the installation is finished, start and enable the PHP-FPM service:

sudo systemctl start php7.4-fpm

sudo systemctl enable php7.4-fpm

Step 4: Configure Nginx to Use PHP-FPM

To configure Nginx to use PHP-FPM, we need to modify the Nginx configuration file. Open the file using your preferred text editor:

sudo nano /etc/nginx/sites-available/default

Inside the server block, locate the location ~ .php$ block and modify it as follows:

<code>location ~ .php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi_params;                
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}</code>Code language: PHP (php)

Save the file and exit the text editor. Next, test the Nginx configuration for any syntax errors:

sudo nginx -t

If there are no errors, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 5: Test PHP-FPM with Nginx

To test if PHP-FPM is working correctly with Nginx, create a new PHP file in the document root directory. For example:

sudo nano /var/www/html/info.php

Add the following PHP code to the file:

<?php phpinfo(); ?>

Save the file and exit the text editor. Now, open your web browser and navigate to http://your_server_ip/info.php. You should see the PHP information page, which confirms that PHP-FPM is working properly with Nginx. Once confirmed, be sure to delete the file. It can help an attacker identify specific tech versions used, so better safe than sorry!

Step 6: Additional Configuration (Optional)

Depending on your specific needs, you may want to make additional configurations to optimize the performance and security of your Nginx and PHP-FPM setup. Here are a few suggestions:

Enable HTTPS (SSL/TLS)

If you want to secure your website with HTTPS, you can obtain an SSL certificate from a trusted certificate authority and configure Nginx to use HTTPS. This will encrypt the communication between the client and the server, providing an extra layer of security.

Enable Caching

Nginx has built-in caching capabilities that can significantly improve the performance of your website. By caching static content, you can reduce the load on your server and deliver content to users more quickly. You can configure Nginx to cache specific files or entire pages, depending on your requirements.

Configure Security Measures

To enhance the security of your server, consider implementing security measures such as securing SSH access, using strong passwords, and configuring a firewall. Additionally, you can enable security modules in Nginx to protect against common web vulnerabilities, such as cross-site scripting (XSS) and SQL injection.

Conclusion

By following this tutorial, you have successfully installed and configured Nginx with PHP-FPM on Ubuntu 22.04. This tutorial was created as part of our Docker LEMP/LNMP series, so stay tuned or if you’re here because of that series, hope you found this helpful!

By Zeekz

Leave a Reply

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