Portage Distfiles Mirror

Setting up local distfiles mirror

This tutorial explains how to save bandwidth when serveral local computers need to pull updates from a single remote distfiles repository.

Use case

This tutorial will be about hosting a local mirror of funtoo/gentoo based portage distfiles with an apache based caching reverse proxy and multiple redundant upstream mirrors.

Following terms should be adapted

Terms Definition
mirror.example.com The local distfiles mirror host
localhost Any local host
/var/cache/apache2/dist-files Base path where to store the cached files
http://ftp.is.co.za/mirror/ftp.gentoo.org/ SA sample mirror, replace with one for your location
http://gentoo.mirror.ac.za/ SA sample mirror, replace with one for your location

Local mirror

Install apache with the required modules enabled

# echo "www-servers/apache apache2_modules_cache apache2_modules_cache_disk apache2_modules_deflate apache2_modules_lbmethod_byrequests apache2_modules_proxy apache2_modules_proxy_balancer apache2_modules_proxy_ftp apache2_modules_proxy_http apache2_modules_slotmem_shm" >> /etc/portage/package.use
# emerge -av www-servers/apache:2

Prepare directories for the cache storrage

# mkdir /var/cache/apache2/dist-files
# chown apache:apache /var/cache/apache2/dist-files

Apache daemon configuration

Add -C CACHE -D PROXY to APACHE2_OPTS for example


Set the VirtualHost section of the default vhost config to:

<VirtualHost *:80>
	ServerName mirror.example.com
	Include /etc/apache2/vhosts.d/default_vhost.include

	CacheEnable disk /
	CacheRoot /var/cache/apache2/dist-files
	CacheDefaultExpire 63072000
	CacheMinExpire 63072000
	CacheMinFileSize 1
	CacheMaxFileSize 10000000000

	ProxyRequests Off

	#LogLevel debug

	<Proxy *>
		Require all granted

	ProxyPass /balancer-manager !
	ProxyPass /gentoo-distfiles balancer://mycluster/
	ProxyPassReverse /gentoo-distfiles http://ftp.is.co.za/mirror/ftp.gentoo.org/
	ProxyPassReverse /gentoo-distfiles http://gentoo.mirror.ac.za/
	<Proxy balancer://mycluster>
		BalancerMember http://ftp.is.co.za/mirror/ftp.gentoo.org/ route=node1
		BalancerMember http://gentoo.mirror.ac.za/ route=node2

	<Location /balancer-manager>
		SetHandler balancer-manager
	<IfModule mpm_peruser_module>
		ServerEnvironment apache apache

This is for 2 South Africa local mirrors, you will need to pic the best for your location and replace the ProxyPassReverse and BalancerMember with a unique route=node<x> for each mirror chosen.

Service configuration

To start daemon at boot add apache2 to default runlevel

# rc-update add apache2 default

To make changes start immediately just run rc

# rc

Prevent the cache from becoming to big

Add the following to /etc/cron.daily/clean-cache.sh and set the required size, the sample 50G is for 50Gb

htcacheclean -ntp /var/cache/apache2/dist-files/ -l 50G

make the script executable with:

# chmod +x /etc/cron.daily/clean-cache.sh

Downstream Clients Settings

Set the GENTOO_MIRRORS variable for portage to the new mirror:



Uncomment the #LogLevel debug in /etc/apache2/vhosts.d/00_default_vhost.conf and reload apache with:

# /etc/init.d/apache2 reload

Follow /var/log/apache2/error.log for detail:

# tail -f /var/log/apache2/error_log