How to Install and Configure PHP-FPM on AnonVM

PHP-FPM is an advanced process manager that replaces the traditional PHP handler, improving resource management and enabling faster request processing. In this guide, you’ll learn how to install, configure, and optimize PHP-FPM on your AnonVM server to maximize your web application’s efficiency.


Step 1: Log In to Your AnonVM Server

Start by logging into your server using SSH:

 
ssh root@your-vps-ip

Replace your-vps-ip with the actual IP address of your AnonVM server.


Step 2: Update System Packages

Make sure your system is up-to-date by running:

 
sudo apt update && sudo apt upgrade -y

Step 3: Install PHP and PHP-FPM

For Ubuntu/Debian:

  1. Install PHP-FPM along with any necessary PHP extensions.
     
    sudo apt install php-fpm php-mbstring php-xml php-mysql -y

For CentOS/RHEL:

  1. Install the Remi repository for the latest PHP version:

     
    sudo yum install epel-release -y sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
  2. Enable the desired PHP module:

     
    sudo yum-config-manager --enable remi-php80
  3. Install PHP-FPM:

     
    sudo yum install php-fpm php-mbstring php-xml php-mysql -y

Step 4: Configure PHP-FPM

After installing PHP-FPM, you can adjust its settings in the main configuration file:

  1. Open the PHP-FPM configuration file:

     
    sudo nano /etc/php/8.0/fpm/php-fpm.conf

    (Adjust the PHP version path if needed, for example, 8.1 instead of 8.0.)

  2. Modify PHP-FPM Pool Configuration: Each application should ideally have its own pool. The default pool configuration file is usually located at:

     
    sudo nano /etc/php/8.0/fpm/pool.d/www.conf
  3. Set Basic Options:

    • User and Group: Set the user and group under which PHP-FPM will run:

      plaintext
       
      user = www-data group = www-data
    • Listen Address: PHP-FPM listens on a Unix socket by default. You can change this to an IP and port, especially if you are using multiple servers or a load balancer.

      plaintext
       
      listen = /run/php/php8.0-fpm.sock

      or to use a TCP/IP socket:

      plaintext
       
      listen = 127.0.0.1:9000
    • Adjust Process Management: PHP-FPM supports static and dynamic process management.

      • Static: A fixed number of child processes are created, ideal for high-traffic, dedicated servers.
      • Dynamic: Creates child processes as needed, which is efficient for servers with fluctuating traffic.
      plaintext
       
      pm = dynamic pm.max_children = 10 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 5

      Adjust these values based on your server's resources and expected traffic load.


Step 5: Start and Enable PHP-FPM Service

Enable and start the PHP-FPM service so that it starts at boot.

For Ubuntu/Debian:

 
sudo systemctl enable php8.0-fpm sudo systemctl start php8.0-fpm

For CentOS/RHEL:

 
sudo systemctl enable php-fpm sudo systemctl start php-fpm

Step 6: Integrate PHP-FPM with Web Server

For Nginx:

  1. Open your Nginx site configuration file:

     
    sudo nano /etc/nginx/sites-available/yourdomain.com
  2. Update the PHP location block to use PHP-FPM:

    plaintext
     
    location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.0-fpm.sock; }
  3. Restart Nginx:

     
    sudo systemctl restart nginx

For Apache:

  1. Enable the proxy_fcgi module:

     
    sudo a2enmod proxy_fcgi setenvif
  2. Update your Apache virtual host to use PHP-FPM:

    plaintext
     
    <FilesMatch "\.php$"> SetHandler "proxy:unix:/run/php/php8.0-fpm.sock|fcgi://localhost/" </FilesMatch>
  3. Restart Apache:

     
    sudo systemctl restart apache2

Step 7: Test PHP-FPM Setup

To verify PHP-FPM is working, create a phpinfo.php file in your web root directory:

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

Add the following PHP code:

php
 
<?php phpinfo(); ?>

Access http://yourdomain.com/phpinfo.php in your browser. If PHP-FPM is set up correctly, you should see a PHP information page. Be sure to delete this file afterward to prevent exposing sensitive information.


Step 8: Optimize PHP-FPM Performance

  1. Tweak PHP-FPM Settings in /etc/php/8.0/fpm/pool.d/www.conf:

    • Increase pm.max_children to handle more concurrent requests if your server has high traffic.
    • Enable slow log to monitor any scripts that are taking longer than expected:
      plaintext
       
      request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/slow.log
  2. Configure Opcache: Enable and configure Opcache to improve PHP performance by caching precompiled script bytecode.

     
    sudo nano /etc/php/8.0/fpm/php.ini

    Add or adjust the following settings:

    plaintext
     
    opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.validate_timestamps=1
  3. Restart PHP-FPM to apply these optimizations:

     
    sudo systemctl restart php8.0-fpm

Conclusion

Installing and configuring PHP-FPM on your AnonVM server optimizes the handling of PHP requests, leading to faster response times and improved server performance. By following these steps, your PHP applications will be better equipped to handle high traffic efficiently.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution