Tutorial

How To Install Nginx on Ubuntu 14.04 LTS

Published on April 30, 2014
How To Install Nginx on Ubuntu 14.04 LTS
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy.

In this guide, we’ll discuss how to get Nginx installed on your Ubuntu 14.04 server.

Prerequisites

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following steps 1-4 in our initial server setup guide for Ubuntu 14.04.

When you have an account available, log in as your non-root user to begin.

Step One — Install Nginx

We can install Nginx easily because the Ubuntu team provides an Nginx package in its default repositories.

Since this is our first interaction with the apt packaging system in this session, we should update our local package index before we begin so that we are using the most up-to-date information. Afterwards, we will install nginx:

sudo apt-get update
sudo apt-get install nginx

You will probably be prompted for your user’s password. Enter it to confirm that you wish to complete the installation. The appropriate software will be downloaded to your server and then automatically installed.

Step Two — Check your Web Server

In Ubuntu 14.04, by default, Nginx automatically starts when it is installed.

You can access the default Nginx landing page to confirm that the software is running properly by visiting your server’s domain name or public IP address in your web browser.

If you do not have a domain name set up for your server, you can learn how to set up a domain with DigitalOcean here.

If you do not have a spare domain name, or have no need for one, you can use your server’s public IP address. If you do not know your server’s IP address, you can get it a few different ways from the command line.

Try typing this at your server’s command prompt:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

You will get back one or two lines. You can try each in your web browser to see if they work.

An alternative is typing this, which should give you your public IP address as seen from another location on the internet:

curl http://icanhazip.com

When you have your servers IP address or domain, enter it into your browser’s address bar:

<pre> http://<span class=“highlight”>server_domain_name_or_IP</span> </pre>

You should see the default Nginx landing page, which should look something like this:

Nginx default page

This is the default page included with Nginx to show you that the server is installed correctly.

Step Three — Manage the Nginx Process

Now that you have your web server up and running, we can go over some basic management commands.

To stop your web server, you can type:

sudo service nginx stop

To start the web server when it is stopped, type:

sudo service nginx start

To stop and then start the service again, type:

sudo service nginx restart

We can make sure that our web server will restart automatically when the server is rebooted by typing:

sudo update-rc.d nginx defaults

This should already be enabled by default, so you may see a message like this:

System start/stop links for /etc/init.d/nginx already exist.

This just means that it was already configured correctly and that no action was necessary. Either way, your Nginx service is now configured to start up at boot time.

Conclusion

Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

Learn how to use Nginx server blocks here. If you’d like to build out a more complete application stack, check out this article on how to configure a LEMP stack on Ubuntu 14.04.

<div class=“author”>By Justin Ellingwood</div>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
20 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi Justin, Thanks for the tutorials. I’m currently looking for a tutorial for installing the latest stable version of nginx on 14.04 using PPA release. Im not sure if any steps have changed since last tutorial here; https://www.digitalocean.com/community/articles/how-to-install-the-latest-version-of-nginx-on-ubuntu-12-10

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
May 5, 2014

@desiredpersona: The PPA used in that tutorial also provides packages for use on 14.04. So, you shouldn’t have any issues using it.

Andrew SB: Thanks

Pardon my newbie question, but which server should I choose - Apache or nginx? Or what considerations should I make to chose one?

Or, given that my droplet is mainly a playground for micro-web-apps and speed is not an issue, should i even bother? =)

Apache gives you the ability with .htaccess to apply folder rules in each folder without needing admin access, great for team work or development servers, but then you can just convert the rules into the master NGINX file to have the same affect. You can also install Ajenti, it will give you a panel that you can edit the NGINX and APACHE files, so you can run both or only one of them but easy editing.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 25, 2014

@kibostoz: There are pros and cons to both Apache or nginx. While I’m partial to nginx, I generally tell people to use the one they’re most comfortable with. Though for your particular case, it sounds like nginx is probably the way to go. If you’re just using the web server as a proxy in front of an app listening on some other port, Nginx is a great choice. It’s what it was originally designed to do.

@astarr: Thank you! I guess, I’ll give it a shot then!

I switched to using Nginx for all of my servers a few years ago and it is VERY good! The clean configuration syntax is a joy to work with. It’s like switching from using XML to JSON as an data interchange format :-)

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2014

Switching from XML to JSON is a great comparison!

$ nginx -V

This installs nginx 1.4.6

Latest stable version is 1.6 as of this writing. Can I get this on my ubuntu 14.04 without compiling? As in just upgrade to it?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 23, 2014

@SaM5246: See How To Install The Latest Version Of Nginx On Ubuntu 12.10. It should work fine on any version of Ubuntu.

Once you’ve installed nginx, here are the locations of important files:

nginx is installed in folder: /etc/nginx

nginx config file: /etc/nginx/nginx.conf

The default site is served from: /usr/share/nginx/html

Default site config info: /etc/nginx/sites-available/default

Thanks @aral

Glad you found it useful, Gabriel — the little details matter ;)

Am I able to install and use nginx in a development environment without a custom domain name?

Justin Ellingwood
DigitalOcean Employee
DigitalOcean Employee badge
October 9, 2014

Definitely. You can substitute your server’s IP address anywhere where it asks for a domain name.

Thanks a lot for the manual. Everything works like a charm, except font-faces. I use font-awesome and can’t get it working with nginx, however it worked with apache. Here is the config I use:

# URL: add a permanent redirect if required.
server {
    # URL: Correct way to redirect URL's
    server_name site.info;
    rewrite ^/(.*)$ http://www.site.info/$1 permanent;


}
server {
    server_name www.site.info;
    root /usr/share/nginx/html/site;
    access_log /var/log/nginx/site.com.access.log;
    error_log /var/log/nginx/site.com.error.log;
    # custom rules

        location ~* \.(?:ico|css|js|gif|jpe?g|png|ttf|svg|woff)$ {
               add_header Access-Control-Allow-Origin *;
 }

}

May be someone faced and solved the problem?

Try

# This order might seem weird - this is attempted to match last if rules below fail.
location / {
        try_files $uri $uri/ /index.php?$args;
         add_header Access-Control-Allow-Origin "*";
#       add_header 'Access-Control-Allow-Credentials' 'true';
#       add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken';
#       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';


}

location ~ \.(ttf|ttc|otf|eot|woff|font.css|css)$ {
           add_header Access-Control-Allow-Origin "*";
}

I found it somewhere ;)

I’m very new to this, I followed all instructions but on step 2, when I try to visit the webpage for my server’s IP address, instead of the default nginx landing page I get an error page that says “The connection has timed out.”

I double checked to make sure nginx is running (it is). I have it set up on a virtual box and when I ping the IP address from the virtual machine it gets a response, but when I ping from my windows machine it does not, I’m not sure if that’s helpful or not. I am visiting the webpage on my windows machine.

Did you ever find a solution to this? I’m having the same issue.

When I ran sudo update-rc.d nginx defaults, I got an error:

The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'mongod' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `mongod'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `mongod'

Is there a new way of doing this now?

Justin Ellingwood
DigitalOcean Employee
DigitalOcean Employee badge
January 19, 2015

@danyll: From the output, it looks like you’re trying to set up MongoDB, which this tutorial does not cover.

If the MongoDB init script is using Upstart, (which Ubuntu currently uses for many of its services), you do not use update-rc.d.

You can tell if the MongoDB script is an Upstart script by seeing if this command works:

status mongod

If it return a status, that means the Upstart system has a script for it.

The service will probably be configured to automatically start at boot already. If you need to disable that functionality, you can type:

echo "manual" | sudo tee /etc/init/mongod.override

This will create the file mongod.override with the contents “manual”, telling the system not to automatically start MongoDB. If you want to later re-enable the service, simply delete this file:

sudo rm /etc/init/mongod.override

Hope that helps.

Thank you @jellingwood You have been a great help! Thank you!

Thanks, for this ;')!

A nice tool to pair with NGINX or Apache on the server is Ajenti as it allows you to login and edit, restart and modify the NGINX and Apache configs on the fly in a GUI. Once installed it will auto detect Apache or NIGINX and will give you a menu item to edit them with. Great for messing around with NGINX for the first time and the configs.

Can You provide Tutorials for centOS 6 with cpanel?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.