Host your Own Yum Repository

Here we create our own yum repository on a fresh up-to-date installation of Centos 7.

Configure Nginx

First install the EPEL repository.

[root@centos7 ~]# yum install epel-release

And then Nginx.

[root@centos7 ~]# yum install nginx

Start and enable Nginx.

[root@centos7 ~]# systemctl start nginx
[root@centos7 ~]# systemctl enable nginx

Install and configure a local firewall.

[root@centos7 ~]# yum install firewalld
[root@centos7 ~]# systemctl enable firewalld
[root@centos7 ~]# systemctl start firewalld
[root@centos7 ~]# firewall-cmd --zone=public --permanent --add-service={http,https}
[root@centos7 ~]# firewall-cmd --reload
[root@centos7 ~]# reboot

Once back online and from another machine, check the URL or IP is accessible. You can use a browser or curl as shown below.

[apike@workpc ~]$ curl -s | grep Welcome
<h1>Welcome to <strong>nginx</strong> on Fedora!</h1>

Create a virtual host entry for Nginx.

[root@centos7 ~]# vim /etc/nginx/conf.d/


server {
        listen   80;
        root   /var/www/vhosts/;
        location / {
                index  index.php index.html index.htm;
                autoindex on;	#enable listing of directory index

Create the directory structure:

[root@centos7 ~]# mkdir -pv /var/www/vhosts/

Check for configuration errors and restart Nginx.

[root@centos7 ~]# nginx -t
[root@centos7 ~]# systemctl restart nginx

Local Yum Repository

[root@repo ~]# yum install createrepo yum-utils -y

Create directories structure.

[root@centos7 ~]# mkdir -pv /var/www/vhosts/{base,centosplus,extras,updates}

Now use reposync to synchronise the CentOS yum repository.

[root@centos7 ~]# reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/www/vhosts/
[root@centos7 ~]# reposync -g -l -d -m --repoid=centosplus --newest-only --download-metadata --download_path=/var/www/vhosts/
[root@centos7 ~]# reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/var/www/vhosts/
[root@centos7 ~]# reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/var/www/vhosts/

Now create new repository data:

[root@centos7 ~]# createrepo -g comps.xml /var/www/vhosts/
[root@centos7 ~]# touch /var/www/vhosts/
[root@centos7 ~]# createrepo -g comps.xml /var/www/vhosts/
[root@centos7 ~]# touch /var/www/vhosts/
[root@centos7 ~]# createrepo -g comps.xml /var/www/vhosts/
[root@centos7 ~]# touch /var/www/vhosts/
[root@centos7 ~]# createrepo -g comps.xml /var/www/vhosts/

Check you can browse to your new repository.

Cron Job to Synchronise Repository

To keep our repository up-to-date, we need to create a cronjob.

[root@centos7 ~]# vim update-local-mirror



# specify all local repositories in a single variable
LOCAL_REPOS='base centosplus extras updates'

# a loop to update repos one at a time 

for REPO in ${LOCAL_REPOS}; do
  reposync -g -l -d -m --repoid=$REPO --newest-only --download-metadata --download_path=/var/www/vhosts/

if [ $REPO = 'base' ]
    createrepo -g comps.xml /var/www/vhosts/$REPO/  
    createrepo /var/www/vhosts/$REPO/  

Make it executable and move the file to /etc/cron.daily/:

[root@centos7 ~]# chmod -v 755 update-local-mirror
[root@centos7 ~]# mv update-local-mirror /etc/cron.daily/

You should now have a working yum mirror site. I’ll create another post on how to configure another CentOS/RHEL device as a client.

Be the first to comment

Leave a Reply