This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
12 min
Edited:14-10-2024
PHP-FPM (FastCGI Process Manager) is an advanced version of PHP's FastCGI implementation. It provides a faster and more efficient way of serving PHP scripts, with features like process management, adaptive spawning, and better handling of dynamic content. PHP-FPM is highly preferred for high-traffic websites because it can handle more requests with fewer resources compared to traditional mod_php setups. PHP-FPM is often used in conjunction with web servers like Nginx or Apache and helps optimize the performance of PHP applications by managing multiple PHP processes efficiently.
Here’s a step-by-step guide to installing PHP-FPM on an Ubuntu VPS, along with an example of how to use it with Nginx.
Before installing any software, it’s important to ensure that your system is up-to-date:
1. sudo apt update && sudo apt upgrade -y
On Ubuntu, PHP-FPM comes as part of the PHP package. To install PHP-FPM along with PHP, run the following command:
1. sudo apt install php-fpm -y
This installs both PHP and the FastCGI Process Manager (FPM) for managing PHP processes.
The main PHP-FPM configuration file is located at /etc/php/7.x/fpm/php.ini (the version number may vary depending on the PHP version installed). You can edit this file to fine-tune PHP settings.
For basic setup, the default configuration is usually sufficient, but you can adjust the following options if needed:
1. sudo nano /etc/php/7.x/fpm/php.ini
memory_limit: Limits the amount of memory a script can consume.
upload_max_filesize and post_max_size: These settings control the maximum file upload size.
max_execution_time: Controls how long a script can run before being terminated.
After making changes, save the file and restart PHP-FPM to apply the settings:
1. sudo systemctl restart php7.x-fpm
PHP-FPM is commonly used with Nginx because Nginx does not have native PHP support like Apache. Follow these steps to configure Nginx with PHP-FPM.
Step 1: Install Nginx
If Nginx isn’t already installed, use the following command to install it:
1. sudo apt install nginx -y
Step 2: Configure Nginx to Use PHP-FPM
To make Nginx process PHP files using PHP-FPM, you need to modify the server block configuration file for your website. This file is typically located in /etc/nginx/sites-available/.
For example, edit the default Nginx site configuration:
1. sudo nano /etc/nginx/sites-available/default
Find the section for the server block and add the following configuration to enable PHP processing with PHP-FPM:
1. server {
2. listen 80;
3. server_name yourdomain.com;
4. root /var/www/html;
5. index index.php index.html index.htm;
6. location / {
7. try_files $uri $uri/ =404;
8. }
9. location ~ \.php$ {
10. include snippets/fastcgi-php.conf;
11. fastcgi_pass unix:/run/php/php7.x-fpm.sock; # Replace x with your PHP version
12. }
13. location ~ /\.ht {
14. deny all;
15. }
16. }
In the configuration above:
- fastcgi_pass: This line directs Nginx to use PHP-FPM for processing .php files. The php7.x-fpm.sock socket corresponds to the version of PHP installed.
Save the file and exit the editor.
Step 3: Test and Restart Nginx
After making changes to the configuration file, test Nginx to ensure there are no syntax errors:
1. sudo nginx -t
If the test is successful, restart Nginx to apply the changes:
1. sudo systemctl restart nginx
To verify that PHP-FPM is working correctly with Nginx, create a simple PHP file and place it in your web root directory:
1. sudo nano /var/www/html/info.php
Add the following PHP code to the file:
1. <?php
2. phpinfo();
3. ?>
Save and close the file. Now, open a browser and navigate to http://yourdomain.com/info.php. You should see a PHP information page, confirming that PHP-FPM is working correctly with Nginx.
By default, PHP-FPM creates a single pool of processes to handle requests. You can create multiple pools to better manage traffic for different websites or applications. Each pool can have its own configuration, including settings like memory limits, max children, and more.
To create or modify pools, edit the pool configuration file located at:
1. sudo nano /etc/php/7.x/fpm/pool.d/www.conf
Here, you can configure specific parameters for the pool:
pm.max_children: Defines the maximum number of child processes.
pm.start_servers: Specifies how many servers should start when PHP-FPM boots.
pm.min_spare_servers and pm.max_spare_servers: Control the number of idle processes to keep.
You can also create separate pool configurations for different applications by copying the www.conf file and customizing it:
1. sudo cp /etc/php/7.x/fpm/pool.d/www.conf /etc/php/7.x/fpm/pool.d/app1.conf
Then edit the new pool configuration as needed.
After any changes, restart PHP-FPM to apply them:
1. sudo systemctl restart php7.x-fpm
PHP-FPM logs can be found at /var/log/php7.x-fpm.log. To monitor how PHP-FPM is handling requests and check for issues, tail the log:
1. tail -f /var/log/php7.x-fpm.log
This will show real-time logging information, which can help you troubleshoot performance problems or misconfigurations.
Let’s say you’re running a WordPress site with Nginx and PHP-FPM. When a visitor requests a page, Nginx will check if the requested page exists. If it’s a static file (like an image or CSS), Nginx serves it directly. If the request is for a PHP page (like index.php), Nginx passes the request to PHP-FPM for processing.
Here’s how the process works:
1 - Visitor Request: A visitor accesses http://yourdomain.com.
2 - Nginx: Nginx receives the request and looks for index.php in the root directory.
3 - PHP-FPM: Nginx forwards the request to PHP-FPM, which processes the PHP script and sends the response back to Nginx.
4 - Response: Nginx sends the processed output (HTML) back to the visitor.
This separation between the web server (Nginx) and the application server (PHP-FPM) ensures efficient handling of requests, with PHP-FPM focusing solely on executing PHP code.
PHP-FPM is a powerful and efficient way to manage PHP processes, especially for high-traffic websites. By following this guide, you can install PHP-FPM on your Ubuntu VPS, configure it with Nginx or Apache, and optimize your server for better performance. Whether you're running a simple blog or a complex web application, PHP-FPM can handle your PHP processing needs efficiently.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This guide walks you through the installation and configuration of Varnish, a high-performance caching HTTP accelerator, on an Ubuntu VPS. Learn how to set up Varnish with web servers like Apache or Nginx, manage cache, and optimize your website for faster content delivery and improved server performance.
Varnish
speed
optimise
10 min