How to Set Up a FreeBSD Mirror Server on FreeBSD Operating System

How to set up a FreeBSD mirror server on FreeBSD operating system.

A FreeBSD mirror server helps distribute FreeBSD’s software, updates, and installation files efficiently. This guide covers setting up a FreeBSD mirror server on a FreeBSD operating system, ensuring that it syncs properly with upstream servers and serves files efficiently to clients.

Prerequisites

Before setting up your FreeBSD mirror server, ensure you have the following:

  • A FreeBSD system (preferably a dedicated server or a virtual machine with ample storage and bandwidth).
  • Root or sudo access.
  • A stable internet connection with sufficient bandwidth.
  • A minimum of 500GB storage (more may be required for a full mirror).

Step 1: Install Required Packages

First, update your FreeBSD system and install rsync, which is essential for synchronizing with upstream FreeBSD mirrors:

sudo pkg update && sudo pkg upgrade -y
sudo pkg install rsync nginx

Alternatively, you can use ports to install rsync:

cd /usr/ports/net/rsync && make install clean

Step 2: Choose an Upstream Mirror

To ensure your mirror stays updated, choose a reliable upstream FreeBSD mirror from the official list:

https://www.freebsd.org/doc/en_US.ISO8859-1/articles/hubs/mirror-howto.html

Pick a geographically close mirror to optimize download speeds.

Step 3: Set Up Rsync for Synchronization

Create a directory to store FreeBSD files:

sudo mkdir -p /usr/local/mirror/FreeBSD
sudo chown -R root:wheel /usr/local/mirror/FreeBSD

Now, set up a cron job to synchronize your mirror with an upstream FreeBSD mirror. Open your crontab file:

sudo crontab -e

Add the following line to sync the mirror daily:

0 3 * * * rsync -avz --delete rsync://ftp.freebsd.org/pub/FreeBSD/ /usr/local/mirror/FreeBSD/

This ensures that your mirror remains up to date.

Step 4: Set Up an HTTP Server (Nginx)

To allow users to access your FreeBSD mirror via HTTP, set up an Nginx web server. First, create an Nginx configuration file:

sudo nano /usr/local/etc/nginx/nginx.conf

Modify it as follows:

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name your-mirror.example.com;

        location / {
            root   /usr/local/mirror/FreeBSD;
            autoindex on;
        }
    }
}

Save and exit the file, then restart Nginx:

sudo service nginx restart

Your FreeBSD mirror should now be accessible via HTTP.

Step 5: Configure FTP Access (Optional)

If you want to provide FTP access to your mirror, install and configure vsftpd:

sudo pkg install vsftpd

Edit the vsftpd configuration file:

sudo nano /usr/local/etc/vsftpd.conf

Add or modify the following lines:

anonymous_enable=YES
local_enable=NO
write_enable=NO
anon_root=/usr/local/mirror/FreeBSD
listen=YES
listen_ipv6=NO

Start vsftpd:

sudo service vsftpd start

Step 6: Test the Mirror

To verify that the FreeBSD mirror is working, open a web browser and enter:

http://your-mirror.example.com/

You should see the FreeBSD files listed. You can also test the FTP service with:

ftp your-mirror.example.com

If the files are accessible, your mirror is working correctly.

Step 7: Automate and Monitor Syncing

To monitor synchronization, check the logs:

cat /var/log/cron | grep rsync

For troubleshooting, check the Nginx logs:

cat /var/log/nginx/access.log
cat /var/log/nginx/error.log

If your server is public, consider registering it with FreeBSD to help distribute traffic efficiently.

Conclusion

Setting up a FreeBSD mirror server ensures that users in your region have a reliable source for FreeBSD downloads. By using rsync, nginx, and vsftpd, you can maintain an up-to-date, efficient, and accessible FreeBSD mirror. Regularly monitor your logs and adjust your synchronization schedule as needed to keep your mirror optimized.