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 7. We will also cover some basic configuration options and how to secure your Nginx server. By the end of this tutorial, you will have a fully functional Nginx web server running on your CentOS 7 server.
How to Install and Configure Nginx on CentOS 7
1. Update the system:
sudo yum update
2. Install Nginx:
sudo yum install nginx
3. Start the Nginx service:
sudo systemctl start nginx
4. Enable Nginx to start on boot:
sudo systemctl enable nginx
5. Check the status of Nginx:
sudo systemctl status nginx
6. Configure Nginx:
sudo vi /etc/nginx/nginx.conf
7. Test the Nginx configuration:
sudo nginx -t
8. Restart Nginx:
sudo systemctl restart nginx
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.
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
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
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
There should be a green piece of text that reads ‘active (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.
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
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.
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.
- Events are global settings that define how Nginx handles connections in general.
- HTTP defines how the server handles HTTP and HTTPS connections.
- 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.
How to Install and Configure Nginx on CentOS 7
Nginx is a popular open source web server and reverse proxy. It is used to serve static content quickly and efficiently, and is also used as a load balancer and reverse proxy. In this tutorial, we will show you how to install and configure Nginx on a CentOS 7 server.
Prerequisites
Before you begin, you will need to have a CentOS 7 server with a non-root user with sudo privileges. You can learn how to set up a user with these privileges by following our Initial Server Setup with CentOS 7 guide.
Step 1 — Installing Nginx
Nginx is available in the default CentOS 7 repositories. We can install it by running the following command:
sudo yum install nginx
Once the installation is complete, start the Nginx service and enable it to start on boot with the following commands:
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
You should see the following output:
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2016-07-07 15:45:45 UTC; 1min 11s ago
Main PID: 2887 (nginx)
CGroup: /system.slice/nginx.service
├─2887 nginx: master process /usr/sbin/nginx
└─2888 nginx: worker process
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 options for the Nginx server. You can edit this file to configure the server.
By default, Nginx is configured to serve files from the /usr/share/nginx/html directory. You can change this by editing the root directive in the /etc/nginx/nginx.conf file.
You can also create virtual hosts to serve multiple websites from a single server. To do this, create a new configuration file in the /etc/nginx/conf.d directory. For example, if you want to create a virtual host for example.com, create a file named example.com.conf and add the following content:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
}
Save the file and restart the Nginx service for the changes to take effect:
sudo systemctl restart nginx
Conclusion
In this tutorial, we have shown you how to install and configure Nginx on a CentOS 7 server. You can now configure your server to serve your websites.