How to Install Nginx Mainline on Ubuntu Server

Nginx has two branches: mainline version and stable version. It is recommended that in general you deploy the NGINX mainline branch at all times. This article will show you how to install the mainline branch on ubuntu server.

Install Nginx Mainline Branch on Ubuntu

First, remove your existing Nginx Installation.

sudo apt-get remove nginx nginx-common nginx-full nginx-core

Next install Nginx PGP key on your system.

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

sudo apt-key add nginx_signing.key

After that edit /etc/apt/sources.list file.

sudo vi /etc/apt/sources.list

Add the following two lines at the end of the file. Replace codename with your specific ubuntu codename, such as trusty for ubuntu 14.04.

deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx

During the installation process, Nginx will automatically install new configuration files. So before the installation you should backup the main config file /etc/nginx/nginx.conf. Your server block file /etc/nginx/sites-available/yourdomain.com will be changed, so you don’t have to back up that file.

sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old

Now update local package index and install Nginx mainline branch.

sudo apt-get update && sudo apt-get install nginx

Once the installation is completed, check your nginx version.

user@www:~$ nginx -v
nginx version: nginx/1.9.8

Configure Your New Nginx Server

By default, the new config file /etc/nginx/nginx.conf set the user as nginx.

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

Make sure your PHP pool file www.conf has the same user as defined in nginx.conf, or your website will be broken.  And also add the following two lines in nginx.conf file.

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;

Then reload your nginx configuration.

 sudo service nginx reload

Now your website should be running as usual. If you don’t want to use the new config file, just copy the old configurations to nginx.conf.

Rate this tutorial
[Total: 4 Average: 3.8]

2 Responses to “How to Install Nginx Mainline on Ubuntu Server

  • Hi Xiao,

    Would you still recommend installing mainline via this method or via the ondrej/nginx-mainline PPA?

    Thanks,
    Matt

  • sherlock_61_
    11 months ago

    Very helpful.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here