How to setup NGINX on a CentOS 6 or 7 VPS Server

Posted on February 01, 2017

NGINX has a smaller draw on your VPS’s resources than its rival Apache, but like Apache in the majority of cases it can be used as a reverse proxy. NGINX has proved itself to be an extremely popular web server solution, and it can be found hosting some of the largest and traffic-heavy sites on the net.

 

Should I use Nginx or Apache?

Generally speaking, if you are looking to run a single site with an advanced configuration at lightning fast speeds, NGINX is the way to go. If you are looking to host many sites with more flexibility and an easier configuration, then Apache is the better route to take.

 

Okay, I want to setup NGINX, what’s next?

There are a few things that you will need before we start setting up your VPS with NGINX:

  • A CentOS 6 or CentOS 7 server
  • SSH access and an SSH client (Putty for example), and check you can connect via SSH
  • A domain (in the examples given below we have used yoursite.com, replace this with your domain)
  • A browser

 
Once you have those, we can get started.

 

Installing Nginx

All of the major Linux distros now come with NGINX by default, however if your server has an existing Apache configuration you can run into problems. So, the first step is to disable Apache on your server.

 

1 – Log into you server through SSH, and then get to the root user by running the following command:

sudo su –

 

2 – Next, run the following command to shut down Apache:

service httpd stop

 

3 – Then we need to remove Apache from your servers boot cycle, this will stop it from trying to start up again when your server boots up. Enter the following command:

chkconfig httpd off

 

Now that Apache is shut down and won’t be attempting to restart we can start installing NGINX.

4 – Use the following command to add the EPEL-Release yum repository, which will contain NGINX.

yum install epel-release

 

5 – Once the repository is installed on your server, you can use yum to install. Use the following command:

yum -y install nginx

 

6 – Then start NGINX with this command:

service nginx start

 

7 – This next command will configure NGINX to start up on the next reboot:

chkconfig nginx on

 

Open you browser and go to the IP address of you server, you should now see an NGINX test page.

 

Setting up and configuring NGINX to serve for your domain.

First of all, you need to create a UNIX user for the webspace.

 

1 – Run the following command to create a user, replacing USERNAME with the name of the new user:

useradd USERNAME

 

2 – Then run this command to add a password, replacing PASSWORD with a suitable password:

passwd PASSWORD

 

As you type in the password, the characters will not show in the terminal window.

 

Next, you need to create a directory for the DocumentRoot of the site. It’s good practice to use the standard naming convention.

1 – Make the document root based on the public_html, like in the example below:

mkdir -p /var/www/yoursite.com/public_html

 

2 – Create a test index file in this directory, this will help us with testing later.

vim /var/www/yoursite.com/public_html/index.html

 

3 – Add some copy inside h1 tags to the index.html file, something along the lines of:

Huzzah! NGINX is serving on this domain!

 

4 – Next step, give the user in question ownership of this directory:

chown -R yoursite:yoursite /var/www/yoursitesite.com/public_html

 

5 – In order for this folder to be seen by the outside world, you need to set the permissions for this folder:

chmod 755 /var/www/yoursite.com/public_html

 

Now you have setup a directory and you have an index.html for testing.

 

Configuring NGINX to recognize new server blocks

In Apache server blocks are referred to as VirtualHosts, and the process for configuring server blocks in NGINX is similar to the process for configuring VirtualHosts in Apache.

 

1 – Set up the directories that will contain the server blocks by running the following code:

mkdir /etc/nginx/sites-available

 

2 – Open the nginx.conf file in a text editor (in this example we are using vim):

vim /etc/nginx/nginx.conf

 

3 – Add the following lines to the end of the http{} block, then save the file:

include /etc/nginx/sites-enabled/*.conf;

server_names_hash_bucket_size 64;

 

Once this is done, NGINX can recognize the server block.

 

Configuring the NGINX server blocks

1 – First, you need to create a file for the server block for your site, use the command below to do this and open it in vim.

vim /etc/nginx/sites-available/yoursite.com.conf

 

2 – Copy and paste the following code, this is a new NGINX server block.

server {
listen 80;
server_name yoursite.com www.yoursite.com;
location / {
root /var/www/yoursite.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;
}
}

 

In the code above, the server_name part is where you put the domain you’ll be using for your site. You need to use the public facing domain and the www version, like in this example:

server_name yoursite.com www.yoursite.com;

 

The root part of the code is where you can set the directory where your sites files will be, like in the example below:

root /var/www/yoursite.com/public_html;

 

The try_files part of the code is added to the location block, and it tells the server to display a 404 error when a page or file can’t be found. In the code it needs to be directly underneath the index definition and before the closing } bracket:

try_files $uri $uri/ =404;

 

When you have set these parameters you can close and save the file and move on to the next step.

 

3 – Next, you need to create a symbolic link between sites-available and sites-enabled, use the following code:

ln -s /etc/nginx/sites-available/yoursite.com.conf /etc/nginx/sites-enabled/yoursite.com.conf

 

4 – With that done, the final step is to restart NGINX:

service nginx restart

 

And you’re done! You now have NGINX set up on your VPS!

 

If you have any questions then please get in touch on our contact page.

 

 

Comments are closed.