This brief tutorial is going to show you how to install Nginx stable and mainline versions in Ubuntu as well as CentOS. If you’re reading this post, then you probably know a thing or two about Nginx web server so I won’t spend much time defining it or go into details here.

The one thing I will say about Nginx is that Nginx is a lightweight, fast and great web server. We recently switched to Nginx from Apache2, and Nginx has been fantastic. Our web pages are loading fast, our server is stable and I can now focus on other things than worrying about the stability of our servers.

I am not saying Apache2 isn’t great. In fact, Apache2 is the most popular web server online today and many more people are installing it everyday. For those already on Apache2 and loving it, there’s no need to switch to other web servers. If you feel that other web severs might serve you better, then it’s up to you to switch.

For those who want to install or switch to Nginx web server, you can continue below to learn how to install it in Ubuntu or CentOS.

If you’re on a CentOS server, then you certainly must configure Nginx repository or repositories that contain Nginx packages. For those using Ubuntu, Nginx packages are already included in Ubuntu default repositories, however, the version the comes with Ubuntu is only the stable version.

The stable version of Nginx is a step or two behind the current mainline version. If you need the latest version of Nginx, then you’ll have to install the mainline repository in Ubuntu or CentOS.

 

  • Installing Nginx Stable Version

There are two official channels you can install Nginx from. One is from the stable repository which contains packages that are have been tested and proven to be stable in most environments.

New features being developed to support Nginx web server are usually not added to packages in the stable repository, unless it’s a security update or patche.

Packages in the stable repository do not change very often, they’re stable, more reliable and proven to work. If you’re cautious and want to operate your server on the safe side, then you should install Nginx packages from its stable repository.

 

  • Installing Nginx from the stable repository in CentOS

To install Nginx from the stable repository in CentOS, run the commands below to create Nginx repository.

sudo vi /etc/yum.repos.d/nginx.repo

 

Then add these lines as shown below in the file and save it.

 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Replace OSRELEASE the release number of CentOS. (ex. 5 / 6 / 7)

Next, run the commands below to install Nginx

sudo yum install nginx

 

 

  • Installing Nginx from the stable repository in Ubuntu

In Ubuntu all you have to do is run the commands below. Nginx stable version is already added to the default Ubuntu repository.

sudo apt-get install nginx

 

 

  • Installing Nginx Mainline Version

The next channel to Nginx from is the Mainline or formerly known as the development version. This repository contains the latest and greatest Nginx releases.

Every new feature is added to the mainline repository first and after it has been tested and proven to be stable, before the repository switches to be stable.

If you’re not overly cautious and wish to install the latest and greatest Nginx packages and features, you may have to install Nginx from it Mainline repository.

 

  • Installing Nginx from the mainline repository in CentOS

To install Nginx from the mainline repository in CentOS, run the commands below to create nginx repository file in CentOS

sudo vi /etc/yum.repos.d/nginx.repo

 

The add these lines into the file and save it.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Replace OSRELEASE with CentOS release number. (ex 5 / 6 / 7)

 

Nginx run the commands below to install Nginx.

sudo yum install nginx

 

  • Installing Nginx from the mainline repository in Ubuntu

To install Nginx from the mainline repository in Ubuntu, run the commands below to create a Nginx repository file.

First, get Nginx repository signing key installed. To do that, run the commands below.

wget http://nginx.org/keys/nginx_signing.key

Next, run the commands below to install the key.

sudo apt-key add nginx_signing.key

After that, run the commands below to create Nginx repository file

sudo vi /etc/apt/sources.list.d/nginx.list

 

Then add the lines shown below into the file.
deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx

Replace codename with Ubuntu codename for releases  (ex. 14.04 is known as trusty)

 

Then run the commands below to install nginx

sudo apt-get install nginx

 

That’s it!

Enjoy!