UbuntuUbuntu Based

Boost Your Website’s Performance: How To Configure PHP-FPM on Ubuntu 22.04 LTS

How To Configure PHP-FPM on Ubuntu 22.04

In this tutorial, we will show you how to configure PHP-FPM on Ubuntu 22.04 LTS. For those of you who didn’t know, In the world of web development, PHP is a popular server-side scripting language used to create dynamic websites and web applications. PHP-FPM (FastCGI Process Manager) is a PHP FastCGI implementation that allows web servers like Nginx to handle PHP requests efficiently. In this blog post, we will explore how to configure PHP-FPM on Ubuntu 22.04 LTS and get the most out of this powerful tool.

We will cover a range of topics, including configuring Nginx to use PHP-FPM, customizing PHP-FPM pools for optimal performance, fine-tuning PHP-FPM settings to suit your specific needs, monitoring PHP-FPM performance with tools like PM2, and securing PHP-FPM and Nginx to protect your server from potential security threats.

If you are new to PHP-FPM or want to learn more about its capabilities, this blog post will provide a comprehensive guide to getting started with PHP-FPM on Ubuntu 22.04 LTS. By the end of this post, you will have a solid understanding of how to configure and optimize PHP-FPM for your web applications, ensuring they run smoothly and efficiently. So, let’s get started!

Configure PHP-FPM on Ubuntu 22.04

Installing Nginx and PHP-FPM on Ubuntu 22.04

Firstly, let’s start by installing Nginx and PHP-FPM on Ubuntu 22.04. You can install both packages by running the following command in your terminal:

sudo apt install nginx php-fpm

After the installation, you can verify the PHP-FPM service is running by running the following command:

systemctl status php-fpm

If PHP-FPM is running, you should see the status message indicating that the service is active.

For additional resources on installing Nginx, read the post below:

Configure PHP-FPM and Nginx on Ubuntu

Once Nginx and PHP-FPM are installed, you will need to configure Nginx to work with PHP-FPM. To do this, you will need to edit the Nginx configuration file.

The Nginx configuration file is located at /etc/nginx/sites-available/default. Open this file using your favorite text editor:

nano /etc/nginx/sites-available/default

In this file, find the following lines:

index index.html index.htm;

Add index.php to the list of index files:

index index.html index.htm index.php;

Next, find the following lines:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
#       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       # fastcgi_pass 127.0.0.1:9000;
#}

Uncomment these lines and modify the fastcgi_pass line to point to the PHP-FPM socket file:

location ~ \.php$ {
       include snippets/fastcgi-php.conf;

       # With php-fpm (or other unix sockets):
       fastcgi_pass unix:/var/run/php/php-fpm.sock;
       # With php-cgi (or other tcp sockets):
       # fastcgi_pass 127.0.0.1:9000;
}

Save and close the file, then restart Nginx to apply the changes:

sudo systemctl restart nginx

Customizing PHP-FPM Pools

By default, PHP-FPM creates a single pool of processes to handle all PHP requests. However, you can create additional pools for different PHP applications or virtual hosts.

To create a new PHP-FPM pool, you will need to create a new configuration file in the /etc/php/7.4/fpm/pool.d/ directory. You can create a copy of the default pool configuration file and modify it to suit your needs:

cp /etc/php/7.4/fpm/pool.d/www.conf /etc/php/7.4/fpm/pool.d/idroot.conf

Open the new file for editing:

nano /etc/php/7.4/fpm/pool.d/idroot.conf

Change the pool name, listen address and port, user and group, and other settings as needed:

[idroot]

user = nginx
group = nginx

listen = 127.0.0.1:9000

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

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 5

php_admin_value[upload_max_filesize] = 10M
php_admin_value[post_max_size] = 10M

In this example, we have created a new PHP-FPM pool called “idroot” that listens on port 9000 and runs as the “nginx” user and “nginx” group. We have also set some custom PHP configuration values using the php_admin_value directive.

Save and close the file, then restart the PHP-FPM service to apply the changes:

sudo systemctl restart php7.4-fpm

Fine-tuning PHP-FPM Settings

In addition to configuring the PHP-FPM pools, it’s also possible to fine-tune various PHP-FPM settings to optimize its performance. The following are some of the key settings that you can adjust:

  • pm.max_children: This setting defines the maximum number of child processes that can be spawned per pool. You should set this value based on your server’s available resources and the amount of traffic your site receives.
  • pm.start_servers: This setting defines the number of child processes that should be started when the PHP-FPM service starts. You should set this value based on the expected traffic load on your site.
  • pm.min_spare_servers and pm.max_spare_servers: These settings define the minimum and maximum number of idle child processes that should be kept running to handle incoming requests. You should set these values based on the expected traffic load on your site.
  • pm.max_requests: This setting defines the number of requests that a child process should handle before being terminated and replaced with a new process. This can help prevent memory leaks and other issues that can arise from long-running processes.
  • request_terminate_timeout: This setting defines the maximum amount of time that a request can take before being terminated. This can help prevent long-running or stalled requests from tying up resources and impacting the overall performance of the PHP-FPM service.

To adjust these settings, you’ll need to edit the www.conf file for each PHP-FPM pool that you’ve created. This file is typically located in the /etc/php/7.4/fpm/pool.d/ directory.

For example, adjust the pm.max_children setting for the example.com pool, you would add the following line to the www.conf file:

pm.max_children = 50

Similarly, adjusting the pm.start_servers, pm.min_spare_servers, and pm.max_spare_servers settings, you would add the following lines:

pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

To adjust the pm.max_requests and request_terminate_timeout settings, you would add the following lines:

pm.max_requests = 1000
request_terminate_timeout = 60s

After making any changes to the PHP-FPM settings, you should restart the PHP-FPM service to ensure that the changes take effect:

sudo systemctl restart php7.4-fpm

By fine-tuning these settings, you can optimize the performance of your PHP-FPM service and ensure that it can handle the expected traffic load on your site.

Monitoring PHP-FPM Performance

Once PHP-FPM is up and running, you should monitor its performance regularly to ensure it is running smoothly and efficiently. Some of the tools and techniques that you can use for monitoring PHP-FPM performance are:

  • PHP-FPM Status Page:

PHP-FPM has a built-in status page that provides real-time information on the status of the pool, processes, and performance metrics. To access the status page, add the following lines to the PHP-FPM pool configuration file:

pm.status_path = /status
ping.path = /ping

Once added, you can access the status page by visiting the URL http://<your-IP-address>:<port>/status. The page will display a wealth of information about the PHP-FPM pool, including the number of active and idle processes, the number of requests processed, the average request time, and much more.

  • System Monitoring Tools: System monitoring tools like top, htop, and ps can be used to monitor PHP-FPM processes and system resources. These tools provide real-time information on the CPU, memory, and I/O usage of the PHP-FPM processes, as well as the system as a whole.
  • Log Files: PHP-FPM also generates log files that contain information about errors, warnings, and other events. By analyzing these log files, you can identify and troubleshoot issues with PHP-FPM performance.
  • Performance Testing: You can also use performance testing tools like Apache Bench or Siege to test the performance of PHP-FPM under different loads and scenarios. These tools can help you identify bottlenecks and optimize your PHP-FPM configuration for better performance.

By regularly monitoring the performance of PHP-FPM, you can ensure that your web applications are running smoothly and efficiently, and troubleshoot any issues that arise before they impact your users.

Securing PHP-FPM and Nginx

While PHP-FPM and Nginx are both secure by default, there are some additional steps that you can take to further secure your web server and protect it from attacks. Some of the best practices for securing PHP-FPM and Nginx are:

  1. Use HTTPS: Always use HTTPS to encrypt traffic between the client and server. HTTPS ensures that data is transmitted securely and cannot be intercepted by third parties. To enable HTTPS, you need to install an SSL/TLS certificate on your web server and configure Nginx to use it.
  2. Enable Firewall: Configure a firewall to restrict access to your web server and only allow traffic from trusted sources. You can use the UFW firewall, which is included with Ubuntu 22.04, to configure firewall rules for your web server.
  3. Disable Unused Services: Disable any unnecessary services and modules that are not needed by your web applications. This reduces the attack surface of your server and makes it more difficult for attackers to find vulnerabilities.
  4. Use Strong Passwords: Always use strong passwords for your web applications, PHP-FPM, and Nginx.
  5. Keep Software Up to Date: Regularly update PHP-FPM, Nginx, and other software components to ensure that your web server is protected against known vulnerabilities and exploits. Use a package manager like apt or yum to install updates and security patches.

Conclusion

In conclusion, PHP-FPM is a powerful and flexible way to run PHP applications on your web server. By configuring and fine-tuning your PHP-FPM pools and settings, you can optimize your server’s performance and ensure that your PHP applications are running smoothly.

We have covered the essential steps for configuring PHP-FPM on Ubuntu, including installation, configuring Nginx, customizing PHP-FPM pools, fine-tuning PHP-FPM settings, monitoring PHP-FPM performance, and securing PHP-FPM and Nginx. We hope this guide has been helpful to you, and you feel confident in your ability to configure and manage PHP-FPM on your Ubuntu server.

Remember to always test your configurations thoroughly before implementing them in a production environment. Additionally, stay up-to-date with the latest security advisories and best practices to keep your server and applications secure.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Back to top button