How to Install Nginx on CentOS 8
Choose a different version or distribution
Introduction
Before we begin talking about how to install Nginx on CentOS 8, let's briefly understand – What is Nginx?
Nginx is a high-performance web server and reverse proxy server known for its speed, scalability, and easy configuration.
Free, open-source, high-performance HTTP and reverse proxy server Nginx—pronounced "engine X"
- is in charge of managing the traffic on some of the busiest websites on the Internet.
In this tutorial, you will install and manage Nginx on your CentOS 8 machine. We will also address a few FAQs on how to install Nginx on CentOS 8.
Advantages of Using Nginx on CentOS 8
- High Performance: Nginx is designed to handle high loads and perform efficiently even under heavy traffic conditions. It is known for its ability to handle many concurrent connections while consuming minimal system resources.
- Reverse Proxy and Load Balancing: Nginx can act as a reverse proxy, distributing incoming client requests to multiple backend servers for load balancing and increased reliability. It can handle dynamic content caching, connection management, and fault tolerance.
- Easy Configuration and Scalability: Nginx's configuration is simple and easy to understand, allowing you to quickly set up and manage multiple websites or applications on a single server. It supports horizontal scalability by adding more server nodes to handle increased traffic.
- SSL/TLS Support: Nginx provides native support for SSL/TLS encryption, allowing you to secure your websites with HTTPS. It supports the latest encryption protocols and can efficiently handle SSL/TLS termination.
- Caching and Performance Optimization: Nginx provides caching capabilities to improve performance and reduce server load. It can cache static content, compress responses, implement browser caching, and handle content delivery efficiently.
Prerequisites
Make sure that Apache or any other service is not currently operating on port 80 or 443, and that you are signed in as a user with Sudo rights.
Installing Nginx on CentOS
The installation of Nginx on a CentOS server may be accomplished by following the instructions below:
1) Packages for Nginx may be found in the EPEL archives. This will install the EPEL repository if it hasn't previously been done:
sudo yum install epel-release
2) To install Nginx, run the yum command shown below:
sudo yum install nginx
When installing a package from the EPEL repository for the first time, yum could ask you to import the EPEL GPG key:
Output
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-9.noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]:
If so, type y
and press Enter
.
3) When the installation is finished, you may start the Nginx service by typing:
sudo systemctl enable nginx
sudo systemctl start nginx
Use the following command to see the Nginx service's current status:
sudo systemctl status nginx
The output should look something like this:
Output
● 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 Mon 2018-03-12 16:12:48 UTC; 2s ago
Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1673 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1680 (nginx)
CGroup: /system.slice/nginx.service
├─1680 nginx: master process /usr/sbin/nginx
└─1681 nginx: worker process
4) In order for your server to be accessible, you must allow traffic across both the HTTP (80)
and HTTPS (443)
ports if it is behind a firewall.
In order to enable the required services, run the following commands:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
5) Open http://YOUR IP
in your browser of choice and you should see the default Nginx welcome page, as seen in the picture below, proving that your Nginx installation was successful:
Manage the Nginx Service with systemctl
The Nginx service may be controlled in the same manner as any other systemd unit.
Run the following command to terminate the Nginx service:œ
sudo systemctl stop nginx
To start it again, type:
sudo systemctl start nginx
To restart the Nginx service :
sudo systemctl restart nginx
After you have made any configuration changes, reload the Nginx service:
sudo systemctl reload nginx
If you don't want the Nginx service to start when the computer boots:
sudo systemctl disable nginx
And to re-enable it again:
sudo systemctl enable nginx
Nginx Configuration File’s Structure and Best Practices
/etc/nginx/
directory contains all Nginx configuration files./etc/nginx/nginx.conf
is the main Nginx configuration file.- It is advised to construct a distinct configuration file for each domain in order to make the Nginx setup easier to maintain.
- New Nginx server block files must be kept in the
/etc/nginx/conf.d
directory and must finish with.conf
. As many server blocks as you require are permissible. - It is a good idea to adhere to a consistent naming convention. For instance, your configuration file should be titled
/etc/nginx/conf.d/mydomain.com.conf
if your domain name ismydomain.com
. - Creating a directory called
/etc/nginx/snippets
, restructuring repeated configuration portions into snippets, and then adding the snippet file to the server blocks is a smart practice if you utilize server blocks for several domains. -
/var/log/nginx/
directory contains the Nginx log files (access.log
anderror.log
). It is preferred to have a differentaccess
anderror
log files for each server block. - Your domain's document root directory can be adjusted to any location you like. Webroot can be found most frequently in:
/home/<user_name>/<site_name>
,/var/www/<site_name>
,/var/www/html/<site_name>
,/opt/<site_name>
,/usr/share/nginx/html
FAQs on Installing Nginx on CentOS 8
Why choose Nginx as a web server?
Nginx offers several advantages, including high performance, low resource usage, scalability, easy configuration, and the ability to handle concurrent connections efficiently. It can serve static and dynamic content and handle reverse proxy configurations with ease.
What are the prerequisites for installing Nginx on CentOS 8?
To install Nginx on CentOS 8, you need a CentOS 8 server with administrative access and a working internet connection. It is recommended to have a basic understanding of Linux commands and networking concepts.
How do I install Nginx on CentOS 8?
The installation process for Nginx on CentOS 8 involves adding the Nginx repository, importing the GPG key, installing the Nginx package using dnf
or yum
, and starting the Nginx service.
How can I configure Nginx after installation?
After installing Nginx, the main configuration file is located at /etc/nginx/nginx.conf
. You can modify this file to customize Nginx's behavior, such as configuring server blocks, enabling SSL/TLS, setting up load balancing, or creating reverse proxy configurations.
Can Nginx serve multiple websites on the same server?
Yes, Nginx can serve multiple websites on the same server using server blocks. Each server block represents a separate website or application with its own configuration settings.
What are the advantages of using Nginx on CentOS 8?
Using Nginx on CentOS 8 provides several advantages, including high performance, resource efficiency, support for reverse proxy and load balancing, easy configuration, SSL/TLS support, caching capabilities, access controls, and extensive community support.
Can I use Nginx with other web servers like Apache?
Yes, it is possible to use Nginx alongside other web servers like Apache to improve performance and handle specific tasks efficiently. This configuration is known as a reverse proxy setup. Nginx can act as a front-end proxy server, while Apache handles the application code and processing.
Conclusion
You have completed the Nginx installation on your CentOS 8 server successfully. You may now use Nginx as a web or proxy server and begin deploying your apps. Learning how to configure Nginx server blocks is essential if you want to host numerous domains on a single CentOS server.
These days, no website can get away without installing some kind of trusted security certificate.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.