PHPPHP-FPMNginx7 min read

Install PHP and PHP-FPM

Install PHP-FPM with common extensions and configure pools for Nginx.


PHP-FPM (FastCGI Process Manager) is the recommended way to run PHP with Nginx. It offers better performance and process control than mod_php.

Step 1 — Add updated repository

bash
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Step 2 — Install PHP-FPM

bash
sudo apt install php8.3-fpm -y

Step 3 — Install common extensions

bash
sudo apt install php8.3-mysql php8.3-pgsql php8.3-redis \
  php8.3-curl php8.3-gd php8.3-intl php8.3-mbstring \
  php8.3-xml php8.3-zip php8.3-bcmath php8.3-soap \
  php8.3-imagick php8.3-opcache -y

Step 4 — Verify installation

bash
php -v
php -m
sudo systemctl status php8.3-fpm

Step 5 — Configure PHP-FPM

Edit the pool configuration:

bash
sudo nano /etc/php/8.3/fpm/pool.d/www.conf

Recommended settings for a 2-4 GB RAM VPS:

ini
[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10
pm.max_requests = 500

Step 6 — Configure php.ini

bash
sudo nano /etc/php/8.3/fpm/php.ini

Recommended settings:

ini
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256M
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000

Step 7 — Integrate with Nginx

In your Nginx server block:

nginx
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Step 8 — Restart services

bash
sudo systemctl restart php8.3-fpm
sudo systemctl restart nginx

Verify it works

Create a test file:

bash
echo '<?php phpinfo();' | sudo tee /var/www/html/info.php

Access http://YOUR_IP/info.php then remove it:

bash
sudo rm /var/www/html/info.php

PHP-FPM is ready to serve high-performance PHP applications on your Baires Host VPS.


Was this guide helpful?