一、Apache 安装[rootlocalhost ~]# dnf install httpd -y[rootlocalhost ~]# 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测试[rootlocalhost ~]# curl 192.168.17.138192.168.17.138b. 默认发布文件的修改修改主配置文件调整DirectoryIndex的优先级[rootlocalhost ~]# vim /etc/httpd/conf/httpd.confIfModule dir_moduleDirectoryIndex test.html index.html/IfModule[rootlocalhost ~]# systemctl reload httpd[rootlocalhost ~]# echo test /var/www/html/test.html[rootlocalhost ~]# curl 192.168.17.138test2. 修改默认发布目录a. 生成新的默认发布目录和默认发布文件[rootlocalhost ~]# mkdir /var/web/html -p[rootlocalhost ~]# echo 123 /var/web/html/index.html修改主配置文件DocumentRoot /var/web/html Directory /var/web AllowOverride None # Allow open access: Require all granted /Directory[rootlocalhost ~]# systemctl restart httpd测试[rootlocalhost ~]# curl 192.168.17.138123四、修改服务端口[rootlocalhost ~]# vim /etc/httpd/conf/httpd.conf将文件中的Listen 80修改为Listen 8080测试[rootlocalhost ~]# netstat -antlupe | grep httpd五、目录的访问控制1. 基于 IP 的访问控制需求访问http://192.168.17.138/test1时只允许192.168.17.131主机访问其他主机拒绝。[rootlocalhost ~]# vim /etc/httpd/conf/httpd.confDirectory /var/www/html/test1 Order deny,allow Deny from ALL Allow from 192.168.17.131 /Directory[rootlocalhost ~]# systemctl restart httpd2. 基于用户认证的访问控制需求访问http://192.168.17.138/test2目录时需要通过用户名密码认证。第一步创建密码文件[rootlocalhost ~]# htpasswd -cm /etc/httpd/.htpasswd 123New password: Re-type new password: Adding passwordforuser123⚠️ 注意如果文件已存在不要用-c参数-c会创建新文件并覆盖旧文件。第二步配置认证[rootlocalhost ~]# vim /etc/httpd/conf/httpd.confDirectory /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.com1. 建立各个站点的默认发布目录和文件[rootbogon /]# mkdir /var/www/virtual/qwe.com/news -p[rootbogon /]# mkdir /var/www/virtual/qwe.com/bbs[rootbogon /]# mkdir /var/www/virtual/qwe.com/www[rootbogon /]# echo hello1 /var/www/virtual/qwe.com/news/index.html[rootbogon /]# echo hello2 /var/www/virtual/qwe.com/bbs/index.html[rootbogon /]# echo hello3 /var/www/virtual/qwe.com/www/index.html2. 配置虚拟主机[rootbogon ~]# vim /etc/httpd/conf.d/vhosts.confVirtualHost _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[rootbogon bbs]# systemctl restart httpd测试在客户端配置 hosts 文件rootqwe-virtual-machine:~# vim /etc/hosts添加以下内容192.168.17.138 www.qwe.com news.qwe.com bbs.qwe.com图片说明七、HTTPS 配置1. 生成自签名证书[rootbogon ~]# mkdir /etc/httpd/certs[rootbogon ~]# openssl req -newkey rsa:2048 -nodes -sha256 \-keyout/etc/httpd/certs/qw.org.key\-x509-days365\-out/etc/httpd/certs/qw.org.crt2. 安装加密套件[rootbogon ~]# dnf install mod_ssl3. 配置加密虚拟主机[rootbogon ~]# mkdir /var/www/virtual/qwe.com/login[rootbogon ~]# echo jiami /var/www/virtual/qwe.com/login/index.html[rootbogon ~]# vim /etc/httpd/conf.d/vhosts.conf# HTTP - HTTPS 强制跳转 VirtualHost *:80 ServerName login.qwe.com RewriteEngine On RewriteRule ^/(.*)$ https://login.qwe.com/$1 [R301] /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图片说明测试