Book Image

Instant Nginx Starter

By : Martin Bjerretoft Fjordvald, Martin Fjordvald
Book Image

Instant Nginx Starter

By: Martin Bjerretoft Fjordvald, Martin Fjordvald

Overview of this book

<p>Nginx is an open source web server and a reverse proxy server for HTTP, SMTP, POP3, and IMAP protocols. It is used to deploy dynamic HTTP content on a network using FastCGI and SCGI handlers for scripts and it serves as a software load balancer . Nginx enables the user to save resources compared to other major web servers such as Apache HTTP as well as provides site visitors with a faster load time for an improved user experience.</p><p>Instant Nginx Starter will show you how to install and set up nginx web server by introducing you to basics that covers an understanding of the nginx modules, which will help you to migrate from Apache to achieve high concurrency, performance and low memory usage.</p><p></p><p>Nginx Starter introduces you to the configuration file in its entirety, explaining the importance of different sections. The book then takes you through various examples based on core syntax and specific features enabling you to configure nginx.</p><p></p><p>During the course of the book, you will see how modern technology is merged with nginx by detailing an example based on websockets. The starter guide emphasizes optimizing load time through compression and caching of dynamic responses, while highlighting that nginx can seamlessly become part of a productive ecosystem of the software.</p><p></p><p>Through this book, you will have learned everything you need to control nginx behavior with an understanding over the tools to debug problems and execute plans.</p>
Table of Contents (7 chapters)

Installation


There are two ways to install nginx, either by building it from source, or by installing a binary package via a package manager, such as yum or apt. Each method has its own pros and cons, and which method we choose depends on what we need nginx to do and which OS we're using.

Historically nginx, Inc has only provided the source files for nginx so that we could compile the software ourselves, and only recently have they begun distributing binary packages for the various Linux distributions. Additionally, nginx requires that third party modules are compiled statically instead of being loaded at runtime. The end result of this is that the nginx ecosystem ends up with a number of native binary packages and custom binary packages built by different people to include different modules.

Even today many of the Linux distributions ship very old versions of nginx, which means we'll have to be careful when we install nginx to make sure we get the version we need. If we need any third party modules enabled, we are almost guaranteed to have to build from source. Thankfully, nginx is easy to install from source and this book details how to do it without suffering a nervous breakdown.

A last note before we continue to the installation process. nginx has three versions available: development, stable, and legacy. Development here refers to the program API stability, not runtime stability. This means that the development version is usually just as stable, or even more stable than the stable branch. This is because bug fixes are added to the development branch before the stable branch. In general, if I personally want features in a new development version, I will give it a week or two to be tested by the community and then feel safe upgrading to it. Legacy versions should be avoided, as they are not supported by either nginx, Inc or the community, and usually bugs are fixed by simply updating to the stable or development version.

Step 1 – Different operating systems

Now, we will have a look at installing nginx on different operating systems.

Windows

Installing on Windows is the easiest of them all, as it's really only available as a binary file unless we want to start compiling through Cygwin, for most people this is overkill. Instead, just head to the nginx download page and get one of the Windows releases as signified by nginx/Windows-1.X.XX. Extract that anywhere and we're ready to go!

A word of warning about nginx on Windows though. Windows has a unique version of event polling called IOCP and nginx does not currently support this. This means that nginx falls back to a slower variant, which means that nginx on Windows is not at the same performance standard as nginx on Linux. Additionally, there are a number of limitations that we should be aware of. At the time of writing the following are the known limitations:

  • Only one worker will be used

  • A worker can handle no more than 1,024 concurrent connections

  • Cache modules do not work on Windows Vista or later

nginx, Inc maintains an updated list of known limitations at the following URL:

http://www.nginx.org/en/docs/windows.html

Linux

To install on Linux we first need to decide whether we'll compile from source or install via a binary package. To help decide, here's a brief overview of the pros and cons:

Installation via source

The pros of installing nginx on Linux via source are as follows:

  • It can easily use third party modules

  • It can use the latest version immediately

The cons of installing nginx on Linux via source are as follows:

  • It is more difficult than installing a binary package

  • You have to keep on top of updates yourself

Installation via binary package

The pros of installing nginx on Linux via binary package are as follows:

  • It is very easy to install

  • You don't have to keep track of updates yourself

The cons of installing nginx on Linux via binary package are as follows:

  • It is difficult to find a binary package with third party modules

  • It is potentially lagging behind on versions

  • It has many different versions, need to research them

Ultimately I personally think it comes down to whether or not you need third party modules. Finding binary packages that contain the modules you need is often difficult and you rely on external people to keep their binary package updated. Compiling from source if you need third party modules also means that we can restrict binary packages to the official nginx provided repositories. This makes it far easier and reduces the research required into the various custom repositories and Ubuntu Personal Package Archives (PPAs).

Installing from source

Installing nginx from source is not as daunting as it might sound, as nginx is a fairly simple piece of software and we can still utilize yum and apt-get to simplify the installation of the dependencies.

If using apt, simply run the following:

sudo apt-get build-dep nginx

To automatically install the dependencies for their nginx package, usually these are the same as for what we will install.

If using yum, run the following:

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

At this point we have the dependencies and are ready to compile nginx. Make sure we're in the directory we want to download the source code into and then run the following:

wget http://www.nginx.org/download/nginx-1.3.15.tar.gz
tar zxf nginx-1.3.15.tar.gz
cd nginx-1.3.15
./configure --help

After running the last command we should get a large amount of text on the screen. If you're not used to compiling from source, this would probably be pretty daunting at first, but let's read through the important points.

--prefix

This sets the base path where nginx is installed. If not defined, this will default to /usr/local/nginx.

--sbin-path

This sets the path where the binary file is installed.

--conf-path

This sets the path of the configuration file.

Any of the other install specific switches can be configured in the configuration file, so those really aren't important to set yet. Further down we start seeing switches named as —with-* and —without-*. Each of these allow us to define which standard modules go into our compiled binary package, and the switches we use depend on which features we want. Each module we include increases the size of the binary package, which increases memory usage. Although, even if we include all the modules, the memory size won't be more than a few megabytes. Do note that some modules might have other dependencies, for instance the GeoIP module relies on external GeoIP software which will have to be installed through your package manager. To read about what each module does, please refer to the official documentation at: http://www.nginx.org/en/docs/.

Once we have decided the modules we want to be included, simply run the following:

./configure —with-foo

If all the dependencies are correct, a summary screen should be presented as follows:

If the information there is as expected, complete the compile by running the follow commands:

make
make install

If we already have nginx installed, we can have the make script automatically and seamlessly rotate the running binary package by using the following command:

make upgrade

If we did everything right, we should get the following message after running make install:

make[1]: Leaving directory '/path/to/nginx-1.3.15'

The quickest way to install from a binary package is to simply use the native packages and run either of the following:

yum install nginx

or

apt-get install nginx

If the version installed is fairly recent, we might want to do just that for convenience, if it's old then move on and use the nginx provided binary packages. To use these first install the repository like so.

For yum:

Create the file /etc/yum.repos.d/nginx.repo and add the following to it:

 [nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

Where OS is centos if CentOS is used and rhel if RHEL, or another RHEL-based distribution is used. OSRELEASE is the OS version number, being either 5 or 6. If unsure check uname -a for a clue, or use the tried and tested method of trial and error.

For apt:

Add the following to /etc/apt/sources.list for Debian:

deb http://nginx.org/packages/debian/ squeezenginx
deb-src http://nginx.org/packages/debian/ squeezenginx

For Ubuntu, add the following:

deb http://nginx.org/packages/ubuntu/ codenamenginx
deb-src http://nginx.org/packages/ubuntu/ codenamenginx

Where codename is one of lucid, oneiric, precise, or quantal, depending on which version is used and then run:

apt-get update
apt-get install nginx

Step 2- Starting nginx

Regardless of how nginx was installed, we will most likely want to start it by using a script. Our options are the classic init.d script or an upstart/systemd script depending on our platform. If nginx was installed via a binary package, one such script should already have been provided for us and can be used by running:

service nginx start

If nginx was installed via source, we'll need to install the init script ourselves. An nginx community effort to gather .init scripts can be found at: http://wiki.nginx.org/InitScripts, which will help us get set up quickly.

Download the init script for the relevant platform and save it to /etc/rc.d/init.d/nginx, check the paths in the .init file to make sure they fit the install configurations we set with the ./configure arguments (or the defaults!), and then run the following:

chmod +x /etc/rc.d/init.d/nginx

Now run the preceding command up above to see the options available.

And that's it

At this point nginx should be installed and ready to be configured. It's time to experiment a bit and learn the good stuff!