Nginx Installation from nginx.org
by joe

Posted on 2018-09-28



Installing Nginx from the nginx.org repository yields a somewhat different setup than we get from a standard Ubuntu package install. For one thing, we wind up with the latest stable version of Nginx; for another, we get a different, considerably leaner setup of default files.

These instructions are brief. They track the instructions given at the nginx.org site.


Installation Steps

NOTE: As of January 28, 2019, these same instructions work for installing NGINX version 1.14.2.

First, get the nginx.org signing key and install it. (See manpage documentation of apt-key.)


$ cd /tmp/
$ wget https://nginx.org/keys/nginx_signing.key

$ sudo apt-key add nginx_signing.key

Create a config file identifying the packages to update. This is a supplemental file to identify a package that originates from a provider other than the standard Ubuntu release repository. That file ("nginx.list") consists of the instructions to identify the alternate repository and the files to download from that repository.


$ sudo nano /etc/apt/sources.list.d/nginx.list

Add these lines to the new config file. Note that these lines refer specifically to the "xenial" release of "ubuntu".


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

Once you have identified the nginx.org repository in the "nginx.list" file, you need to register that repository by updating the set of packages. Then the subsequent "apt-get install" command will pick up the nginx package from the nginx.org repository.


$ sudo apt-get update
$ sudo apt-get install nginx

Below we show the results of the "update" and "install" commands on a system that is otherwise fully up-to-date.


$ sudo apt-get update
sudo: unable to resolve host antlet18
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
Get:3 http://nginx.org/packages/ubuntu xenial InRelease [4,339 B]
Hit:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
Get:5 http://nginx.org/packages/ubuntu xenial/nginx Sources [10.8 kB]
Get:6 http://nginx.org/packages/ubuntu xenial/nginx amd64 Packages [15.8 kB]
Fetched 31.0 kB in 1s (28.2 kB/s)
Reading package lists... Done



$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 823 kB of archives.
After this operation, 2,951 kB of additional disk space will be used.
Get:1 http://nginx.org/packages/ubuntu xenial/nginx amd64 nginx amd64 1.14.0-1~xenial [823 kB]
Fetched 823 kB in 1s (477 kB/s)
Selecting previously unselected package nginx.
(Reading database ... 16471 files and directories currently installed.)
Preparing to unpack .../nginx_1.14.0-1~xenial_amd64.deb ...
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
Unpacking nginx (1.14.0-1~xenial) ...
Processing triggers for systemd (229-4ubuntu21.4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up nginx (1.14.0-1~xenial) ...
Processing triggers for systemd (229-4ubuntu21.4) ...
Processing triggers for ureadahead (0.100.0-19) ...

Check the Install Results

Let's look at the result of this installation process.

We put in crlfs in the "nginx -V" output for readibility here.


$ which -a nginx
/usr/sbin/nginx

$ nginx -v
nginx version: nginx/1.14.0


$ nginx -V
nginx version: nginx/1.14.0
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx 
--modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid 
--lock-path=/var/run/nginx.lock 
--http-client-body-temp-path=/var/cache/nginx/client_temp 
--http-proxy-temp-path=/var/cache/nginx/proxy_temp 
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx 
--group=nginx --with-compat --with-file-aio --with-threads 
--with-http_addition_module 
--with-http_auth_request_module --with-http_dav_module 
--with-http_flv_module --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_mp4_module 
--with-http_random_index_module --with-http_realip_module 
--with-http_secure_link_module --with-http_slice_module 
--with-http_ssl_module --with-http_stub_status_module 
--with-http_sub_module --with-http_v2_module --with-mail 
--with-mail_ssl_module --with-stream --with-stream_realip_module 
--with-stream_ssl_module --with-stream_ssl_preread_module 
--with-cc-opt='-g -O2 -fstack-protector-strong -Wformat 
-Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' 
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro 
-Wl,-z,now -Wl,--as-needed -pie'

And here's the structure of the installed Nginx product standard directories.


$ ls -al /etc/nginx
total 22
drwxr-xr-x  3 root root   12 Sep 28 21:26 .
drwxr-xr-x 69 root root  143 Sep 28 21:26 ..
drwxr-xr-x  2 root root    3 Sep 28 21:26 conf.d
-rw-r--r--  1 root root 1007 Apr 17 15:22 fastcgi_params
-rw-r--r--  1 root root 2837 Apr 17 15:22 koi-utf
-rw-r--r--  1 root root 2223 Apr 17 15:22 koi-win
-rw-r--r--  1 root root 5170 Apr 17 15:22 mime.types
lrwxrwxrwx  1 root root   22 Apr 17 16:00 modules -> /usr/lib/nginx/modules
-rw-r--r--  1 root root  643 Apr 17 15:59 nginx.conf
-rw-r--r--  1 root root  636 Apr 17 15:22 scgi_params
-rw-r--r--  1 root root  664 Apr 17 15:22 uwsgi_params
-rw-r--r--  1 root root 3610 Apr 17 15:22 win-utf

And this is the default nginx.conf configuration file in the /etc/nginx directory.


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

What's Different?

The Ubuntu distribution offers four different Nginx installation options: nginx-core, nginx-extras, nginx-full, and nginx-light. (See this Ubuntu Nginx package page.)

Compare the /etc/nginx directory from a standard Ubuntu installation of "nginx-full" with the results shown above for an installation based on the nginx.org source code.


$ which nginx
/usr/sbin/nginx

$ nginx -v
nginx version: nginx/1.10.3 (Ubuntu)


$ nginx -V
nginx version: nginx/1.10.3 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong 
-Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' 
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro 
-Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf 
--http-log-path=/var/log/nginx/access.log 
--error-log-path=/var/log/nginx/error.log 
--lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid 
--http-client-body-temp-path=/var/lib/nginx/body 
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi 
--http-proxy-temp-path=/var/lib/nginx/proxy 
--http-scgi-temp-path=/var/lib/nginx/scgi 
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit 
--with-ipv6 --with-http_ssl_module --with-http_stub_status_module 
--with-http_realip_module --with-http_auth_request_module 
--with-http_addition_module --with-http_dav_module 
--with-http_geoip_module --with-http_gunzip_module 
--with-http_gzip_static_module --with-http_image_filter_module 
--with-http_v2_module --with-http_sub_module --with-http_xslt_module 
--with-stream --with-stream_ssl_module --with-mail 
--with-mail_ssl_module --with-threads 
--add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-auth-pam 
--add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-dav-ext-module 
--add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-echo 
--add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/nginx-upstream-fair 
--add-module=/build/nginx-Q158zN/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_module

Here's the /etc/nginx directory structure:


$ ls -al /etc/nginx
total 26
drwxr-xr-x  6 root root   16 Sep 28 22:16 .
drwxr-xr-x 72 root root  145 Sep 28 22:16 ..
drwxr-xr-x  2 root root    2 Jul 12  2017 conf.d
-rw-r--r--  1 root root 1077 Feb 11  2017 fastcgi.conf
-rw-r--r--  1 root root 1007 Feb 11  2017 fastcgi_params
-rw-r--r--  1 root root 2837 Feb 11  2017 koi-utf
-rw-r--r--  1 root root 2223 Feb 11  2017 koi-win
-rw-r--r--  1 root root 3957 Feb 11  2017 mime.types
-rw-r--r--  1 root root 1462 Feb 11  2017 nginx.conf
-rw-r--r--  1 root root  180 Feb 11  2017 proxy_params
-rw-r--r--  1 root root  636 Feb 11  2017 scgi_params
drwxr-xr-x  2 root root    3 Sep 28 22:16 sites-available
drwxr-xr-x  2 root root    3 Sep 28 22:16 sites-enabled
drwxr-xr-x  2 root root    4 Sep 28 22:16 snippets
-rw-r--r--  1 root root  664 Feb 11  2017 uwsgi_params
-rw-r--r--  1 root root 3071 Feb 11  2017 win-utf


And here's the default nginx.conf file in /etc/nginx:


user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

What if Nginx is Already Installed?

Assuming that you already have some version of Nginx already installed, you can still successfully install Nginx from the nginx.org repository pretty straightforwardly.

Just follow the instructions at the top of this post for installing Nginx from the nginx.org repository. When you execute the "apt-get update" command, you'll see that Ubuntu recognizes nginx.org as the appropriate source for an Nginx installation-upgrade. When you execute the "apt-get upgrade" command, the system informs you that it will un-install the existing Nginx ("nginx-common" and "nginx-full") and install the version from nginx.org ("nginx").


$ sudo apt-get update
sudo: unable to resolve host antlet19
Get:1 http://nginx.org/packages/ubuntu xenial InRelease [4,339 B]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:3 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:4 http://nginx.org/packages/ubuntu xenial/nginx Sources [10.8 kB]
Get:5 http://nginx.org/packages/ubuntu xenial/nginx amd64 Packages [15.8 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Fetched 247 kB in 1s (237 kB/s)
Reading package lists... Done

$ sudo apt-get install nginx
sudo: unable to resolve host antlet19
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  fontconfig-config fonts-dejavu-core geoip-database libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu55
  libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxml2 libxpm4 libxslt1.1 sgml-base xml-core
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nginx-common nginx-full
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 2 to remove and 4 not upgraded.
Need to get 823 kB of archives.
After this operation, 1,436 kB of additional disk space will be used.
Do you want to continue? [Y/n]
When we check the system status, we see that we still have only one Nginx installation (at "/usr/sbin/nginx") , but that now the version is 1.14.0. The exact file and directory makeup, though, is a mixed bag.

Some of the preceding standard Ubuntu installation is preserved, even though it was additional to the nginx.org installation. The additional directories for "sites-available", "sites-enable", and "snippets" remain along with the files "fastcgi.conf" and "proxy_params". However, wherever the two installations disagree as to the exact contents of a standard installation file, the nginx.org version replaced the Ubuntu package version.


$ which -a nginx
/usr/sbin/nginx

$ nginx -v
nginx version: nginx/1.14.0

$ ls -al /etc/nginx
total 26
drwxr-xr-x  6 root root   17 Sep 28 23:03 .
drwxr-xr-x 72 root root  146 Sep 28 23:03 ..
drwxr-xr-x  2 root root    3 Sep 28 23:03 conf.d
-rw-r--r--  1 root root 1077 Feb 11  2017 fastcgi.conf
-rw-r--r--  1 root root 1007 Feb 11  2017 fastcgi_params
-rw-r--r--  1 root root 2837 Feb 11  2017 koi-utf
-rw-r--r--  1 root root 2223 Feb 11  2017 koi-win
-rw-r--r--  1 root root 5170 Apr 17 15:22 mime.types
lrwxrwxrwx  1 root root   22 Apr 17 16:00 modules -> /usr/lib/nginx/modules
-rw-r--r--  1 root root  643 Apr 17 15:59 nginx.conf
-rw-r--r--  1 root root  180 Feb 11  2017 proxy_params
-rw-r--r--  1 root root  636 Feb 11  2017 scgi_params
drwxr-xr-x  2 root root    3 Sep 28 22:16 sites-available
drwxr-xr-x  2 root root    3 Sep 28 22:16 sites-enabled
drwxr-xr-x  2 root root    4 Sep 28 22:16 snippets
-rw-r--r--  1 root root  664 Feb 11  2017 uwsgi_params
-rw-r--r--  1 root root 3610 Apr 17 15:22 win-utf

Cites
We found the basics of these instructions originally at nixCraft. nixCraft has proven useful on more than one occasion.

These installation steps track those available from the Nginx site - with a little explanation added.
Antsle Experience
Another Antsle-assisted installation. We spun up multiple Ubuntu 16.04 LXCs to test the various installation options.

Then we snuffed them.

Which reminds us to caution that these instructions are suitable to a new or clean Ubuntu platform. There may be complications based on other packages running on your computer.

Comments

It will be some time yet before we get a comments section working here. In the meantime feel free to send comments via email. On this site our name is Joe Python. The email address is our first name at joepython.com.

Edited: 2019-01-30 20:55:12(utc) Generated: 2019-06-10 17:29:58(utc)