Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current Ubuntu documentation for the latest information.

In this step-by-step guide, you’ll learn build your own version of nginx. Ubuntu is a popular Linux distribution based on Debian, widely used for servers and development environments.

Prerequisites

Before you begin, make sure you have:

  • A system running Ubuntu (desktop or server edition)
  • Terminal access with sudo privileges
  • Basic familiarity with Linux command line

How to: Build your own version of NginX

I was following a tutorial on how to use NginX with (http://wordpress.org “WordPress”) and the NginX Fast CGI cache caught my interest. The only problem is that in order to NginX to purge the cached version it needs a custom third party module that does not come out of the box. There is a repository that has it but it is built for Ubuntu 12.0.4TLS and even when I try to deploy it in that environment it fails. I decided I would toy around with building from source for the first time! I keep the files I am using and settings available in Github: https://github.com/JCBauza/NginX/


I. Permissions

If you get error: ”./configure — Permission denied” you need to give the file execute permissions with: sudo chmod +x /path/to/configure/script


II. Dependencies

If you don’t install this on the computer you are doing the build you won’t be able to compile. Below are the common errors and the libraries you need to install:

a) (http://www.pcre.org “Perl Compatible Regular Expressions”) library

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using –without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using –with-pcre= option. apt-get install libpcre3 libpcre3-dev

b) C++ Compiler

configure: error: You need a C++ compiler for C++ support. apt-get install build-essential

c) (http://www.openssl.org/ “OpenSSL”)

./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using –without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using –with-http_ssl_module –with-openssl= options. ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using –with-openssl= option. sudo apt-get install openssl libssl-dev libperl-dev

d) Xml xslt

./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries. apt-get install libxslt-dev

e) GD Library

./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. apt-get install libgd2-xpm libgd2-xpm-dev

f) (http://en.wikipedia.org/wiki/Geolocation “Geolocation”) Library

./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. apt-get install libgeoip-dev I hope this solves the issue, at least after this I am able to complete the configuration (.configure) but I sill see some not founds:

checking for setproctitle() … not found checking for pread() … found checking for pwrite() … found checking for sys_nerr … found checking for localtime_r() … found checking for posix_memalign() … found checking for memalign() … found checking for mmap(MAP_ANON|MAP_SHARED) … found checking for mmap(“/dev/zero”, MAP_SHARED) … found checking for System V shared memory … found checking for POSIX semaphores … not found checking for POSIX semaphores in libpthread … found checking for struct msghdr.msg_control … found checking for ioctl(FIONBIO) … found checking for struct tm.tm_gmtoff … found checking for struct dirent.d_namlen … not found checking for struct dirent.d_type … found checking for sysconf(_SC_NPROCESSORS_ONLN) … found checking for openat(), fstatat() … found checking for getaddrinfo() … found configuring additional modules adding module in /Build/NginX/nginxmodules/nginx_http_auth_pam/1.2 + ngx_http_auth_pam_module was configured adding module in /Build/NginX/nginxmodules/echo-nginx-module/0.49 + ngx_http_echo_module was configured adding module in /Build/NginX/nginxmodules/nginx-upstream-fair + ngx_http_upstream_fair_module was configured adding module in /Build/NginX/nginxmodules/nginx_http_substitutions_filter_module/0.6.2 + ngx_http_subs_filter_module was configured adding module in /Build/NginX/nginxmodules/nginx_cache_purge/2.1 + ngx_http_cache_purge_module was configured checking for PCRE library … found checking for PCRE JIT support … not found checking for OpenSSL library … found checking for zlib library … found checking for libxslt … found checking for libexslt … found checking for GD library … found checking for GeoIP library … found checking for GeoIP IPv6 support … found creating objs/Makefile   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: “/usr/share/nginx” nginx binary file: “/usr/share/nginx/sbin/nginx” nginx configuration prefix: “/etc/nginx” nginx configuration file: “/etc/nginx/nginx.conf” nginx pid file: “/run/nginx.pid” nginx error log file: “/var/log/nginx/error.log” nginx http access log file: “/var/log/nginx/access.log” nginx http client request body temporary files: “/var/lib/nginx/body” nginx http proxy temporary files: “/var/lib/nginx/proxy” nginx http fastcgi temporary files: “/var/lib/nginx/fastcgi” nginx http uwsgi temporary files: “/var/lib/nginx/uwsgi” nginx http scgi temporary files: “/var/lib/nginx/scgi”

g) PAM authentication module

If you encounter this error while doing the make:

cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/include/libxml2 -I objs -I src/http -I src/http/modules -I src/mail \ -o objs/addon/1.2/ngx_http_auth_pam_module.o \ /Build/NginX/nginxmodules/nginx_http_auth_pam/1.2/ngx_http_auth_pam_module.c /Build/NginX/nginxmodules/nginx_http_auth_pam/1.2/ngx_http_auth_pam_module.c:13:31: fatal error: security/pam_appl.h: No such file or directory compilation terminated. make: *** Error 1

then you need to add the pam library: apt-get install libpam-dev Run Make again.

III. Script to follow

Below is a script you can follow to deploy to rebuild with the latest dependencies:

sudo su -   # stuff we need to build from source apt-get install libpcre3-dev build-essential libssl-dev libxslt-dev libgd2-xpm libgd2-xpm-dev libgeoip-dev   # configure with chosen modules - see http://wiki.nginx.org/InstallOptions ./configure \ —prefix=/usr/share/nginx \ —conf-path=/etc/nginx/nginx.conf \ —error-log-path=/var/log/nginx/error.log \ —http-client-body-temp-path=/var/lib/nginx/body \ —http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ —http-log-path=/var/log/nginx/access.log \ —http-proxy-temp-path=/var/lib/nginx/proxy \ —http-scgi-temp-path=/var/lib/nginx/scgi \ —http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ —lock-path=/var/lock/nginx.lock \ —pid-path=/run/nginx.pid \ —with-pcre-jit \ —with-debug \ —with-http_addition_module \ —with-http_dav_module \ —with-http_geoip_module \ —with-http_gzip_static_module \ —with-http_image_filter_module \ —with-http_realip_module \ —with-http_stub_status_module \ —with-http_ssl_module \ —with-http_sub_module \ —with-http_xslt_module \ —with-ipv6 \ —with-mail \ —with-mail_ssl_module \ —add-module=/Build/NginX/nginxmodules/nginx_http_auth_pam/1.2 \ —add-module=/Build/NginX/nginxmodules/echo-nginx-module/0.49 \ —add-module=/Build/NginX/nginxmodules/nginx-upstream-fair \ —add-module=/Build/NginX/nginxmodules/nginx_http_substitutions_filter_module/0.6.2 \ —add-module=/Build/NginX/nginxmodules/nginx_cache_purge/2.1   make make install vi /etc/init.d/nginx # edit the DEAMON with the correct new path, which is now /usr/share/nginx/sbin/nginx from /usr/sbin/nginx   /etc/init.d/nginx start

   

(http://img.zemanta.com/zemified_h.png?x-id=422455c0-3cb5-4f7d-b233-50acb91f2964)](http://www.zemanta.com/?px “Enhanced by Zemanta”)

Summary

You’ve successfully learned build your own version of nginx. If you run into any issues, double-check the prerequisites and ensure your Ubuntu environment is properly configured.