How to re-compile nginx webserver on Debian Linux

Let’s say you wish to do some changes to nginx source code in
order to add or remove some features before the actual nginx
package installation. In this config we will show how to recompile
nginx package on Debian linux.

First install package building tools:

# apt-get install dpkg-dev

Next, we need to install all nginx build dependencies:

# apt-get build-dep nginx

Download nginx source code:

$ mkdir nginx-local
$ cd nginx-local/
$ apt-get source nginx

The above command will download all necessary nginx source files
to be alter used to build *.deb debian package.

$ tree -L 2 
.
├── nginx-1.6.2
│   ├── auto
│   ├── CHANGES
│   ├── CHANGES.ru
│   ├── conf
│   ├── configure
│   ├── contrib
│   ├── debian
│   ├── html
│   ├── LICENSE
│   ├── man
│   ├── README
│   └── src
├── nginx_1.6.2-5.debian.tar.xz
├── nginx_1.6.2-5.dsc
└── nginx_1.6.2.orig.tar.gz

8 directories, 8 files

As an example we can now amend the source code to change web servers name from
nginx to Labnix Private Web Server. Edit
nginx-1.6.2/src/http/ngx_http_header_filter_module.c
lines

FROM:
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
TO:
static char ngx_http_server_string[] = "Server: Labnix Private Web Server" CRLF;
static char ngx_http_server_full_string[] = "Server: Labnix Private Web Server" CRLF;

Once you have performed all necessary changes to the nginx’s source code
it’s time to build a new *.deb package:

$ cd nginx-1.6.2/
$ dpkg-buildpackage -rfakeroot -uc -b
....
        dpkg-deb --build debian/nginx ..
dpkg-deb: building package `nginx' in `../nginx_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-doc ..
dpkg-deb: building package `nginx-doc' in `../nginx-doc_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-common ..
dpkg-deb: building package `nginx-common' in `../nginx-common_1.6.2-5_all.deb'.
        dpkg-deb --build debian/nginx-full ..
dpkg-deb: building package `nginx-full' in `../nginx-full_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-full-dbg ..
dpkg-deb: building package `nginx-full-dbg' in `../nginx-full-dbg_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-light ..
dpkg-deb: building package `nginx-light' in `../nginx-light_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-light-dbg ..
dpkg-deb: building package `nginx-light-dbg' in `../nginx-light-dbg_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-extras ..
dpkg-deb: building package `nginx-extras' in `../nginx-extras_1.6.2-5_amd64.deb'.
        dpkg-deb --build debian/nginx-extras-dbg ..
dpkg-deb: building package `nginx-extras-dbg' in `../nginx-extras-dbg_1.6.2-5_amd64.deb'.
 dpkg-genchanges -b >../nginx_1.6.2-5_amd64.changes
dpkg-genchanges: binary-only upload (no source code included)
 dpkg-source --after-build nginx-1.6.2
dpkg-buildpackage: binary-only upload (no source included)

The new re-comiled packages are now ready to be installed:

$ cd ..
$ ls
nginx-1.6.2            nginx_1.6.2-5_amd64.changes  nginx_1.6.2-5.dsc        nginx-common_1.6.2-5_all.deb  nginx-extras_1.6.2-5_amd64.deb      nginx-full_1.6.2-5_amd64.deb      nginx-light_1.6.2-5_amd64.deb
nginx_1.6.2-5_all.deb  nginx_1.6.2-5.debian.tar.xz  nginx_1.6.2.orig.tar.gz  nginx-doc_1.6.2-5_all.deb     nginx-extras-dbg_1.6.2-5_amd64.deb  nginx-full-dbg_1.6.2-5_amd64.deb  nginx-light-dbg_1.6.2-5_amd64.deb

Install nginx from newly build packages:

# dpkg -i nginx_1.6.2-5_all.deb nginx-full_1.6.2-5_amd64.deb nginx-common_1.6.2-5_all.deb nginx-doc_1.6.2-5_all.deb

Check status webserver status:

 systemctl status nginx
   nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Wed 2015-04-15 09:46:53 AEST; 1min 18s ago
  Process: 3535 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 3534 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 3538 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3538 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─3539 nginx: worker process
           ├─3540 nginx: worker process
           ├─3541 nginx: worker process
           └─3542 nginx: worker process

Confirm server name changes:

# curl -I http://localhost
HTTP/1.1 200 OK
Server: Labnix Private Web Server
Date: Tue, 14 Apr 2015 23:49:37 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Tue, 14 Apr 2015 23:45:07 GMT
Connection: keep-alive
ETag: "552da683-363"
Accept-Ranges: bytes