Nginx is a lightweight and high performance web server designed with the purpose of delivering large amounts of static content quickly and with efficient use of system resources. In contrast to the Apache HTTP server that uses a threaded or process-oriented approach to handling requests, Nginx uses an asynchronous event-driven model which provides more predictable performance under load.
Although Nginx is a relatively new entry in the web server field, it has achieved a great deal of respect for its agility and efficiency, particularly in high profile situations. Many very high traffic/profile websites have begun to use Nginx for its efficiency. At the same time administrators of smaller systems have found Nginx ideal for their systems for its slim memory footprint.
Before you begin installing the Nginx web server, it is assumed that you have followed our Getting Started Guide. If you are new to Linux server administration, you may be interested in our Using Linux document series including the Beginner's Guide and Administration Basics Guide.
Before you begin installing and configuring the components described in this guide, make sure you have followed the instructions for setting your hostname. Issue the following commands to make sure the hostname is set properly:
hostname hostname -f
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).
All of the following updates, upgrades, and installs use a non-root account. Therefore, sudo is required before commands, if you are using root to perform these commands than sudo would not be required. For more information regarding account permissions, view our Linux Users and Groups Guide.
Nginx is included as part of the Debian software repositories. The preferred installation method utilizes the Debian repositories. The commands for installation are as follows:
sudo apt-get update sudo apt-get upgrade sudo apt-get install nginx
Installing Nginx with the Debian package maintainers provides quality control, testing, and security updates that ensure that you are running the best possible version of the software for your operating system. This document is written against the release version 1.2.1-22.
To test if the installation of Nginx was successful, open a web browser and in the address bar enter the IP address of your server. You should see a message similar to the one below:
The Debian project does not track the latest development of Nginx server. Therefore, if you require a newer version, you will need to download and compile the newest distribution of Nginx (current version as of this publication is 1.5.9).
Due to rapid development of the Nginx web server and recent changes to the interface, many users of Nginx compile their version of the software from sources provided by the Nginx developers. Additional benefits include the ability to configure Nginx to support additional third party modules and options which must be set at the time of compile.
Begin by making sure, your system's package database and installed programs are up to date. The commands for updating and upgrading are as follows:
sudo apt-get update sudo apt-get upgrade
You will also need to install several dependent packages before proceeding with Nginx installation. The command for updating dependencies is as follows:
sudo apt-get install libpcre3-dev build-essential libssl-dev
The source files and binaries will be downloaded in the /opt/ directory of the file system in this example. Issue the following sequence of commands to enter this directory, download the required files, and extract the source files from the archive:
cd /opt/ sudo wget http://nginx.org/download/nginx-1.5.9.tar.gz sudo tar -zxvf nginx-1.5.9.tar.gz cd /opt/nginx-1.5.9/
Now you may compile the Nginx server. If you want to enable third-party modules, append options to ./configure at this point. Issue the following command to configure the build options:
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
When the configuration process completes successfully, you will see the following output:
nginx path prefix: "/opt/nginx" nginx binary file: "/opt/nginx/sbin/nginx" nginx configuration prefix: "/opt/nginx/conf" nginx configuration file: "/opt/nginx/conf/nginx.conf" nginx pid file: "/opt/nginx/logs/nginx.pid" nginx error log file: "/opt/nginx/logs/error.log" nginx http access log file: "/opt/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"
To build and install Nginx with the above configuration, use the following command sequence:
sudo make sudo make install
You will also need to create a user and group for Nginx, issue the following command:
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
Nginx is now installed in /opt/nginx.
When running software compiled or installed directly from sources provided by upstream developers, you are responsible for monitoring updates, bug fixes, and security issues. After becoming aware of releases and potential issues, update your software to resolve flaws and prevent possible system compromise. Monitoring releases and maintaining up-to-date versions of all software is crucial for the security and integrity of a system.
Please follow the announcements, lists, and RSS feeds on the following pages to ensure that you are aware of all updates to the software and can upgrade appropriately or apply patches and recompile as needed:
When upstream sources offer new releases, repeat the instructions for installing Nginx and recompile your software as needed. These practices are crucial for the ongoing security and functioning of your system.
Before you can use the Nginx server, you must create a means of controlling the daemon process. You can create an "init script" using this example to control Nginx. Issue the following commands to download the file, change the execution mode, and set the system to initialize Nginx on boot:
sudo wget -O init-deb.sh http://library.linode.com/assets/1538-init-deb.sh sudo mv init-deb.sh /etc/init.d/nginx sudo chmod +x /etc/init.d/nginx sudo /usr/sbin/update-rc.d -f nginx defaults
You can now start, stop, and restart Nginx just like any other server daemon. For example, to start the server, issue the following command:
sudo /etc/init.d/nginx start
You now have a running and fully functional HTTP server powered by the Nginx web server. To test if the installation of Nginx was successful, open a web browser and in the address bar enter the IP address of your server. You should see a message similar to the one below:
Continue reading our introduction to Basic Nginx Configuration for more information about using and setting up a web server.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.
Last edited by Bobbi L on Tuesday, January 28th, 2014 (r4178).