CentOS - Installing Nginx from source

Nginx is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.

This article will look at installing Nginx from source on a CentOS slice.


Versions

At the time of writing, the latest stable version of Nginx is 0.6.34. You can check the latest versions and change logs at the main nginx site.

Building from source will allow you to have the latest available versions of Nginx. However, keep in mind that security updates will not be applied automatically.

Please see this article if you want to install Nginx with the package manager which ensures that any dependencies as well as security updates are installed as needed.

Dependencies

As we are not using the package manager to install nginx, we need to take care of some dependencies.

Not many are needed and include pcre-devel zlib-devel and openssl-devel packages.

sudo yum install pcre-devel zlib-devel openssl-devel

Download

Now we can download the source code.

Create a 'sources' directory (you can download it anywhere you want to):

mkdir ~/sources

Now move into the folder:

cd ~/sources/

And then download the source code:

wget http://sysoev.ru/nginx/nginx-0.6.34.tar.gz

Unpack

Unpack the downloaded tar file and move into the newly created directory:

tar -zxvf nginx-0.6.34.tar.gz
...
cd nginx-0.6.34/

Options

There are quite a few compile time options that are available to use.

Have a look at the Install Options page of the Nginx wiki for full details.

We're going to use just two options to customize the install. The first of which is:

--sbin-path=/usr/local/sbin

By default, nginx will be installed in /usr/local/nginx which, although a good place, does mean the main Nginx binary will be found in /usr/local/nginx/sbin/nginx.

Not a location we are likely to find in our default search paths. So instead of adding new directories to our path (which may cause errors later on) we simply define where to put the binary.

The second option is:

--with-http_ssl_module

Probably self explanatory, but this will enable the SSL module to be compiled so we can parse https requests.

Compile

Let's go ahead and compile Nginx using those two options:

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module

There will be a nice summary at the end of the compile which includes such items as:

...
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/sbin"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
...

Keep a note of the output as it does contain some useful paths and locations for various files.

Make

Ok, let's go ahead and 'make' and then 'make install':

make
...
sudo make install

Defaults

Done. We can test the install by starting Nginx:

sudo /usr/local/sbin/nginx

and navigating to our Slice IP address (assuming you have no other server running):

http://123.45.67.890

You will be greeted with the standard welcome page:

Nginx Welcome Screen

Stop

If you have used Apache or some other web server (or even installing Nginx via a package manager), you may be used to using a script to start and stop it such as you might find in '/etc/init.d/'.

Unfortunately, compiling from source does not create a script like that (we will create an init script in the next article or two).

You may have noticed that one of the paths noted in the compile summary was:

nginx pid file: "/usr/local/nginx/logs/nginx.pid"

Let's make use of that to stop Nginx:

sudo kill `cat /usr/local/nginx/logs/nginx.pid`

That will use the output of the pid file to kill the Nginx process - note the use of backticks (`) and not single quotes (') in the command.

Summary

Although quite detailed, we have installed the latest stable version of Nginx from source and compiled it with some custom options to make life slightly easier.

We also know how to start and stop Nginx by hand.

As mentioned, the next couple of articles will deal with learning how to create an init script so we have more control over Nginx.

Once done, we will then look at configuring Nginx, setting up vhosts and proxying to a backend (such as a cluster of mongrels).

Article Comments:

Trafikant commented Mon Jul 26 14:42:19 UTC 2010:

Nice, thx that RULES :D!

Martin commented Thu Aug 05 19:02:52 UTC 2010:

Wow guys. You really rock! You made a total dummy (me) get Nginx up and running installing it from source!

I left Apache installed just in case but it is stopped now.

I am now going to configure a virtual host for my site, set up some re-write rules and I bet I will have the site up and running under nginx in a few moments.

Thank you so much for all your excellent tutorials.

Regards.

Martin

GREG commented Tue Sep 28 23:17:07 UTC 2010:

Great article, everything worked exactly as expected, and this made the install very easy. Thanks!!

Aladin commented Tue Nov 23 23:31:43 UTC 2010:

Thanks,you saved my life.

JOGLOHosting commented Wed Jan 12 18:10:38 UTC 2011:

easy step by step tutorial, do u have configuration for proxy cache? thanks

Abhishek Amralkar commented Tue Nov 08 13:46:52 UTC 2011:

Hi,

Nice tutorial I would like to load balance my JETTY server behind NGINX ,also I dont want NGINX server should become the Single Point of fialure how can I acheive that?

Jered commented Fri Nov 11 17:13:30 UTC 2011:

Hi Abhishek. Usually you do have to have just that one point of entry when you're load balancing. So long as nginx itself isn't doing anything too complicated it should usually be fairly safe (you could also put a dedicated reverse proxy like "varnish" in front of it).

If you do want to make sure it's not a single point of failure then you'd probably want to set up some sort of failover so that another machine will take over that IP address with its own instance of nginx if the main machine goes down. You can find a fair amount of documentation on setting up HA at linux-ha.org.

waz commented Tue Dec 20 08:48:56 UTC 2011:

but have some question.

im doing this as nginx as front on port 80 and apache on port 81

once this is installed and complete I think you still do need port redirect right,advice please??

iptables -t nat -A PREROUTING -p tcp -s ! MyServerIP --dport 80 -j REDIRECT --to-ports 81

and one thing sometimes I get 503 and 504 gateway error nginx any solution for this?

Jered commented Thu Dec 22 15:49:10 UTC 2011:

Hi waz. If you redirect traffic between ports through iptables then nginx would never see the traffic on port 80, I believe - it would send it on to 81 instead. If you want nginx to receive the traffic then proxy it back to apache on port 81, look into using the nginx "HttpProxyModule" to control the proxying.

As to the errors: 503 means that the server reached its connection limit. For that one, look into controlling the max clients that you're allowing to connect to nginx and see if you can increase them (or if you need a larger slice, or to cluster more than one server). The 504 error is a gateway timeout, meaning that nginx is trying to proxy a connection back to another service but that service isn't responding. In what you describe, that could happen if apache is down or not responding when nginx tries passing a connection to it.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)