≡ Menu

How to Install and Configure Nginx from Source on Linux

Before we understand what is nginx, we should know how to pronounce nginx.

Its not N G I N X. nginx is pronounced as “Engine X”.

nginx is short form after you remove both the e’s from “Engine X”.

nginx is an open source web server that is similar to Apache, but very light weight. nginx is both web server and reverse proxy server.

The following are some of the features of nginx:

  • It serves static and index files
  • Reverse proxy with caching
  • Supports SSL
  • Simple load balancing with fault tolerance
  • Both name-based and ip-based virtual server can be configured
  • HTTP basic authentication
  • Supports rewrite module
  • Supports gzip, XSLT, SSI and image resizing filters
  • All the main mail proxy server features are supported
  • and lot more..

This is part of an ongoing series of articles on nginx.

Let us get started by installing nginx and get it up and running, which takes only 5 minutes.

1. Download nginx

Download nginx from here, or use wget as shown below. The current stable version is nginx 1.0.5

cd
wget http://nginx.org/download/nginx-1.0.5.tar.gz
tar xvfz nginx-1.0.5.tar.gz
cd nginx-1.0.5

2. Install nginx

There are lot of options that you can pass to ./configure. To identify list of all the configuration options do the following.

./configure --help

The following are some of the http modules that are automatically enabled. If you need to disable them, yo should pass “–without-http_[module_name]” to the ./configure option. For example, to disable “proxy_module”, you should do ./configure –without-http_proxy_module.

  • charset_module – HTTP Character set module
  • gzip_module – HTTP gzip module for compression
  • ssi_module – Server side include modules
  • auth_basic_module – HTTP Basic authentication modules.
  • autoindex_module – Auto index
  • rewrite_module – HTTP rewrite that supports rewrite rules
  • proxy_module – HTTP reverse proxy module
  • fastcgi_module – Support for fastcgi
  • memcached_module – Memcached module for nginx
  • ..

Following are some of the httpd modules that are not automatically enabled. If you need to enable them, you should pass “–with-http_[module_name]” to the ./configure option. For example, to enable SSL in nginx, you should do “./configure –with-http_ssl_module”.

  • ssl_module – Support for Secure Socket Layer (SSL) module
  • xslt_module – Support for Extensible Stylesheet Language Transformations (XSLT) module
  • perl_module – Support for Perl
  • ..

To begin with, install nginx with the default configuration options by doing ./configure as shown below.

./configure
make
make install

During ./configure, you might get the “./configure: error: the HTTP rewrite module requires the PCRE library.” error message about missing PCRE library that is required by nginx http rewrite module.

To solve the problem, either install “pcre” and “pcre-devel” packages on your Linux, or disable the rewrite module by doing “./configure –without-http_rewrite_module”

nginx will be installed under /usr/local/nginx as shown by the ./configure output.

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

3. Change the Default nginx Listen Port

Nginx is configured to listen by default on port 80. If you are installing this on a machine for testing purpose, that already has an apache server running, you should change the nginx listen port.

Similar to apache’s httpd.conf file, nginx has nginx.conf file located under /usr/local/nginx/conf.

In the server section of nginx.conf, change the port 80 to 8081.

# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       8081;
        server_name  localhost;

4. Start Nginx Server

nginx executable is located under /usr/local/nginx/sbin directory. Just call this executable to start the nginx server.

cd /usr/local/nginx/sbin
./nginx

Once you start this, you’ll see the nginx “master process” and “worker process” if you do ps.

# ps -ef | grep -i nginx
root     18596 13:16 nginx: master process ./nginx
nobody   18597 13:16 nginx: worker process

After you start the nginx server, go to http://your-ip-address/ (or http://your-ip-address:8081, if you changed the listen directive in nginx.conf), you should see the default nginx index.html, which should say “Welcome to nginx!”

5. Stop Nginx Server

To stop the nginx server, do the following.

cd /usr/local/nginx/sbin
./nginx -s stop

To view the current version of nginx, do the following:

# ./nginx -v
nginx: nginx version: nginx/1.0.5

To debug issues, view the error.log and access.log files located under /usr/local/nginx/logs

# ls /usr/local/nginx/logs/
access.log
error.log
nginx.pid
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • jalal hajigholamali July 25, 2011, 3:25 am

    Hi,

    Thanks a lot, very nice material…

  • shark巨菜 July 25, 2011, 6:31 am

    Is there any further technology to share?

  • Andrew July 25, 2011, 11:22 pm

    Would be nice to hear how to tune nginx on the high loaded Linux server for the maximum performance )

  • sakthivel August 29, 2012, 6:43 am

    I’d installed apache on my centos 6.3………then i tried to install nginx………i’ve got almost step no.2……but i searched nginx under in /usr/local/ directory i couldn’t find it………..Could you fix this

  • ravi January 8, 2013, 11:22 am

    very nice material to to install nginx server and plz refer me the configuration of nginx server

  • ekri February 3, 2014, 10:40 pm

    Thanks a million

  • kavitha February 2, 2015, 11:12 pm

    Thanks a lot.. Very much helpful as a starter.

  • Rifki chaplin February 6, 2017, 10:31 am

    very usefully for beginner.. thanks a lot..