How to serve a Debian package with the Apache web server

Aptly is a repository management tool in Debian. Aptly enables managing, mirroring, and merging repositories of packages. It also allows taking snapshots of repositories, publishing them, and merging them. We are going to look at a few important commands of Aptly. We can serve to publish Debian packages using Aptly and server technology. In this Answer, we will use the Apache server.

Steps

Here are the steps we can follow.

  1. First, generate a public key by executing the command given below in the terminal.

gpg --gen-key

It will ask you to provide a username, an email, and a password to construct a user ID. After the ID creation process, it will provide information for the account created. In this information, search for a line of the form: gpg: key 1487EA1C8459EB2B marked as ultimately trusted. In this line, 1487EA1C8459EB2B is the public key.

  1. Export this key to a file called pubkey.asc. Before executing this command, make sure to replace <public-key> with the key we obtained in the first step.

gpg --export --armor <public-key> > pubkey.asc
  1. Copy the pubkey.asc file to a directory /etc/apt/keyrings/. Create this directory if it is not already present.

cp pubkey.asc /etc/apt/keyrings/
  1. Now that we have our key set up, we are ready to publish our package. Build the package called hello-debian.

cd /hello-debian/ && dpkg-buildpackage -b -us -uc && cd ..
  1.  Now, create an aptly repo with the following command:

aptly repo create -distribution=bullseye \
-component=main \
-architectures=amd64,all \
my-repo
  1. Add our hello-debian package to the new repo my-repo.

aptly repo add my-repo hello-debian_0.1_amd64.deb
  1. Create a snapshot of our repo called my-repo-001.

aptly snapshot create my-repo-001 from repo my-repo
  1. Next, publish the snapshot.

aptly publish snapshot my-repo-001
  1. We must copy the published folders to the Apache default server to be accessible through the server.

cp -r /root/.aptly/public/* /var/www/html
  1. We must also remove the default Apache server .html file.

rm /var/www/html/index.html
  1. Lastly, start the Apache server.

service apache2 start

Running commands on the terminal

These instructions are demonstrated in the widget below. You will need to carry out instructions 1–2 yourself. After that, you might execute the script commands.sh that we have set up for the remaining instructions using ./usercode/commands.sh. Note that we have already set up the files for the hello-debian package. Use the terminal of the below widget to try out what we just learned. The terminal will appear after clicking run.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Publishing Debian packages using Aptly and apache server

The steps outlined above guided us through generating a public key, setting up the repository, adding packages, and publishing the repository using an Apache server.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved