Create a local repository for Ubuntu 16.04LTS using apt-mirror and apache2.

To make a local repository you will need the following requirements:

  • apt-mirror package (sudo apt install apt-mirror)
  • apache2 package (sudo apt install apache2)
  • about 200GB of storage per release, per architecture

Once these requirements are met you can begin configuring the apt-mirror tool. The main items that you’ll need to define are:

  • storage location (base_path)
  • number of download threads (nthreads)
  • release(s) and architecture(s) you want

The configuration is in the /etc/apt/mirror.list file. A default config was put in place when you installed the package, but it is generic. You’ll need to update the values mentioned above.

Below I have included a complete configuration which will mirror Ubuntu 16.04 LTS for both 32 and 64 bit installations. It will require nearly 250GB of storage space, which I will be putting on a removable drive. The drive will be mounted at /media/STORAGE/.

# apt-mirror configuration file
##
## The default configuration options (uncomment and change to override)
##
#

set base_path /media/STORAGE/
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
#
# set defaultarch <running host architecture>
set nthreads 20
#
# 16.04 "xenial" i386 mirror
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial main restricted universe 
multiverse
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial-updates main restricted 
universe multiverse
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial-security main restricted 
universe multiverse
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial-backports main restricted 
universe multiverse
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial-proposed main restricted 
universe multiverse
deb-i386 http://vn.archive.ubuntu.com/ubuntu xenial main/debian-installer 
restricted/debian-installer universe/debian-installer multiverse/debian-installer
deb-i386 http://packages.medibuntu.org/ xenial free non-free

# 16.04 "xenial" amd64 mirror

deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial main restricted universe 
multiverse
deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial-updates main restricted 
universe multiverse
deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial-security main restricted 
universe multiverse
deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial-backports main restricted 
universe multiverse
deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial-proposed main restricted 
universe multiverse
deb-amd64 http://vn.archive.ubuntu.com/ubuntu xenial main/debian-installer 
restricted/debian-installer universe/debian-installer multiverse/debian-installer
deb-amd64 http://packages.medibuntu.org/ xenial free non-free

# Cleaning section
clean http://vn.archive.ubuntu.com/
clean http://packages.medibuntu.org/

Once your configuration is saved you can begin to populate your mirror by running the command:

apt-mirror

In order for this repository to be available to other clients on your local area network you’ll need to share the contents over http. This is why we listed Apache2 as a requirement above. In my example configuration above I mirrored the repository on a removable drive, and mounted it at /media/STORAGE. What I need to do now is make that address available over the web. This can be done by way of a symbolic link.

cd /var/www/
sudo ln -s /media/STORAGE/mirror/vn.archive.ubuntu.com/ubuntu/ ubuntu

The commands above will tell the filesystem to follow any requests for “ubuntu” back up to the mounted external drive, where it will find your mirrored contents. If you have any problems with this linking double-check your paths (as compared to those suggested here) and make sure your link points to the ubuntu directory, which is a subdirectory of the mirror you pulled from. If you point anywhere below this point your clients will not properly be able to find the contents.

If you get the “403 Forbidden” error then make sure the user and group of apache2 process have at least read and execute permission for every folder in the full-path to your local repository, for this article, the full-path to your local repository is:

/media/STORAGE/mirror/vn.archive.ubuntu.com/ubuntu/

shell commands that helps you check user and group of apache2 process are:

ps -ef | grep apache2#orps -aux | grep apache2

The default user and group of apache2 process are: “www-data” and “www-data”. If the “403 Forbidden” error happens, you usually can solve this error by “chmod” command. For example:

sudo chmod -R o+rx /media

The other cause for “403 Forbidden” error is wrong configurations for apache2. In Ubuntu 16.04LTS the apache2 version is usually 2.4.18. Version of apache2 can be checked with command:

apache2 -v

make sure that /etc/apache2/apache2.conf or /etc/apache2/sites-available/000-default.conf has the following line:

DocumentRoot /var/www/html
<Directory />
        Options FollowSymLinks Indexes
        AllowOverride None
        Require all granted
</Directory>

The additional task of keeping this newly downloaded mirror updated with the latest updates can be automated by way of a cron job. By activating the cron job your machine will automatically run the apt-mirror command on a regular daily basis, keeping your mirror up to date without any additional effort on your part.

To activate the automated cron job, edit the file /etc/cron.d/apt-mirror. There will be a sample entry in the file. Simply uncomment the line and (optionally) change the “4” to an hour more convenient for you. If left with its defaults it will run the apt-mirror command, updating and synchronizing your mirror, every morning at 4:00am.

The final step needed, now that you have your repository created, shared over http and configured to automatically update, is to configure your clients to use it. Ubuntu clients define where they should grab errata and security updates in the /etc/apt/sources.list file. This will usually point to the default or regional mirror. To update your clients to use your local mirror instead you’ll need to edit the /etc/apt/sources.list and comment the existing entries (you may want to revert to them later!)

Once you’ve commented the entries you can create new entries, this time pointing to your mirror. A sample entry pointing to a local mirror might look something like this:

deb http://192.168.0.10/ubuntu xenial main restricted universe multiverse
deb http://192.168.0.10/ubuntu xenial-updates main restricted universe multiverse
deb http://192.168.0.10/ubuntu xenial-security main restricted universe multiverse

Basically what you are doing is recreating your existing entries but replacing the archive.ubuntu.com with the IP of your local mirror. As long as the mirrored contents are made available over http on the mirror-server itself you should have no problems. If you do run into problems check your apache logs for details.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s