Home GuideCentOS How To Install NGINX on CentOS 8

How To Install NGINX on CentOS 8

by schkn

This tutorial focuses on how to install a NGINX web server on Centos 8 hosts.

NGINX is probably one of the most popular web servers in use nowadays.

Pronounced “engine-x“, NGINX is used to serve 32% of all the active websites online, just above Apache HTTP Web servers.

NGINX is also used as a load balancer or a reverse proxy for Apache.

Known for its performance, NGINX can handle a very large number of incoming connections, even if it suffers from a lack of flexibility compared to Apache.

In this tutorial, we are going to see how you can install NGINX on CentOS 8.

Prerequisites

Before starting, it is important for you to have sudo privileges on CentOS 8.

To make sure that this is the case, run the following command

$ sudo -l

User user may run the following commands on localhost:
    (ALL) ALL

Also, make sure that nothing is already running on port 80 on your host.

NGINX uses this port by default. As a consequence, if you already have an Apache Web Server installed, you may not be able to run your NGINX server.

$ netstat -tulpn | grep :80

If no results are shown for this command, you are good to go.

Install NGINX on CentOS 8

In order to install NGINX on CentOS 8, you simply have to install the following packages.

$ sudo yum install -y nginx
Install NGINX on CentOS 8

Start NGINX on CentOS 8

First of all, you want to enable your NGINX server in order for it to start as soon as your host starts.

$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service -> /usr/lib/systemd/system/nginx.service

When you are ready, you can start NGINX with the following command

$ sudo systemctl start nginx

Make sure that NGINX is correctly started with the status command.

$ sudo systemctl status nginx
NGINX status on CentOS 8

Now that NGINX is running, you have to get the IP of your host.

To find your current IP address on CentOS 8, run the following command

$ hostname -I | awk '{print $1}'
192.168.178.27

Open your favorite web browser and navigate to this IP address.

NGINX installed on CentOS 8

Congratulations!

You successfully installed NGINX on CentOS 8.

However, you have to configure it properly in order for your websites to be accessible to the public.

Check your firewall rules

In order for external users to have access to your webserver, you need to enable HTTP and HTTPS traffic to your host.

Modify your firewall rules and add the following entries if not done already

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Manage your NGINX server on CentOS 8

In order to manage your NGINX server, you have multiple options.

To check the status of NGINX, you have to run the following command

$ sudo systemctl status nginx

To stop your NGINX server, run

$ sudo systemctl stop nginx

If you want to start it again, you have to run

$ sudo systemctl start nginx

In case you made some modifications to your NGINX server, you can reload it without having to stop and start it again.

To reload NGINX, you simply have to run

$ sudo systemctl reload nginx

If you don’t want your NGINX server to be started on boot, you have to disable it by running

$ sudo systemctl disable nginx

NGINX best practices on CentOS 8

By default, your static HTML files are located at “/usr/share/nginx/html“.

As a consequence, if you were to navigate to this path, you would find the HTML for the file displayed when you browsed it using your web browser.

NGINX default files on host

File Location

If you want to use NGINX as your default web server, meaning that you are not proxying requests to Apache, you can use the “/var/www” folder in order to store your different websites.

Moreover, you will have to create NGINX server blocks in order to match requests with the websites hosted on your server.

However, if you are proxying requests to Apache, you only have to modify your NGINX configuration files, and you can let the “/var/www” path for Apache website files.

Server Blocks

Similarly to Apache, NGINX can handle custom configuration files in order to store many different websites.

Those configuration files can be stored at “/etc/nginx/conf.d” and they will have to end with .conf.

Technologies used by NGINX and Apache are very similar, so if you used one in the past, there really should not be any differences for administration.

Conclusion

In this tutorial, you learnt how you can install NGINX on CentOS 8.

However, you should now start to create server blocks in order to store your different websites. You can also choose to have a NGINX proxy server in order to forward requests to a primary web server like Apache.

If you are interested in Linux System Administration, we have a complete section dedicated to it on the website, so make sure to check it out!

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.