一.Apache的作用
在web被访问时通常使用http://的方式
http:// | 超文本传输协议 |
---|
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
二.Apache的安装及启用
更改Apache的服务端口
关闭防火墙
访问服务器的8080端口
启用
systemctl enable --now httpd | 开启服务并设定服务位开机启动 |
---|---|
firewall-cmd --list-all | 查看火墙信息 |
firewall-cmd --permanent --add-service=http | 在火墙中永久开启http访问 |
firewall-cmd --permanent --add-service=https | 在火墙中永久开启https访问 |
firewall-cmd --reload | 刷新火墙使设定生效 |
三.Apache的基本信息
服务名称 | httpd |
---|---|
配置文件 | /etc/httpd/conf/httpd.conf ##主配置文件 |
配置文件 | /etc/httpd/conf.d/*.conf ##子配置文件 |
默认发布目录 | /var/www/html |
默认发布文件 | index.html |
默认端口 | 80 #http 443 https |
用户 | apache |
日志 | /etc/httpd/logs |
四.Apache的基本配置
Apache端口修改
1.vim /etc/httpd/conf/httpd.conf ##编辑主配置文件修改端口号为1111 (默认80)
2.如果打开了selinux ,需要查看系统是否给httpd服务开放的有1111端口,如果没有需要手动添加
默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
默认发布目录
vim /etc/httpd/conf/httpd.conf
DocumentRoot “/westos/html”
<Directory “/westos/html”>
Require all granted
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’
restorecon -RvvF /westos/
systemctl restart httpd
firefox http://192.168.0.11
注:Options Indexes 和 Options Indexes FollowSymLinks 都可以显示目录的链接,但是前一个参数会导致关于这个文件下的访问规则不生效,后一个参数则不会产生这个效果
五.Apache的访问控制
1.基于客户端ip的访问控制
ip白名单
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from 192.168.0.10
Deny from All
172.25.254.7的访问请求被拒绝:
ip黑名单
<Directory “/var/www/html/westos”>
Order Allow,Deny
Allow from All
Deny from 192.168.0.10
2.基于用户的认证
用命令生成认证文件
htpasswd -cm /etc/httpd/httpauth westos
修改配置文件 vim /etc/httpd/conf/httpd.conf
六.Apache的虚拟主机
创建虚拟用户的发布目录和发布文件内容;添加地址解析,即可通过域名访问:
更改虚拟主机的配置文件:
ServerName | 虚拟主机域名 |
---|---|
DocumentRoot | 指定默认发布目录 |
CustomLog | 指定日志存放位置 |
编辑虚拟主机的配置文件
vim /etc/httpd/conf.d/Vhost.conf
重启服务:
测试:
七.Apache的语言支持
PHP语言
1.dnf install php -y ##安装PHP语言所用到的解释器
2.vim /var/www/html/index.php
cgi语言
1.dnf install perl-CGI.noarch -y #下载cgi解释器
2.创建cgi文件目录:mkdir /var/www/html/cgi
3.vim /var/www/html/cgi/index.cgi 编辑文件
在http.conf中,定义的pl运行位置是/var/www/cgi-bin/,现在运行的位置变化,重新说明:
八.Apache的加密访问
1.安装加密插件:dnf install mod_ssl.x86_64 -y
2.生成私钥和证书签名文件
3.生成证书
x509 | 证书格式 |
---|---|
-req | 请求 |
-in | 加载签证名称 |
-signkey | /etc/pki/tls/private/www.westos.com.key私钥 |
4.vim /etc/httpd/conf.d/ssl.conf 编辑配置文件
编辑配置文件/etc/httpd/conf.d/vhost.conf:
普通访问也会跳转至加密访问:
自拟发布文件内容
重启服务
九.squid 正向代理
实验环境:
单网卡主机设定ip不能上网
双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网
1.dnf install squid.x86_64 下载软件包
2.vim /etc/squid/squid.conf 编辑主配置文件
在单网卡中选择
NetWork Proxy
172.25.254.7 3128
没有网络也可以访问互联网
十.squid反向代理
实验环境:
172.25.254.20 ##Apache服务器
172.25.254.30 ##squid,没有数据负责缓存
1.修改配置文件
vim /etc/squid/squid.conf
http_port 80 vhost vport |vhost 支持虚拟域名 vport 支持虚拟端口
#当172.25.254.30的80端口被访问会从172.25.254.20的80端口缓存数据
cache_peer 172.25.254.20 parent 80 0 proxy-only
测试:
firefox http:/172.25.254.30
访问看到的是172.25.254.20上的数据