Nginx is an open-source, light weight, high-performance HTTP / HTTPS and reverse proxy server responsible for handling the load of some of the largest websites on the Internet. It can be used as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers.
Compared to Apache, Nginx can handle a much large number of concurrent connections and has a smaller memory footprint per connection. Hence, it is highly preferred for the systems with more traffic requests.
This tutorial explains how to install and manage Nginx on CentOS 8.
Table of Contents
Prerequisites
Before continuing, make sure you are logged in as a user with sudo privileges, and you don’t have Apache or any other process running on port 80 or 44. Make sure these ports are also not blocked by firewalls.
Installing Nginx on CentOS 8
Starting with CentOS 8, the Nginx package is available in the default CentOS repositories. Hence there is no need to import any 3rd party repository. Simple execute the following command on your CentOS terminal.
sudo yum install nginx
On completion of successful installation, you would like to start Nginx server by executing command below.
sudo systemctl start nginx
On rebooting server, Nginx will not start automatically. To automatically start Nginx server on reboot or startup, execute the command below.
sudo systemctl enable nginx
To verify the status of Nginx, you can use the following command.
sudo systemctl status nginx
Output will look like as:
● nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)Active: active (running) since Saturday 2019-10-05 17:32:45 UTC; 5min ago...
Setting up Firewall
FirewallD is the default firewall solution on Centos 8. However, you may be using UFW.
During the installation, Nginx creates firewalld service files with predefined rules for allowing access to HTTP (80
) and HTTPS (443
) ports.
Use the following commands to open the necessary ports permanently:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Now, you can test your Nginx installation, by opening http://YOUR_IP
in your web browser. You should see the default Nginx welcome page, which should look like the image below:
Nginx useful commands to remember
Start Nginx
With Systemd service:
sudo systemctl start nginx
Without Systemd service:
sudo service start nginx
To enable auto start of Nginx on rebooting:
sudo systemctl enable nginx
Stop Nginx
With Systemd service:
sudo systemctl stop nginx
Without Systemd service:
sudo service stop nginx
Restart Nginx
With Systemd service:
sudo systemctl restart nginx
Without Systemd service:
sudo service restart nginx
Reload Nginx
With Systemd service:
sudo systemctl reload nginx
Without Systemd service:
sudo service reload nginx
Nginx Test Configurations:
sudo nginx -t
View Nginx Status
sudo systemctl status nginx
Check Nginx Version
sudo nginx -v
Nginx Configuration Best Practices and File’s Structure
- All Nginx configuration files are located in the
/etc/nginx/
directory. - The main Nginx configuration file is
/etc/nginx/nginx.conf
. - Creating a separate configuration file for each domain makes the server easier to maintain.
- The Nginx server block files must end with
.conf
and be stored in/etc/nginx/conf.d
directory. You can have as many server blocks as you want. - It is a good practice to follow a standard naming convention. For example, if the domain name is
mydomain.com
then the configuration file should be namedmydomain.com.conf
- If you use repeatable configuration segments in your domain server blocks, it is a good idea to refactor those segments into snippets.
- Nginx log files (
access.log
anderror.log
) are located in the/var/log/nginx/
directory. It is recommended to have a differentaccess
anderror
log files for each server block. - You can set your domain document root directory to any location you want. The most common locations for webroot include:
/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>
/usr/share/nginx/html
Conclusion
Congratulations, you have successfully installed Nginx on your CentOS 8 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server.