RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


How to Install and Configure Nginx Web Server

This tutorial discusses how to install and configure Nginx web server on a CentOS VPS and how to migrate from Apache to Nginx.


Although Apache is the most widely used web server software, Nginx is becoming more and more popular. Nginx is a free and open-source web server optimized for high concurrency, performance and low memory usage, meaning that the same server configuration would be able to handle more users when Nginx is installed. This performance increase is useful for medium and large websites, websites that have occasional spikes in traffic, as well as owners of multiple websites or VPS owners. Nginx web server can also be used as a reverse proxy server and as a load balancer.

In this tutorial, we will discuss how to install and configure Nginx web server on a CentOS VPS, as well as how to migrate from Apache to Nginx.

Installing Nginx

Nginx can be installed from the yum package manager. However, we need to add EPEL and Remi repositories:

sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 

Then we can install Nginx:

yum install nginx 

Before configuring Nginx, we need to install the PHP-FPM package (PHP FastCGI process manager) that is used by Nginx web server:

yum install php-fpm php-common 

If any of these commands throw a "No package found" error, double-check that the EPEL and Remi repositories are enabled:

yum repolist 

If the repositories are not listed, try running the commands the following way:

yum --enablerepo=remi,remi-php55 install nginx 

After both Nginx and PHP-FPM are installed, let's start the PHP-FPM:

chkconfig php-fpm on
service php-fpm start 

At this point, Nginx web server is installed and ready to use. However, we will not start it immediately, as we are migrating from Apache and need to configure it first.

Migrating from Apache to Nginx

The most common migration scenario is that Apache is already running on a live website. Since we would want to minimize downtime in that case, we will set up Nginx on a different port during the migration. Once we are 100% ready, we will switch Nginx to port 80 and shut down the Apache web server.

So, let's edit the default configuration:

vi /etc/nginx/conf.d/default.conf 

Note that vi is the default text editor on UNIX operating systems (read more on how to use it here). Now, find the following lines in the configuration file and set the port to 8000:

server {
               listen  8000 default_server;

Save the file and then start Nginx:

chkconfig nginx on
service nginx start 

You can use the following commands to stop or restart the web server:

service nginx stop
service nginx restart 

If everything went well, you will see the Nginx default page after typing your server's address in your browser:


After Nginx is installed, we need to translate Apache configuration to it. This is done manually and the process mostly consists of translating Apache virtual hosts to Nginx server blocks. The server blocks are located in:


You could add server block either to this file or to create a separate .conf file for each block. Check the table below to see how the main Apache directives are converted to Nginx directives.

Note that each Nginx directive is written in a new line and ends with a semicolon. A sample server block would look like this (more info can be found here, in Nginx documentation):

http {
  index index.html;
  server {
    server_name www.domain1.com;
    access_log logs/domain1.access.log main;
    root /var/www/domain1.com/htdocs;
  server {
    server_name www.domain2.com;
    access_log  logs/domain2.access.log main;
    root /var/www/domain2.com/htdocs;

After creating your server block, restart the Nginx web server for the changes to take effect. Once you have created all server blocks, tested everything and made sure it works as it should, you are ready to switch to Nginx. Just change the port from above from 8000 to 80, and run the following commands:

service apache2 stop
service nginx reload 

For possible complications during the migration process, check the following tutorial.

At this point, Nginx will be running on your live website. Enjoy the increased performance!

Vojislav is a web developer, designer and entrepreneur, based in Belgrade, Serbia. He has been working as a freelancer for more than 6 years, having completed more than 50 projects for clients from all over the worlds, specializing in designing and developing personal portfolios and e-commerce websites using Laravel PHP framework and WordPress content management system. Right now, he works as a full-time senior web developer in a company from Copenhagen.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date