Nginx is a high-performance, reverse proxy, scalable, and open source HTTP web server that beats Apache in almost all sectors. Yes, I know there is a LiteSpeed Enterprise Web Server (LSWS) out there, but I’m falling in love with Nginx because of its reliability, stability, and of course it’s completely free.
Nginx can also work together with Apache as a reverse proxy in almost all web-based control panels like cPanel-WHM, Plesk, and VestaCP. Yes, that’s a bit of Nginx’s superiority over its competitors.
Well, to install and configure Nginx with PHP 7.4 FPM (FastCGI Process Manager) on CentOS 8 is very easy. Simply follow the guide below.
1.) Add Nginx Repository
On this tutorial, we will using Nginx mainline branch, instead of stable branch.
$ nano /etc/yum.repos.d/nginx.repo
[nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
2.) Run Nginx Installer
$ dnf update $ dnf install nginx
Then, let’s start Nginx web server.
$ systemctl enable nginx.service $ systemctl start nginx
3.) Install Epel and Remi Repository.
You have Nginx web server installed inside your virtual machine (cloud server or VPS). Now, we need to build the latest stable release of PHP 7.4 with FastCGI Proccess Manager or also known as PHP-FPM.
Since CentOS 8 shipped with PHP 7.2 as its default PHP version, so we need to install Extra Packages using Epel and Remi repository. Run the following command to install Epel and Remi repository on your CentOS 8 machine.
$ dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm $ dnf install epel-release
Or
$ dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
4.) Setting Up PHP 7.4 FPM
Here we already have all the extra packages and repositories needed to build PHP 7.4 FPM on CentOS 8. Now let’s build it.
$ dnf update $ dnf config-manager --set-enabled remi $ dnf config-manager --set-enabled remi-modular $ dnf module enable php:remi-7.4 $ dnf install php-fpm php-cli php-common php-gd php-imap php-mbstring php-mcrypt php-mysqlnd php-pdo php-soap php-tidy php-xml php-xmlrpc php-pecl-apc php-opcache php-curl php-pear php-xsl php-gmp
5.) Make PHP FastCGI Process Manager to Communicate with Nginx
Now, lets let’s configure PHP 7.4 FPM to work with Nginx. Make sure to change UNIX Socket to listen on TCP Socket. On CentOS 8 machine, we need to open PHP-FPM default pool directives which is located at /etc/php-fpm.d/www.conf.
$ nano /etc/php-fpm.d/www.conf
Search for this line:
listen = /run/php-fpm/www-sock;
And replace with this one:
listen = 127.0.0.1:9000;
Why we should to switch from Unix Socket to TCP Socket?
From what I learned about PHP-FPM over the past few years, the Unix Socket connection is slightly faster than TCP Socket, but overall it’s less scalable. That’s why we have to switch to TCP socket.
6.) Enable PHP-FPM to Start Automatically from Boot
$ systemctl enable php-fpm.service $ systemctl start php-fpm
7.) Create Your First Nginx ServerBlock
Here, we will create simple Nginx ServerBlock to run on subdomain dev.knot35.com. Be sure to replace dev.knot35.com with your actual domain name.
$ mkdir -p /home/dev.knot35.com/public_html $ chown -R nginx:nginx /home/dev.knot35.com/public_html
We using nano editor to open this serverblock, since it very simple and easy to use.
$ nano /etc/nginx/conf.d/dev.knot35.com.conf
# ServerBlock Configuration for dev.knot35.com server { listen 80; ## Listen for IPv4 Address; #listen [::]:80 Default IPv6only=on; root /home/dev.knot35.com/public_html/; index index.htm index.php index.html; server_name dev.knot35.com www.dev.knot35.com; access_log /var/log/nginx/dev.knot35.com.access.log; error_log /var/log/nginx/dev.knot35.com.error.log; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_index index.php; fastcgi_param HTTP_PROXY ""; #include fastcgi.conf; # fastcgi_intercept_errors on; } location = /robots.txt { access_log off; log_not_found off; } location ~ /\. { deny all; access_log off; log_not_found off; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
8.) Restart Nginx and PHP-FPM
$ systemctl restart nginx $ systemctl restart php-fpm
Now you have Nginx and PHP 7.4 FPM running on your virtual machine. You can continue to install and configure your web application like WordPress for example, but make sure to install the database server first.
You can jump to my previous article to learn On How to Install MariaDB 10.4 on CentOS 8 machine.