Install Nginx and PHP-FPM on a Linux VPS
Nginx with PHP-FPM is the most common stack for serving a PHP site, such as WordPress, Laravel or Symfony, on a Linux VPS. Nginx serves the files and passes PHP scripts to PHP-FPM, which runs them. This guide installs both on Debian 13 or Ubuntu 24.04, creates a first site, adds HTTPS and tunes performance.
Debian 13 ships Nginx 1.26 and PHP 8.4 in its repositories. Ubuntu 24.04 ships Nginx 1.24 and PHP 8.3. You need root or sudo access and, for HTTPS, a domain name that points to the VPS IP.
Install Nginx
First update the system. The second line reboots the VPS only if an update requires it.
sudo apt update && sudo apt upgrade -y
[ -f /var/run/reboot-required ] && sudo reboot
Then install Nginx and check its version and status.
sudo apt install nginx -y
nginx -v
sudo systemctl status nginx
The service should be active (running). If UFW is enabled, open ports 80 and 443. The guide How to set up the UFW firewall on Ubuntu or Debian explains these rules.
sudo ufw status
sudo ufw allow 'Nginx Full'
# Or port by port
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Open http://203.0.113.10 in your browser, replacing the address with the VPS main IP. The Welcome to nginx! page confirms that the server responds.
Manage the Nginx service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # reloads the configuration without dropping connections
sudo systemctl enable nginx # starts automatically with the VPS
sudo systemctl disable nginx
sudo nginx -t # tests the configuration
Always run sudo nginx -t before reloading Nginx. A syntax error is then reported without taking the site down.
Install PHP-FPM
Install the PHP version that comes with your system, along with the extensions most applications ask for.
Debian 13 (PHP 8.4)
sudo apt install php8.4-fpm php8.4-cli php8.4-common php8.4-mysql \
php8.4-zip php8.4-gd php8.4-mbstring php8.4-curl php8.4-xml \
php8.4-bcmath php8.4-intl php8.4-soap php8.4-opcache php8.4-readline -y
Ubuntu 24.04 (PHP 8.3)
sudo apt install php8.3-fpm php8.3-cli php8.3-common php8.3-mysql \
php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml \
php8.3-bcmath php8.3-intl php8.3-soap php8.3-opcache php8.3-readline -y
Another PHP version on Ubuntu
Ondřej Surý's PPA offers other PHP versions for Ubuntu. Debian 13 already has PHP 8.4 in its own repositories. Check how long a version is supported on the php.net Supported Versions page before you pick it.
# Add Ondřej Surý's PPA
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
# Example, install PHP 8.4
sudo apt install php8.4-fpm php8.4-cli php8.4-common php8.4-mysql \
php8.4-zip php8.4-gd php8.4-mbstring php8.4-curl php8.4-xml \
php8.4-bcmath php8.4-intl php8.4-soap php8.4-opcache -y
Check PHP-FPM and find its socket
Nginx talks to PHP-FPM through a socket whose name includes the version. Adjust 8.4 to your version.
php -v
sudo systemctl status php8.4-fpm
ls /run/php/
The folder holds php8.4-fpm.sock on Debian 13 and php8.3-fpm.sock on Ubuntu 24.04. The service should be active (running).
Connect Nginx to PHP-FPM
Open the configuration of the default site.
sudo nano /etc/nginx/sites-available/default
Replace its content with the block below. On Ubuntu 24.04, replace php8.4-fpm.sock with php8.3-fpm.sock.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
# PHP scripts run by PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Block access to .htaccess files
location ~ /\.ht {
deny all;
}
}
Test the configuration, then reload Nginx.
sudo nginx -t
sudo systemctl reload nginx
nginx -t should answer with these two lines.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Test PHP
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Open http://203.0.113.10/info.php. The page shows the PHP version, the Server API line set to FPM/FastCGI and the loaded extensions. Then delete the file.
sudo rm /var/www/html/info.php
Never leave a
phpinfo()file online. It reveals paths, versions and settings that are useful to an attacker.
Create a site with its own server block
A server block, the Nginx counterpart of an Apache virtual host, describes one site. Each domain gets its own. Replace example.com with your domain in the commands.
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com
nano /var/www/example.com/html/index.php
Put a test page in index.php.
<?php
echo 'Nginx + PHP-FPM OK, PHP ' . phpversion();
Then create the site configuration.
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.php index.html index.htm;
# Site logs
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Block hidden files
location ~ /\. {
deny all;
}
# Cache static files
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|txt|woff|woff2|ttf|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
On Ubuntu 24.04, the socket is still php8.3-fpm.sock. Enable the site, remove the default site if needed, then test and reload.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
Enable, disable and list sites
# Enable a site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Disable a site
sudo rm /etc/nginx/sites-enabled/example.com
# List active sites
ls -la /etc/nginx/sites-enabled/
For several sites on the same VPS, with one PHP-FPM pool per site, follow the guide Host multiple websites on a VPS with Nginx.
Enable HTTPS with Let's Encrypt
The domain must already point to the VPS IP. Certbot obtains the certificate and edits the server block for you.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
Certbot asks for an email address and for your agreement to the Let's Encrypt terms. Accept the redirect from HTTP to HTTPS. Renewal is then automatic. Test it with a dry run.
sudo certbot renew --dry-run
The guide Certbot: install a Let's Encrypt SSL certificate on a VPS covers special cases such as a wildcard certificate.
Tune PHP-FPM and php.ini
The PHP-FPM pool
The pool sets the number of PHP processes. Too low, and visitors wait. Too high, and the VPS runs out of memory.
sudo nano /etc/php/8.4/fpm/pool.d/www.conf
On Ubuntu 24.04, the path starts with /etc/php/8.3/. Keep pm = dynamic and start from these values, based on the VPS memory.
| Parameter | Role | 1 to 2 GB | 4 GB | 8 GB and more |
|---|---|---|---|---|
pm.max_children | Maximum number of processes | 10 | 25 | 50 |
pm.start_servers | Processes started at launch | 2 | 5 | 10 |
pm.min_spare_servers | Minimum idle processes | 1 | 3 | 5 |
pm.max_spare_servers | Maximum idle processes | 3 | 10 | 20 |
pm.max_requests | Requests before a process is recycled | 500 | 1000 | 2000 |
The pm setting also accepts static, for a fixed number of processes, and ondemand, which only starts a process when needed.
The php.ini file
sudo nano /etc/php/8.4/fpm/php.ini
Here are common starting values. Put your time zone in place of UTC, for example Europe/London.
; Memory
memory_limit = 256M
; File uploads
upload_max_filesize = 64M
post_max_size = 64M
; Execution
max_execution_time = 300
max_input_time = 300
max_input_vars = 3000
; Time zone
date.timezone = UTC
; OPcache (speeds up PHP)
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 2
opcache.validate_timestamps = 1
; Security
expose_php = Off
Test the PHP-FPM configuration, then restart the service.
sudo php-fpm8.4 -t
sudo systemctl restart php8.4-fpm
On Ubuntu 24.04, the commands become sudo php-fpm8.3 -t and sudo systemctl restart php8.3-fpm.
Optimise and secure Nginx
The main configuration
Keep a copy of the original file, then open it.
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo nano /etc/nginx/nginx.conf
This configuration turns on gzip compression, hides the Nginx version and accepts 64 MB uploads, like the php.ini above.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
# Basic settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# Upload size
client_max_body_size 64M;
client_body_buffer_size 128k;
# MIME types
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logs
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript
application/rss+xml application/atom+xml image/svg+xml;
# Includes
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
sudo nginx -t
sudo systemctl restart nginx
Security headers
Add these lines to your site's server block.
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
An
add_headerplaced in alocationblock cancels those of theserverblock for that location. Repeat the headers in thelocationblock if you add one there.
Limit requests
In /etc/nginx/nginx.conf, add the limit zones to the http section.
# Limit zones
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
Then apply them in your site's server block.
location / {
limit_req zone=one burst=20 nodelay;
limit_conn addr 10;
}
Fix common errors
502 Bad Gateway
PHP-FPM is not running, or Nginx points to a socket that does not exist. Adjust the version in the commands.
sudo systemctl status php8.4-fpm
ls -la /run/php/
sudo systemctl restart php8.4-fpm
grep fastcgi_pass /etc/nginx/sites-enabled/*
The socket version in fastcgi_pass must match the file in /run/php/.
403 Forbidden
Nginx cannot read the site files, or the folder has no index file.
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
404 on PHP files or blank page
Check that the file exists, that the root directive points to the right folder and that the location ~ \.php$ block is there. A blank page often means the PHP code is sent without being run.
Look for the cause in the logs
# PHP-FPM processes
ps aux | grep php-fpm
# Ports opened by Nginx
sudo ss -tlnp | grep -E 'nginx|php'
# Live errors
sudo tail -f /var/log/nginx/error.log
Where the files are
| Path on Debian 13 | Role |
|---|---|
/etc/nginx/nginx.conf | Main Nginx configuration |
/etc/nginx/sites-available/ | Site configurations |
/etc/nginx/sites-enabled/ | Active sites, as symbolic links |
/etc/nginx/snippets/ | Reusable configuration snippets |
/var/log/nginx/ | Access and error logs |
/etc/php/8.4/fpm/php.ini | PHP configuration |
/etc/php/8.4/fpm/pool.d/www.conf | PHP-FPM pool configuration |
/run/php/php8.4-fpm.sock | PHP-FPM socket |
/var/www/html/ | Default site |
On Ubuntu 24.04, replace 8.4 with 8.3 in the PHP paths.
Need help?
Open a ticket from Tickets › Open a Ticket, or from the Contact support tile at the bottom of the VPS page. In the Related Service field, pick the VPS so the team saves time. You can also ask your question on the OuiHeberg Discord.
Root access, your choice of distribution and management from the OuiHeberg client area.
View plans →