How to Install and Configure Nginx on CentOS 7
Nginx is a high-performance, open-source software for web serving. It can be used as a proxy server for email (IMAP, POP3, and SMTP), as a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Install Nginx on CentOS 7
– Update Repository Package Lists
Use the following command to update the repository package lists:
yum update
– 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:
yum install epel-release
– Install Nginx
Now, you can install Nginx by running the following command:
yum install nginx
– Start Nginx as a Service
Set up Nginx to start automatically at a server boot.
You can do this by running the following command:
systemctl enable nginx
Nginx does not start on its own. To start Nginx, type the following command:
systemctl start nginx
Make sure the service had no errors by running:
systemctl status nginx
– Configure Firewall to Allow Traffic
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
– Verify Nginx is working fine
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://your_server_IP/
Configure Nginx on CentOS 7
– Nginx Global Configuration
Nginx primary configuration file is /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.
– 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
.
– Nginx Server Blocks
Nginx does not have Virtual hosts to run multiple websites like Apache, it has “Server Blocks” that 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.
– Check for syntax errors
It is very important to check for any syntax errors when configuring Nginx. This can be done by issuing the following command:
nginx -t
– Reload Nginx
If you made some modifications to the Nginx main configuration changes. You need to reload the server to propagate the changes.
systemctl reload nginx