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 the config below in your /etc/apt/mirror.list
file:
############# config ##################
#
# set base_path /var/spool/apt-mirror
#
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 10
set _tilde 0
#
############# end config ##############
# Main
deb-aarch64 https://packages.termux.org/apt/termux-main stable main
deb-arm https://packages.termux.org/apt/termux-main stable main
deb-i686 https://packages.termux.org/apt/termux-main stable main
deb-x86_64 https://packages.termux.org/apt/termux-main stable main
clean https://packages.termux.org/apt/termux-main
# Game packages
deb-aarch64 https://packages.termux.org/apt/termux-games games stable
deb-arm https://packages.termux.org/apt/termux-games games stable
deb-i686 https://packages.termux.org/apt/termux-games games stable
deb-x86_64 https://packages.termux.org/apt/termux-games games stable
clean https://packages.termux.org/apt/termux-games
# Root packages
deb-aarch64 https://packages.termux.org/apt/termux-root root stable
deb-arm https://packages.termux.org/apt/termux-root root stable
deb-i686 https://packages.termux.org/apt/termux-root root stable
deb-x86_64 https://packages.termux.org/apt/termux-root root stable
clean https://packages.termux.org/apt/termux-root
# Science packages
deb-aarch64 https://packages.termux.org/apt/termux-science science stable
deb-arm https://packages.termux.org/apt/termux-science science stable
deb-i686 https://packages.termux.org/apt/termux-science science stable
deb-x86_64 https://packages.termux.org/apt/termux-science science stable
clean https://packages.termux.org/apt/termux-science
# Unstable packages
deb-aarch64 https://packages.termux.org/apt/termux-unstable unstable main
deb-arm https://packages.termux.org/apt/termux-unstable unstable main
deb-i686 https://packages.termux.org/apt/termux-unstable unstable main
deb-x86_64 https://packages.termux.org/apt/termux-unstable unstable main
clean https://packages.termux.org/apt/termux-unstable
# X11 packages
deb-aarch64 https://packages.termux.org/apt/termux-x11 x11 main
deb-arm https://packages.termux.org/apt/termux-x11 x11 main
deb-i686 https://packages.termux.org/apt/termux-x11 x11 main
deb-x86_64 https://packages.termux.org/apt/termux-x11 x11 main
clean https://packages.termux.org/apt/termux-x11
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.
Update (25 June 2021)
This mirror has been added to the termux-tools
package. On the next update, you should be able to use it if you wish by running termux-change-repo
.
Comments
One response to “A New Termux Mirror”
[…] 很多人白嫖甲骨文云(确实有非常丰厚的免费额度),我之前架设的Termux的APT镜像也是用的甲骨文的免费资源。但是最近发现免费账户基本allocate不到资源了,所以填了个信用卡升级到了Pay As You Go账户级别,突然就能allocate到VM了。虽然是付费账户,但只要还是在免费额度以内,并不会产生费用所以其实无所谓。 […]