Nginx + PHP is one of the most popular software groups that you can use to build your website. This step-by-step tutorial will show you how to install and configure Nginx to execute PHP on your server using PHP-FPM.
Nginx is the ideal combination with PHP-FPM. It’s a stable webserver recognized for its impressive performance and low resource consumption.
PHP runs as an isolated service when you use PHP-FPM. Employing this PHP version as the language interpreter means requests will be processed via a UNIX socket. Nginx server will handle HTTP requests only, while PHP-FPM interprets the PHP code.
1. Installing Nginx
Ubuntu / Debian
Because Nginx is available in Debianโs and Ubuntuโs default repositories, installing it from these repositories using the apt packaging system is possible.
sudo apt install nginx
CentOS / AlmaLinux / Rocky Linux
To get the latest Nginx version, CentOS, AlmaLinux, or Rocky Linux users need first to install the EPEL repository.
EPEL (Extra Packages for Enterprise Linux) is an open-source and free community-based repository project from the Fedora team which provides high-quality add-on software packages for RHEL-based Linux distros.
sudo yum install epel-release
Then you can install the Nginx server.
sudo yum install nginx
Fedora
Nginx is available on the default Fedora repositories and can be installed directly using the dnf
package manager.
sudo dnf install nginx
Related: How to Install Nginx Web Server on Linux
2. Installing PHP-FPM
Ubuntu / Debian
To install PHP on Ubuntu or Debian, just run the following command:
sudo apt install php-fpm
CentOS
The PHP version available by default within CentOS servers is outdated. For that reason, weโll need to install a third-party package repository to obtain PHP 7.
CentOS 7
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm
Code language: JavaScript (javascript)
CentOS 8
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm
Code language: JavaScript (javascript)
3. Configuring NGINX to Execute PHP Using PHP-FPM
Once Nginx and PHP are installed, we can configure Nginx to send PHP requests off to PHP-FPM.
We’ll need to create an Nginx server block configuration file to run PHP with FPM. After that, you can create a new VirtualHost as per your requirements, enabling any new VirtualHost.
sudo vim /etc/nginx/sites-available/example.com
Replace example.com
with your siteโs domain or IP address, and the root
directive with your site’s root directory.
<strong>server</strong> {
<strong>ย ย ย ย ย ย ย ย listen</strong> 80;
<strong>ย ย ย ย ย ย ย ย root</strong> /var/www/html;
<strong>ย ย ย ย ย ย ย ย index</strong> index.php index.html index.htm;
<strong>ย ย ย ย ย ย ย ย server_name</strong> example.com;
ย
<strong>ย ย ย ย ย ย ย ย location</strong> / {
<strong>ย ย ย ย ย ย ย ย ย ย ย ย try_files</strong> $uri $uri/ =404;
ย ย ย ย ย ย ย ย }
ย
<strong>ย ย ย ย ย ย ย ย location</strong> ~ \.php$ {
<strong>ย ย ย ย ย ย ย ย ย ย ย ย include</strong> snippets/fastcgi-php.conf;
<strong>ย ย ย ย ย ย ย ย ย ย ย ย fastcgi_pass</strong> unix:/var/run/php/php7.4-fpm.sock;
ย ย ย ย ย ย ย ย }
}
Code language: HTML, XML (xml)
Save your changes to the configuration file and create a link to site enabled directory.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Make sure that the name of the PHP socket file (in our case php7.4-fpm.sock
) specified in the fastcgi_pass
directive is correct.
ls -l /var/run/php/
Code language: JavaScript (javascript)
total 4
-rw-r--r-- 1 root root 5 Dec 1 17:43 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 Dec 1 17:43 php7.4-fpm.sock
lrwxrwxrwx 1 root root 30 Dec 1 17:43 php-fpm.sock -> /etc/alternatives/php-fpm.sock
Code language: JavaScript (javascript)
If the name is different from php7.4-fpm.sock
, just replace it in the server block shown above with the current one from your server.
Then restart the Nginx service to reload the changes.
sudo systemctl restart nginx.service
Code language: CSS (css)
4. Testing the Setup
Create a test PHP file to verify that PHP-FPM works and is integrated with Nginx. In the Server Block above, our site is being served from /var/www/html
, so weโll create a test file there:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Code language: PHP (php)
Finally, access info.php
from a web browser, using your siteโs domain or server’s IP address. You should see the PHP configuration page:
Conclusion
Congratulations! Youโve set up Nginx to handle PHP requests through PHP-FPM.
There are some next steps you could take from here. For example, you should ensure that connections to your server are secured. But how to do it?
Follow our step-by-step tutorial to acquire a free Letโs Encrypt SSL certificate for your server.