Ways to install Nginx on CentOS 7

Before we begin our journey, we must prepare a CentOS 7 Linux Server, if you don’t have one, you can consider getting one from Linode for very cheap price. Or you can follow along with your existing CentOS 7 Linux Server if you have one. If you are ready, then let’s begin our High-Performance Journey!

There are two ways of installation:

1. Nginx package installation

This option is suggested to the newcomers since they don’t know much details about what exactly Nginx use for, so the first thing they need to do is to quickly install it and start playing around with it.

Add Nginx repo into yum.repos.d directory

Create nginx.repo under yum.repos.d directory

1
sudo vi /etc/yum.repos.d/nginx.repo

Paste in the following content:

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

Update the local yum repository

‌Update the local repo:

1
sudo yum update

Installation begins

Install Nginx:

1
sudo yum install nginx -y

Start Nginx and enable it at boot

After the installation, we start Nginx immediately, and we should enable Nginx at boot:

1
sudo systemctl start nginx && sudo systemctl enable nginx

Check if it is working properly

Make sure Nginx has started both master and worker processes:

1
ps -ef | grep nginx

The results of running the command should be similar to the screenshot listed below:

img

Nginx Master & Worker Processes

Check if it is listening on port 80

Make sure Nginx is listening on port 80:

1
sudo netstat -anp | grep nginx

The results of running the command should be similar to the screenshot listed below:

img

Nginx Master & Worker Processes

Check to see if it is actually working

Finally, let’s also make sure we can visit Nginx’s default home page:

1
curl http://localhost

Result should look like:

img

Nginx Master & Worker Processes

2. Nginx source code installation

Source code installation is recommended for more complex situations, for example: customizing prefix directory, include necessary modules and exclude unnecessary modules.

Download source code

Download the source code to tmp directory:

1
2
3
4
yum install wget -y    # Install wget
cd /tmp    # Go to tmp directory.
wget http://nginx.org/download/nginx-1.14.2.tar.gz    # Download .tar.gz file to current directory.
tar -xzf nginx-1.14.2.tar.gz    # Unpack the compressed file.

Configure a path to install

1
2
cd nginx-1.14.2
./configure --prefix=/etc/nginx    # install binary files to /etc/nginx directory

Compile to executable files

1
2
yum install gcc -y
make    # make compilation based on the built configuration.

Copy executable files to the configured prefix directory

1
make install    # This copies all the built executable files to the prefix directory.

References: Pre-Built Packages for Stable version, Building nginx from Sources

If you think the content of this article has helped you, and if you would like some more high-quality materials in the near future, please give me some modest support.

Buy me a coffeeBuy me a coffee