Programster's Blog

Tutorials focusing on Linux, programming, and open-source

CentOS 7 - Create CentOS 7 Mirror

The tutorial below will show you how to configure a CentOS 7 server with Nginx to act as a mirror for other CentOS 7 servers.


First we need to update and install all the necessary packages.

sudo yum update
sudo yum install -y createrepo rsync nginx

Now we need to setup our directories and permissions:

sudo mkdir -p /var/www/html/repos/centos/7/os/x86_64/
sudo mkdir -p /var/www/html/repos/centos/7/updates/x86_64/
sudo chmod 770 -R /var/www
sudo chown $USER:nginx -R /var/www

Now we are going to configure Nginx to use the location we just created. Replace the contents of /etc/nginx/nginx.conf with:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/;

events {
    worker_connections 1024;

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

Create a file at /etc/nginx/conf.d/repo.conf with the following contents

server {
    listen  80;
    server_name  localhost;
    root  /var/www/html/repos;

    location  / {
        autoindex  on;


Run the following command

createrepo /var/www/html/repos/centos/7/os/x86_64/
createrepo /var/www/html/repos/centos/7/updates/x86_64/

Now we need to fetch the data for the mirror. Go to the CentOS mirrors list and pick the mirror closest to you that has an rsync address (6th column in the table). For me, I am going to use Bytemark since I am in the United Kingdom.


Take the given url, and add the following to the end of it:


Now use those urls in the commands below

rsync -avz -avz --delete --exclude='repo*' \
rsync:// \

rsync -avz --delete --exclude='repo*' \
rsync:// \

Next we need to update the repo metadata by running:

createrepo --update /var/www/html/repos/centos/7/os/x86_64/
createrepo --update /var/www/html/repos/centos/7/updates/x86_64/

Configure Cron For Automatic Updating

Create a script with the following contents:

/usr/bin/rsync -avz --delete --exclude='repo*' \
rsync:// \

/usr/bin/rsync -avz --delete --exclude='repo*' \
rsync:// \

/usr/bin/createrepo --update \

/usr/bin/createrepo --update \

Now configure the cron service to call that script from at midnight every day by executing crontab -e and adding the following line:

@daily /bin/bash /path/to/

Configure Automatic Startup

Run the commands below to ensure nginx starts up on boot.

sudo systemctl enable nginx.service && sudo systemctl enable firewalld.service
sudo systemctl start firewalld.service
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
sudo systemctl reboot

Configure SELinux

If you are running selinux and you don't want to disable it, then run the following command to have selinux allow nginx to serve content from /var/www

chcon -Rt httpd_sys_content_t /var/www

Configure Client To Use Own Mirror

There's no point having a mirror unless you configure your other servers to use that mirror for updates. Edit the file at /etc/yum.repos.d/CentOS-Base.repo and comment out any lines starting with mirrorlist or baseurl underneath [base] or [updates]. Then add your own baseurl to these sections, with your own mirrors url. For example, my mirror is located internally at, so my base urls were:



Last updated: 16th August 2018
First published: 16th August 2018