A New Termux Mirror

TL; DR. https://termux.librehat.com is a new Termux packages mirror! Maintained by me, synchronised every six hours, located in the United Kingdom, hosted by Oracle Cloud.

In the full article below, I’ll write up how to set up a Termux mirror (or in general, a Debian packages repository mirror).

First of all, you need either a dedicated server, a virtual machine, or a VPS. A public mirror could use a lot of bandwidth, I won’t recommend using the same host where your blog is hosted for example.

To host a Debian packages repository mirror, it’d work out the best if you use a Debian-based Linux distribution, e.g. Debian, or Ubuntu. In Oracle Cloud, Ubuntu 20.04 LTS is the only choice in the Platform Images category, so I’ve chosen Ubuntu here.

It is possible to use other Linux distributions such as CentOS, RHEL or so on. However, they don’t have apt-mirror package available and you’ll likely end up with chrooted Debian.

Set up apt-mirror

I’m using apt-mirror here. There are other tools and they all depend on what works and what not for a specific distribution. Termux doesn’t support rsync for example, so any solutions based on rsync wouldn’t be viable.

My configuration for apt-mirror is pasted below. You should have something similar to this sample config in /etc/apt/mirror.list file.

Now, let’s try to manually initialise the mirror as the apt-mirror user and verify whether the apt-mirror configuration is correct.

sudo su apt-mirror -c "apt-mirror"

This can take a long time (depending on the network and disk I/O speed etc.). Once it is finished, you should see the mirrored repositories in /var/spool/apt-mirror/mirror/grimler.se/. If something went wrong, you can find logs in /var/spool/apt-mirror/var/.

Set up HTTP Server

Choose your favourite web server. In my setup, I’ve chosen nginx which is a super fast and lightweight web server. Either the autoindex or the fancyindex module will work.

Alternatively, use Apache and enable the directory listing.

The directory where the repositories are stored by apt-mirror is /var/spool/apt-mirror/mirror/grimler.se/ (the last directory would be different if the upstream is configured to be a different site).

Once this is all done, make sure you’ve opened the HTTP port (80) in the firewall settings (both in the VM and in the infrastructure provider’s control panel).

Now, if everything is set correctly, you can type the public IP address (or the domain if you had one) and check whether you can see the mirrored packages being listed.

You may also want to set up an SSL certificate so this mirror is accessible via HTTPS protocol, for which please check out Let’s Encrypt.

Set up Automatical Synchronisation

Many articles online talk about how to use cron which is the old and trusted management tool for cron jobs. Here, we’ll use systemd to do that (it requires a reasonably recent Linux distribution that is shipped with systemd).

First, let’s create /etc/systemd/system/apt-mirror.service:

[Unit]
Description=Mirror APT repositories
Wants=apt-mirror.timer

[Service]
Type=oneshot
User=apt-mirror
Group=apt-mirror
ExecStart=/usr/bin/apt-mirror

[Install]
WantedBy=multi-user.target

This is the actual systemd service unit. It should look familiar if you’ve seen any systemd services configurations. Next, we need to create the timer unit which will trigger this service repeatedly. Create /etc/systemd/system/apt-mirror.timer:

[Unit]
Description=Periodically run apt-mirror
Requires=apt-mirror.service

[Timer]
Unit=apt-mirror.service
OnUnitInactiveSec=21600

[Install]
WantedBy=timers.target

Here the timer trigger I’ve set is to run the service in 6 hours after the service unit becomes inactive (so it’ll definitely be less frequent than every 6 hours because apt-mirror takes more than 0 seconds to run).

Enable the service and the timer so they’re active when the system boots. To kick start it now, manually start the timer after enabling. Run the commands below in the terminal.

sudo systemctl enable apt-mirror.service
sudo systemctl enable apt-mirror.timer
sudo systemctl start apt-mirror.timer

To check when is the next trigger, simply do

sudo systemctl status apt-mirror.timer

Now you’re all set. The mirror is online and it’ll be synchronised about every six hours.

Author: librehat

自由軟體萬歲!

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.