Setting Up Local Yum Repository and Accessing Remote Yum

In client development, we often encounter situations where there is no internet connection and various resources need to be transferred through CDs or USB drives. This article documents the process of setting up a local yum repository, serving as a backup for myself. However, after doing this, the issue of package version compatibility arises because even the “everything” repository does not provide different versions of dependencies.(English version Translated by GPT-3.5, 返回中文)

Environment

  1. Using CentOS 7.4 Everything system.
  2. Demonstrating with a CentOS virtual system based on Mac OS Parallels virtual machine.

Setting Up CentOS 7 Local Yum Environment

  1. First, copy the complete Everything disc to a directory in CentOS. This source can be copied from a disc (requires 2 DVDs) or a USB drive, and copy the contents to a directory of your choice. Here, we use /home/centosiso.
  2. Create a directory and a mount directory: mkdir /home/centosiso && mkdir /mnt/cdrom
  3. Mount the cdrom: mount /dev/cdrom /mnt/cdrom/
  4. Copy all the contents from the disc: cp -rf /mnt/cdrom/* /home/centosiso
  5. Use du -sh /home/centosiso to see the size of the image I used, which is 8.2GB.
1
2
3
[root@localhost cdrom]# cd /home/centosiso/
[root@localhost centosiso]# du -sh .
8.2G .
  1. Go to the directory /etc/yum.repos.d/.
1
cd /etc/yum.repos.d/
  1. By default, there are these repositories installed by the official installation, there may be more or less, it doesn’t matter.
1
2
3
4
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Media.repo CentOS-fasttrack.repo
CentOS-CR.repo CentOS-Sources.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
  1. Delete or backup these resources, because they are not necessary in an internal network environment.
1
2
cp -rf /etc/yum.repos.d /etc/yum.repos.d.backup
rm -rf /etc/yum.repos.d/* # 我这里是做备份加删除了
  1. In the yum.repos.d directory, create a file with any name and with the .repo extension. Here, we create CentOS-Local.repo with the following content:
1
2
3
4
5
6
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///home/centosiso
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Fill in the baseurl with the local path, and the rest can be copied as is. Alternatively, you can refer to the original CentOS-Media.repo file that was deleted before, as the configuration is basically the same. Below is the content of CentOS-Media.repo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# CentOS-Media.repo
#
# This repo can be used with mounted DVD media, verify the mount point for
# CentOS-7. You can use this repo and yum to install items directly off the
# DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
# yum --enablerepo=c7-media [command]
#
# or for ONLY the media repo, do this:
#
# yum --disablerepo=\* --enablerepo=c7-media [command]

[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  1. Now it’s done. Run yum makecache, and you will see the following output. Now, you can use yum to install any package. CentOS-Everything contains most of the common dependency packages, as long as they are not very specific.
1
2
3
4
5
6
7
8
9
10
[root@localhost yum.repos.d]# yum makecache
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
c7-media | 3.6 kB 00:00
(1/4): c7-media/group_gz | 156 kB 00:00
(2/4): c7-media/primary_db | 5.7 MB 00:00
(3/4): c7-media/filelists_db | 6.7 MB 00:00
(4/4): c7-media/other_db | 2.5 MB 00:00
Loading mirror speeds from cached hostfile
Metadata Cache Created

Using Http to Build a Remote CentOS Yum Environment

The above steps are for building a local environment. Here, we will explain how to use http to install dependencies on a machine with a local yum environment, assuming that there is network connectivity between these two servers.

Use the previously configured server as the yum source, which is server 133

  1. Suppose this Linux machine has nothing installed, no tomcat, no nginx, just a very new environment. However, there is a python SimpleHTTPServer service, which is a component that is installed by default and can be used to test if the port is open and perform basic file services. When started, it will open a listening port and provide basic file services when accessed.

    1
    python -m SimpleHTTPServer 9000

    Of course, if the server environment allows, you can install nginx on the server with the local yum environment. Everything provides all the basic dependencies for nginx as well (the installation of nginx is also provided below).

  2. First, use python to create a temporary remote yum environment. Don’t forget that CentOS has firewall enabled by default.

1
python -m SimpleHTTPServer 9000

This is a simple command to remember.

1
2
python -m(odule) SimpleHTTPServer, 
python执行一个模块, 简单的HTTP服务.

If you do not enter 9000 at the end, it will default to listening on port 8000. After it starts, open the browser and access it as shown in the image below:

img

You can see that it is a list of files.

  1. On the new server, go to /etc/yum.repos.d and create a new repo file with any name and the extension “repo”. The content of the file is just changing the baseurl to an http address.
1
2
3
4
5
6
[CentOS-Remote-HTTP]
name=CentOS-$releasever - Media
baseurl=http://10.211.55.133:9000
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  1. Run yum makecache, and you will see that it is usable now. At the same time, the yum source server will display the information shown in the image below:

img

  1. The whole process is actually quite simple. If you are done using it, press Ctrl-Z to cancel, and next time you can just run python -m SimpleHTTPServer 9000.

Warning: Pay attention to the issue of dependency versions. In many cases, there may be problems when the server has higher versions of dependencies installed and the ones provided by everything are lower. In this case, careful operation is required.

Appendix: Compiling and Installing Nginx, and Proxying Local Directory for File Service

SimpleHTTPServer is a temporary solution. If you want a solution that is always available, you can use nginx.

  1. Download nginx from the official website Download Link.

  2. Upload it to the server, assuming the directory is /root.

  3. Extract and go into the directory.

1
2
3
cd /root
tar -zxvf nginx-1.16.0.tar.gz
cd cd nginx-1.16.0
  1. Install dependencies and execute the following commands to compile:
1
yum  install pcre-devel zlib-devel openssl-devel gcc make -y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream

等ready后, 执行

make

等make后, 执行

make install
  1. Create the nginx group and user.
1
2
[root@localhost nginx-1.16.0]# groupadd nginx
[root@localhost nginx-1.16.0]# useradd -g nginx -s /sbin/nologin nginx
  1. Start nginx. Since there is no prompt, open the browser and access it as shown in the image below:
1
/usr/local/nginx/sbin/nginx

img

  1. Edit vi /usr/local/nginx/conf/nginx.conf, and add the following line above or below location / {:
1
2
3
location /centosyum {
alias /home/centosiso;
}

The complete configuration looks like this:
img

  1. Execute nginx configuration reload.
1
/usr/local/nginx/sbin/nginx -s reload
  1. Since it is listening on port 80, the access address is http://10.211.55.133/centosyum. As mentioned before, create a new file with the suffix “.repo” in /etc/yum.repos.d, and the content is as follows:
1
2
3
4
5
6
[CentOS-Remote-HTTP]
name=CentOS-$releasever - Media
baseurl=http://10.211.55.133/centosyum #就是这里的地址换了下而已
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  1. Now, if you access http://10.211.55.133/centosyum in the browser, it will prompt 403 Forbidden. If you want to see the files just like in SimpleHTTPServer, then add the following to the location /centosyum configuration mentioned above:

    1
    2
    3
    4
    location /centosyum {
    alias /home/centosiso;
    autoindex on; # 增加了这么一行
    }

    Result:

    img

  2. You can continue the operations according to the steps in “Using Http to Build a Remote CentOS Yum Environment - Step 3.”