How to install and configure NGINX on CentOS 7

Installing NGINX on CentOS 7

Step 1: Turning Apache off

Every major Linux distribution comes packages with Apache by default; its literally integrated into the OS by now (similar to how Windows comes packaged with IIS natively). However, since we are setting up a dedicated space for NGINX, its possible that the existing Apache configuration can cause problems when NGINX is put in its place. What we are going to do is turn Apache off, then configure Apache so that it does not start upon server reboot.

Turning Apache off on a server with live sites will bring those sites down. Act accordingly.

  1. Log into your server via SSH, then get to the root user by running:

sudo su -

NOTE: We will remain as the root user for the remainder of the guide.

  1. Shut Apache Down. This will bring down any current websites that are hosted on the server.

service httpd stop
  1. Now we need to remove Apache from the boot cycle, so that it doesn’t try to start up during server boot.

systemctl disable httpd

NOTE: If you have buyers remorse later on about NGINX, and want Apache to start on boot again, you can easily correct this previous command by running:

systemctl enable httpd

Apache is now fully shut down, and won’t be starting up again until we say so.

Step 2: Install NGINX

Now that Apache is riding off into the sunset, we can start to install NGINX.

  1. First, we need to add the CentOS EPEL package so that we can install NGINX.

yum install epel-release
  1. Now that our repository is installed on the server, we can now use yum to install NGINX, like so:

yum -y install nginx
  1. Start NGINX.

service nginx start
  1. Configure the server to start NGINX upon reboot.

systemctl enable nginx

You should now be able to see an NGINX test page by going to, using your IP address for your server.

Step 3: Configure NGINX to serve for your domain

Alrighty, we’ve switched from the Apache schooner to the NGINX steamboat. Now it’s time to get it working for your domain. Before doing anything, we need to create a UNIX user for your webspace.

Create a new user for the webspace

  1. Type the following command to create your user:

To demonstrate, I’ll add my user, nginxsite: 

useradd nginxsite
  1. Give this user a password:

For our example:

passwd nginxsite
  1. You will then be prompted to set the password for this user. Your characters won’t register in the terminal when you type — that’s just Linux protecting you by not logging the password entry. Follow safe password practices.Your user should now be properly set up.

Create a new directory for the site DocumentRoot

  1. Next, we need to create the directory that will act as the DocumentRoot for this website. It is a good idea to follow a standard naming convention if you are hosting multiple websites. We’ll follow the standard used by cPanel, and make our DocumentRoot based on the name public_html, like so:
mkdir -p /var/www/nginxsite.com/public_html
  1. Let’s create a test index.html in this directory so that we have something to look at when we test the configuration later:
vim /var/www/nginxsite.com/public_html/index.html
  1. Use the HTML below the fold for this test index file:



Success! Nginx is properly serving on this domain!


  1. Now that our directory and test index is created, we must give ownership of that directory over to the user in question. So following our previous example:
chown -R nginxsite:nginxsite /var/www/nginxsite.com/public_html
  1. We need to also set permissions for this folder so that it can be viewed by the outside world:
chmod 755 /var/www/nginxsite.com/public_html

Our directory is now set up, and we have a test index.html file to use.

Configure NGINX to recognize new VirtualHosts (Server Blocks)

Now for the fun part. Configuring a VirtualHost for NGINX is very similar to Apache, though the layout of the configuration file is a bit different. Also, in NGINX, they are referred to as ‘server blocks,’ and not the Apache VirtualHost label. It is worth noting that when editing an Apache configuration file we are editing XML. With NGINX, we are actually editing the C code.

  1. First, we need to set up our directories where the server blocks will live:
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Note: In theory, instead of doing this by having a directory tree, you could simply edit the global configuration file. However, by setting up a directory tree (which is what Debian-based Linux distros, such as Ubuntu will do), it allows for an easier configuration down the line as more website are added.

  1. Now we need to tell NGINX to use look at those directories for the server blocks. Open the global NGINX configuration file in the text editor of your choice. We will use vim: 
vim /etc/nginx/nginx.conf
  1. Add these lines to the end of the http {} block, then save the file:
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Great. Now NGINX can recognize the server block.

Configure the actual NGINX server blocks

  1. Create a new file specifically for the server block for your site. The line below will do this and open it in vim.
vim /etc/nginx/sites-available/nginxsite.com.conf
  1. We are going to paste a new NGINX server block in here, which should look like this:
server {
  listen       80;
  server_name  nginxsite.com www.nginxsite.com;
  location / {
    root   /var/www/nginxsite.com/public_html;
    index  index.html index.htm;
    try_files $uri $uri/ =404;
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;

Let’s break down a few important parts of this.

  • server_name. This is the domain you will be using for your site. Instead of localhost, we will use the public facing domain and www version of the domain you want to use, like so:
server_name  nginxsite.com www.nginxsite.com;
  • root. This should be set to the directory where the files live. In our example this can be changed to /var/www/nginxsite.com/public_html
root /var/www/nginxsite.com/public_html;
  • try_files. This is something we need to add in the location block. What we are doing here is telling the server to display a 404 error when a given file is not found. So you’ll place this right under the index definition, before the closing } bracket:
try_files $uri $uri/ =404;

Create your server block using these parameters, then go ahead and save and close the file.

  1. We need to create a symbolic link between sites-available and sites-enabled:
ln -s /etc/nginx/sites-available/nginxsite.com.conf /etc/nginx/sites-enabled/nginxsite.com.conf

4. Time to restart NGINX:

service nginx restart

You’re done! Provided your DNS and/or hosts file is pointed for your domain, you should now be able to go to the domain in a web browser and see the test HTML page we created earlier.

Leave a Reply