11.07.2025
Installing and Configuring Nginx with PHP Support
To install the nginx web server and the php-fpm process manager, run the following command:
user@COMPXX:~\$ sudo apt-get update && sudo apt-get install -y nginx php php-fpm
After installation, stop nginx while configuring it:
user@COMPXX:~\$ sudo systemctl stop nginx
Disable the default site configuration:
user@COMPXX:~\$ sudo rm -f /etc/nginx/sites-enabled/default
Creating the Site Directory and Test Page
Create a folder where your website files will be stored (for example, /SITE in the root directory) and set its owner to www-data:
user@COMPXX:~\$ sudo mkdir -p /SITE
user@COMPXX:~\$ sudo chown -R www-data:www-data /SITE
user@COMPXX:~\$ sudo chmod -R 0775 /SITE
Create a test index.php page to verify that PHP is working:
user@COMPXX:~\$ sudo nano /SITE/index.php
<?php
phpinfo();
?>
Configuring Nginx
Check the installed PHP version and the php-fpm socket filename:
user@COMPXX:~\$ php -v
user@COMPXX:~\$ ls /var/run/php/
The output will show your active socket (for example, php8.2-fpm.sock or php8.4-fpm.sock) — you will need to specify it in the configuration below.
Create the site configuration file in /etc/nginx/sites-available/:
user@COMPXX:~\$ sudo nano /etc/nginx/sites-available/site.ru.conf
server {
listen 80 default_server;
server_name site.ru _;
root /SITE;
index index.php index.html index.htm;
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)\$ {
access_log off;
expires max;
log_not_found off;
}
location / {
try_files \$uri uri/ /index.php?query_string;
}
location ~* \.php\$ {
try_files \$uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \(document_root\)fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Note: In the
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;line, replace8.2with the PHP version obtained in the previous step.
Enable the configuration by creating a symbolic link in sites-enabled:
user@COMPXX:~\$ sudo ln -s /etc/nginx/sites-available/site.ru.conf /etc/nginx/sites-enabled/
Testing and Starting the Services
Check the Nginx configuration for syntax errors:
user@COMPXX:~\$ sudo nginx -t
If the output shows syntax is ok and test is successful, open port 80 in the firewall (if UFW is enabled):
user@COMPXX:~\$ sudo ufw allow 'Nginx HTTP'
Start nginx and enable it at boot:
user@COMPXX:~\$ sudo systemctl enable --now nginx
Verifying the Setup
Open your server's IP address (http://SERVER_IP_ADDRESS) or domain name in a browser. If everything is configured correctly, you will see the phpinfo() diagnostic page.
Important: After testing, be sure to delete the
/SITE/index.phptest file containingphpinfo()so you do not expose sensitive server configuration details to the public.