Install and Configure the Nginx Web Server on Linux

Nginx is a lightweight, fast, and efficient web and proxy server. It is one of the fastest static conent web servers available, and can also be deployed to delivery dynamic content through a FastCGI interface. In addition, nginx is a very powerful reverse proxy (frontend) server and very capable software load balancer.

For a full listing of features, please refer to http://nginx.org.

Installation

Installation of the latest stable release of nginx can be done easily with the EPEL (Extra Packages for Enterprise Linux) package repository. To use this repository, execute the following as the root user:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

Once the EPEL repository is in use, nginx can be easily installed through yum.

yum install nginx

Configuration

Nginx uses the configuration file /etc/nginx/nginx.conf, which can be edited using nano. Virtual hosts (web sites) are configured in this file with ‘server’ code blocks, which are located under the main ‘http’ block. The default ‘server’ block will listen on port 80, and has a document (web root) of ‘/usr/share/nginx/html’.

To help explain configuration of the server block, below is a very basic entry.

server {
listen 80;
server_name www.domain.com domain.com;
location / {
root /var/www/domain.com/html;
index index.html index.htm;
}

}

  • listen: Specifies the port on which this virtual host listens.
  • server: Lists the host headers for the site.
  • location /: Specifies how to handle requests under the location ‘/’, the site root.
  • root: The document root for the site.
  • index: An ordered priority list of default documents.

For further details and examples on nginx’s configuration, please refer to their offical wiki page and core documentation.

Note: If your server currently is configured with another web server, you’ll likely need to have Nginx listen on a port other than 80. This is done simply by editing the ‘listen’ setting in the default server block, as well as any additional server blocks that are created.

To test your configuration, execute the following and it will report on any errors.

/usr/sbin/nginx -t

Starting and Testing

Once you’ve setup a working configuration, you can start the nginx server.

/etc/init.d/nginx start

If using the default document root (/usr/share/nginx/html), visiting your server’s IP in a browser should yield the default server page.

nginx_test

Now that Nginx is running successfully, you’ll want to be sure it’s added to the default run level so it will start automatically at boot time.

chkconfig nginx on