Nginx pronounced “engine x” is an open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites 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.
This tutorial explains how to install and manage Nginx on CentOS 8.
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 443.
Installing Nginx on CentOS 8
Starting with CentOS 8, the Nginx package is available in the default CentOS repositories.
Installing Nginx on CentOS 8 is as simple as typing:
sudo yum install nginx
Once the installation is complete, enable and start the Nginx service with:
sudo systemctl enable nginx
sudo systemctl start nginx
To verify that the service is running, check its status:
sudo systemctl status nginx
The output should look something like this:
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-10-06 18:35:55 UTC; 17min ago
...
Adjusting the Firewall
FirewallD is the default firewall solution on Centos 8.
During the installation, Nginx creates a 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 Configuration File’s Structure and Best Practices
- 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//
/var/www/
/var/www/html/
/opt/
/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.
You can manage the Nginx service in the same way as any other systemd unit.
If you have any questions or feedback, feel free to leave a comment.