Installing Nginx on Ubuntu 12.04 LTS (Precise Pangolin)
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
DeprecatedThis guide has been deprecated and is no longer being maintained.
Nginx is a lightweight, high performance web server designed to deliver large amounts of static content quickly and with efficient use of system resources. In contrast to the Apache server, Nginx uses an asynchronous event-driven model which provides more predictable performance under load.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Before You Begin
If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
Follow our Setting Up and Securing a Compute Instance guide to update your system and configure your hostname. You may also wish to set the timezone, create a limited user account, and harden SSH access.
To check your hostname run:
hostname hostname -f
The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN) if you have one assigned.
Installing Nginx
There are two branches of Nginx. Nginx Open Source will be the focus of this guide and there are two sources from which you can install it on your Linode: Either from a distro’s repositories or from NGINX Inc., the company which formed behind the software to provide commercial features and support. Each way has its benefits and drawbacks.
Installing from Ubuntu’s Repositories
This method is the easiest and it ensures that Nginx has been tested to run at its best on Ubuntu. The Ubuntu repositories are often a few versions behind the latest Nginx stable release, so while Nginx will still receive security patches, it can be lacking features and bug fixes in comparison.
Install the Nginx web server:
sudo apt-get install nginx
The server will automatically start after the installation completes.
Go to the Testing Nginx section of this guide to ensure your server is accessible.
Installing from NGINX
Nginx’s downloads page has two more ways to install the web server: Using pre-built packages from the official Nginx repository or by building from source code. Either method will give you a more current version than what’s available in Trusty Tahr but with a slightly higher chance of encountering unforeseen issues because of newly-introduced bugs, and that these releases are not tested exclusively for a specific Linux distribution.
Installing from the Official Nginx Repository
The binary packages from Nginx’s repo will update you to new versions of the web server when available. You can choose the stable or mainline versions. If unsure, choose stable, which will be the example used for the remainder of this guide.
Add the Nginx repository to Ubuntu’s
sources.list
file:- File: /etc/apt/sources.list
1 2
deb http://nginx.org/packages/ubuntu/ trusty nginx deb-src http://nginx.org/packages/ubuntu/ trusty nginx
Note
Thedeb-src
line is only needed if you want repository access to Nginx’s source code.Download and add Nginx’s repository key to your GPG keyring:
sudo wget http://nginx.org/keys/nginx_signing.key sudo apt-key add nginx_signing.key
Update the repository lists and install Nginx:
The server will automatically start after the installation completes.
Go to the Testing Nginx section of this guide to ensure your server is accessible.
Installing from Source Distribution
Compiling from source gives you the most flexibility and choice for optimization with compiling options and third-party modules. You can also verify the PGP signature of the distributed tarball before compiling.
Install the needed dependencies to build Nginx:
sudo apt-get install libpcre3-dev build-essential libssl-dev
You can use any location you prefer to build from. Here,
/opt
will be used. Navigate to it:cd /opt
Download the latest version of Nginx Open Source and its PGP signature. You will have the choice of mainline, stable or legacy versions. Again, stable (1.8.0 at the time of this writing) is used as an example.
sudo wget http://nginx.org/download/nginx-1.8.0.tar.gz sudo wget http://nginx.org/download/nginx-1.8.0.tar.gz.asc
Attempt to verify the tarball’s signature:
gpg nginx-1.8.0.tar.gz.asc
The check will fail because you don’t yet have the public RSA key of the signer, and to get it you first need the RSA key ID from the output:
gpg: Signature made Tue 21 Apr 2015 02:14:01 PM UTC using RSA key ID A1C052F8 gpg: Can't check signature: public key not found
Run the key check again:
gpg nginx-1.8.0.tar.gz.asc
The output should include:
gpg: Good signature from "Maxim Dounin <mdounin@mdounin.ru>" gpg: key A1C052F8: public key "Maxim Dounin <mdounin@mdounin.ru>" imported gpg: no ultimately trusted keys found gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
Expand the source code and change to the new directory:
sudo tar -zxvf nginx*.tar.gz cd /opt/nginx-*
Configure the build options:
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module
When the configuration process completes successfully, you will see the following output:
Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library 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"
Build and install Nginx with the above configuration:
sudo make sudo make install
As the root user, create a separate user and group for Nginx:
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
NGINX is now installed in
/opt/nginx
.Installing from source doesn’t include an init file to control when Nginx starts and stops during boot and shutdown. You can either extract that file from the nginx-common package at packages.ubuntu.com, or create an SysV script to manage NGINX as shown below:
- File: /etc/init.d/nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/opt/nginx/sbin/nginx NAME=nginx DESC=nginx test -x $DAEMON || exit 0 # Include nginx defaults if available if [ -f /etc/default/nginx ] ; then . /etc/default/nginx fi set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /opt/nginx/logs/$NAME.pid --exec $DAEMON sleep 1 start-stop-daemon --start --quiet --pidfile \ /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 exit 1 ;; esac exit 0
Make the file executable and add it to the default run levels:
sudo chmod +x /etc/init.d/nginx sudo /usr/sbin/update-rc.d -f nginx defaults
Start Nginx:
sudo service nginx start
Testing Nginx
Regardless of installation source or method, Nginx can be tested by navigating to your Linode’s IP address or FQDN in your browser. You should see the NGINX welcome banner shown below.
Continue reading our introduction to Basic NGINX Configuration for more information about using and setting up a web server.
More Information
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 page was originally published on