Linux基于Apache服务搭建简易镜像站

简介: Linux基于Apache服务搭建简易镜像站


Linux基于Apache服务搭建简易镜像站

  • 安装Apache服务器
yum install -y httpd.x86_64
  • 配置Apache服务器:编辑Apache配置文件/etc/httpd/conf/httpd.conf
#ServerName www.example.com:80
#Listen 12.34.56.78:80
Listen

修改为:

#ServerName www.example.com:80
ServerName 192.168.51.140:80
#Listen 12.34.56.78:80
Listen 192.168.51.140:80

Apache配置文件末尾添加如下:配置虚拟主机

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    #网站的根目录,存放位置
    DocumentRoot /var/www/html/mirrors
    #网站的域名或 IP 地址
    ServerName 192.168.51.140
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • 使用httpd -t命令检查配置文件是否书写错误:重启Apache服务器
httpd -t
systemctl restart httpd
  • 创建镜像站目录
mkdir /var/www/html/mirrors
  • 编写HTML代码

mirrors 目录下创建一个名为 index.html 的文件:

nano /var/www/html/mirrors/index.html
<!DOCTYPE html>
<html>
<head>
  <title>My Mirror Site</title>
</head>
<body>
  <h1>Hello, world!</h1>
</body>
</html>
  • 配置阿里云镜像源:创建目录备份yum.repos.d路径下的文件
[root@localhost yum.repos.d]# mkdir bak
[root@localhost yum.repos.d]# mv CentOS-* bak/
  • 下载阿里云Yum源
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2023-04-14 22:51:21--  https://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 119.36.62.54, 119.36.62.60, 119.36.62.61, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|119.36.62.54|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[==========================================================================>] 2,523       --.-K/s 用时 0s      
2023-04-14 22:51:26 (1.36 GB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])
[root@localhost yum.repos.d]# ll
总用量 4
drwxr-xr-x. 2 root root  219 4月  15 2023 bak
-rw-r--r--. 1 root root 2523 8月   4 2022 CentOS-Base.repo
  • 清除yum缓存,测试是否成功
[root@localhost yum.repos.d]# yum clean all 
已加载插件:fastestmirror, langpacks
正在清理软件源: base extras updates
Cleaning up list of fastest mirrors
Other repos take up 19 M of disk space (use --verbose for details)
[root@localhost yum.repos.d]# yum repolist 
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                         | 3.6 kB  00:00:00     
extras                                                                                       | 2.9 kB  00:00:00     
updates                                                                                      | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                | 153 kB  00:00:05     
(2/4): extras/7/x86_64/primary_db                                                            | 249 kB  00:00:05     
(3/4): updates/7/x86_64/primary_db                                                           |  20 MB  00:00:06     
base/7/x86_64/primary_db       FAILED                                          
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: (28, 'Connection timed out after 30000 milliseconds')
正在尝试其它镜像。
(4/4): base/7/x86_64/primary_db                                                              | 6.1 MB  00:00:00     
源标识                                     源名称                                                             状态
base/7/x86_64                              CentOS-7 - Base - mirrors.aliyun.com                               10,072
extras/7/x86_64                            CentOS-7 - Extras - mirrors.aliyun.com                                515
updates/7/x86_64                           CentOS-7 - Updates - mirrors.aliyun.com                             4,886
repolist: 15,473
  • 测试是否成功
curl 192.168.51.140:80

或者浏览器访问192.168.51.140:80

  • 如无法访问则关闭防火墙SELinux
stop firewalld.service
setenforce 0

基于Apache页面搭建镜像站

  • 使用yum 工具同步镜像:下载yum工具
sudo yum install yum-utils
yum install createrepo reposync
sudo yum clean all
yum makecache
  • 创建对应镜像目录
mkdir -p /var/www/html/mirrors/centos/7/os/x86_64/
  • 使用 reposync 命令同步 CentOS 7 OS 软件源
reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/www/html/mirrors/centos/7/os/x86_64/
  • 使用createrepo 工具来创建一个 RPM 包的元数据库
createrepo /var/www/html/mirrors/centos/7/os/x86_64/
  • 配置Apache配置文件/etc/httpd/conf/httpd.conf

配置映射路径:访问/mirrors映射到/var/www/html/mirrors

# Alias /webpath /full/filesystem/path
 Alias /mirrors "/var/www/html/mirrors"

配置目录访问权限

<Directory "/var/www/html/mirrors">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
  1. "var/www/html/mirrors" 指定了要配置的目录路径
  2. Options Indexes FollowSymLinks 指定了该目录允许列出文件和子目录,并且允许使用符号链接
  3. AllowOverride None 指定了该目录不允许使用 .htaccess 文件覆盖 Apache 的全局配置
  4. Require all granted 指定了该目录允许所有用户访问

镜像站常用目录

/var/www/html/mirrors
├── centos
│   ├── 7
│   │   ├── os
│   │   ├── updates
│   │   ├── extras
│   │   └── ...
│   ├── 8
│   │   ├── BaseOS
│   │   ├── AppStream
│   │   ├── extras
│   │   └── ...
│   └── ...
├── epel
│   ├── 7
│   │   ├── x86_64
│   │   ├── noarch
│   │   └── ...
│   ├── 8
│   │   ├── x86_64
│   │   ├── noarch
│   │   └── ...
│   └── ...
├── docker-ce
│   ├── centos7
│   │   ├── x86_64
│   │   └── ...
│   ├── centos8
│   │   ├── x86_64
│   │   └── ...
│   └── ...
└── ...

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


目录
相关文章
|
8天前
|
存储 缓存 算法
分布式锁服务深度解析:以Apache Flink的Checkpointing机制为例
【10月更文挑战第7天】在分布式系统中,多个进程或节点可能需要同时访问和操作共享资源。为了确保数据的一致性和系统的稳定性,我们需要一种机制来协调这些进程或节点的访问,避免并发冲突和竞态条件。分布式锁服务正是为此而生的一种解决方案。它通过在网络环境中实现锁机制,确保同一时间只有一个进程或节点能够访问和操作共享资源。
25 3
|
17天前
|
Linux 网络安全 Apache
CentOS 7.2配置Apache服务httpd(上)
CentOS 7.2配置Apache服务httpd(上)
133 1
|
2月前
|
Linux
在Linux中,怎么把脚本添加到系统服务里,即用 service 来调用?
在Linux中,怎么把脚本添加到系统服务里,即用 service 来调用?
|
11天前
|
Ubuntu Linux 网络安全
Linux中服务管理问题
【10月更文挑战第4天】
11 2
|
13天前
|
存储 分布式计算 druid
大数据-155 Apache Druid 架构与原理详解 数据存储 索引服务 压缩机制
大数据-155 Apache Druid 架构与原理详解 数据存储 索引服务 压缩机制
32 3
|
16天前
|
Linux
linux开机挂载镜像
【10月更文挑战第1天】在 Linux 系统中,开机挂载镜像通常涉及几个关键步骤,包括创建挂载点、编辑配置文件以及重新加载配置
50 0
|
17天前
|
Linux PHP Apache
CentOS 7.2配置Apache服务httpd(下)
CentOS 7.2配置Apache服务httpd(下)
44 1
|
14天前
|
应用服务中间件 Linux Shell
Linux 配置 Nginx 服务的详细步骤,绝对干货
Linux 配置 Nginx 服务的详细步骤,绝对干货
43 0
|
18天前
|
资源调度 JavaScript 前端开发
yarn源|yarn 国内镜像+linux删除大量文件
yarn源|yarn 国内镜像yarn源|yarn 国内镜像+linux删除大量文件
187 0
|
1月前
|
NoSQL Linux Redis
Linux Redis 服务设置开机自启动
【9月更文挑战第2天】在 Linux 系统中,可使用两种方法设置 Redis 开机自启动:一是通过创建 `redis.service` 文件并利用 systemd 进行管理,包括定义服务参数和启动脚本;二是编辑 `/etc/rc.local` 文件,在其中添加启动命令。推荐使用 systemd 方法,因为它更符合现代 Linux 系统的设计理念。设置完成后,可通过 `sudo systemctl status redis.service` 检查服务状态。
121 3