Nginx is one of the most popular web servers in the world and is responsible for hosting some of the most popular websites on the planet. Nginx (pronounced as Engine-x) is used as an alternative to Apache, and it consumes fewer resources and improves overall server responsiveness.
The main factor that makes it resource-friendly is its event-driven design. Above all, it allows admins to set up advanced configurations and deal with a high load of concurrent connections. Besides being a good fit for an HTTP web server, Nginx works as a reverse proxy, load balancer, and standard mail server.
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 include this in the command, you will not be prompted with confirmation of commands. You can omit this part; when prompted, you can always enter y and press ENTER.
Step 2: Install the EPEL repository
With the default CentOS repositories, Nginx is not available. Therefore, we will 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 the Nginx service
Installation is done, but Nginx won’t start automatically. Enter the following command to start Nginx:
sudo systemctl start nginx
Enter the following command to check if Nginx is active and running successfully. You will see an output similar to the one below.
sudo systemctl status nginx
If you don’t see an “active” sign, then Nginx might not have started successfully. You should troubleshoot and try it again! One reason could be that 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
This step will configure the CentOS firewall to allow incoming traffic. CentOS 7 enables firewalls and blocks access to ports 80 and 443. 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
You should see a success message under each command if all the configurations were done.
Step 7: verifying Nginx
Everything is set. Now we need to check whether Nginx is running properly. The easiest way is by entering your server’s public IP address on your browser.
If you are unsure of your IP address, 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, and 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. The default Nginx service listens to IPv4 and IPv6 addresses, and the test will fail if your server doesn’t support IPv6.
The solution is to modify the main Nginx configuration file. Open the file using the nano command:
Find the following part and comment on 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 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 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, 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 run Nginx 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 encounter any problems along the way, feel free to reach us through the dedicated comment section below.
People also read: