Hosting 101 – How To Use Nginx As Your Web Server

18th May, 2016 by
In the standard LAMP stack model for web hosting, Apache is often used as the web server component. As the most popular web server in the market, this is a fairly sensible decision to make, but there are some good alternatives.

Nginx Web Server

The second most popular web server is Nginx. Nginx was designed to be a fast and efficient web server when handling high loads, and as such is a popular choice for when a website is scaling up to high usage or is on a platform with limited resources available such as a VPS or low end dedicated server. As well as operating as a web server, Nginx is also capable of operating as a reverse proxy for many protocols and as a load balancing application.

Nginx: Static vs Dynamic Content

Having started out in 2002 as a project to create a more efficient web server for www.rambler.ru one of Russia’s most popular websites – the Nginx web server is a mature and stable product used by many websites across the world. Nginx’s main performance improvements come when serving static content rather than dynamic content – this is something to be mindful of when planning to use it in a deployment. For those unfamiliar with the concept, static content refers to files that the web server will read directly from the server’s storage and transmit to the web browser, so this normally refers to images, videos, documents, and plain HTML and CSS files. Dynamic content, on the other hand, means content that is generated on the fly by a script and then passed to the web server to be transmitted to the web browser, such as Perl or PHP files. The performance difference is down to a lot of the time being spent serving dynamic content is based on how long it takes the script to generate the web page to be served.

Nginx Limitation of Local Configuration Files

One key limitation of Nginx is that it cannot have any settings overruled by local configuration files. Where Apache makes use of .htaccess files that allow users the ability to make limited changes to how they want the web server to work on their directories, Nginx only reads configuration from its main configuration file and as such can only be configured by the systems administrator. This can make Nginx unsuitable for use on shared hosting systems where user-level configuration may be desired.

How to Install Nginx on Debian and Ubuntu

Installation on Debian/Ubuntu is as simple as:

sudo apt-get remove apache2

sudo apt-get install nginx

Note that if you are testing on a server that already has Apache installed then you will need to remove that first or the install will fail. If you are installing to a fresh server then you can skip the command to remove Apache.

Once Nginx is installed, configured and running with the basic settings, it will be similar to the Apache default configuration on the system and will be able to serve static content. Configuration is performed by editing the files in /etc/nginx with the main configuration in the nginx.conf file and domain-specific configuration in files within the sites-available directory in a similar manner to Apache.

How to Install Nginx on CentOS and Red Hat

Installing Nginx on CentOS/Red Hat the process is fairly similar, though Nginx is provided by the Fedora Project’s EPEL repositories rather than in the base repositories. Information about the EPEL repositories is available at https://fedoraproject.org/wiki/EPEL, and once you have the repositories configure you can use the following commands to install Nginx.

sudo yum remove httpd

sudo yum install nginx

You then need to start Nginx with:

sudo service nginx start

At this point, you should then be able to be able to contact the Nginx web server on your server using your web browser. Note that all the configuration for Nginx on CentOS is done through the main /etc/nginx/nginx.conf file as default though it is set up to support configuration snippets within the /etc/nginx/conf.d directory in a somewhat similar manner to Apache.

(Visited 145 times, 1 visits today)