We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How To Create CentOS 8 Local Repository Mirrors With Rsync & Nginx

Learn how to – How To Create CentOS 8 Local Repository Mirrors With Rsync & Nginx.

In this guide we’ll explore how you can create Local repositories of CentOS 8 by using Rsync and hosting them with Nginx web server. The Local CentOS 8 mirrors are created by having a Local cron job which regularly checks for updates in the upstream repositories and update them locally accordingly.

By Creating CentOS 8 Local repository mirrors, you’ll be able to configure your CentOS 8 servers to access packages without the need for internet access. This will improve your security and reduces the amount of bandwidth consumed by pulling RPM packages from external sources.

In this setup, we’ll use a CentOS 7 or CentOS 8 server to perform the actions. You should have an external storage with enough capacity to host repository packages and potential growth.

For CentOS 7 & CentOS 6, check: How To Create Local CentOS 7 6 & EPEL Repository Sync Mirrors

Step 1: Install Nginx Web Server

As Nginx is used as a web server of choice, install it in your CentOS 7 system.

CentOS 8

sudo dnf -y install @nginx

CentOS 7:

sudo yum -y install epel-release
sudo yum -y install nginx

After installation, start and enable the service.

sudo systemctl enable --now nginx

Confirm that the service is running.

$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-11-01 00:28:39 EAT; 4s ago
  Process: 7378 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 7376 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 7375 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 7380 (nginx)
    Tasks: 2 (limit: 11512)
   Memory: 9.4M
   CGroup: /system.slice/nginx.service
           ├─7380 nginx: master process /usr/sbin/nginx
           └─7381 nginx: worker process

Nov 01 00:28:39 centos8.novalocal systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 01 00:28:39 centos8.novalocal nginx[7376]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 01 00:28:39 centos8.novalocal nginx[7376]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 01 00:28:39 centos8.novalocal systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Nov 01 00:28:39 centos8.novalocal systemd[1]: Started The nginx HTTP and reverse proxy server.

Open http port in the firewall.

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

Step 2: Create Repository directories

I have a secondary disk in my server that will be used to store repository data – /dev/vdc of 100GB.

$ lsblk 
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda         252:0    0  20G  0 disk 
├─vda1      252:1    0   1G  0 part /boot
└─vda2      252:2    0   9G  0 part 
  ├─cl-root 253:0    0   8G  0 lvm  /
  └─cl-swap 253:1    0   1G  0 lvm  [SWAP]
vdc         252:32   0  100G 0 disk 

I’ll create a partition on this block device and mount it under the /data directory. Let’s start by creating a partition on it.

sudo parted -s -a optimal -- /dev/vdc mklabel gpt
sudo parted -s -a optimal -- /dev/vdc mkpart primary 0% 100%
sudo parted -s -- /dev/vdc align-check optimal 1

Create a physical volume and volume group in it.

$ sudo pvcreate /dev/vdc1
Physical volume "/dev/vdc1" successfully created.

$ sudo vgcreate data /dev/vdc1
 Volume group "data" successfully created

Create a Logical volume

$ sudo lvcreate -n repos -l+100%FREE data
Logical volume "repos" created.

Create a file system

$ sudo mkfs.xfs /dev/mapper/data-repos
meta-data=/dev/mapper/data-repos isize=512    agcount=4, agsize=1965824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=7863296, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=3839, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Create directory for mounting the Logical volume.

sudo mkdir /data

Create a mount point

$ sudo vim /etc/fstab

/dev/mapper/data-repos /data xfs defaults 0 0 

Mount it.

sudo mount -a

Confirm:

$ df -hT /data
Filesystem             Type  Size   Used   Avail Use% Mounted on
/dev/mapper/data-repos xfs   100G   247M   100G   1%  /data

Create base directory for CentOS 8 repository data.

sudo mkdir -p /data/repos/centos/8/

Confirm all directories are created.

$ tree /data/
/data/
└── repos
    └── centos
        └── 8

3 directories, 0 files

Step 3: Create Repositories Sync script

Let’s now create a script that will be used to sync contents from the remote repositories to our local system.

$ sudo vim /etc/centos8_reposync.sh

Paste below data.

#!/bin/bash
repos_base_dir="/data/repos/centos/8/"

# Start sync if base repo directory exist
if [[ -d "$repos_base_dir" ]] ; then
  # Start Sync
  rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/8/  "$repos_base_dir"
  # Download CentOS 8 repository key
  wget -P $repos_base_dir wget https://www.centos.org/keys/RPM-GPG-KEY-CentOS-Official
fi

Make the script executable.

sudo chmod +x /etc/centos8_reposync.sh

Install Tmux and initiate first execution.

$ sudo yum -y install tmux
$ tmux
$ sudo /etc/centos8_reposync.sh

The script should start running.

A number of directories should be created.

$ ls -1 /data/repos/centos/8/
AppStream
BaseOS
centosplus
COMPOSE_ID
cr
extras
fasttrack
isos
PowerTools

Wait for the initial script to finish running then create a cron job for weekly sync.

I’ll set my sync to run every Sunday 6pm.

$ sudo crontab -e

00 18 * * 7  /etc/centos8_reposync.sh

Step 4: Configure Nginx

Create Nginx configuration file to serve the repository contents.

$ sudo vim /etc/nginx/conf.d/centos.conf
server {
	listen 80;
	server_name repos.example.com;
        root /data/repos/;

       location / {
                autoindex on;
        }
}
  • Where repos.example.com is the domain name you’ll use to access the repositories.

Configure SELinux Labels.

sudo semanage fcontext -a -t httpd_sys_content_t "/data/repos(/.*)?"
sudo restorecon -Rv /data/repos

Restart nginx after the change.

sudo nginx -t
sudo systemctl restart nginx

Open the server URL to confirm it is working.

Step 5: Configure CentOS 8 Client machines

Now that our mirror is ready, we can configure our CentOS 8 machines to use the local repositories.

Start by backing up current repositories

cd /etc/yum.repos.d/
sudo mkdir old-repos
sudo mv *.repo old-repos 

Create a new repository file

sudo vim local.repo

Modify below and paste it.

[BaseOS]
name=CentOS-$releasever - Base
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[AppStream]
name=CentOS-$releasever - AppStream
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[centosplus]
name=CentOS-$releasever - Plus
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[extras]
name=CentOS-$releasever - Extras
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/extras/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-$releasever - PowerTools
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[cr]
name=CentOS-$releasever - cr
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/cr/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

[fasttrack]
name=CentOS-$releasever - fasttrack
baseurl=//repos.ownyourlife.com.ng/centos/$releasever/fasttrack/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=//repos.ownyourlife.com.ng/centos/$releasever/RPM-GPG-KEY-CentOS-Official

Where:

  • repos.ownyourlife.com.ng is the Domain used for repos access

Clean current repo cache:

sudo yum clean all

The update:

$ sudo yum makecache
CentOS-8 - AppStream                                                                                               1.2 MB/s | 4.3 kB     00:00    
CentOS-8 - Base                                                                                                    3.9 kB/s | 3.9 kB     00:01    
CentOS-8 - Extras                                                                                                  881 kB/s | 1.5 kB     00:00    
Metadata cache created.

List repositories:

$ sudo yum repolist 
Last metadata expiration check: 0:02:10 ago on Fri 01 Nov 2019 12:41:38 PM EAT.
repo id                                                          repo name                                                                   status
AppStream                                                        CentOS-8 - AppStream                                                        5,089
BaseOS                                                           CentOS-8 - Base                                                             2,843
extras                                                           CentOS-8 - Extras                                                               3

Run system update from it.

sudo yum -y update

Step 6: Enabling Disabled repositories

Some repositories are disabled by default. To enable them, install yum-utils package.

sudo yum install yum-utils

To activate the repository, use the command:

sudo yum-config-manager --enable reponame

Below are the examples to enable PowerTools and centosplus repositories:

sudo yum-config-manager --enable PowerTools
sudo yum-config-manager --enable centosplus

Confirm:

$ sudo yum repolist 
CentOS-8 - AppStream                                                                                               1.7 MB/s | 4.3 kB     00:00    
CentOS-8 - Base                                                                                                    2.0 MB/s | 3.9 kB     00:00    
CentOS-8 - PowerTools                                                                                               75 MB/s | 1.8 MB     00:00    
CentOS-8 - Plus                                                                                                     58 MB/s | 833 kB     00:00    
CentOS-8 - Extras                                                                                                  783 kB/s | 1.5 kB     00:00    
repo id                                                          repo name                                                                   status
AppStream                                                        CentOS-8 - AppStream                                                        5,089
BaseOS                                                           CentOS-8 - Base                                                             2,843
PowerTools                                                       CentOS-8 - PowerTools                                                       1,507
centosplus                                                       CentOS-8 - Plus                                                                26
extras                                                           CentOS-8 - Extras                                                               3

To disable, substitute –enable with –disable.

You now have working Local CentOS 8 Mirrors. Enjoy and check a similar guide we have for users running Satellite / Katello / Foreman.

How To Sync CentOS 8 repositories on Satellite / Katello / Foreman

Tags:

  • Setup CentOS 8 repository
  • How to create CentOS 8 mirrors
  • How to host CentOS 8 repositories locally
  • Setup Local CentOS 8 Yum Repository

Cart

Your Cart is Empty

Back To Shop