You are on page 1of 7

1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

Programster's Blog
Tutorials focusing on Linux, programming, and open-source

CentOS 7 - Create CentOS 7 Mirror


mirror CentOS CentOS-7

The tutorial below will show you how to con gure 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.

sudo yum update


sudo yum install -y createrepo rsync nginx
Copy to clipboard

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
Copy to clipboard

Now we are going to con gure 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" '

https://blog.programster.org/centos-7-create-centos-7-mirror 1/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

'$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;
}
Copy to clipboard

Create a le 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;
}

}
Copy to clipboard

Run the following command

createrepo /var/www/html/repos/centos/7/os/x86_64/
createrepo /var/www/html/repos/centos/7/updates/x86_64/
Copy to clipboard

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.bytemark.co.uk/centos
Copy to clipboard

https://blog.programster.org/centos-7-create-centos-7-mirror 2/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

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

/7/os/x86_64/
/7/updates/x86_64/
Copy to clipboard

Now use those urls in the commands below

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


rsync://mirror.bytemark.co.uk/centos/7/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/

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


rsync://mirror.bytemark.co.uk/centos/7/updates/x86_64/ \
/var/www/html/repos/centos/7/updates/x86_64/
Copy to clipboard

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/
Copy to clipboard

Configure Cron For Automatic Updating


Create a script with the following contents:

#!/bin/bash
/usr/bin/rsync -avz --delete --exclude='repo*' \
rsync://mirror.bytemark.co.uk/centos/7/os/x86_64/ \
/var/www/html/repos/centos/7/os/x86_64/

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


rsync://mirror.bytemark.co.uk/centos/7/updates/x86_64/ \
/var/www/html/repos/centos/7/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/
Copy to clipboard

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

https://blog.programster.org/centos-7-create-centos-7-mirror 3/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

@daily /bin/bash /path/to/script.sh


Copy to clipboard

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
Copy to clipboard

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


Copy to clipboard

Con gure Client To Use Own Mirror


There's no point having a mirror unless you con gure your other servers to use that mirror for
updates. Edit the le 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.programster.org/centos/$releasever/os/$basearch/
...
[updates]
baseurl=http://centos-mirror.programster.org/centos/$releasever/updates/$basearch/
...
Copy to clipboard

https://blog.programster.org/centos-7-create-centos-7-mirror 4/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

References
Deviantengineer.com - Install your own local Repo server for CentOS 7
Tecmint - How to Setup a Network Repository to Install or Update Packages – Part 11
StackOver ow - Nginx 403 forbidden for all les

4 Comments Programster's Blog 


1 Login

 Recommend 2 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name
https://blog.programster.org/centos-7-create-centos-7-mirror 5/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

Anssi Johansson • 2 months ago


Hi, I'm afraid your instructions are not correct. You must not use createrepo to create the repodata, but
instead you should use the repodata you get from the mirror you sync from. If you create the repodata
yourself, you will break the GPG signing of repomd.xml (see repomd.xml.asc). Please remove all
references to "createrepo" and "--exclude='repo*'". For those thinking of setting up a public CentOS
mirror based on this blog posting, you should sync everything with one rsync command, and not by
individual version or repository. In this case the -H (preserve hard links) rsync option would save you
some bandwidth and hard disk space. Best regards, avij on behalf of the CentOS Linux Project.
△ ▽ • Reply • Share ›

John Petrucci > Anssi Johansson • 6 days ago


I used `reposync` and no repomd.xml was copied down. Why would that be?
△ ▽ • Reply • Share ›

Anssi Johansson > John Petrucci • 5 days ago


That's because reposync does not write that file. Use rsync instead.
△ ▽ • Reply • Share ›

John Petrucci > Anssi Johansson • 2 days ago


Interesting. Is reposync just fundamentally flawed, or am I misunderstanding the
intended use? The thing I liked about reposync over rsync is that you do not
have to specify the source URL statically.
△ ▽ • Reply • Share ›

ALSO ON PROGRAMSTER'S BLOG

Ubuntu 18.04 - Install Unetbootin Ubuntu 16.04 - Install PostGIS | Programster's


3 comments • 7 months ago Blog
Aditya Naik — Phew. Thanks for the comment, 3 comments • 6 months ago
Artem. I actually thought it was something to do Young D. Kwon — Programster Helpful post! This
with my theming. I hope the issue gets resolved post helps me a lot!

PostgreSQL Parallel Queries Using the CLI to Set Custom Keyboard


2 comments • a year ago Shortcuts
Programster — Hey Eric,Apologies, but I haven't 1 comment • a year ago
provided information about how I created a table of Bobby — Thanks for the code snippet! Worked
random integers with 30,000,000 random … perfectly.

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy

We are a participant in the Amazon Services LLC Associates Program, an af liate advertising
program designed to provide a means for us to earn fees by linking to Amazon.com and
af liated sites. More info.
https://blog.programster.org/centos-7-create-centos-7-mirror 6/7
1/23/2019 CentOS 7 - Create CentOS 7 Mirror | Programster's Blog

Programster's Blog Ⓒ 2017

https://blog.programster.org/centos-7-create-centos-7-mirror 7/7

You might also like