How to Install Nginx on CentOS 8 and Configure Nginx with Multiple PHP Versions
In this guide, we’ll discuss how to install Nginx on a CentOS 8 server and how to run Multiple PHP Versions on one server using nginx and PHP-FPM on CentOS 8
After installing CentOS 8 you will have the root user an you might have {another user} that you created during the installation, anyway and in both cases we will not use any user from those users an we will start create our own {web user}
the idea is you’ll need access to a CentOS 8 server as a non-root user with sudo privileges, and an active firewall installed on your server
Step 0 — Create the user follow this wiki
https://www.wikicoode.com/centos/how-create-sudo-user-centos-78
Step 1 — Installing the Nginx Web Server
sudo dnf install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
Step 2 — Adjusting Firewall Rules
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
To apply the changes, you’ll need to reload the firewall service:
sudo firewall-cmd --reload
now use below command to see your IP in order to open it in the browser to see the nginx home page
ip addr show
important directory that we might need them
- /usr/share/nginx/html: main nginx root directory
- etc/nginx: he Nginx configuration directory.
/etc/nginx/conf.d/
: This directory contains server block configuration files, where you can define the websites that are hosted within Nginx- /var/log/nginx/access.log:Every request to your web server is recorded in this log file
/var/log/nginx/error.log
: Any Nginx errors will be recorded in this log.
Step 3 – Setting Up Server Blocks (Optional)
In case you’d like to host multiple websites within the same Nginx web server, you’ll need to set up server blocks. Nginx server blocks work in a similar way to Apache virtual hosts, allowing a single server to respond to multiple domain names and serving different content for each of them. On CentOS 8, server blocks are defined in .conf
files located at /etc/nginx/conf.d
in this step we will setup two server blocks then we will install two versions of PHP to use them in those blocks, we will work on PHP7.3, PHP7.4, so we will have each block work with different PHP version
lets first install two PHP-FPM instances and configure them, First let’s discover what versions of PHP 7 are available on Remi:
sudo dnf module list php
here you will see the list of available PHP versions
Next, disable the default PHP module and enable Remi’s PHP7.3 module using the below command:
- sudo dnf module reset php
the above step will be used always to disable current version and use another version , so the full steps will be like this
- sudo dnf module reset php
- sudo dnf module enable php:remi-7.3
sudo dnf install php73 php73-php-fpm -y
- sudo systemctl start php73-php-fpm
sudo systemctl enable php73-php-fpm
the same steps can be for PHP 74
- sudo dnf module reset php
- sudo dnf module enable php:remi-7.4
sudo dnf install php74 php74-php-fpm -y
- sudo systemctl start php74-php-fpm
sudo systemctl enable php74-php-fpm
Install any extension by this command
sudo dnf install php74-php-{mysqli} -y
Example :
sudo yum -y install php74-php-pdo php74-php-mysqlnd php74-php-opcache php74-php-xml php74-php-gd php74-php-devel php74-php-mysql php74-php-intl php74-php-mbstring php74-php-bcmath php74-php-json php74-php-iconv php74-php-soap
important directory that we might need to configure PHP
- /etc/opt/remi/php74/php-fpm.d: we will add new configuration file here to configure the PHP with nginx by our created linux user
- /etc/opt/remi/php73/php-fpm.d: we will add new configuration file here to configure the PHP with nginx by our created linux user
- /var/opt/remi/php74/run/php-fpm: we will need to look at this directory to see the new created .sock file that we will listen to it
- /var/opt/remi/php73/run/php-fpm: we will need to look at this directory to see the new created .sock file that we will listen to it
now let's create .conf file inside this directory "/etc/opt/remi/php74/php-fpm.d" , we can name it as we want , I named it web-user.conf and add the below info to it
we assumed that we create new web user with {web} as username
[web-user] user = web group = nginx listen = /var/opt/remi/php74/run/php-fpm/web.sock listen.owner = web listen.group = nginx listen.allowed_clients = 127.0.0.1:9004 pm = ondemand pm.max_children = 50 pm.process_idle_timeout = 50s pm.max_requests = 500 chdir = / catch_workers_output = yes
do the same for PHP 73 and restart both PHP by executing this command
sudo systemctl start php74-php-fpm
sudo systemctl start php73-php-fpm
the main part now is coming here, lets configure our server block and add the needed configuration to it
we will setup this directory
/var/www/example.com/public_html
- sudo mkdir -p /var/www/example.com/public_html
- sudo chown -R $USER:$USER /var/www/example.com/public_html or sudo chown -R web:web /var/www/example.com/public_html
- create this file /var/www/example.com/public_html/index.php (add phpinfo function to the content of this file)
- now create the conf file for nginx by creating {example.conf} inside this directory /etc/nginx/conf.d/example.conf
example.conf content is
upstream fastcgi_backend_php74 { server unix:/var/opt/remi/php74/run/php-fpm/web.sock; }
server { listen 80; listen [::]:80; server_name example.com www.example.com; root /var/www/example.com/public_html; index index.html index.htm index.nginx-debian.html; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/error.log; location / { try_files $uri $uri/ =404; } location ~ \.php$ { try_files $uri =404; fastcgi_pass fastcgi_backend_php74 ; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000"; fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Now restart nginx
- sudo systemctl restart nginx
very important step
you need to allow your custom document root to be served as HTTP content:
# sudo chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/example.com/
# sudo chcon -Rt httpd_sys_content_t /var/www/example.com
# sudo chcon -Rt httpd_sys_rw_content_t /var/www/example.com
# sudo ls -dZ /var/www/example.com
Notes:
1- if you install mysql by using docker and you could not connect to the database because of permession denied , but you could do that from mysql client then you need to execute this command
setsebool -P httpd_can_network_connect 1
2- if you tried to restart nginx and you have permesion denied because of the logs directory then you need to use the below command (you have to put your logs directory)
sudo chcon -Rt httpd_sys_rw_content_t /var/www/{example.com}/logs
Reference
- install centos
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-8 - https://www.linode.com/docs/guides/how-to-install-nginx-centos-8/
- install multiple PHP-FPM
https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-centos-8 - useful link: https://opensource.com/article/21/4/share-files-linux-windows
- https://serverfault.com/questions/131105/how-do-i-get-selinux-to-allow-apache-and-samba-on-the-same-folder