How to Install and Configure Nginx on CentOS 7

What is Nginx?
Nginx (pronounced ‘engine X’) is an alternative to Apache and is considered to be one of the most popular HTTP web servers in the world. As opposed to Apache, Nginx tends to consume fewer resources and improves server responsiveness. Its event-driven design is what makes it resource-friendly.
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 article, you will learn how to install and configure Nginx on CentOS 7.
how to install nginx on centos7
Prerequisites
  • A CentOS self-managed server or VM
  • A Domain. You will need to connect a domain to your Nginx web server
  • Sudo or root privileges [root@user ~]
  • SELinux set up properly

How to Install Nginx on CentOS 7

Generally, CentOS is the preferred distribution package in the hosting industry. Currently, CentOS 8 is the latest version, while CentOS 7 is supported until the year 2024.
The configuration procedure below details the steps necessary to set up Nginx on CentOS 7.
For installation instructions for CentOS 8, please read How to Install Nginx on CentOS 8.

Step 1: Update Repository Package Lists

To do so, run the following command:
sudo yum -y update
-y is an optional parameter. It serves to avoid confirmation questions. We will be using it a lot, as you will notice. If you do not use this parameter, you will be prompted to confirm your commands.

Step 2: Install Extra Packages for Enterprise Linux (EPEL)

Nginx is not available in the standard repositories that come with the CentOS package, so you will need to install the EPEL repository on your server. EPEL is free to use and provides numerous open-source packages to install with Yum.
To install EPEL, run the following command using the Yum package manager:
sudo yum install -y epel-release
Installing the EPEL repository so that Nginx can be installed.

Step 3: Install Nginx

Step number two has added the Nginx repository to your server. Now, you can install Nginx by running the following yum command:
sudo yum –y install nginx
Install Nginx on CentOS via terminal.

Step 4: Start Nginx Service

Your instance of Nginx is installed, but it won’t start automatically.
To start Nginx, run:
sudo systemctl start nginx
After you hit enter, you will not get a return value. However, Nginx should have started. To check its status, run:
sudo systemctl status nginx
Test the status of your Nginx service to verify it's active.
There should be a green piece of text that readsactive (running).’ If you can’t find it, your instance of Nginx might not have started successfully.

Note: If you already have an Apache server running, you will need to disable it before starting Nginx. Use the sudo service httpd stop command. Bear in mind that disabling Apache will bring down any currently hosted website.
You disabled Apache, but it still might start automatically during server reboot. Disable automatic starts by running the following command:
sudo systemctl disable httpd

Step 5: Configure Nginx to Start on Boot

Most admins will want to make sure that NGINX starts up automatically whenever the server restarts. To set this up, run:
sudo systemctl enable nginx

Step 6: Configure Firewall to Allow Traffic

CentOS 7 enables firewalls by default and blocks access to ports 80 and 443. It will block any inbound HTTPS and HTTP packets from Nginx.
To allow HTTP and HTTPS traffic, run the following commands:
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload
After each command, you should see success indicating that the command was executed correctly.
Modifying the firewall on CentOS to allow HTTP and HTTPS traffic.

Step 7: Verify Nginx Install

The easiest way to check whether Nginx is running properly is by visiting your server’s public IP address. Just open your web browser and visit http://server_IP_or_domain_name/
First, find your server’s public IP address.
ip a
Find your server's IP address to access the Nginx page.
Our IP address is 192.168.122.1/24. Copy your IP address and paste it into your browser. This should load the Welcome to Nginx page.
The default nginx landing page.

How to Resolve the 'test failed' Error

If you receive a ‘test failed’ error message for the nginx.conf file, you might be facing an IP address issue.
The Nginx service listens to IPv4 and IPv6, by default. Your test will fail if your server doesn’t support IPv6. However, this can be fixed by modifying the main configuration file.
Open the main configuration file, /etc/nginx/nginx.conf. Find and comment out the following line:
listen [::]:80 default_server;
Just add ‘#’ at the beginning of the line. It should look like this:
# listen [::]:80 default_server;
Save the changes you have made and reload the Nginx service.
sudo systemctl reload nginx
Browse to your server’s IP address in your browser. You should see the default Nginx test page.

Nginx Configuration Files and Root

You will most definitely need to know the location of Nginx configuration files and the default Nginx server root directory.

Additional Server Blocks

On Apache, admins use virtual hosts to run multiple websites. With Nginx, running several websites on a single server is accomplished with server blocks.
Additional server blocks are added by creating new configuration files with a .conf file extension. Place these files in /etc/nginx/conf.d and they will be loaded each time Nginx starts.

Default Nginx Server Root

The default Nginx server root directory is /usr/share/nginx. This is specified in the default server block configuration file, located at /etc/nginx/conf.d/default.conf.
The default server document root directory which contains web files is usr/share/nginx/html.

Global Configuration

Global configurations can be tweaked by modifying the main Nginx configuration file located at /etc/nginx/nginx.conf. This configuration file is broken down into contexts. By default, you can identify three (3) contexts.
  1. Events are global settings that define how Nginx handles connections in general.
  2. HTTP defines how the server handles HTTP and HTTPS connections.
  3. Server is defined within the HTTP context. It specifies server ports, document root, etc.
You can always add additional contexts.

Managing Nginx – Common Tasks

Stop Nginx

To stop an Nginx server, run:
sudo systemctl stop nginx

Restart Nginx

To restart the Nginx service, run:
sudo systemctl restart nginx

Reload Nginx

If you made some modifications to the Nginx main configuration changes. You need to reload the server to propagate the changes.
sudo systemctl reload nginx

Disable Automatic Start on Boot

If you don’t want Nginx to automatically start on boot, run:
sudo systemctl disable nginx

Configuring a New Directory

If you are hosting multiple websites, a good rule of thumb is to follow standard naming conventions. Let’s use cPanel’s standard naming and create a directory.
sudo mkdir -p /var/www/yourdomain.com/public_html
Next thing you need is an index page. This will help you test the configuration.
sudo nano /var/www/yourdomain.com/public_html/index.html
For testing purposes, input a single line of text in index.html. Save and close the file.
Change Linux file permissions, so that data can be accessed online.
sudo chmod 755 /var/www/yourdomain.com/public_html
Try opening the index.html page. It should be available online.
Conclusion
After successfully installing Nginx on CentOS 7, many opt to fully install LEMP stack, an alternative to the well-known LAMP stack. LEMP is an acronym; each letter stands for a single open-source component.
  • L – Linux OS
  • E – Nginx (pronounced as you would pronounce ‘engine x’)
  • M – MySQL or MariaDB relational database management system
  • P – PHP for hosting dynamic websites
Each component ‘sits on top of the other.’ Your Linux OS serves as the base layer.
On top of that sits Nginx, your web daemon. The relational database management system stores all the data processed and served by your web daemon. Finally, PHP allows users to interact with that data.
For newer versions of CentOS, check out our article on installing and configuring Nginx on CentOS 8.

Comments

Popular posts from this blog

Cách đăng nhập VPS bằng SSH trên MAC OS

Cách kiểm tra Website trên VPS sống hay chết