How to Install and Configure Nginx on CentOS 8
Posted on
TABLE OF CONTENTS
Nginx is widely recognized as a top-tier web server, crucially hosting numerous high-traffic and renowned websites across the Internet. In most cases, it is more efficient in terms of resource usage than Apache. It can serve as a web server or reverse proxy.
Nginx is a widely used, open-source HTTP web server commonly used to host websites with high traffic. It is more efficient and uses fewer resources than other web servers.
The software handles requests individually and uses a flexible event-driven (asynchronous) architecture. In addition to functioning as a web server, it serves as a reverse proxy, mail proxy, HTTP cache, and load balancer.
Requirements
- An operating system based on CentOS 8
- Provide a server IP or domain for connecting to your Nginx web server.
- An individual with administrative access
- SELinux configured correctly
Install Nginx on CentOS 8
Before any installation, it is crucial to update the local repository to guarantee that you are acquiring the most up-to-date software. Here is the command you can use:
sudo yum update
You can examine the Nginx package before installing it on your system. I would like to request access to the RPM metadata included in every RPM package.
sudo yum info nginx
Afterwards, you can proceed to install Nginx on CentOS 8 by using the following command:
sudo yum install nginx
The output displays a list of installed Nginx packages, similar to the image provided.
Just a heads up: Nginx is a popular web server option that can be used instead of Apache. If you’re still uncertain about which option would be most suitable for your website, you might find it helpful to review this brief comparison of Apache and Nginx.
How to start Nginx on Centos 8
Despite having Nginx installed, the service won’t initiate on its own.
To initiate the service, enter the following command:
sudo systemctl start nginx
To ensure that the service starts running automatically during boot time, use the following command:
sudo systemctl enable nginx
When you review the service status, the output will indicate that Nginx is active and running.
sudo systemctl status nginx
Pause, refresh, or initiate a new session for Nginx
To halt Nginx, execute the following command:
sudo systemctl stop nginx
To restart Nginx, you can use the following command:
sudo systemctl restart nginx
Reload the configuration files while the service is still running:
sudo systemctl reload nginx
Make changes to the firewall settings
Nginx comes with firewalld service files that indicate the service utilizes ports 80 (HTTP) and 443 (HTTPS) for web traffic. Thus, it is necessary to open and enable these ports in order to allow permanent access.
Open the HTTP and HTTPS ports using the following commands:
sudo firewall-cmd --permanent --zone=public --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services --zone=public
Test the Firewall
Take advantage of Netstat to display a comprehensive list of active ports and confirm the successful opening of ports 80 and 443:
netstat -tulpn
Ensure that Nginx is functioning properly by accessing your public IP address (or domain name).
To view the IP address of your server, enter the following command in the terminal:
ip addr
Locate the IP address and make a copy of it.
Next, open a web browser and enter the IP address (or domain name) into the URL bar. Opening the Nginx Welcome Page will confirm that the server has been installed and set up successfully.
Set up Nginx
No configuration of Nginx is required upon installation. It’s important to be familiar with the location of the configuration files and the Nginx root directory in case any modifications to the configuration are necessary.
- The directory for Nginx configuration is located at /etc/nginx.
- The Nginx root directory is located at /usr/share/nginx/html, while the master/global configuration file can be found at /etc/nginx/nginx.conf.
To modify the Global configuration file, you can simply open it (etc/nginx/nginx.conf) using a text editor and make the necessary changes.
A typical scenario involves modifying the Nginx configuration file to direct HTTP traffic towards HTTPS.
In conclusion
By now, you should have a good understanding of how to install Nginx on CentOS 8.
Nginx is a crucial component of the LEMP stack, a compilation of open-source software used to create web applications and websites. LEMP is a widely used alternative to the conventional LAMP stack. There is a distinction between the two: the first employs Nginx as its web server, whereas the second relies on Apache.
Mastering the deployment of Nginx on CentOS 8 equips you with a robust toolset for optimizing web server performance in high-traffic environments. Nginx stands out for its asynchronous, event-driven architecture, ensuring efficient resource utilization and scalability. As part of the LEMP stack, it’s vital for tech professionals aiming to deploy dynamic web applications with enhanced speed and security. This guide navigates you through the installation and setup and fortifies your server with essential security tweaks, ensuring that your deployment is both potent and secure. Ready to leverage Nginx’s capabilities? Dive into your server configuration and prepare to experience web performance that aligns with your needs.
FAQs
Nginx is an open-source web server that also serves as a reverse proxy, mail proxy, HTTP cache, and load balancer. It is known for its high performance, stability, scalability, and low resource consumption.
Nginx is often favored for its efficiency and its ability to handle large numbers of concurrent connections due to its event-driven (asynchronous) architecture. This makes it particularly suitable for high-traffic websites and applications.
Before installing Nginx, ensure that your system runs CentOS 8, has a server IP or domain, and that the user has administrative access with SELinux configured correctly.
Use the command `sudo yum install nginx` after updating your system with `sudo yum update`. This installs the latest version of Nginx available in the repositories.
After installing Nginx, start the service using `sudo systemctl start nginx` and enable it on boot with `sudo systemctl enable nginx`.
To check the status of Nginx, use the command `sudo systemctl status nginx`. This will show whether Nginx is active and running.
You can stop Nginx with `sudo systemctl stop nginx`, restart it using `sudo systemctl restart nginx`, and reload its configuration files without dropping connections with `sudo systemctl reload nginx`.
You need to open HTTP and HTTPS ports (80 and 443) to allow web traffic. This can be done with `sudo firewall-cmd –permanent –zone=public –add-service=http –add-service=https` followed by `sudo firewall-cmd –reload`.
After configuring firewall settings, verify by accessing your server’s public IP address or domain name in a web browser. Successful loading of the Nginx Welcome Page indicates a correct setup.
Nginx’s main configuration files are located in `/etc/nginx`, with the primary global configuration file found at `/etc/nginx/nginx.conf`. The root directory for HTML files is `/usr/share/nginx/html`.