Using Debian package archive as a configuration tool

Introduction

How often do you need to install your favorite Linux distribution in a single year either virtually or using real hardware? How frequently it happens that you just want to test new release of certain Linux distribution so you install it on different partition or simply virtually using your current system as a host. How often do you need to deploy a server which is a complete clone of the one you configured yesterday. The installation part of any Linux system today is a very straightforward process. Nonetheless, the hardest and the most tedious part comes with a fine tuning, customization and configuration of your system’s services as well as your own user environment. You can simply copy your custom system configuration files from one system to another but engaging in this concept this task can become quite disorganized, time consuming and most importantly error-prone.

In this article, we are going to take a different approach which involves a creation of a Debian package archive containing all required custom user and configuration files. First part of this article describes a rather simple way on how to create a Debian package archive containing all custom files followed by its installation. In the second part, we will look at the way on how to create our own very basic Debian Repository and use it to deploy a simple website including Apache webserver installation and configuration on a freshly installed Linux system.

Part1

In this section, we create and install simple Debian package. The package will accommodate some sample user data to serve as an example.

Creating a Debian Package

It took you a while to configure your desktop to have it the way it best suits your needs and convenience. In your custom environment, you may for example include some bash scripts, create several aliases using .bashrc file or changed default behavior of a vim text editor by altering .vimrc file. Furthermore, you may also have customized numerous system configuration files such as /etc/network/interfaces and so on. All this hard work can be saved within a Debian package and installed and removed from any system with a single dpkg command. As a first step we need to create a bare minimum skeleton for a Debian package. This is a fairly simple task as it only involves a single DEBIAN/control file. So let us start by creating a directory named “myenv”. This directory will hold all data for our own version 1.0 Debian package.

$ mkdir myenv

In the next step, we need to create a control file:

$ cd myenv
$ mkdir DEBIAN
$ vi DEBIAN/control

Use vi or any other text editor and place a following package control information into DEBIAN/control file:

Package: myenv
Version: 1.0
Section: custom
Priority: optional
Architecture: all
Essential: no
Installed-Size: 1024
Maintainer: linuxconfig.org
Description: My Linux settings and environment

All control file options are at this point rather self-explanatory. Our next task is to include all files we wish to store within the package. All files must be stored within a myenv directory and must contain a full path. This means that if we wish to include /etc/network/interfaces file, we need to:

$ pwd
/home/lilo/myenv
$ mkdir -p etc/network
$ cp /etc/network/interfaces etc/network

To include our own scripts / files, we need to follow the same full path rule. This is how we can add our custom .bashrc file if a full path to our home directory is /home/lilo/:

$ pwd
/home/lilo/myenv
$ mkdir -p home/lilo
$ cp /home/lilo/.bashrc home/lilo/

If we would wish to include whole /home/lilo directory we could:

$ pwd
/home/lilo/myenv
$ mkdir home
$ cp -vr /home/lilo/ home/

This way we can include as many directories or files as we see fit. Once we have completed this preparation part, we can build a debian package. Navigate to a myenv’s parent directory:

$ cd ../

and execute a following linux command to create a Debian package:

$ dpkg-deb --build myenv
dpkg-deb: building package `myenv' in `myenv.deb'.

If all went well you should have a debian package called myenv.deb sitting in your current working directory.

Debian package installation

The installation of our new Debian package is rather simple. Became root or use sudo to execute a following linux command:

# dpkg -i myenv.deb

All files withing a Debian package had been copied to relevant locations. All done. If you wish to remove all files from you system use -P for purge:

# dpkg -P myenv

Part 2

In this section, we will create a custom Debian package to be used to install and configure Apache server. We will use the same technique as described in Part 1. However, this time our package will fetch all prerequisites and the package its self will be installed from a custom made Debian repository.

Building a Debian package

Next, we will create a package called mywebsite.deb. This package will contain a simple “my website” index.html file. The steps are exactly the same as described previously with one addition that our DEBIAN/control file will now contain an extra row “Depends:” to instruct the system to fetch all prerequisites upon the package installation. Which in this case is apache2. If you wish to include more dependencies make sure that you separate them with “,”. Let’s create a base directory called “mywebsite”:

$ cd
$ mkdir mywebsite

Now create a control file within a DEBIAN directory:

$ cd mywebsite
$ mkdir DEBIAN
$ vi DEBIAN/control

and include a following information within a control file:

Package: mywebsite
Version: 1.0
Section: custom
Priority: optional
Architecture: all
Essential: no
Depends: apache2
Installed-Size: 1024
Maintainer: linuxconfig.org
Description: My example website

Note that the content of a control file is same is it was in the previous example with a small change, and that is a new row “Depends: apache2” had been included, which means that upon the package installation an Apache webserver will also be installed as part of package settings. Next, we insert an index.html file:

$ pwd
/home/lilo/mywebsite
$ mkdir -p var/www/mywebsite
$ echo "my website" > var/www/mywebsite/index.html

Furthermore, we can include a custom apache config file where a root directory of our website will be /var/www.mywebsite . We could use the default configuration file, but we include it anyway just to demonstrate this possibility.

$ mkdir -p /etc/apache2/sites-available/
$ vi etc/apache2/sites-available/mywebsite

mywebsite file can contain a following settings:

<VirtualHost *:80>

        DocumentRoot /var/www/mywebsite
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

</VirtualHost>

Now we can build a package:

$ cd ..
$ dpkg-deb --build mywebsite
dpkg-deb: building package `mywebsite' in `mywebsite.deb'.

Setting up a simple Debian package repository

The requirement for a Debian repository is a web server. So here we assume that we already have some form of a web server running on our system either it is a local or remote configuration. If your web server’s root directory is /var/www then create a new packages directory with a following linux command:

# mkdir /var/www/packages

Copy your brand new package into this new directory and create index:

NOTE: you need to place your new package into /var/www/package before executing a following linux commands.

# cd /var/www
# dpkg-scanpackages packages /dev/null | gzip -9c > packages/Packages.gz
dpkg-scanpackages: warning: Packages in archive but missing from override file:
dpkg-scanpackages: warning:   mywebsite
dpkg-scanpackages: info: Wrote 1 entries to output Packages file

All is done. We now have our own local Debian repository up and running.

Package installation

If all went well we should be able to install mywebsite package with simple apt-get install command. However, first we need to include our Debian repository in /etc/apt/sources.list file of our target system ( local or remote ). If the IP address of our Debian repository is 10.1.1.60 then a following linux command will do the job:

# echo "deb http://10.1.1.60 packages/" >> /etc/apt/sources.list
# apt-get update

Now we are ready to install mywebsite package:

# apt-get install mywebsite

The above command will install apache2 web server, create a mywebsite configuration file within /etc/apatache2/sites-available and deploy our simple index.html website into /vaw/www/mywebsite. As we can see all configurations had been done automatically. All we need at this point is to simply navigate a web browser to: http://localhost/mywebsite:

Conclusion

This article lists just simple examples of what can be achieved with deb package archives. It clearly demonstrates a flexibility of this method to install, configure or deploy virtually anything. I have seen multiple websites with a configuration of various perfect a desktop systems. Perhaps the whole article can be replaced by couple lines on how to include a deb repository into /etc/apt/sources.list and how to execute an apt-get install command. I personaly use this approach for creation of Live USB key where instead of spelling out number of directories and files I wish to include in my Live USB key I simply point live helper to a single custom made Debian package. It is clear that possibilities are endless and this is the beauty of Linux OS.