時間:2024.11.24
計劃:使用apache搭建HTTP(Hypertext Transfer Protocol)伺服器,共享iso映象為環境內其他主機提供repo(repository)源
參照:
- 馬哥教育王老師課程
- 基於Linux系統的本地Yum源搭建與配置(ISO方式、RPM方式)
https://developer.aliyun.com/article/1356520 - 如何在 RHEL 9 上建立本地 Yum/DNF 倉庫
https://linux.cn/article-14697-1.html
安裝httpd(apache)
[root@RHEL9 ~]# dnf info httpd
Updating Subscription Management repositories.
Last metadata expiration check: 2:16:03 ago on Sun 24 Nov 2024 12:21:38 AM CST.
Available Packages
Name : httpd
Version : 2.4.62
Release : 1.el9
Architecture : x86_64
Size : 51 k
Source : httpd-2.4.62-1.el9.src.rpm
Repository : rhel-9-for-x86_64-appstream-rpms
Summary : Apache HTTP Server
URL : https://httpd.apache.org/
License : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
: web server.
[root@RHEL9 ~]# dnf install -y httpd
在http預設共享目錄 /var/www/html/ 建立一個軟連線 /var/www/html/iso 指向autofs掛載iso檔案的路徑 /misc/
[root@RHEL9 ~]# ln -s /misc/ /var/www/html/iso
[root@RHEL9 ~]# ls /var/www/html/iso/alma8
AppStream BaseOS EFI EULA extra_files.json GPL images isolinux media.repo RPM-GPG-KEY-AlmaLinux TRANS.TBL
啟動服務
[root@RHEL9 ~]# systemctl status httpd.service
○ httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
Active: inactive (dead)
Docs: man:httpd.service(8)
[root@RHEL9 ~]# systemctl enable --now httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
配置防火牆
[root@RHEL9 ~]# firewall-cmd --list-services
ssh
[root@RHEL9 ~]# firewall-cmd --info-service=http
http
ports: 80/tcp
protocols:
source-ports:
modules:
destination:
includes:
helpers:
[root@RHEL9 ~]# firewall-cmd --query-service=http || firewall-cmd --add-service=http
no
success
[root@RHEL9 ~]# firewall-cmd --runtime-to-permanent
success
[root@RHEL9 ~]# firewall-cmd --list-services
http ssh
區域網訪問成功
指令碼化從autofs自動掛載到http共享
[root@centos7 ~]# cat rhel9iso2web.sh
#rhel9iso2web.sh
#Date: 2024-11-24
#!/bin/bash
#指令碼功能
#將映象檔案實現autofs自動掛載並透過安裝apache使用http協議共享出來
isodir=/data/ISO/
webdir=/var/www/html/iso
#安裝autofs服務
rpm -q autofs || yum install -y autofs
#配置/etc/auto.misc檔案
sed -Ei.bak "/^cd[[:blank:]]+-fstype=iso9660,ro,nosuid,nodev[[:blank:]]+:\/dev\/cdrom/a\* -fstype=iso9660,ro,nosuid,nodev :${isodir}&.iso" /etc/auto.misc
#啟動autofs服務
systemctl enable --now autofs.service
#安裝httpd服務
rpm -q httpd || yum install -y httpd
#為misc目錄建立軟連線至http共享目錄
[ -L ${webdir} ] || ln -s /misc/ ${webdir}
#啟動httpd服務
systemctl enable --now httpd.service
#配置防火牆
firewall-cmd --query-service=http || firewall-cmd --add-service=http
firewall-cmd --runtime-to-permanent