一、Apache 安装
[root@localhost ~]# dnf install httpd -y[root@localhost ~]# systemctl enable --now httpd二、Apache 服务基本信息
| 项目 | 内容 |
|---|---|
| 软件名 | httpd |
| 端口 | HTTP 80 / HTTPS 443 |
| 主配置文件 | /etc/httpd/conf/httpd.conf |
| 子配置文件目录 | /etc/httpd/conf.d/ |
| 服务启动脚本 | httpd.service |
| 默认发布目录 | /var/www/html |
| 默认发布文件 | index.html |
三、Apache 的基本配置
1. 发布文件
a. 默认发布文件的生成
echo172.25.254.100>/var/www/html/index.htmlcat/var/www/html/index.html测试:
[root@localhost ~]# curl 192.168.17.138192.168.17.138b. 默认发布文件的修改
修改主配置文件,调整DirectoryIndex的优先级:
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf<IfModule dir_module>DirectoryIndex test.html index.html</IfModule>[root@localhost ~]# systemctl reload httpd[root@localhost ~]# echo test > /var/www/html/test.html[root@localhost ~]# curl 192.168.17.138test2. 修改默认发布目录
a. 生成新的默认发布目录和默认发布文件
[root@localhost ~]# mkdir /var/web/html -p[root@localhost ~]# echo 123 > /var/web/html/index.html修改主配置文件:
DocumentRoot "/var/web/html" <Directory "/var/web"> AllowOverride None # Allow open access: Require all granted </Directory>[root@localhost ~]# systemctl restart httpd测试:
[root@localhost ~]# curl 192.168.17.138123四、修改服务端口
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf将文件中的Listen 80修改为:
Listen 8080测试:
[root@localhost ~]# netstat -antlupe | grep httpd五、目录的访问控制
1. 基于 IP 的访问控制
需求:访问http://192.168.17.138/test1时,只允许192.168.17.131主机访问,其他主机拒绝。
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf<Directory "/var/www/html/test1"> Order deny,allow Deny from ALL Allow from 192.168.17.131 </Directory>[root@localhost ~]# systemctl restart httpd2. 基于用户认证的访问控制
需求:访问http://192.168.17.138/test2目录时,需要通过用户名密码认证。
第一步:创建密码文件
[root@localhost ~]# htpasswd -cm /etc/httpd/.htpasswd 123New password: Re-type new password: Adding passwordforuser123⚠️ 注意:如果文件已存在,不要用
-c参数(-c会创建新文件并覆盖旧文件)。
第二步:配置认证
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf<Directory "/var/www/html/test2"> AuthUserFile /etc/httpd/.htpasswd AuthName "Please input username and passwd" AuthType basic Require valid-user </Directory>六、Apache 虚拟主机
需求:发布三个站点:www.qwe.com、news.qwe.com、bbs.qwe.com
1. 建立各个站点的默认发布目录和文件
[root@bogon /]# mkdir /var/www/virtual/qwe.com/news -p[root@bogon /]# mkdir /var/www/virtual/qwe.com/bbs[root@bogon /]# mkdir /var/www/virtual/qwe.com/www[root@bogon /]# echo hello1 > /var/www/virtual/qwe.com/news/index.html[root@bogon /]# echo hello2 > /var/www/virtual/qwe.com/bbs/index.html[root@bogon /]# echo hello3 > /var/www/virtual/qwe.com/www/index.html2. 配置虚拟主机
[root@bogon ~]# vim /etc/httpd/conf.d/vhosts.conf<VirtualHost _default_:80> DocumentRoot /var/www/html CustomLog /etc/httpd/logs/default.log combined </VirtualHost> <VirtualHost *:80> ServerName www.qwe.com DocumentRoot /var/www/virtual/qwe.com/www CustomLog /etc/httpd/logs/www.log combined </VirtualHost> <VirtualHost *:80> ServerName news.qwe.com DocumentRoot /var/www/virtual/qwe.com/news CustomLog /etc/httpd/logs/news.log combined </VirtualHost> <VirtualHost *:80> ServerName bbs.qwe.com DocumentRoot /var/www/virtual/qwe.com/bbs CustomLog /etc/httpd/logs/bbs.log combined </VirtualHost>[root@bogon bbs]# systemctl restart httpd测试:在客户端配置 hosts 文件
root@qwe-virtual-machine:~# vim /etc/hosts添加以下内容:
192.168.17.138 www.qwe.com news.qwe.com bbs.qwe.com图片说明:
七、HTTPS 配置
1. 生成自签名证书
[root@bogon ~]# mkdir /etc/httpd/certs[root@bogon ~]# openssl req -newkey rsa:2048 -nodes -sha256 \-keyout/etc/httpd/certs/qw.org.key\-x509-days365\-out/etc/httpd/certs/qw.org.crt2. 安装加密套件
[root@bogon ~]# dnf install mod_ssl3. 配置加密虚拟主机
[root@bogon ~]# mkdir /var/www/virtual/qwe.com/login[root@bogon ~]# echo jiami > /var/www/virtual/qwe.com/login/index.html[root@bogon ~]# vim /etc/httpd/conf.d/vhosts.conf# HTTP -> HTTPS 强制跳转 <VirtualHost *:80> ServerName login.qwe.com RewriteEngine On RewriteRule ^/(.*)$ https://login.qwe.com/$1 [R=301] </VirtualHost> # HTTPS 虚拟主机 <VirtualHost *:443> ServerName login.qwe.com DocumentRoot /var/www/virtual/qwe.com/login CustomLog /ert/httpd/logs/login.log combined SSLEngine on SSLCertificateFile /etc/httpd/certs/qw.org.crt SSLCertificateKeyFile /etc/httpd/certs/qw.org.key </VirtualHost>图片说明:
测试: