12 C
Texas
angeloma
Senior Writer and partner

How to create an Nginx Server Block on CentOS 8 / RHEL 8 / Oracle Linux 8?

Configuring Nginx to work with PHP is quite simple. However, many applications also require us to use a server block or as Apache calls it a Virtualhost. This means that we can set specific rules for each of our applications, as well as define different folders dedicated to each of them. It is quite simple and useful. So, in this post, you will learn to establish an Nginx server block on CentOS 8, RHEL 8 and Oracle Linux 8.

So, let us start.

1. Install Nginx

Of course, the first step is to install Nginx. As we well know, it comes in the official repositories so there is a bigger problem in doing so.

Open a terminal and run:

- Advertisement -
:~$ su
:~# dnf install nginx

1.- Install Nginx on Centos 8 / RHEL 8 / Oracle Linux 8
1.- Install Nginx on Centos 8 / RHEL 8 / Oracle Linux 8

When the installation is finished, the service must be started. It is also recommended to make it start together with the system.

:~# systemctl start nginx
:~# systemctl enable nginx

In order for Nginx to run correctly, it is necessary to open the ports in the Firewall. To do this, run the following:

:~# firewall-cmd --permanent --add-service=http
success
:~# firewall-cmd --reload
success

Now, open your web browser and go to your server. http://server-ip.

2.- Nginx default page on Oracle Linux 8
2.- Nginx default page on Oracle Linux 8

So, Nginx is working.

2) Install and configure PHP

The second step is to install PHP which is also in the official repositories.

:~# dnf install php php-fpm php-mysqlnd php-cli

3.- Install PHP on CentOS 8, RHEL 8, Oracle Linux 8
3.- Install PHP on CentOS 8, RHEL 8, Oracle Linux 8

Now you need to configure PHP to work with Nginx. First, edit PHP file.

:~# nano /etc/php.ini

And change the following value:

cgi.fix_pathinfo=0

Next, edit the /etc/php-fpm.d/www.conf file.

And uncomment the following values:

pm.min_spare_servers = 5	 	 
pm.max_spare_servers = 35	 	 

And change the listen values.

listen = 127.0.0.1:9000

Next, save the changes and close the file.

Now, start and enable the PHP-fpm service.

:~# systemctl start php-fpm
:~# systemctl enable php-fpm

3) Create a new Nginx Server Block

Now we will create the new server block. For this also create a folder in /usr/share/nginx/html/ i will name it example.

Inside it, I will make a file called index.php that will only contain a phpinfo method.

:~# mkdir -p /usr/share/nginx/html/example
:~# nano /usr/share/nginx/html/example/index.php

And add the following:

<?php
phpinfo();
?>

Save the changes and close the file.

Now it is necessary to create the configuration file of the new Server Block. Create the file in /etc/nginx/conf.d/ and call it example.conf.

:~# nano /etc/nginx/conf.d/example.local.conf

And add the following:

server {
   server_name example.local;
   root /usr/share/nginx/html/example;

   location / {
       index index.html index.htm index.php;
   }

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

4.- Create a Nginx Server Block on CentOS 8 / RHEL 8 / Oracle Linux 8
4.- Create a Nginx Server Block on CentOS 8 / RHEL 8 / Oracle Linux 8

Save the file and then close it. Then, restart the services.

:~# systemctl restart nginx
:~# systemctl restart php-fpm

Now, open your web browser and go to example.local.

5.- PHP info
5.- PHP info

And that is it.

Conclusion

To manipulate different applications on the same web server, it is necessary to create a server block for each of them. As you have seen in the post, it is not complicated and allows to

Please share this post and join our Telegram channel.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article