Nginx is a free, open-source, and one of the most powerful web servers worldwide. It is mainly used for high-loaded and high-traffic websites. Nginx is known for its stability, simple configuration, and low resource consumption. You can use Nginx as a web server and reverse proxy.
FPM stands for FastCGI Process Manager is an alternative PHP FastCGI implementation of PHP and used for high-traffic websites. It is commonly used with webserver to serve PHP pages. PHP-FPM uses less memory and CPU compared to any other method of running PHP. PHP-FPM is faster than traditional CGI-based methods for multi-user PHP environments. PHP-FPM also allows to run multiple version of PHP at a time.
This tutorial will teach us how to install Nginx with PHP-FPM support in CentOS 8.
Prerequisites
- A server running CentOS 8.
- A root password is configured on your server.
Getting Started
By default, SELinux is enabled in CentOS 8 server. So you will need to disable it first.
You can do this by editing /etc/selinux/config file:
nano /etc/selinux/config
Make the following changes:
SELINUX=disabled
Save and close the file. Then, restart your server to apply the changes.
Install Nginx web Server
First, install the Nginx web server with the following command:
yum install nginx -y
Once the Nginx has been installed, start the Nginx service and enable it to start on boot with the following command:
systemctl start nginx
systemctl enable nginx
Once you have finished, you can proceed to the next step.
Install PHP and PHP-FPM
Next, install PHP and PHP-FPM by running the following command:
yum install php php-cli php-common php-fpm -y
Once all the packages are installed, start the PHP-FPM service and enable it to start after system reboot:
systemctl start php-fpm
systemctl enable php-fpm
You can also check the status of PHP-FPM service with the following command:
systemctl status php-fpm
You should get the following output:
? php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-10-17 05:39:11 EDT; 4min 40s ago Main PID: 1475 (php-fpm) Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 6 (limit: 5060) Memory: 28.5M CGroup: /system.slice/php-fpm.service ??1475 php-fpm: master process (/etc/php-fpm.conf) ??1478 php-fpm: pool www ??1479 php-fpm: pool www ??1480 php-fpm: pool www ??1481 php-fpm: pool www ??1482 php-fpm: pool www Oct 17 05:39:10 centos8 systemd[1]: Starting The PHP FastCGI Process Manager... Oct 17 05:39:11 centos8 systemd[1]: Started The PHP FastCGI Process Manager.
Create an Index Page for Nginx
Next, you will need to create a sample info.php page to test whether Nginx uses PHP-FPM or not.
You can create an info.php inside Nginx default document root directory as shown below:
nano /var/www/html/info.php
Add the following lines:
<?php phpinfo(); ?>
Save and close the file when you are finished.
Next, change the ownership of info.php file to the nginx:
chown -R nginx: /var/www/html/info.php/
Configure Nginx with PHP-FPM
Next, you will need to create an Nginx virtual host configuration file and enable PHP-FPM support.
You can create it with the following command:
nano /etc/nginx/conf.d/example.conf
Add the following lines:
server { listen 80; server_name example.com; root /var/www/html/; index info.php; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { expires max; log_not_found off; } }
Save and close the file. Then, test Nginx for any syntax error with the following command:
nginx -t
Next, enable the Nginx virtual host file and restart the Nginx service to apply the configuration:
systemctl restart nginx
Test Nginx with PHP-FPM Support
Now, open your web browser and type the URL http://example.com. You will be redirected to the following page:
In the above page, you should see that PHP-FPM is loaded with Nginx webserver.
Conclusion
Congratulations! you have successfully installed Nginx with PHP-FPM support on a CentOS 8 server. You can now use PHP-FPM to host multiple websites that use different versions of PHP.