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.
Steps
First we need to update and install all the necessary packages.
yum update
yum install -y createrepo rsync nginx
Now we need to setup our directories and permissions:
mkdir -p /var/www/html/repos/centos/7.2/os/x86_64/
mkdir -p /var/www/html/repos/centos/7.2/updates/x86_64/
chmod 770 -R /var/www
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/nginx.pid;
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
http://mirror.bytemark.co.uk/centos/7.2.1511/
createrepo /var/www/html/repos/centos/7.2/os/x86_64/
createrepo /var/www/html/repos/centos/7.2/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.
rsync://mirror.nus.edu.sg/centos
Take the given url, and add the following to the end of it:
/7/os/x86_64/
/7/updates/x86_64/
Now use those urls in the commands below
rsync -avz -avz –delete –exclude=’repo*’ \
rsync://mirror.bytemark.co.uk/centos/7.2.1511/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/
rsync -avz –delete –exclude=’repo*’ \
rsync://mirror.bytemark.co.uk/centos/7.2.1511/updates/x86_64/ \
/var/www/html/repos/centos/7.2/updates/x86_64/
Next we need to update the repo metadata by running:
createrepo –update /var/www/html/repos/centos/7.2/os/x86_64/
createrepo –update /var/www/html/repos/centos/7.2/updates/x86_64/
Configure Cron For Automatic Updating
Create a script with the following contents:
#!/bin/bash
rsync -avz -avz –delete –exclude=’repo*’ \
rsync://mirror.bytemark.co.uk/centos/7.2.1511/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/
rsync -avz –delete –exclude=’repo*’ \
rsync://mirror.bytemark.co.uk/centos/7.2.1511/updates/x86_64/ \
/var/www/html/repos/centos/7.2/updates/x86_64/
/usr/bin/createrepo –update \
/var/www/html/repos/centos/7/os/x86_64/
/usr/bin/createrepo –update \
/var/www/html/repos/centos/7/updates/x86_64/
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/script.sh
Configure Automatic Startup
Run the commands below to ensure nginx starts up on boot.
systemctl enable nginx.service && sudo systemctl enable firewalld.service
systemctl start firewalld.service
firewall-cmd –permanent –add-service=http
firewall-cmd –reload
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 http://centos-mirror.programster.org, so my base urls were:
[base]
baseurl=http://centos-mirror.rmohan.com/centos/$releasever/os/$basearch/
…
[updates]
baseurl=http://centos-mirror.rmohan.com/centos/$releasever/updates/$basearch/
…
Recent Comments