Wednesday, September 13, 2017

Build nginx from source on AWS EC2




Launch a new EC2

Type: t2.small (t2.micro may also be fine)
AMIUbuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-6e1a0117

Configure nginx repository

ubuntu@ubuntu:sudo su
root@ubuntu:edit /etc/hosts and add entry for hostname to ip
root@ubuntu:cd /opt
root@ubuntu:add-apt-repository ppa:nginx/stable
edit /etc/apt/sources.list.d/nginx-ubuntu-stable-xenial.list and uncomment deb-src line
root@ubuntu:apt-get update

Install nginx dependencies and get source

root@ubuntu:apt-get build-dep nginx

root@ubuntu:/opt# apt-get source nginx
Reading package lists... Done
NOTICE: 'nginx' packaging is maintained in the 'Git' version control system at:
https://anonscm.debian.org/cgit/pkg-nginx/nginx.git
Please use:
git clone https://anonscm.debian.org/cgit/pkg-nginx/nginx.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 1,835 kB of source archives.
Get:1 http://ppa.launchpad.net/nginx/stable/ubuntu xenial/main nginx 1.12.1-0+xenial0 (tar) [981 kB]
Get:2 http://ppa.launchpad.net/nginx/stable/ubuntu xenial/main nginx 1.12.1-0+xenial0 (diff) [849 kB]
Get:3 http://ppa.launchpad.net/nginx/stable/ubuntu xenial/main nginx 1.12.1-0+xenial0 (dsc) [4,187 B]
Fetched 1,835 kB in 1s (924 kB/s)
gpgv: Signature made Wed 12 Jul 2017 11:41:54 PM UTC using RSA key ID F569EF55
gpgv: Can't check signature: public key not found
dpkg-source: warning: failed to verify signature on ./nginx_1.12.1-0+xenial0.dsc
dpkg-source: info: extracting nginx in nginx-1.12.1
dpkg-source: info: unpacking nginx_1.12.1.orig.tar.gz
dpkg-source: info: unpacking nginx_1.12.1-0+xenial0.debian.tar.xz
dpkg-source: info: applying 0002-Make-sure-signature-stays-the-same-in-all-nginx-buil.patch
dpkg-source: info: applying 0003-define_gnu_source-on-other-glibc-based-platforms.patch
W: Can't drop privileges for downloading as file 'nginx_1.12.1.orig.tar.gz' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
root@ubuntu:/opt# ls 
nginx-1.12.1  nginx_1.12.1-0+xenial0.debian.tar.xz  nginx_1.12.1-0+xenial0.dsc  nginx_1.12.1.orig.tar.gz
root@ubuntu:/opt#

Build nginx (debian package)

root@ubuntu:/opt/nginx-1.12.1# cd nginx-1.12.1/
root@ubuntu:/opt/nginx-1.12.1# dpkg-buildpackage -uc -b
dpkg-buildpackage: source package nginx
dpkg-buildpackage: source version 1.12.1-0+xenial0
dpkg-buildpackage: source distribution xenial
dpkg-buildpackage: source changed by Thomas Ward
dpkg-buildpackage: host architecture amd64
 dpkg-source --before-build nginx-1.12.1
 debian/rules clean
dh clean --with systemd
   dh_testdir
   dh_auto_clean
   debian/rules override_dh_clean
make[1]: Entering directory '/opt/nginx-1.12.1'
if [ -s /opt/nginx-1.12.1/debian/modules/nginx-cache-purge/.pc/applied-patches ]; then \
cd /opt/nginx-1.12.1/debian/modules/nginx-cache-purge && QUILT_PATCHES=/opt/nginx-1.12.1/debian/modules/patches/nginx-cache-purge quilt pop -q -a; \
...
...
...
   dh_builddeb
dpkg-deb --build debian/nginx ..
dpkg-deb: building package 'nginx' in '../nginx_1.12.1-0+xenial0_all.deb'.
dpkg-deb --build debian/nginx-doc ..
dpkg-deb: building package 'nginx-doc' in '../nginx-doc_1.12.1-0+xenial0_all.deb'.
dpkg-deb --build debian/nginx-common ..
dpkg-deb: building package 'nginx-common' in '../nginx-common_1.12.1-0+xenial0_all.deb'.
dpkg-deb --build debian/nginx-full ..
dpkg-deb: building package 'nginx-full' in '../nginx-full_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/nginx-light ..
dpkg-deb: building package 'nginx-light' in '../nginx-light_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/nginx-extras ..
dpkg-deb: building package 'nginx-extras' in '../nginx-extras_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-geoip ..
dpkg-deb: building package 'libnginx-mod-http-geoip' in '../libnginx-mod-http-geoip_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-image-filter ..
dpkg-deb: building package 'libnginx-mod-http-image-filter' in '../libnginx-mod-http-image-filter_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-xslt-filter ..
dpkg-deb: building package 'libnginx-mod-http-xslt-filter' in '../libnginx-mod-http-xslt-filter_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-mail ..
dpkg-deb: building package 'libnginx-mod-mail' in '../libnginx-mod-mail_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-stream ..
dpkg-deb: building package 'libnginx-mod-stream' in '../libnginx-mod-stream_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-perl ..
dpkg-deb: building package 'libnginx-mod-http-perl' in '../libnginx-mod-http-perl_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-auth-pam ..
dpkg-deb: building package 'libnginx-mod-http-auth-pam' in '../libnginx-mod-http-auth-pam_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-lua ..
dpkg-deb: building package 'libnginx-mod-http-lua' in '../libnginx-mod-http-lua_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-ndk ..
dpkg-deb: building package 'libnginx-mod-http-ndk' in '../libnginx-mod-http-ndk_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-nchan ..
dpkg-deb: building package 'libnginx-mod-nchan' in '../libnginx-mod-nchan_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-echo ..
dpkg-deb: building package 'libnginx-mod-http-echo' in '../libnginx-mod-http-echo_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-upstream-fair ..
dpkg-deb: building package 'libnginx-mod-http-upstream-fair' in '../libnginx-mod-http-upstream-fair_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-headers-more-filter ..
dpkg-deb: building package 'libnginx-mod-http-headers-more-filter' in '../libnginx-mod-http-headers-more-filter_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-cache-purge ..
dpkg-deb: building package 'libnginx-mod-http-cache-purge' in '../libnginx-mod-http-cache-purge_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-fancyindex ..
dpkg-deb: building package 'libnginx-mod-http-fancyindex' in '../libnginx-mod-http-fancyindex_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-uploadprogress ..
dpkg-deb: building package 'libnginx-mod-http-uploadprogress' in '../libnginx-mod-http-uploadprogress_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-subs-filter ..
dpkg-deb: building package 'libnginx-mod-http-subs-filter' in '../libnginx-mod-http-subs-filter_1.12.1-0+xenial0_amd64.deb'.
dpkg-deb --build debian/libnginx-mod-http-dav-ext ..
dpkg-deb: building package 'libnginx-mod-http-dav-ext' in '../libnginx-mod-http-dav-ext_1.12.1-0+xenial0_amd64.deb'.
 dpkg-genchanges -b >../nginx_1.12.1-0+xenial0_amd64.changes
dpkg-genchanges: binary-only upload (no source code included)
 dpkg-source --after-build nginx-1.12.1
dpkg-buildpackage: binary-only upload (no source included)
root@ubuntu:/opt/nginx-1.12.1#



Verify the build output

root@ubuntu:/opt/nginx-1.12.1# ls -l ../
total 4084
-rw-r--r--  1 root root  28526 Sep 13 21:51 libnginx-mod-http-auth-pam_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  30820 Sep 13 21:51 libnginx-mod-http-cache-purge_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  30218 Sep 13 21:51 libnginx-mod-http-dav-ext_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  39874 Sep 13 21:51 libnginx-mod-http-echo_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  33470 Sep 13 21:51 libnginx-mod-http-fancyindex_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  29760 Sep 13 21:51 libnginx-mod-http-geoip_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  33794 Sep 13 21:51 libnginx-mod-http-headers-more-filter_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  32984 Sep 13 21:51 libnginx-mod-http-image-filter_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root 166044 Sep 13 21:51 libnginx-mod-http-lua_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  29014 Sep 13 21:51 libnginx-mod-http-ndk_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  40154 Sep 13 21:51 libnginx-mod-http-perl_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  31482 Sep 13 21:51 libnginx-mod-http-subs-filter_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  34958 Sep 13 21:51 libnginx-mod-http-uploadprogress_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  31676 Sep 13 21:51 libnginx-mod-http-upstream-fair_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  31466 Sep 13 21:51 libnginx-mod-http-xslt-filter_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  59964 Sep 13 21:51 libnginx-mod-mail_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root 170052 Sep 13 21:51 libnginx-mod-nchan_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root  80960 Sep 13 21:51 libnginx-mod-stream_1.12.1-0+xenial0_amd64.deb
drwxr-xr-x 10 root root   4096 Sep 13 21:46 nginx-1.12.1
-rw-r--r--  1 root root  24320 Sep 13 21:51 nginx_1.12.1-0+xenial0_all.deb
-rw-r--r--  1 root root  10357 Sep 13 21:51 nginx_1.12.1-0+xenial0_amd64.changes
-rw-r--r--  1 root root 849428 Jul 14 14:28 nginx_1.12.1-0+xenial0.debian.tar.xz
-rw-r--r--  1 root root   4187 Jul 14 14:28 nginx_1.12.1-0+xenial0.dsc
-rw-r--r--  1 root root 981093 Jul 14 14:28 nginx_1.12.1.orig.tar.gz
-rw-r--r--  1 root root  56958 Sep 13 21:51 nginx-common_1.12.1-0+xenial0_all.deb
-rw-r--r--  1 root root  35892 Sep 13 21:51 nginx-doc_1.12.1-0+xenial0_all.deb
-rw-r--r--  1 root root 425094 Sep 13 21:51 nginx-extras_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root 412056 Sep 13 21:51 nginx-full_1.12.1-0+xenial0_amd64.deb
-rw-r--r--  1 root root 389160 Sep 13 21:51 nginx-light_1.12.1-0+xenial0_amd64.deb
root@ubuntu:/opt/nginx-1.12.1#

Install newly build nginx and dependencies

root@ubuntu:/opt# dpkg --install libnginx-mod-*
Selecting previously unselected package libnginx-mod-http-auth-pam.
(Reading database ... 57824 files and directories currently installed.)
Preparing to unpack libnginx-mod-http-auth-pam_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-auth-pam (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-cache-purge.
Preparing to unpack libnginx-mod-http-cache-purge_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-cache-purge (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-dav-ext.
Preparing to unpack libnginx-mod-http-dav-ext_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-dav-ext (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-echo.
Preparing to unpack libnginx-mod-http-echo_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-echo (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-fancyindex.
Preparing to unpack libnginx-mod-http-fancyindex_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-fancyindex (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-geoip.
Preparing to unpack libnginx-mod-http-geoip_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-geoip (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-headers-more-filter.
Preparing to unpack libnginx-mod-http-headers-more-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-headers-more-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-image-filter.
Preparing to unpack libnginx-mod-http-image-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-image-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-lua.
Preparing to unpack libnginx-mod-http-lua_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-lua (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-ndk.
Preparing to unpack libnginx-mod-http-ndk_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-ndk (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-perl.
Preparing to unpack libnginx-mod-http-perl_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-perl (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-subs-filter.
Preparing to unpack libnginx-mod-http-subs-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-subs-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-uploadprogress.
Preparing to unpack libnginx-mod-http-uploadprogress_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-uploadprogress (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-upstream-fair.
Preparing to unpack libnginx-mod-http-upstream-fair_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-upstream-fair (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-http-xslt-filter.
Preparing to unpack libnginx-mod-http-xslt-filter_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-http-xslt-filter (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-mail.
Preparing to unpack libnginx-mod-mail_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-mail (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-nchan.
Preparing to unpack libnginx-mod-nchan_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-nchan (1.12.1-0+xenial0) ...
Selecting previously unselected package libnginx-mod-stream.
Preparing to unpack libnginx-mod-stream_1.12.1-0+xenial0_amd64.deb ...
Unpacking libnginx-mod-stream (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-auth-pam (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-cache-purge (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-dav-ext (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-echo (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-fancyindex (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-geoip (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-headers-more-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-image-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-ndk (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-perl (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-subs-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-uploadprogress (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-upstream-fair (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-xslt-filter (1.12.1-0+xenial0) ...
Setting up libnginx-mod-mail (1.12.1-0+xenial0) ...
Setting up libnginx-mod-nchan (1.12.1-0+xenial0) ...
Setting up libnginx-mod-stream (1.12.1-0+xenial0) ...
Setting up libnginx-mod-http-lua (1.12.1-0+xenial0) ...
root@ubuntu:/opt#

root@ubuntu:/opt# dpkg --install nginx-full_1.12.1-0+xenial0_amd64.deb
(Reading database ... 57921 files and directories currently installed.)
Preparing to unpack nginx-full_1.12.1-0+xenial0_amd64.deb ...
Unpacking nginx-full (1.12.1-0+xenial0) over (1.12.1-0+xenial0) ...
Setting up nginx-full (1.12.1-0+xenial0) ...
Processing triggers for man-db (2.7.5-1) ...


Verify Installation


root@ubuntu:/opt# which nginx
/usr/sbin/nginx

root@ubuntu:/opt# ls /etc/init.d/nginx
/etc/init.d/nginx
root@ubuntu:/opt#

root@ubuntu:/opt# nginx -V
nginx version: nginx/1.12.1
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 -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -fPIC' --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 --modules-path=/usr/lib/nginx/modules --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-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/opt/nginx-1.12.1/debian/modules/nginx-auth-pam --add-dynamic-module=/opt/nginx-1.12.1/debian/modules/nginx-dav-ext-module --add-dynamic-module=/opt/nginx-1.12.1/debian/modules/nginx-echo --add-dynamic-module=/opt/nginx-1.12.1/debian/modules/nginx-upstream-fair --add-dynamic-module=/opt/nginx-1.12.1/debian/modules/ngx_http_substitutions_filter_module
root@ubuntu:/opt#


Try accessing "http://ipaddress" in browser. You will see following:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.


References:

http://www.geoffstratton.com/install-latest-nginx-source-ubuntu-1604
https://serversforhackers.com/c/compiling-third-party-modules-into-nginx