Setting Up apt-cacher-ng on Debian/Ubuntu to Create a Local Mirror

This guide will walk you through the process of setting up apt-cacher-ng with Nginx as a reverse proxy to efficiently manage package downloads and updates on your Debian or Ubuntu systems. By implementing apt-cacher-ng, you can significantly reduce the disk space and bandwidth usually required to maintain a local mirror of your Linux packages.

Introduction to apt-cacher-ng

apt-cacher-ng is a caching proxy server for software packages, which stores local copies of any data passed through it from Debian and Ubuntu package repositories. This method ensures that only requested packages are downloaded and stored, reducing unnecessary data transfer and storage space.

Prerequisites

Before you begin, ensure you have the following:

Installation of apt-cacher-ng

Start by installing apt-cacher-ng on your server:

sudo apt update
sudo apt install apt-cacher-ng

Once installed, apt-cacher-ng will automatically start running. You can check the status with:

sudo systemctl status apt-cacher-ng

Configuring apt-cacher-ng

Edit the main configuration file to tailor the caching behavior:

sudo nano /etc/apt-cacher-ng/acng.conf

Add or modify the following lines to optimize performance and security:

CacheDir: /var/cache/apt-cacher-ng
LogDir: /var/log/apt-cacher-ng
Port:3142
BindAddress: 0.0.0.0
Remap-debrep: file:deb_mirror*.gz /debian ; file:backends_debian

Setting Up Nginx as a Reverse Proxy

To make apt-cacher-ng accessible via HTTP on port 80, install Nginx and configure it as a reverse proxy:

sudo apt install nginx
sudo nano /etc/nginx/sites-available/apt-cacher-ng

Add the following configuration to the file:

server {
    listen 80;
    server_name your-server-domain.com;

    location / {
        proxy_pass http://localhost:3142;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the site by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/apt-cacher-ng /etc/nginx/sites-enabled/

Test the Nginx configuration for errors and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

Configuring Client Machines

To direct package downloads through your caching server, configure the apt sources on each client machine:

echo 'Acquire::http { Proxy "http://your-server-domain.com:80"; };' | sudo tee /etc/apt/apt.conf.d/01proxy

This configuration tells the client systems to fetch packages through your apt-cacher-ng setup.

Monitoring and Maintenance

Monitor the performance and cache usage of apt-cacher-ng through its web interface accessible at:

http://your-server-domain.com/acng-report.html

Learn More

To further enhance your server management and patching strategies, consider visiting:

Visit LinuxPatch

For further details, refer to the official apt-cacher-ng documentation.