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!

Frequently Asked Questions

How to install Nginx stable version in Ubuntu?

To install Nginx stable version in Ubuntu, you can simply use the default Ubuntu repositories for package installation. Run the necessary commands to install Nginx and start using it.

What is the main difference between Nginx stable and mainline versions?

The main difference between Nginx stable and mainline versions is that the stable version is a few steps behind the mainline version in terms of features and updates. Mainline versions offer the latest features but may be less stable compared to the stable version.

How can I switch from Apache2 to Nginx in CentOS?

To switch from Apache2 to Nginx in CentOS, you need to uninstall Apache2 first and then install Nginx using the appropriate repositories or packages. Make sure to configure Nginx properly after installation.

Are Nginx mainline versions suitable for production environments?

While Nginx mainline versions offer the latest features, they may not be as stable as the stable versions. It is recommended to thoroughly test mainline versions before deploying them in production environments.

What are the benefits of using Nginx over Apache2?

Nginx is known for its lightweight and fast performance, making it a popular choice for high-traffic websites. Compared to Apache2, Nginx consumes less memory and can handle more concurrent connections efficiently.

Can I install Nginx mainline version in Ubuntu without using external repositories?

To install Nginx mainline version in Ubuntu without external repositories, you may need to manually download the source code and compile it. However, using official repositories is the recommended method for easier maintenance and updates.

How do I check the Nginx version installed on my Ubuntu server?

You can check the Nginx version installed on your Ubuntu server by running the command 'nginx -v' in the terminal. This will display the installed Nginx version along with other relevant information.

Is it possible to run both Apache2 and Nginx on the same server?

Yes, it is possible to run both Apache2 and Nginx on the same server. You can configure them to listen on different ports or use a reverse proxy setup to distribute incoming traffic between the two web servers.