To install Nginx and PHP-FPM on a Linux server, you can follow the steps below:
Update system packages:
sudo apt update
Install Nginx:
sudo apt install nginx
Start Nginx service:
sudo systemctl start nginx
Verify the installation by accessing your server's IP address in a browser. You should see the default Nginx welcome page.
Install PHP-FPM:
sudo apt install php-fpm
Configure PHP-FPM:
Open the PHP-FPM configuration file in a text editor:
sudo nano /etc/php/VERSION/fpm/php.ini
Replace VERSION
with the installed PHP version (e.g., 7.4), or find the appropriate path to the php.ini
file.
Uncomment the cgi.fix_pathinfo
line and set its value to 0:
cgi.fix_pathinfo=0
Save and close the file.
Restart PHP-FPM service:
sudo systemctl restart phpVERSION-fpm
Replace VERSION
with the installed PHP version (e.g., 7.4).
Configure Nginx to use PHP-FPM:
Open the Nginx default server block configuration file:
sudo nano /etc/nginx/sites-available/default
Add the following location block inside the existing server
block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/phpVERSION-fpm.sock;
}
Replace VERSION
with the installed PHP version (e.g., 7.4).
Save and close the file.
Check Nginx configuration syntax:
sudo nginx -t
If the syntax is OK, restart Nginx service:
sudo systemctl restart nginx
Now, Nginx and PHP-FPM should be installed and configured on your Linux server. You can test PHP-FPM by creating an index.php
file in the default Nginx document root directory (/var/www/html
on Ubuntu) and accessing it in a browser.