How to Setup Nginx as Web Server on CentOS 7

Nginx is an open source high-performance web server and the best alternative for Apache Web Server and Microsoft’s IIS. Nginx is highly scalable Web server and more resource-friendly than Apache. Nginx is very popular web server due to its performance, lightweight and ease of use. It also can be served in front of other web servers as a reverse proxy.

Web server is a software which serves web pages in response to browser requests. Mean while reverse proxy is an intermediary proxy service or front end which takes a client request, passes it on to one or more servers, and subsequently delivers the server’s response back to the client.

The following article will share the basic steps to setup Nginx as Web server on CentOS 7.

Note : If you use a non-root user, then you must add sudo to all the commands, basically in front of each command. Learn about sudo here.

1) Prepare epel repository.

# yum install epel-release -y

2) Install Nginx :

# yum install nginx -y
  • All Nginx configuration files are stored in the /etc/nginx/ directory and /etc/nginx/nginx.conf is the primary configuration file.
  • The default server root directory in CentOS is /usr/share/nginx/html

3) Backup original nginx config file for future reference :

# cp -rp /etc/nginx/nginx.conf /etc/nginx/nginx.conf-ori

4) Make nginx config file simple, add whenever you think necessary :

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format blocked '$time_local: Blocked request from $remote_addr $request';
    access_log  /var/log/nginx/access.log  main;

    include /etc/nginx/sites-available/*.conf;
}

5) Create sites-available directory, where you keep all the domain file.

# mkdir -p /etc/nginx/sites-available

6) Create test.local80.conf for domain test.local under sites-available directory :

# vim /etc/nginx/sites-available/test.local80.conf

Add below setting, configure nginx web server running on port 80 :

server {
        listen 80;
        server_name test.local wwww.test.local;

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

7) Create a document root directory for a website called test.local under /var/www/html and add sample html web pages in this directory :

# mkdir -p /var/www/html/test.local
# vim /var/www/html/test.local/index.html

Add below basic html code and save :

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

8) Test nginx syntax :

# nginx -t

Note : This to ensure you safe to proceed, if you notice some systax problem, you need to fix it one by one. Best practice to run systax checking before run restart the service :

9) Restart Nginx service :

# systemctl restart nginx

10) Verify port 80 listen under nginx service :

# netstat -plunt | grep nginx
tcp        0      0 0.0.0.0:80            0.0.0.0:*               LISTEN      18607/nginx: master

11) Visit http://test.local

How to Setup Nginx as Web Server on CentOS 7

Leave a Comment