The main factor that makes it resource-friendly is its event-driven design. Above all, it allows admins to set up advanced configurations and can deal with a high load of concurrent connections. Besides being a good fit for an HTTP web server, Nginx also works as a reverse proxy, load balancer, and standard mail server.
In this tutorial, we will teach you how to install and start Nginx on CentOS 7. The methods mentioned here will be suitable for installing Nginx on CentOS 8 also.
Before getting with the tutorial, you will need:
- A CentOS 7 server
- A root user or a user with root privileges
- Basic understanding of Linux commands
Step 1: Update repository package list
Let’s go ahead and update the package list so that we will get the most recent updated Nginx software installed on your server.
sudo yum -y update
Here –y is an optional parameter. When you will include this in the command, you will not be prompted with confirmation of commands. You can omit this part and when prompted you can always enter y and press ENTER.
Step 2: Install EPEL repository
With the default CentOS repositories, Nginx is not available. Therefore we will go ahead and install the EPEL repository (Extra Packages for Enterprise Linux) on the server. This is free to use repo with numerous open-source packages that can be installed using Yum.
Enter the following command:
sudo yum install -y epel-release
Step 3: Install Nginx
Now everything is set, let’s go ahead and install Nginx on your server using the yum command:
sudo yum –y install nginx
Step 4: Start Nginx service
Installation is done, but Nginx won’t start automatically. Enter the following command to start Nginx:
sudo systemctl start nginx
To check if Nginx is active and running successfully, enter the following command. You will see an output similar to the one below.
sudo systemctl status nginx
If you don’t see an “active” sign, then Nginx night not have started successfully. You should troubleshoot and try it again! One reason could be because you already have an Apache server running. You should disable it before starting Nginx.
sudo service httpd stop
Note: keep in mind that disabling Apache will bring down any currently running websites.
If Apache is still running automatically during the reboot, you can disable it using the following command:
sudo systemctl disable httpd
Step 5: make Nginx start on reboot
If you are going to use Nginx, most likely you want it to be running during each reboot and not activate it manually. It’s simple, enter the following command to make Nginx start during reboot:
sudo systemctl enable nginx
Step 6: Configure the Firewall
In this step, we are going to configure the CentOS firewall to allow incoming traffic. By default CentOS 7 enables firewalls by default and blocks access to ports 80 and 443. This means that any incoming HTTP and HTTPS packets for Nginx will be blocked.
To allow the Nginx traffic, enter the following commands:
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
If all the configurations were done, you should see a success message under each command.
Step 7: verifying Nginx
Everything is set. Now we need to check whether Nginx is running properly. The easiest way to do that is by entering your server’s public IP address on your browser.
If you are not sure of what your IP address is, here’s how you can find the server’s IP address.
You will see your IP address under eth0. Once you get your IP address, enter it on your browser, you will see a result similar to the one below.
Common error – ‘test failed’ solution
If you will receive a test failed error message for the nginx.conf file, you might have a problem with the IP address. Be default Nginx service listens to IPv4 and IPv6 addresses by default and if your server doesn’t support IPv6 then the test will fail.
The solution is to modify the main Nginx configuration file. Open the file using the nano command:
Find the following part and comment it using a # symbol in front of it:
# listen [::]:80 default_server;
Save the changes by exiting the file. To save the file, press CTRL + X, press Y and then press ENTER.
How to manage Nginx Processes
If you want to stop your web server:
sudo systemctl stop nginx
Now to start the server once again you can enter the following command:
sudo systemctl start nginx
If you would like to restart the server, enter the following command:
sudo systemctl restart nginx
If you would like to restart the server when you are making configuration changes without dropping the connection, enter the following command:
sudo systemctl reload nginx
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behaviour by typing:
sudo systemctl disable nginx
To re-enable during start-up, enter:
sudo systemctl enable nginx
These commands are the most basic commands that you can use to manage the server!
Server root and configuration files
If you want to start serving your own pages or application through Nginx, you will want to know the locations of the Nginx configuration files and default server root directory.
Default Server Root
The default server root directory is /usr/share/nginx/html. Files that will be placed in this folder will be shown on your web server. This location is specified in the default server block configuration file that ships with Nginx, which is located at /etc/nginx/conf.d/default.conf.
Server Block Configuration
Any additional server blocks commonly known as Virtual Hosts in Apache can be added by creating new configuration files in /etc/nginx/conf.d. Files that end with .conf in that directory will be loaded when Nginx is started.
The Nginx configuration directory is at /etc/nginx. All of the Nginx configuration files are here.
The main Nginx configuration file is here /etc/nginx/nginx.conf. To make changes to the Nginx global configuration, use this file.
Nginx Global Configuration
The main Nginx configuration file is located at /etc/nginx/nginx.conf. This is where you can change settings like the user that runs the Nginx daemon processes, and the number of worker processes that get spawned when Nginx is running, among other things.
Every request to your web server is recorded in this log file /var/log/nginx/access.log unless Nginx is configured to do otherwise.
Any Nginx errors that will be found will be recorded in this log: /var/log/nginx/error.log
Congratulations on having set up your very own Nginx server! You made all the configurations and security measures to have Nginx running successfully on your server. We reviewed how to manage the Nginx service running on your server, and what are the main directories used by Nginx to store configuration files, content, and logs.
If you will come across any problems along the way, feel free to reach us through the dedicated comment section below. We also made an article on how to set up Nginx on Ubuntu servers, check it out.