32MB.CN

Building Nginx From Source On Debian
This tutorial describes how you can build nginx from sour...
扫描右侧二维码阅读全文
25
2015/12

Building Nginx From Source On Debian

This tutorial describes how you can build nginx from source on Debian.

1 Streamline the vps System (not have to)

Here is a user guide for Debian user《Handmade Clean and Streamlined Debian VPS System

2 Downloading nginx source

The latest version of nginx can be found on the following website: nginx.org/en/download.html. At time of writting it's 1.9.9 - but we are going to download 1.9.5:

cd /tmp
wget http://nginx.org/download/nginx-1.9.5.tar.gz

and unpack the source:

tar -xzvf nginx-1.9.5.tar.gz

3 Additional 3rd-party modules

Nginx supports dynamic loading of modules you need.

3.1 Downloading the modules

SPDY
SPDY is a module that allows your web server to take advantage of SPDY features like stream multiplexing and header compression and lot of people are taking about it to be a new standard (what it seems to be).
You don't have to download spdy patch for nginx right now.

nginx substitutions filter
nginx_substitutions_filter is a filter module which can do both regular expression and fixed string substitutions on response bodies. This module is quite different from the Nginx's native Substitution Module.It scans the output chains buffer and matches string line by line, just like Apache's mod_substitute(http://httpd.apache.org/docs/trunk/mod/mod_substitute.html).

git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

cache purge
You can use this module for nginx in front of apache,acting as a reverse proxy and caching the results of proxy_pass. Otherwise, skip it:

git clone https://github.com/FRiCKLE/ngx_cache_purge

3.2 Another Additional things

PCRE
rewrite module need Perl Compatible Regular Expressions.
You can download the lastest version of pcre on ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
tar zxf pcre-8.37.tar.gz

4 Building nginx

Until now we have downloaded the nginx source, some additional 3rd-party modules for it. It's time to build our nginx installation now.

It's like with all C applications - a 3-steper. We're going through the first step in more detail:

./configure --help 

This will show you a list of all available configuration options for the build like what modules can be disabled, enabled and so on. Mine looks like this (shorten):

--help print this message

--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname

--user=USER set non-privileged user for

                                 worker processes

--group=GROUP set non-privileged group for

                                 worker processes

--build=NAME set build name
--builddir=DIR set build directory

--with-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module

--with-threads enable thread pool support

--with-file-aio enable file AIO support
--with-ipv6 enable IPv6 support

--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_stub_status_module enable ngx_http_stub_status_module

--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_conn_module disable ngx_http_limit_conn_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_hash_module

                                 disable ngx_http_upstream_hash_module

--without-http_upstream_ip_hash_module

                                 disable ngx_http_upstream_ip_hash_module

--without-http_upstream_least_conn_module

                                 disable ngx_http_upstream_least_conn_module

--without-http_upstream_keepalive_module

                                 disable ngx_http_upstream_keepalive_module

--without-http_upstream_zone_module

                                 disable ngx_http_upstream_zone_module

--with-http_perl_module enable ngx_http_perl_module
--with-perl_modules_path=PATH set Perl modules path
--with-perl=PATH set perl binary pathname

--http-log-path=PATH set http access log pathname
--http-client-body-temp-path=PATH set path to store

                                 http client request body temporary files

--http-proxy-temp-path=PATH set path to store

                                 http proxy temporary files

--http-fastcgi-temp-path=PATH set path to store

                                 http fastcgi temporary files

--http-uwsgi-temp-path=PATH set path to store

                                 http uwsgi temporary files

--http-scgi-temp-path=PATH set path to store

                                 http scgi temporary files

--without-http disable HTTP server
--without-http-cache disable HTTP cache

--with-mail enable POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module enable ngx_mail_ssl_module
--without-mail_pop3_module disable ngx_mail_pop3_module
--without-mail_imap_module disable ngx_mail_imap_module
--without-mail_smtp_module disable ngx_mail_smtp_module

--with-stream enable TCP proxy module
--with-stream_ssl_module enable ngx_stream_ssl_module
--without-stream_limit_conn_module disable ngx_stream_limit_conn_module
--without-stream_access_module disable ngx_stream_access_module
--without-stream_upstream_hash_module

                                 disable ngx_stream_upstream_hash_module

--without-stream_upstream_least_conn_module

                                 disable ngx_stream_upstream_least_conn_module

--without-stream_upstream_zone_module

                                 disable ngx_stream_upstream_zone_module

--with-google_perftools_module enable ngx_google_perftools_module
--with-cpp_test_module enable ngx_cpp_test_module

--add-module=PATH enable an external module

--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:

                                 pentium, pentiumpro, pentium3, pentium4,
                                 athlon, opteron, sparc32, sparc64, ppc64

--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation support

--with-md5=DIR set path to md5 library sources
--with-md5-opt=OPTIONS set additional build options for md5
--with-md5-asm use md5 assembler sources

--with-sha1=DIR set path to sha1 library sources
--with-sha1-opt=OPTIONS set additional build options for sha1
--with-sha1-asm use sha1 assembler sources

--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized

                                 for the specified CPU, valid values:
                                 pentium, pentiumpro

--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources

--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL

--with-debug enable debug logging

This will help you a lot deciding what modules you want to include/exclude, as well as what else can be defined.

We are going to use the following configure command:

./configure \

--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--with-pcre=/tmp/pcre-8.37 \
--user=www \
--group=www \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-ipv6 \
--with-http_ssl_module \
 --with-http_spdy_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--add-module=/tmp/ngx_http_substitutions_filter_module \
--add-module=/tmp/ngx_cache_purge-2.0

I did not use Debian standard paths here, but feel free to change them.
Next, we can compile the source:

make

Attention: If this throws any errors, you should carefully read them to see where the problem is, and what it is.

Finally, install the compiled source:

make install

5 Enable autostart

Debian uses init scripts through which you can control service behavior. I'm using the one from official nginx package - all credits belong to it's maintainer:

#!/bin/sh
### BEGIN INIT INFO
# Provides:  nginx
# Required-Start:$local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# 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=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx default if available
if [ -f /etc/default/nginx ]; then
  . /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
    return 0
    else
    $DAEMON -t $DAEMON_OPTS
    return $?
    fi
}
case "$1" in
    start)
    echo -n "Starting $DESC: "
    test_nginx_config
    # Check if the ULIMIT is set in /etc/default/nginx
    if [ -n "$ULIMIT" ]; then
        # Set the ulimits
        ulimit $ULIMIT
    fi
    start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
    stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
    restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON || true
    sleep 1
    test_nginx_config
    # Check if the ULIMIT is set in /etc/default/nginx
    if [ -n "$ULIMIT" ]; then
        # Set the ulimits
        ulimit $ULIMIT
    fi
    start-stop-daemon --start --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
    reload)
    echo -n "Reloading $DESC configuration: "
    test_nginx_config
    start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
    configtest|testconfig)
    echo -n "Testing $DESC configuration: "
    if test_nginx_config; then
        echo "$NAME."
    else
        exit $?
    fi
    ;;
    status)
    status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
    ;;
    *)
    echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
    exit 1
    ;;
esac
exit 0

Don't forget to chmod:

chmod 755 /etc/init.d/nginx

We can also create Debian style default file:

nano /etc/default/nginx

and paste:

# Note: You may want to look at the following page before setting the ULIMIT.
#  http://wiki.nginx.org/CoreModule >  #worker_rlimit_nofile
# Set the ulimit variable if you need defaults to change.
#  Example: ULIMIT="-n 4096"
ULIMIT = "-n 4096"

Last but not least, if you like to autostart nginx on boot you can run:

update-rc.d nginx defaults

Now nginx will autorun on your vps.

Last modification:January 2nd, 2016 at 03:46 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment