How to Install and Configure Nginx Web Server

How to Install and Configure Nginx Web Server

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 rpm -Uvh 

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 onservice 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.

See also  Are Deceased Social Media Accounts Outnumbering the Living?

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 onservice nginx start 

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

service nginx stopservice 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;    access_log logs/domain1.access.log main;     root /var/www/;  }   server {    server_name;    access_log  logs/domain2.access.log main;     root /var/www/;  }} 

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 stopservice nginx reload 

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

See also  Strengthening Business Cybersecurity With Threat Intelligence

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


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist