一、Apache的作用
在web被访问时通常使用http://的方式进行访问
http:超文本传输协议
超文本传输协议所需要的软件:
- Apache
- nginx
- stgw
- jfe
- Tengine
二、Apache安装
安装命令:dnf install http.x86_64 -y
三、Apache的启动
目的 | 命令 |
---|---|
设置httpd服务开机自动启动 | systemctl enable --now httpd |
查看防火墙信息 | firewall-cmd --list-all |
在火墙中永久开启http访问 | firewall-cmdpermanent --add-service=http |
在火墙中永久开启https访问 | firewall-cmd --permanent --add-service=https |
使防火墙设定生效 | firewall-cmd --reload |
四、Apache的基本信息
Apache的服务名称:httpd
配置文件 | 地址 |
---|---|
主配置文件 | /etc/httpd/conf/httpd.conf |
子配置文件 | /etc/httpd/conf.d/*.conf |
http的默认发布目录 | /var/www/html |
默认发布文件 | index.html |
默认端口:
http默认端口为:80
https默认端口为:443
用户名:apache
日志地址:/etc/httpd/logs
五、Apache的基本配置
5.1 Apache端口修改
Apache的端口修改
- vim /etc/httpd/conf/httpd.conf :进入主配置文件
listen 1111 :将端口设置为1111
firewall-cmd --permanent --add-port=1111/tcp:将1111端口添加到防火墙中
firewall-cmd --reload:使防火墙设定生效
semanage port -l | grep http:查找http使用的端口
semanage port -a -t http_port_t -p tcp 1111:添加 1111为http可用端口
systemctl restart httpd 重启httpd服务
5.2 默认文件发布
vim /etc/httpd/conf/httpd.conf :进入主配置文件
DirectoryIndex westos.html index.html :设定westos.html优先发布
systemctl restart httpd:重启服务
5.3 默认目录发布
vim /etc/httpd/conf/httpd.conf
DocumentRoot “/westos/html”
<Directory “/westos/html”>
Require all granted
< /Directory >
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’
restorecon -RvvF /westos/
六、Apache的访问控制
6.1 基于客户端ip的访问控制
ip白名单:
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from 172.25.254.4
Deny From All
< /Directory>
ip黑名单:
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from All
Deny From 172.25.254.4
< /Directory>
6.2 基于用户认证
vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/westos”>
AuthUserFile /etc/httpd/htpasswdfile
AuthName “user and password”
AuthType basic
Require user admin / Require valid-user
< /Directory>
七、Apache的虚拟主机
mkdir -p /var/www/westos.com/{news,bbs,tieba}
mkdir -p /var/www/westos.com/{news,bbs,tieba}/html
echo news.westos.com > /var/www/westos.com/news/html/index.html
echo bbs.westos.com > /var/www/westos.com/bbs/html/index.html
echo tieba.westos.com > /var/www/westos.com/tieba/html/index.html
vim /etc/httpd/conf.d/vhost.conf
< VirtualHost default:80>
DocumentRoot /var/www/html
Customlog logs/default.log combined
< /VirtualHost>
<VirtualHost *:80>
Servername news.westos.com
DocumentRoot /var/www/westos.com/news/html
Customlog logs/news.log combined
< /VirtualHost>
<VirtualHost *:80>
Servername bbs.westos.com
DocumentRoot /var/www/westos.com/bbs/html
Customlog logs/bbs.log combined
< /VirtualHost>
<VirtualHost *:80>
Servername tieba.westos.com
DocumentRoot /var/www/westos.com/tieba/html
Customlog logs/tieba.log combined
< /VirtualHost>
systemctl restart httpd
在/etc/hosts文件中编辑地址索引
查询结果
编写文件完毕后在客户端登陆网页查看结果
八、Apache的语言支持
php:
vim /var/www/html/index.php
dnf install php -y
systemctl restart httpd
cgi:
vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date
;
vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/html/cgi”>
Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
< /Directory>
九、Apache的加密访问
9.1 安装加密插件
dnf install mod_ssl -y
openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048:生成私钥
openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.csr:生成证书签名文件
openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.westos.com.csr -signkey /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.crt :生成证书
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost :80>
Servername login.westos.com
RewriteEngine on
RewriteRule ^(/.)$ https://%{HTTP_HOST}$1
< /VirtualHost>
<VirtualHost *:443>
Servername login.westos.com
DocumentRoot /var/www/westos.com/login/html
Customlog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
< /VirtualHost>
systemctl restart httpd
十、正向代理与反向代理
10.1 squid正向代理
squid和apache服务只能同时开一个
在一台新创建的虚拟机上下载squid
dnf install squid -y
vim /etc/squid/squid.conf
http_access allow all
cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid.service
在Apache主机上选择打开firefox并选择NetWork Proxy
选择正确选项后输入172.25.254.204(squid主机id) 3128(端口号)
10.2 squid反向代理
**vim /etc/squid/squid.conf
http_port 80 vhost vport
cache_peer 172.25.254.104 parent 80 0 proxy-only
systemctl restart squid.service
在172.25.254.104的index.html写入172.25.254.104
访问172.25.254.204时会显示172.25.254.104的index.html内容
在Apache和squid主机外使用一台主机打开firefox输入172.25.254.204:80(squid主机id)会转到172.25.254.104:80(Apache主机id)并显示内容