apache的搭建
发表于:2025-08-04 | 分类: Linux

apache搭建准备工作

权限最小化:改属主属组为apache,改权限为755
1
2
root@farrah ~]# chown apache:apache /var/www/html
[root@farrah ~]# chmod 755 /var/www/html/
把源换成华为的
1
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-8-reg.repo
下载apache服务
1
2
3
4
5
6
[root@farrah 桌面]# yum install httpd
#出现模块依赖问题请查看问题解决

#安装完成开启并查看httpd状态
[root@farrah ~]# systemctl start httpd
[root@farrah ~]# systemctl status httpd

image-20250804155207371

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
##在安装完以后,我们可以检查apache配置文件
# 主配置文件
ls -l /etc/httpd/conf/httpd.conf
# 虚拟主机配置目录
ls -l /etc/httpd/conf.d/
# 自定义配置文件(如 mywebsite.conf)
ls -l /etc/httpd/conf.d/mywebsite.conf
#检查系统新增的系统服务
systemctl list-unit-files | grep httpd
#检查新增的apache进程
ps aux | grep httpd
#检查新增的日志文件
ls -l /var/log/httpd/
#检查新增的用户和组
grep apache /etc/passwd
grep apache /etc/group
tail -3 /etc/passwd
添加虚拟主机配置文件的内容

/etc/httpd/conf.d/自定义文件名

1
2
3
4
5
6
7
<VirtualHost *:80>
ServerName study.example.com # 主域名
ServerAlias www.study.example.com # 备用域名
DocumentRoot /var/www/study/html # 网站根目录
ErrorLog /var/log/httpd/study_error.log # 错误日志
CustomLog /var/log/httpd/study_access.log combined # 访问日志
</VirtualHost>

1.开始创建第一个网站

1
2
3
[root@farrah 桌面]# vim /var/www/html/index.html
#创建index.html文件放入html文件代码
[root@farrah ~]# ifconfig #查看ip

2.创建第二个网站

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#创建第二个网站的文件 日志、html目录
[root@farrah ~]# mkdir -p /var/www/study/html /var/www/study/log
#创建第二个网站的html文件在html的路径下
[root@farrah ~]# vim /var/www/study/html/index.html
#编辑虚拟主机配置文件
[root@farrah ~]# vim /etc/httpd/conf.d/study.conf

#内容为:
<VirtualHost *:80>
ServerName study.example.com # 主域名
ServerAlias www.study.example.com # 备用域名
DocumentRoot /var/www/study/html # 网站根目录
ErrorLog /var/log/httpd/study_error.log # 错误日志
CustomLog /var/log/httpd/study_access.log combined # 访问日志
</VirtualHost>

#配置好后记得要重启httpd
systemctl restart httpd
重启后在虚拟机访问域名可以成功

image-20250804170245396

#安装httpd时出现模块依赖问题的解决办法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@farrah ~]# yum install httpd
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
CentOS-8 - Base - repo.huaweicloud.com 28 kB/s | 3.9 kB 00:00
CentOS-8 - PowerTools - repo.huaweicloud.com 41 kB/s | 4.3 kB 00:00
CentOS-8 - Extras - repo.huaweicloud.com 14 kB/s | 1.5 kB 00:00
模块依赖问题

问题 1: conflicting requests

- nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
问题 2: conflicting requests
- nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
未找到匹配的参数: httpd
错误:没有任何匹配: httpd

问题原因:

  1. 模块依赖冲突
    • 系统缺少perl 5.26模块,导致perl-DBD-SQLite和perl-DBI模块无法满足依赖
    • 这是CentOS 8的模块化系统(Modularity)导致的常见问题
  2. 仓库配置问题
    • 多个仓库(AppStream/extras/PowerTools/centosplus)被重复配置
    • 虽然已配置华为云镜像源,但可能未正确处理模块流

解决方案:

第一步:清理重复仓库配置

1
2
3
4
5
# 备份现有仓库
mkdir ~/repo_backup && cp /etc/yum.repos.d/* ~/repo_backup/

# 删除重复仓库文件(保留华为云的CentOS-Base.repo)
rm -f /etc/yum.repos.d/CentOS-*.repo

第二步:重置华为云仓库配置

1
2
3
4
5
6
7
# 重新下载华为云官方提供的完整仓库配置
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-8-reg.repo
#此时发现还是存在问题


# 更新缓存
dnf clean all && dnf makecache

第三步:解决模块依赖问题

1
2
3
4
5
6
7
8
# 重置模块流
dnf module reset perl

# 启用正确的perl模块流(5.26可能已过时,建议使用更新的版本)
dnf module enable perl:5.32 -y

# 或者直接安装最新perl模块
dnf install perl -y

第四步:安装httpd

1
2
# 尝试安装httpd(使用--allowerasing参数自动解决依赖冲突)
dnf install httpd --allowerasing -y
上一篇:
Linux重定向
下一篇:
压缩和解压缩