Introduction
Nginx is a powerful web server that is used to serve web content to users. It is a popular choice for web hosting due to its scalability, reliability, and performance. In this tutorial, we will show you how to install and configure Nginx on CentOS 8. We will cover the installation process, configuration of the Nginx server, and how to manage the server. By the end of this tutorial, you will have a fully functional Nginx server running on your CentOS 8 system.
How to Install and Configure Nginx on CentOS 8
1. Update the system:
Before installing any packages it is recommended that you update the system with the latest version. To do this, run the following command:
sudo yum update -y
2. Install Nginx:
Once the system is updated, you can install Nginx using the following command:
sudo yum install nginx -y
3. Start and enable Nginx:
Once the installation is complete, you can start and enable Nginx using the following commands:
sudo systemctl start nginx
sudo systemctl enable nginx
4. Configure Nginx:
Once Nginx is installed and running, you can configure it by editing the configuration file located at /etc/nginx/nginx.conf.
5. Test Nginx:
Once the configuration is complete, you can test Nginx by opening a web browser and navigating to http://localhost. If everything is working correctly, you should see the default Nginx welcome page.
Introduction
Nginx (pronounced Engine X) is a popular, open-source HTTP web server, used for hosting high-traffic websites. It’s faster and requires fewer resources than other web servers.
The software uses a scalable event-driven (asynchronous) architecture, approaching requests one at a time. Apart from a web server, it also works as a reverse proxy, mail proxy, HTTP cache, and a load balancer.
In this tutorial, you will learn how to install Nginx on CentOS 8.
Prerequisites
- A CentOS 8 operating system
- A server IP or domain to connect to your Nginx web server
- A user with root privileges
- SELinux set up properly
Install Nginx on CentOS 8
Before any installation, always update the local repository to ensure you are downloading the latest software. Use the command:
sudo yum update
You can inspect the Nginx package before adding it to your system. Request to see the RPM metadata included in every RPM package:
sudo yum info nginx
Next, install Nginx on CentOS 8 with the command:
sudo yum install nginx
The output shows you a list of Nginx packages that have been installed, as in the image below.
Note: Nginx is a well-known web server alternative to Apache. If you are still unsure which one would be best for your website, you may want to check out this quick comparison between Apache and Nginx.
Start Nginx on Centos 8
Although you have installed Nginx, the service will not start automatically.
Start the service by typing:
sudo systemctl start nginx
To enable the service to start running upon boot time use:
sudo systemctl enable nginx
If you check the service status, the output should show you Nginx is active (running):
sudo systemctl status nginx
Stop, Reload or Restart Nginx
Stop Nginx using the command:
sudo systemctl stop nginx
Restart Nginx (stop and start the service again) with the command:
sudo systemctl restart nginx
Reload the configuration files without stopping the service:
sudo systemctl reload nginx
Adjust Firewall
Nginx includes firewalld service files that specify the service uses ports 80 (HTTP) and 443 (HTTPS) for web traffic. Therefore, you need to open and enable these ports to allow permanent access.
Open port HTTP and HTTPS with the 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
Use Netstat to list all open ports and verify whether you have successfully opened 80 and 443:
netstat -tulpn
Double-check Nginx is working by visiting your public IP address (or domain name).
To see the IP address of your server, type the following command in the terminal:
ip addr
Find the IP address and copy it.
Then, open a web browser and paste the IP address (or domain name) in the URL bar. This should open the Nginx Welcome Page, confirming you have successfully installed and set up the server.
Configure Nginx
You don’t need to configure Nginx upon installation. However, you should know the location of the configuration files and the Nginx root directory in case you need to modify the configuration.
- Nginx configuration directory: /etc/nginx
- Nginx root directory: /usr/share/nginx/html
- Master/Global configuration file: /etc/nginx/nginx.conf
If you wanted to change the Global configuration file, you would open it (etc/nginx/nginx.conf) with a text editor and apply the changes.
One common use case is editing the Nginx configuration file to redirect HTTP traffic to HTTPS.
Conclusion
You should now know how to install Nginx on CentOS 8.
Nginx is part of the LEMP stack, a collection of open-source software used for developing web applications and websites. LEMP is a popular alternative to the traditional LAMP stack. The only difference between the two is that the first uses Nginx, while the second one uses Apache as its web server.
How to Install and Configure Nginx on CentOS 8
Nginx is a popular open-source web server and reverse proxy. It is used to serve static content, handle requests from upstream servers, and provide secure connections. In this tutorial, we will show you how to install and configure Nginx on CentOS 8.
Prerequisites
- A server running CentOS 8.
- A root user or a user with sudo privileges.
Step 1 – Installing Nginx
Nginx is available in the default CentOS 8 repositories. You can install it using the following command:
sudo dnf install nginx
Once the installation is complete, start the Nginx service and enable it to start at boot time:
sudo systemctl start nginx
sudo systemctl enable nginx
You can check the status of the Nginx service with the following command:
sudo systemctl status nginx
Step 2 – Configuring Nginx
Nginx configuration files are located in the /etc/nginx directory. The main configuration file is /etc/nginx/nginx.conf. This file contains the main configuration settings for Nginx. You can edit this file to configure Nginx as per your requirements.
By default, Nginx is configured to serve web pages from the /var/www/html directory. You can create a new file in this directory and add some HTML content to it. This will be the default page that Nginx will serve when you access your server.
sudo nano /var/www/html/index.html
Add the following content to the file:
<html>
<head>
<title>Welcome to Nginx!</title>
</head>
<body>
<h1>Welcome to Nginx!</h1>
</body>
</html>
Save and close the file when you are finished.
Now, open your web browser and type the URL http://your_server_ip. You should see the default page that you created.
Step 3 – Securing Nginx
It is recommended to secure your Nginx web server. You can do this by enabling the firewall and allowing only the necessary ports. To enable the firewall, run the following command:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
Now, you can check the status of the firewall with the following command:
sudo firewall-cmd --list-all
You should see the following output:
public (active)
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client http
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
You have successfully installed and configured Nginx on CentOS 8.