Install NGINX (better than Apache or Lighttpd): VPS BIBLE




new password?
login
X

Nginx (better than Apache) Web Server

Install Nginx - Nginx logo

Installing & configuring Nginx web server, tweaking the default file structure, then setting up a vhost file with symlink is the subject of this copy/paste how-to. So open up that CLI and I’ll explain.

So we’re ready to install a star attraction, Nginx (pronounced “engine-x”). But why? Isn’t Apache the best web server?

Apache has served me well for years, both locally and on remote hosts, and is still a desirable choice for many large sites.

But, kinda like Ubuntu, FireFox and jQuery, there’s a new kid in town, and it’s leaner, simpler to configure and use, and faster to serve. Enter the Nginx web server.

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

  .. from blank box to cute-as server ..  

with this easy-to-follow copy/paste guide.

22+ parts with video, here’s the index.

Video: Install & Configure Nginx Web Server

Watch the, er, guvideo for a better idea of how to do this.


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Why Choose Nginx? Why not Apache or Lighttpd?

I don’t profess to be an expert, just someone who has researched ultra-thoroughly. I concentrated primarily on the two most popular, best established web servers, Apache and Lighttpd, and the young Turk alternative, Nginx, about which geekdom is all aflutter. Here’s a summary of my key findings:-

  • Apache is bloatware, loading unused modules that waste resources
  • Lighttpd leaks RAM badly
  • Nginx benchmarks the fastest, using the least resources

I’ve been using this light-weight server for my resource-heavy WordPress blog, guvnr.com, and am impressed with its solid performance. Also importantly, it doesn’t have that Windows-like tendency, an Apache affliction too, of wasting resources by running a bunch of services that I just don’t need.

Don’t take my word for it. You shouldn’t, because I haven’t performed any benchmark tests. Google something like Apache vs Nginx” or Nginx vs Lighttpd and have a read. And here’s the Nginx wiki.

Getting the Latest Nginx Version

There are two ways to install software on Linux; using the integrated installation tools or from sourcecode.

Normally, we install internally, using something like the Linux installer ‘aptitude’, but because this is such an important component of our VPS, I’m going to show you how to install from source. This method takes a little longer, but it’s worth it because we’ll have a much more up-to-date version.

.. With a web server, we shouldn’t cut corners. That would be like buying a yellow Ferrari.

First up, we need some dependency files:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

And to create a directory in which to store the Nginx package:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Change to that directory:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Now we get the latest stable Nginx release, nginx-0.7.62 *.

* As of September 2009, edited by the_guv. You should still check it is still the latest, here, and ammend the filename accordingly:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Unzip it:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Go into the new unzipped folder:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Installing and Testing Nginx

Compile with two options; where to install it, and including ’ssl’ (to enable ‘https’ for secure connections, ie shopping and stuff):-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Install this baby:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Kick it up:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

And test it by popping your IP address in a web browser. You should see “Welcome to nginx!”

Now stop it:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Have Nginx Start, Restart or Stop When Required

This is important, for example, upon reboot. We need a script for this. Create a file:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

And paste this within:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Then give the file permissions and make the script run on reboot, else start/stop/restart when required:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

nginx.conf – Configuring Nginx

Now open the Nginx configuration file:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…and strip out all the content, delete the lot. CTRL-K is the easy way to do that, if you were wondering.

And replace with this:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Creating the Virtual Host File Structure & Symlinks

The Nginx file structure is pretty messy for multiple sites, so we’ll sort that.

First, layout some new folders:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…the first is for our virtual host (vhost) files, the second for their corresponding symlinks which will be referenced by Nginx’ config file.

What are vhosts & symlinks?

You have one of each per domain, and one of each for the default settings.

The symlink, or symbolic link, references the web server to the virtual host file.

The vhost file is a configuration file. It tells the web server, for example, things like where the web files live or the kind of URI structure you want.

For now, we need a default vhost file, and that goes in the sites-available folder. So:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Now paste this:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

And enable it with this symlink:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Boot it up again:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…and check for that “Welcome…” page again, using your IP in a web browser.

Splendid. All pretty. And pretty well organised.

Moving Along

So that’s Nginx up and running.

In Part 12 of this series Set Up an Unmanaged VPS (4 Newbies) I’m taking a quick detour, setting up FileZilla so we’ve got a Secure FTP (SFTP) connection. That’ll be handy to help demonstrate Part 13, when we create another folder structure, this time for our sites and blogs, and pop up a couple of test pages.

Then, in Part 13, I’ll show you how to use Subversion to more easily install and upgrade platforms and their modules/plugins. I’ll example the popular WordPress scenario – and while we’re about it we’ll sort out WordPress caching and friendly-URLs.

And then, this, that, the other. Cue index ..

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

with this easy-to-follow copy/paste guide.

“My local PC runs Windows” Show me for Linux

22+ parts with video, here’s the index ..

  • Appendix 1a: Nginx Control Panel

    You’re migrating shared-to-VPS and want a GUI like cPanel? No you don’t, not after you read this! What’s more, you don’t need one either.

  • Appendix 1b: Nginx Control Panel Workarounds

    For every control panel module there’s an equivalent terminal command. They’re all linked from here so, hey, no excuses!

  • Appendix 2: Add Web Sites Nginx Cheatsheet

    Once you’ve set up your first site, adding more gets easier. Especially when you can refer to this. (Let’s face it, I’m just too good to you.)




Got a Question? Want to Comment?

This site has no comments system. We've got a better system!

Please use the link at the top of the post and goto this topic's forum thread:-

  • to help build community
  • for a superior knowledge base
  • to keep info in one place, not diluted between forum & comments.

For those posts ported from Guvnr.com (pre-Feb/2010), archive comments remain.

Handy info from these comments is in the process of being ported to the forum and attributed to the original author.

If you think this idea sucks, let me know.

Thank you.

  • Install/Upgrade WORDPRESS with SUBVERSION - VPS Bible #15 - GUVNR June 9th, 2009 at 11:38 am

    [...] Part 11: * Nginx (better than Apache) Web Server [...]

  • Configure NGINX Multi-Site Virtual Hosts - VPS Bible Pt 13 - GUVNR June 9th, 2009 at 11:38 am

    [...] Part 11: * Nginx (better than Apache) Web Server [...]

  • mnyman June 10th, 2009 at 6:34 pm

    This has been really amazing, so thanks a bunch.

    For those of us who are completely new at this, I get a warning when I “boot it up again” that says:

    Starting nginx: [warn]: duplicate MIME type “text/html” in /usr/local/nginx/conf/nginx.conf:21

    Looking in the file, I couldn’t find anything duplicating. Do you know why this would happen? Having errors like these explained is especially comforting when we don’t fully understand what is going on. Thanks again for all your work on this.

  • the_guv June 10th, 2009 at 11:10 pm

    Hi Mnyman,

    Hmmn, sorry to say, looks like you’ve made a typo. Unless .. is there any discrepancy between what I have set down from the start to where you are, and what you have done?

    Tell you what though .. click on Contact and send me an email with the content of nginx.conf, and I’ll take a look.

  • crazeworthy June 23rd, 2009 at 10:13 am

    The problem is with line 20, there is a carriage return which puts application/xml+rss text/javascript; in a separate line. That conflicts with the include mime.types;

    Just make sure that is removed and I think it should go smoothly. This is a great write up guv!

  • Matt June 23rd, 2009 at 4:24 pm

    Great series!
    Wanted to let you know that the install step didn’t work for me. Used instruction here: http://www.ubuntugeek.com/howto-install-nginx-webserver-in-ubuntu-804-hardy-heron.html and it worked.
    I’m definitely not saying your wrong (I’m the definition of a newbie) but wanted to let you know.
    Thanks for a great tutorial!

  • crazeworthy June 23rd, 2009 at 5:23 pm

    Previous comment was incorrect see link below:

    http://forum.slicehost.com/comments.php?DiscussionID=3763

  • the_guv June 25th, 2009 at 1:42 pm

    @crazeworthy – pleased you sorted that. tx for the tx!
    @matt – aye, me likes the ubuntu geek. dunno what went wrong for you, this method sure worked in testing, twice or thrice :) can be a bit fiddly though. pay attention m8!
    @all – 1 tip .. don’t try to copy the code direct from the post .. there’s an icon at the top right of each code box, next to the printer icon. Click on that, and then copy/paste from there.

    All that said, damn, any probs, lemme know.

    ***
    UPDATE, FIX! RE: Nginx Warning on Start/Restart. Sorry folks, there was a mistake in the nginx.conf file, which was throwing a Warning (not an error, but annoying all the same.)

    If you really wanna know, I had a duplicate MIME type “text/html” in the conf .. duplicate because the GZIP engine GZIP-ed that MIME type anyway, so it wasn’t needed to be repeated in the conf. Anyhow, sorted.

  • oligolli June 27th, 2009 at 11:18 pm

    Thanks for all these tuts, really helped a newbie here.

    I’m a bit stuck with the permissions on the /wp-content/uploads folder. I admit I didn’t follow your tutorials to the letter, mainly because I am using Centos, but the Nginx user (www-data in your example) confuses me a little.

    Should the Nginx user be the owner of the uploads folder or in the same “webmasters” group?

  • Samuel July 6th, 2009 at 4:29 pm

    When trying to start nginx using sudo /etc/init.d/nginx start I get the following error:

    Starting nginx: start-stop-daemon: –start needs –exec or –startas
    Try `start-stop-daemon –help’ for more information.

    :’(

  • the_guv July 7th, 2009 at 5:12 am

    @ollygolli .. really depends what you want to do, but probably I could have been clearer about permissions.

    a simple scheme would be to add your username to nginx’ www-data group, while owning the html_public directory and within. for that, do this:-

    usermod -G www-data username
    sudo chown -R username:www-data /home/public_html/

    or if you’ve got a bunch of sites for different users, chown username:www-data within their public folders, recursively:-

    sudo chown -R username:www-data /home/public_html/somedomain.com/public

    @Samuel .. real sorry, can’t replicate that error. have you followed the series, sounds like your setup is different somehow.

    @Samuel .. CORRECTION. more sorry! was a new plugin playing havoc. sorted now. apologies all.

  • gordon yeong July 17th, 2009 at 1:39 pm

    nah this still doesn’t work even when i joined lines 21 and 20 into line 20.

    #
    #

    crazeworthy June 23rd, 2009 at 10:13 am

    The problem is with line 20, there is a carriage return which puts application/xml+rss text/javascript; in a separate line. That conflicts with the include mime.types;

    Just make sure that is removed and I think it should go smoothly. This is a great write up guv!

  • the_guv July 19th, 2009 at 11:32 am

    @Gordon .. probably nothing to do with carriage return. what’s not working .. as much info as you can and I’ll try to help you isolate the problem or, if you sorted the issue, let us know how.

    @Crazeworthy .. tx for the tip .. carriage rtn removed.

  • Patrick August 26th, 2009 at 4:55 am

    Hello The_Guv,

    I have problems with wordpress mu and the rewrite rules for nginx

    I tried a lot of different codes but anything is work.

    http://mu.wordpress.org/forums/topic/3410

    I was making the chances in sudo nano /usr/local/nginx/sites-available/default

    My wordpress mu blog is in the root http://www.blognetwerk.net/blogs/

    I hope you can help me.

  • the_guv August 26th, 2009 at 12:29 pm

    @Patrick .. read the comments from Trav19 on Setup WordPress on NGINX (FURLs & Cache) – VPS Bible Pt 13 .. I’ve not had time to play properly with my MU install yet, but think Travis found the answer.

    Let me know how you get on. In fact, print the rule! And if it doesn’t work, let me know and I’ll have a think.

  • Pierre September 2nd, 2009 at 10:23 am

    If you are a programmer, have a look at the source code.

    Nginx is simply clearer than others, better engineered and stronger. It is made by more experienced people than all the others I have seen.

    In my humble opinion, that’s a piece of Art (and I know one thing or two on the subject).

    Pierre.

  • Wai Wong September 16th, 2009 at 6:01 am

    Is there a proper way to update nginx? Initially I installed with Nginx 0.7.61, the latest is now Nginx 0.7.62. I want to make sure I don’t mess anything up.

    thanks,

  • the_guv September 16th, 2009 at 5:48 pm

    @Pierre .. big cheers, Sir. That’s quite a project you’ve got there with G-WAN, BTW. Dunno I can wean off PHP quite so easily tho :P

    @Wai .. You’ve got a point .. it’s a pretty complex procedure to sum up quickly, but very important to add .. I’ll add a guide on how to upgrade next week .. that’s a promise.

    [edit]sorry Wai .. I broke the promise. Then again, I also busted my motherboard :( I do have this on teh list though, for the VPS ADMIN series ..

    Maintain Unmanaged VPS (4 Newbies) .. V-P-S Admin

    .. along with some additions to the Bible’s sequel. On the way ..[/edit]

  • the_guv September 24th, 2009 at 2:15 pm

    IMPORTANT NGINX UPGRADE ANNOUNCEMENT

    @everyone ..

    .. bozo-here upgraded the Bible last week to reflect the latest Nginx stable release – 0.7.62 – then forgot to mention it.

    Apologies for any confusion to those folks who wondered why the version had changed overnight, and what if anything that meant ..

    Actually, the change did impact one thing. In the section Serve Multiple Sites & Blogs with Virtual Hosts, a fastCGI shared library file became obsolete, now replaced by another, but anyone installing from this guide would have been prompted about this newly recommended file .. I know because I’ve tested it, and have had confirmation from Zereshk who followed this guide and kindly nudged me to amend that post, which I have.

    NOW FOR THE MOST IMPORTANT BIT ;)

    Future note: As Nginx or other key application stable releases are upgraded, I’ll be amending the Bible accordingly, the changes tested each time. Also, for the record, and as has been rightly requested by one or two folks, I’ll be adding a How to Upgrade Nginx Safely guide in the VPS Admin section, pretty shortly, so watch for that.

    Cheers all.

  • Anonymous October 13th, 2009 at 5:05 am

    I would like to point out that WordPress.com actually uses LiteSpeed for a backend. nginx is still the best lightweight frontend in my opinion.

  • the_guv October 13th, 2009 at 11:31 am

    @anon .. thank you. I stand corrected, as is the post. I think this clears it up.

    Intriguingly, stated in this post, “we are not using nginx as a web server currently, but may switch at some point.”

    WordPress uses Nginx, currently, for their load-balancing.

  • thanh November 4th, 2009 at 11:06 pm

    how do you go about upgrading to newer versions of nginx?

  • JIm Davis November 11th, 2009 at 3:25 pm

    I have been using your online tutorial … all is going well with one exception. When I issue the n2r
    nginx stops but does restart … the following error message is issued instead (see below). Since I am newbie am not sure how to correct this problem.

    permission denied when trying to open a error log file at “/user/local/nginx/logs/error.log” error 13

    It has something to do with not having master privileges. FYI I did work through all of the preceeding tutorial including creating my user name and aliases.

    Thank you for effort … very good job

    Jim

  • agus November 12th, 2009 at 2:54 am

    Hey Guv,

    Cant wait for your guide on upgrading Nginx… I really need to get rid of 502 bad gateway problem with my site (this happens when the site gets very busy, and when this happens I have to reboot my server)

    I am not sure upgrading will fix it, since it is really fastCGI problem if I am correct…

    thanks!
    agus

  • the_guv November 12th, 2009 at 6:33 am

    @thanh & @agus .. sure, this guide is overdue .. bear with me, I’ve got a lot on at the mo but this should be done by the end of the month.

    thank you, everyone, for your patience .. the Bible’s taken a lot longer, well, months and more months longer, than I’d anticipated.

    .. then again, hopefully it’s better, more detailed than ever, at least, than I thought it would be, and as I said in VPS Bible Reflects New Nginx/WordPress Versions, with Future Commitment, now it looks like I’ll never really finish it, just keep upgrading and adding to it, whether focusing on Nginx or other web servers, and also considering other VPS solutions .. I’ve got a bug about proxying in particular and, hey Agus this may help you, I’ve got a plan to test and write about a fastCGI alternative pretty shortly.

    And then, quite aside from VPS stuff, I wanna be writing more about Linux Desktop solutions, and basically to encourage people to ditch that passe sh1te that is Windows .. and that’s what The 25-Part Ubuntu KARMIC KOALA BIBLE was about .. terribly populist I know, and yes I am a cynical populist (damn, I used to write for the BBC, how cynical is that place!) .. er, it worked .. I’m getting published in a massive online place on 20th this month and, that aside, traffic quadrupled overnight, which was like a shot in the arm.

    .. And I wanna be writing more about making the most of the web, as opposed to just writing about making the web .. so stuff like how to Twitter effectively or into using some of the best Firefox addons for fun and productivity .. but also stuff like how to SEO (Google loves my SEO) and loads on WordPress .. plus a few laughs here and there. So that’s quite a lot, huh, and that’s just about this site!

    Hey folks .. let me know .. throw me some feedback .. I know I’m gaining some loyal community out there, born largely out of this VPS Bible, and I don’t want to turn my back on or somehow abuse that. This is my first blog, and its direction is still evolving, and I have a lot to learn.

    And re the Nginx upgrade piece, bear with me, for sure it is about time done! Er, yeah, that was the point of this reply, huh .. got ranting a bit .. you should hear me watching the news ..

  • agus November 14th, 2009 at 3:42 am

    Thanks Guv… your a good man! :)

  • eran November 16th, 2009 at 9:51 pm

    Hi

    Many thanks for the site

    Eran

  • the_guv November 18th, 2009 at 11:33 am

    @eran .. You’re welcome, thanks for the thanks.

  • Dimitris November 30th, 2009 at 4:19 pm

    I follow the tutorial on my Centos 64 server and i stuck there:
    sudo /usr/sbin/update-rc.d -f nginx defaults

    command not found :(

    I try it also as i am root:

    /usr/sbin/update-rc.d -f nginx defaults

    but again the same error :(

  • the_guv December 7th, 2009 at 9:58 am

    @Dimitris .. sure, this guide is for Debian Linux, using Ubuntu 8.04 LTS (Long Term Support)

  • Barrie December 29th, 2009 at 6:56 pm

    I have the same problem as Samuel and I have done nothing with plugins plus I do not have a clue on what to do.

    When trying to start nginx using sudo /etc/init.d/nginx start I get the following error:

    Starting nginx: start-stop-daemon: –start needs –exec or –startas
    Try `start-stop-daemon –help’ for more information.

  • Barrie December 30th, 2009 at 5:20 pm

    I sent this message in yesterday and it was on the list but today it has gone

    Starting nginx: start-stop-daemon: –start needs –exec or –startas
    Try `start-stop-daemon –help’ for more information.

    So I have done a complete re-install and when I have finished the install and go again to the start nginx it comes up starting nginx and when I put me IP in no connection and nothing else. When asked the first time I get the welcome screen but not he last time.

    Just what can the problem be

  • Barrie January 3rd, 2010 at 1:51 pm

    I got my problem sorted by trial and error so my Pt 11 is working. Now can I get the server working when I figure out how to connect to the WWW with just Pt 11 only, or do I need the whole thing. I am a complete novice as you will notice.