How To Configure Nginx Web Server On RHEL 7/Redhat

How To Configure Nginx Web Server On Centos 7

In this blog post, I will show you how to configure Nginx web server in rhel 7. before going to configure Nginx on rhel 7 Linux server, install Nginx on Redhat/rhel 7. For the installation of Nginx on rhel 7 you can refer here.

after installing nginx on rhel 7/redhat server you will get a lot of configuration files with Nginx. you can find this configuration files in /etc/nginx directory

[root@ip-172-31-45-192 ~]# ls /etc/nginx/
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@ip-172-31-45-192 ~]#

Nginx main configuration file will be located in the same directory(/etc/nginx) and that is nginx.conf file. so the first step to configure the Nginx web server is to create document root directories for your website.

Create Document Root Directory for a domain/website

by using bellow command create a document root directory for a website called example.com

mkdir -p /var/www/example.com/html

after creating the document root directory add sample web pages in this directory. so here I am going to add small HTML page. create an index.html page in /var/www/example.com/html/ directory and add some html to this page.

vi /var/www/example.com/html/index.html

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success!  The example.com is working!</h1>
    </body>
</html>

now we have successfully created document root directory and i added some pages to this directory. so our website content is ready to serve the webpage requests. But for this, we have to create a configuration file(server block file) for this example.com website in nginx.

Create Server Block File for Our Domain

we will create server block configuration files in conf.d directory. you can find this directory in /etc/nginx

cd /etc/nginx/conf.d

now in conf.d directory, create server block configuration file for domain example.com. the sever block configuration files end with .conf

vi example.com.conf

server {
    listen  80;

    server_name example.com www.example.com;

    location / {
        root  /var/www/example.com/html;
        index  index.html index.htm;
        try_files $uri $uri/ =404;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

save this file.

here server_name represents your domain name like example.com. and we can add an additional alias to your domain like with www and without www.

here root will  Point to the site's document root that you created, here document root is /var/www/example.com/html

Configure Main Nginx Configuration File

Next, we should tell Nginx to look for server blocks(.conf files) in the directory that we created in conf.d directory. To accomplish this, we have to edit Nginx's main configuration file i,e nginx.conf file this file you can find in /etc/nginx

vi /etc/nginx/nginx.conf

in this file you can see http { } block. add below lines in http { } bolck in nginx.conf file.

include /etc/nginx/conf.d/*.conf;
server_names_hash_bucket_size 64;

most of the times these lines will be already available in the nginx.conf file if these lines already there keep as it is.

if it is not there add the lines in http {} block and save this file.

verify nginx configuration files

by using nginx -t command you can verify the configuration files are properly configured or not.

[root@ip-172-31-45-192 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ip-172-31-45-192 ~]#

after executing this command it will show like syntax is ok and main nginx.conf test is successful.

after successful configuration of Nginx files restart Nginx service.

configure SELinux  to allow Nginx to serve your www directory 

run below command to allow Nginx to serve your www directory.

chcon -Rt httpd_sys_content_t /var/www/

or you can disable the selinux in your centos 7 server by executing below command.

setenforce 0

you can verify is SELinux is running or not in your server by using below command

[root@ip-172-31-45-192 ~]# getenforce
Permissive

if it is showing permissive SELinux is not running in your server, if it shows enforcing SELinux is running in your server.

you can start or stop SELinux in centos 7 using setenforce command.

setenforce  0   to stop SELinux

setenforce  1    to start SELinux

Restart Nginx 

you can restart nginx with restart command

systemctl restart nginx

Edit Host file

to test your website or domain we have to add example.com and ip of your server in DNS server. But to test this configuration we can add these details in our local hosts file.

This basically works by intercepting requests that would usually go to DNS to resolve domain names. Instead, we can set the IP addresses we want our local computer to go to when we request the domain names. Here hosts file will act like a DNS server and it will convert the domain name into IP address.

if you are using the browser in linux machine(local computer) to test example.com edit /etc/hosts file

vi /etc/hosts

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
       3.19.60.94       example.com

here 3.19.60.94 is public ip of your centos 7 server

if you are using the browser in windows machine(local computer) edit C:\Windows\System32\drivers\etc\hosts file

add below lines

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
      3.19.60.94       example.com

save this file.

Test the web server

enter example.com in your browser you can see like

Configure Nginx Web Server On centos 7

that's it we have successfully configured Nginx web server for example.com domain in centos 7.

Note: if you are using any firewalls like firewalld, add 80 and 443 ports to firewalld and if you are using aws rhel server open ports 80 and 443 in security groups.

 

  • nginx configuration in rhel 7
  • nginx web server configuration in redhat 7
  • web server in centos 7 using nginx
  • configure nginx on rhel 7
  • nginx web server in redhat

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *