任务目标:
- 安装Apache HTTP Server
- 发布静态网站:TCP 80
- 基于端口、虚拟目录、域名发布静态网站:TCP 80-82
任务平台:
VM-Project-1-Task-1-10.10.10.101
部署指南:
1. 系统基本配置
hostnamectl set-hostname VM-Project-1-Task-1
2. 网络配置
#查看网卡信息
nmcli device
#配置IPv4地址(请根据实际情况调整网络链接的名称ens33)
nmcli connection modify ens33 ipv4.addresses "10.10.10.101/24" ipv4.gateway 10.10.10.254
#重新载入网络链接的配置信息
nmcli connection reload
nmcli connection up ens33
3. 系统升级、关闭防火墙和SELinux等配置
#配置防火墙,允许80-82/tcp端口访问
firewall-cmd --zone=public --add-port=80-82/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
#配置SELINUX,允许80-82端口提供服务
#安装SELINUX的配置工具semanage
yum install policycoreutils-python-utils
#查看SELINUX允许的HTTP端口(默认支持80、81)
semanage port -l | grep http
#增加TCP 82端口
semanage port -a -t http_port_t -p tcp 82
#重新启动服务器
reboot
参考文献:
OpenEuler:SELinux配置
使用semanage管理SELinux安全策略
Configuring SELinux for applications and services with non-standard configurations
CentOS下安装semanage
4. 安装Apache
#安装Apache HTTP Server
yum install -y httpd
#启动httpd服务,并设置为自动启动
systemctl start httpd
systemctl enable httpd
#使用浏览器即可访问10.10.10.101,出现欢迎界面,说明apache http server安装成功
#查看httpd服务状态
systemctl status httpd
服务状态信息说明:
ttpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-05-17 16:15:32 CST; 2min 7s ago
Docs: man:httpd.service(8)
Main PID: 1841 (httpd)
Status: "Total requests: 3; Idle/Busy workers 100/0;Requests/sec: 0.0252; Bytes served/sec: 87 B/sec"
Tasks: 230 (limit: 8930)
Memory: 20.1M
CGroup: /system.slice/httpd.service
├─ 1841 /usr/sbin/httpd -DFOREGROUND
├─ 1842 /usr/sbin/httpd -DFOREGROUND
├─ 1843 /usr/sbin/httpd -DFOREGROUND
├─ 1844 /usr/sbin/httpd -DFOREGROUND
├─ 1845 /usr/sbin/httpd -DFOREGROUND
└─ 2041 /usr/sbin/httpd -DFOREGROUND5月 17 16:15:32 VM-Project-1-Task-1 systemd[1]: Starting The Apache HTTP Server...
5. 静态网站的发布
| 序号 | 网站名称 | 访问路径 | 发布方式 |
|---|---|---|---|
| 1 | Site-A | http://10.10.10.101 | IP+默认端口 |
| 2 | Site-B | http://10.10.10.101:81 | 自定义端口 |
| 3 | Site-C | http://10.10.10.101:82 | 自定义端口 |
| 4 | Site-D | http://10.10.10.101/d | Alias |
| 5 | Site-E | http://10.10.10.101/e | Alias |
| 6 | Site-F | www.f.local | Domain |
| 7 | Site-G | www.g.local | Domain |
说明:
本地主机访问域名时,需要配置hosts文件。
6. 创建网站文件:
mkdir /var/www/html/SiteA
echo "<h1>Site-A<br />URL:https://10.10.10.101</h1>" >> /var/www/html/SiteA/index.html
mkdir /var/www/html/SiteB
echo "<h1>Site-B<br />URL:https://10.10.10.101:81</h1>" >> /var/www/html/SiteB/index.html
mkdir /var/www/html/SiteC
echo "<h1>Site-C<br />URL:https://10.10.10.101:82</h1>" >> /var/www/html/SiteC/index.html
mkdir /var/www/html/SiteD
echo "<h1>Site-D<br />URL:https://10.10.10.101/d</h1>" >> /var/www/html/SiteD/index.html
mkdir /var/www/html/SiteE
echo "<h1>Site-E<br />URL:https://10.10.10.101/e</h1>" >> /var/www/html/SiteE/index.html
mkdir /var/www/html/SiteF
echo "<h1>Site-F<br />URL:https://www.f.local</h1>" >> /var/www/html/SiteF/index.html
mkdir /var/www/html/SiteG
echo "<h1>Site-G<br />URL:https://www.g.local</h1>" >> /var/www/html/SiteG/index.html
7. 发布网站Site-A
发布网站Site-A,修改Apache默认网站的配置信息。配置文件:/etc/httpd/conf/httpd.conf
cat /etc/httpd/conf/httpd.conf | grep -vE "#|^$"
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
DocumentRoot "/var/www/html/SiteA"
<Directory "/var/www/html/SiteA">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
8. 发布网站Site-B、Site-C
使用自定义端口发布网站,创建新的配置文件。
cat > /etc/httpd/conf.d/port-SiteB.conf << EOF
Listen 81
<VirtualHost *:81>
DocumentRoot /var/www/html/SiteB
</VirtualHost>
EOF
cat > /etc/httpd/conf.d/port-SiteC.conf << EOF
Listen 82
<VirtualHost *:82>
DocumentRoot /var/www/html/SiteC
</VirtualHost>
EOF
9. 发布网站Site-D、Site-E
使用自定义虚拟目录发布网站,创新新的配置文件。
cat > /etc/httpd/conf.d/alias-SiteD.conf << EOF
Alias /d "/var/www/html/SiteD"
<Directory "/var/www/html/SiteD">
AllowOverride None
Options None
Require all granted
</Directory>
EOF
cat > /etc/httpd/conf.d/alias-SiteE.conf << EOF
Alias /e "/var/www/html/SiteE"
<Directory "/var/www/html/SiteE">
AllowOverride None
Options None
Require all granted
</Directory>
EOF
10. 发布网站Site-F、Site-G
使用网站域名发布网站,创建新的配置文件。
cat > /etc/httpd/conf.d/domain-SiteF.conf << EOF
<VirtualHost *:80>
ServerName www.f.local
ServerAlias f.local
DocumentRoot /var/www/html/SiteF
</VirtualHost>
<Directory "/var/www/html/SiteF">
AllowOverride None
Options None
Require all granted
</Directory>
EOF
cat > /etc/httpd/conf.d/domain-SiteG.conf << EOF
<VirtualHost *:80>
ServerName www.g.local
ServerAlias g.local
DocumentRoot /var/www/html/SiteG
</VirtualHost>
<Directory "/var/www/html/SiteG">
AllowOverride None
Options None
Require all granted
</Directory>
EOF
11. 重启Apache服务,进行测试
在本地主机上通过浏览器进行测试。
systemctl reload httpd
systemctl status httpd