How to Setup Nginx as Web Server on CentOS 7

Introduction

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.

Nginx is one of the most popular open-source web server used by millions of websites across the world. It offers several advantages over other web servers, such as improved performance, scalability, and security. It is designed to handle heavy traffic, making it ideal for websites with large amounts of visitors. Nginx can also be used to host multiple websites on a single server, allowing for efficient resource utilization. With its low memory footprint and fast response time, Nginx is becoming increasingly popular among web developers as an alternative to traditional web servers.

Comparing Nginx to Apache and Other Web Servers

When it comes to web servers, Nginx and Apache are two of the most popular choices. Nginx is known for its superior performance when handling high traffic loads. Apache, on the other hand, is more suited for smaller sites with moderate traffic. When it comes to scalability, Nginx has a clear advantage over Apache due to its ability to handle more simultaneous connections and process requests faster.

The Advantages of Using Nginx as a Web Server

Nginx is a powerful web server that can be used to accelerate the performance of any website. It is known for its caching proxy capabilities, which allow it to serve content faster than other web servers. The advantages of using Nginx as a web server include improved security, scalability, and reliability. Additionally, Nginx offers several acceleration benefits such as faster page loading times, reduced server load, and better SEO rankings.

See also  How to Fix Nginx Bad Gateway 502

What is web server

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.

Since Apache’s fall from dominance, NGINX has risen to prominence as a leading web server choice for developers due to its ability to handle high traffic loads with ease. NGINX now powers more than half of all websites on the internet. It is worth to explore o how we can setup NGINX in linux.

This 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 :

See also  How to Block Specific IP Addresses or user-agents in Nginx Web Server

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