Ubuntu: Create own deb package

I'll show you how to create own deb package using modified nginx
1) we download nginx sources
2) we download some external nginx modules
3) we run

1
2
3
4
5
6
7
./configure --with-cc-opt="-static -static-libgcc" --with-ld-opt="-Bstatic" --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed' \
    --add-module=./modules/naxsi/naxsi_src \
    --add-module=./modules/nginx-goodies-nginx-sticky-module-ng-bd312d586752 \
    --add-module=./modules/headers-more-nginx-module-0.25

4) make
after make we have out/nginx file so we could create own package using it

$ mkdir -p /opt/nginx/deb/nginx-ng
$ cd /opt/nginx/deb/nginx-ng
$ ls -1

1
2
3
4
DEBIAN
etc
usr
var

I downloaded original nginx deb and unpack it, after that I took DEBIAN, usr, var and etc folders from it

$ tree DEBIAN

1
2
3
4
5
6
7
8
9
DEBIAN/
├── changelog
├── conffiles
├── control
├── md5sums
├── postinst
├── postrm
├── preinst
└── prerm

changelog — describe changes of new versions
conffiles — list of conflict files which should be asked for replacing by installator
control — the main file with deb configuration
md5sum — sums of md5 in usr/* folder
postinst, posrtrm, preinst, prerm — script for install and remove before and after deb installation — I didn't modify them just took from original nginx package

Edit control file, set new Package name, depends, description, conflicts, version, etc:
$ cat control

1
2
3
4
5
6
7
8
9
10
11
12
Package: nginx-ng
Version: 1.8.x-1
Architecture: amd64
Maintainer: Vasya Pupkin <v.pupkin@gmail.com>
Depends: libc6 (>= 2.14), libpcre3 (>= 8.10), libssl1.0.0 (>= 1.0.1), zlib1g (>= 1:1.2.0), lsb-base, adduser, libxml2 (>= 2.7.8), libgcrypt11 (>= 1.5.0), libxslt1.1 (>= 1.1.26), libzip2 (>= 0.10), libcryptsetup4 (>= 2:1.4.1), libstdc++6 (>= 4.6.3), libgd2-xpm
Provides: httpd
Section: httpd
Priority: optional
Description: modified nginx package with several new modules
 nginx rebuilded from sources with enabled external modules: naxsi, nginx-sticky-module-ng
Conflicts: nginx-common, nginx-extras, nginx-full, nginx-light, nginx-naxsi
Replaces: nginx

$ cat conffiles

1
2
3
4
5
6
7
8
9
10
11
12
/etc/logrotate.d/nginx
/etc/nginx/sites-enabled/default
/etc/nginx/scgi_params
/etc/nginx/win-utf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/uwsgi_params
/etc/nginx/nginx.conf
/etc/default/nginx
/etc/init.d/nginx

cp etc, usr and var folder from original package to /opt/nginx/deb/nginx-ng folder, in my case they are:
$ tree etc/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
etc/
├── default
│   └── nginx
├── init.d
│   └── nginx
├── logrotate.d
│   └── nginx
└── nginx
    ├── conf.d
    ├── fastcgi_params
    ├── koi-utf
    ├── koi-win
    ├── mime.types
    ├── nginx.conf
    ├── scgi_params
    ├── sites-available
    │   └── default.conf
    ├── sites-enabled
    │   └── default -> ../sites-available/default.conf
    ├── uwsgi_params
    └── win-utf

$ tree var/

1
2
3
4
5
var/
├── cache
│   └── nginx
└── log
    └── nginx

$ tree usr/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
usr/
├── sbin
│   └── nginx
└── share
    ├── doc
    │   └── nginx
    │       ├── changelog.Debian.gz
    │       ├── CHANGES.ru.gz
    │       ├── copyright
    │       └── README
    └── nginx
        └── html
            ├── 50x.html
            └── index.html

usr/sbin/nginx — it's a binary file of nginx, we have to replace it with out compiled file
rsync -avP out/nginx /opt/nginx/deb/nginx-ng/usr/sbin/nginx

now we should write md5sums of our static data
cd /opt/nginx/deb/nginx-ng
md5deep -l -r usr > DEBIAN/md5sums

let's create deb package:
cd /opt/nginx/deb/nginx-ng
fakeroot dpkg-deb --build nginx-ng

you should see nginx-ng.deb file in your folder, take my congratulations!