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
- Using CentOS 7.4 Everything system.
- Demonstrating with a CentOS virtual system based on Mac OS Parallels virtual machine.
Setting Up CentOS 7 Local Yum Environment
- 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
. - Create a directory and a mount directory:
mkdir /home/centosiso && mkdir /mnt/cdrom
- Mount the cdrom:
mount /dev/cdrom /mnt/cdrom/
- Copy all the contents from the disc:
cp -rf /mnt/cdrom/* /home/centosiso
- Use
du -sh /home/centosiso
to see the size of the image I used, which is 8.2GB.
1 | [root@localhost cdrom]# cd /home/centosiso/ |
- Go to the directory
/etc/yum.repos.d/
.
1 | cd /etc/yum.repos.d/ |
- By default, there are these repositories installed by the official installation, there may be more or less, it doesn’t matter.
1 | [root@localhost yum.repos.d]# ls |
- Delete or backup these resources, because they are not necessary in an internal network environment.
1 | cp -rf /etc/yum.repos.d /etc/yum.repos.d.backup |
- In the
yum.repos.d
directory, create a file with any name and with the.repo
extension. Here, we createCentOS-Local.repo
with the following content:
1 | [c7-media] |
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 | # CentOS-Media.repo |
- 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 | [root@localhost yum.repos.d]# yum makecache |
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
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).
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 | python -m(odule) SimpleHTTPServer, |
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:
You can see that it is a list of files.
- 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 | [CentOS-Remote-HTTP] |
- 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:
- 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.
Download nginx from the official website Download Link.
Upload it to the server, assuming the directory is
/root
.Extract and go into the directory.
1 | cd /root |
- Install dependencies and execute the following commands to compile:
1 | yum install pcre-devel zlib-devel openssl-devel gcc make -y |
1 | ./configure \ |
- Create the nginx group and user.
1 | [root@localhost nginx-1.16.0]# groupadd nginx |
- 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 |
- Edit
vi /usr/local/nginx/conf/nginx.conf
, and add the following line above or belowlocation / {
:
1 | location /centosyum { |
The complete configuration looks like this:
- Execute nginx configuration reload.
1 | /usr/local/nginx/sbin/nginx -s reload |
- 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 | [CentOS-Remote-HTTP] |
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 thelocation /centosyum
configuration mentioned above:1
2
3
4location /centosyum {
alias /home/centosiso;
autoindex on; # 增加了这么一行
}Result:
You can continue the operations according to the steps in “Using Http to Build a Remote CentOS Yum Environment - Step 3.”