You may already know Nginx is the best open source web server to serve static files. It is also an alternative to an Apache, most of the time faster than Apache server. Nginx web server has become popular due to its event-driven design which can make better use of today’s computer hardware that Apache’s process-driven design. The end result is that nginx can serve more concurrent clients with higher throughput than Apache on the same hardware.

How To Install Nginx on your Web Server with CentOS 7 3

Nginx excels at serving static content, it can do it more efficiently than Apache can, but Apache can serve static content quickly as well. Static content is the easy stuff for any web server so high traffic sites strive to generate static content from their more complicated back-end systems in order to fulfill the demand of their users quickly. Throw PHP in the mix and things get a lot more difficult.

In my CentOS 7 server, I recently installed Nginx on my web server. If you are looking to install Nginx on CentOS 7 here is how,

How To Install Nginx on CentOS 7

Before you proceed installation of Nginx, make sure your server is updated with stable CentOS 7

Step One—Add Nginx Repository

To add the CentOS 7 Nginx yum repository, open terminal and use the following command:

sudo rpm -Uvh

Step Two—Install Nginx on your Web Server

Now that the Nginx repository is installed on your centos 7 server, install Nginx using the following yum command:

sudo yum install nginx

After you answer yes to the prompt, Nginx will finish installing on your virtual private server (VPS).

Step Three—Start Nginx on your Web Server

Nginx does not start on its own. To get Nginx running, type:

sudo systemctl start nginx.service

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):


You will see the default CentOS 7 Nginx web page, which is there for informational and testing purposes. It should look something like this:

CentOS 7 Nginx Default

If you see this page, then your web server is now correctly installed.

Before continuing, you will probably want to enable Nginx to start when your system boots. To do so, enter the following command:

sudo systemctl enable nginx.service

Congratulations! Nginx is now installed and running!

How To Find Your Server’s Public IP Address

You can run the following command to reveal your server’s public IP address:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Server Root and Configuration

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 are placed in there will be served 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 (known as Virtual Hosts in Apache) 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.

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.