How to Install Nginx on CentOS 8

05 September 2020 | Category : CentOS

How to Install Nginx on CentOS 8

What is Nginx?

NGINX (Engine-X) is one of the famous open-source web servers with powerful load balance, reverse proxy, and caching features. As a popular web server, NGINX can responsible for handling the load of some of the largest sites on the Internet and can handle a much large number of concurrent connections and has a smaller memory footprint per connection.

In this article, we will share about How to Install Nginx on CentOS 8 and focus on how to install only. Maybe in the next article, we will share how to create virtual hosts (server blocks) Nginx or how to install WordPress with Nginx on centos 8.

Install Nginx on CentOS 8

To install Nginx on CentOS 8 you can follow this step by step.

  1. Add EPEL repository

Before we start to install Nginx on the CentOS we must add the EPEL repository and updating the OS first. To do that you can follow the below command :

yum install -y epel-release
yum update -y
  1. Disable SELinux

And then you can disable the SELinux, to disable that you can run this command :

setenforce permissive
  1. Install Nginx

Next step you can install Nginx with this simple command :

yum install -y nginx
  1. Basic Configuration Nginx

If the installation has been done, now you can configure Nginx with a simple configuration. To do that you can move to the folder configuration of Nginx first on /etc/nginx with this command :

cd /etc/nginx

In the folder /etc/nginx you will see many files and folder of the configuration Nginx. Because in here article we focus on the installation of Nginx you can make simple configuration. To create a simple configuration you can edit the file configuration on nginx.conf with this command :

vim nginx.conf

On the nginx.conf you will see the line of configuration nginx, right there we focus on below line configuration. By default the configuration server block nginx is like this :

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

You can edit above line of configuration server block nginx to this :

server {
    listen       80 default_server;
    #listen       [::]:80 default_server;
    server_name  _;
    root         /var/www/html/;
    index	     index.html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

We only edit on line :

listen [::]:80 default_server; to comment/disable this configuration
root /var/www/html/; change the root document to folder /var/www/html
index index.html; Add this line to make load index.html as default page website

When finished changing the configuration on file nginx.conf, now you can create a folder /var/www/html as a root document your site with this command.

mkdir -p /var/www/html

Then copy the default page Nginx page from /usr/share/nginx/html/ to the new folder of root document which in /var/www/html with this command

cp -R /usr/share/nginx/html/ /var/www/html

After that you can check the nginx configuration is right or not with this command :

nginx -t

If the configuration right and don’t have miss configuration the result of the above command will like this :

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. Enable & Start Nginx Service

Now, the last step you can enable and start the service of Nginx with this command :

systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
systemctl start nginx

Note :
Every you change the configuration on nginx.conf and want to reload the nginx service to implementation of your modification you can run this command to reload the nginx service.

nginx -s reload
  1. Result

If you have been done and successful configuration the Nginx when you accessing the IP Address of VPS/Server or your domain configuration it will show like this
How to Install Nginx

Conclusion

Nginx is one of the famous open-source web servers, with the feature of Nginx like powerful load balance, reverse proxy, and caching features. Nginx can handle a much large number of concurrent connections and has a smaller memory footprint per connection. So, this is why Nginx became the famous open-source web server.

Share this article :