Tutorial

How To Install nginx on Ubuntu 12.04 LTS (Precise Pangolin)

Published on June 1, 2012
How To Install nginx on Ubuntu 12.04 LTS (Precise Pangolin)

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About nginx

nginx is a high performance web server software. It is a much more flexible and lightweight program than apache.

Set Up

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Step One—Install nginx

To install nginx, open terminal and type in:

sudo apt-get install nginx

When prompted, say yes. nginx is now installed on your virtual private server.

Step Two—Start nginx

nginx does not start on its own. To get nginx running on your VPS, type:

sudo service nginx start

Step Three—RESULTS: Confirm That nginx Has Started

You can confirm that nginx has been installed as your web server by directing your browser to your IP address.

**You can run the following command to reveal your virtual server’s IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'

When you visit your IP address page in your browser, you will see the words, “Welcome to nginx”

You can see a screenshot of the utilitarian nginx welcome page here

To ensure that nginx will be up after reboots, it’s best to add it to the startup. Type this command into terminal:

update-rc.d nginx defaults

You may see a message like:

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

If that is the case, then nginx is set up to run on startup, and you are all set.

Congratulations! You have now installed nginx

See More

Once you have installed nginx on your virtual private server, you can do a variety of things on your server such as Set Up Virtual Hosts or Create an SSL certificate for your site

By Etel Sverdlov

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 author(s)

Etel Sverdlov
Etel Sverdlov
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
28 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!

For the last step in setting up nginx to startup after reboots, you may need to install chkconfig first. sudo apt-get install chkconfig

Then proceed with the last two commands: sudo chkconfig --add nginx sudo chkconfig nginx on

Than you for the the suggestion.

The tutorial has been updated to reference just update-rc.d which is an ubuntu command that does not need any additional installation.

There is an another alternative, in my opinion better than remembering the path, of ‘sudo /etc/init.d/nginx start’

‘sudo service nginx start’ -will do the same thing.

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
August 24, 2012

Absolutely right, Etel updated the article =]

With 12.10, I am getting the following errors when I try to start nginx:

Starting nginx nginx nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] still could not bind()

It’s not loading

@info - perhaps apache2 is installed? if debian / ubuntu enter: service apache2 status and if it comes back Apache2 is running (pid …) Then you likely have apache configured to run off of port 80.

Another possible solution is found in /etc/nginx/sites-available/default. Do the following and see if it clears it up (I’m running of memory, don’t know if you NEED sudo or not):

sudo vim /etc/nginx/sites-available/default

Look for the following lines: listen 80; listen [::]:80 default_server;

Change them so they read (hit i to enter insert mode): listen 80; listen [::]:80 ipv6only default_server;

Sorry should say ipv6only=on

I was also getting the same error. nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) and when i typed the localhost in the browser, then i was getting

It works!

This is the default web page for this server.

The web server software is running but no content has been added, yet. instead of nginx welcome page

that is apcache2 is running on the same port, so the solution is given below

1- find the apache2 ports.conf file

sudo /etc/apache2/ports.conf

2- change the port other then 80 , i make it as 70

3- save the file

4- restart your system

it will works for you also, if you type the localhost in the browser , you will get nginx welcome page

– Thanks

Saurabh

I’d like to recompile nginx with --with-http_realip_module activated. Having followed these instructions to get things up and running (and then further tweaked and optimised), what’s the best way forward?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
September 10, 2013

@craig: Back up your nginx.conf file and compile nginx (with --prefix set to /etc/nginx). You can then rename nginx.conf.bak back to nginx.conf and restart nginx.

I believe that should work. Let me know how it goes.

Nothing is written to /var/log/nginx/access.log What might be the reason?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
December 5, 2013

@cancandan: Make sure that access logs are enabled in /etc/nginx/nginx.conf.

how to add http_stub_status_module?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
December 14, 2013

@ondiz: It should already be compiled in. You can confirm by running <pre>nginx -V</pre>.

Hi, Currently my server is running fine. I am using Wordpress and LEMP.

I am able to view my website using http://mydomain.com. But if I view it as http://www.mydomain.com (with the WWW), my website will not load.

How do i set both http://www.mydomain.com and http://mydomain.com to load my wordpress post?

Not so sure if this is the right tutorial to ask, but i’m guessing it has something to do with nginx. I do not have Apache, so i cannot get the .htaccess to work. If possible, i don’t want to download Apache, is there another way to go about doing this? thanks!

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
December 21, 2013

@hopefloatt: Add a virtualhost as follows: <a href=“https://p.kk7.me/verumeciba.nginx”>https://p.kk7.me/verumeciba.nginx</a>

I would recommend that you use the official nginx repository instead of the ubuntu source. It is maintained with the latest version verse 1.1.19.

http://nginx.org/en/linux_packages.html

Thanks Kamal. Sorry for the late reply, totally forgotten that I asked this question.

Where should I add the code to? “/etc/nginx/sites-available/default” or “/etc/nginx/sites-available/wordpress”?

I tried adding to both, it didn’t work. Where should I be adding to, in those 2 files? I followed these 2 tutorials:

  1. LEMP https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04

  2. Wordpress https://www.digitalocean.com/community/articles/how-to-install-wordpress-with-nginx-on-ubuntu-12-04

Thank you!

service nginx doesnot restart. i have tried many things but it does not give any error .But when i check confg files by nginx -t . i am getting this error. nginx: [emerg] “location” directive is not allowed here in /etc/nginx/sites-enabled/www.exapmle.com:30 nginx: configuration file /etc/nginx/nginx.conf test failed

Kindly help out

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
March 9, 2014

@bilal.butter: Please pastebin nginx’s config file.

is it possible to switch from Apache to Nginx on a Active server running Cpanel on Cent OS?

Thank you

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 18, 2014

@martyaria: You can do it, but by default CPanel integrates with Apache. There are a number of plugins that will integrate with Nginx, but I don’t have any experience with those first hand.

Good!!

Thanks!

Success

How could I find the location of nginx.conf after installing nginx via sudo apt-get install nginx?

So I’m stuck on installing it. I run sudo apt-get install nginx on my root user. It’s been spinning for several hours. Is it supposed to take this long?

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.