一、Apache的作用
1、在web被访问时通常使用http://的方式
http:// ##超文本传输协议
2、http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
二、Apache的安装
dnf install httpd -y
三、Apache的启用
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的基本配置
1、Apache端口修改
修改操作:
vim /etc/httpd/conf/httpd.conf
#修改Listen 8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
修改前:
创建默认发布文件,并在浏览器查看172.25.254.172
修改端口,在浏览器查看172.25.254.172
要想正确查看到发布文件,需要配置火墙,开启8080端口号
在浏览去输入172.25.254.172:8080可以正常访问了
做完实验后把配置文件中端口号还原为80
2、默认发布文件
在配置文件中修改默认发布文件
vim /etc/httpd/conf/httpd.conf
#修改DirectoryIndex test.html index.html
## 若test.html默认发布文件没有,就顺位给index.html
systemctl restart httpd
创建test.html
设置test.html为默认发布文件
在浏览器访问
删除test.html,index.html顺位成为默认发布文件
3、默认发布目录
mkdir /www/westos -p #创建新的默认发布目录
vim /www/westos/index.html #创建默认发布文件
cat /www/westos/index.html
it is /www/westos/index.html #文件内容
vim /etc/httpd/conf/httpd.conf
#修改:
DocumentRoot "/www/westos"
<Directory "/www/westos">
Require all granted
</Directory>
systemctl restart httpd
selinux开启,要把新的默认发布目录安全上下文设置好
实验后还原默认发布目录
六、Apache的访问控制
实验素材:
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
cat /var/www/html/westos/index.html
# it is /var/www/html/westos/index.html
1、基于客户端ip的访问控制
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Order Deny,Allow或者Allow,Deny
Allow from 172.25.254.50
Deny from All
</Directory>
要看Order 后第一个是Deny还是Allow,第一个是Allow,就是黑名单;第一个是Deny,就是白名单
a、ip黑名单
先看allow,再看deny,允许了所有之后,拒绝了172.25.254.72。(只拒绝172.25.254.72访问)
b、ip白名单
先看deny,再看allow,拒绝了所有之后,允许了172.25.254.72。(只允许172.25.254.72访问)
2、基于用户认证的访问控制
#2.基于用户认证#
htpasswd -cm /etc/httpd/.htpasswd admin ##生成认证文件
htpasswd -m /etc/httpd/.htpasswd dyh ##生成认证文件
#认证文件里已经有内容时不要加-c,否则会覆盖原有文件内容
vim /etc/httpd/conf/httpd.conf
#修改:
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/.htpasswd ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许指定用户admin通过认证 (2选1)
Require valid-user ##允许所有用户通过认证 (2选1)
</Directory>
systemctl restart httpd
测试:访问172.25.254.72/westos
七、Apache的虚拟主机
实验前设置:
在真机的解析文件给172.25.254.172 设置三个域名
通过三个域名都能访问172.25.254.172
现在想让www.westos.org域名访问原有主页,其他两个域名分别访问其他主页
mkdir -p /var/www/virtual/westos.org/{linux,luck}
echo "linux.westos.org" >/var/www/westos.com/wenku/index.html
echo "luck.westos.org" > /var/www/westos.com/news/index.html
vim /etc/httpd/conf.d/vhost.conf ###子配置文件必须在子配置目录conf.d里面
<VirtualHost_default_:80>
DocumentRoot "/var/www/html" ##默认发布目录
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName linux.westos.com
DocumentRoot "/var/www/virtual/westos.org/linux" ##当访问linux.westos.org网址时的默认文件目录
CustomLog logs/linux.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName luck.westos.org
DocumentRoot "/var/www/virtual/westos.org/luck" ###当访问luck.westos.org网址时的默认文件目录
CustomLog logs/luck.log combined
</VirtualHost>
systemctl restart httpd
访问不同的域名,显示不同的页面
八、Apache的语言支持
1、php
-
httpd服务默认发布的index.html文件,即超文本标记语言,是Apache默认支持的语言;
-
当我们在虚拟机westosa共享位置 /var/www/html目录下编写一个index.php文件(文件内容为展示php测试页面)在浏览器所在真实主机访问该index.php文件时,页面无显示,即index.php文件中的代码不执行,这表明Apache不支持php语言;
-
在服务器中安装php软件后,在浏览器所在真实主机中再次访问该index.php文件,出现了php测试页面,index.php文件中的代码成功执行,此时Apache支持php语言
dnf install php -y
vim /var/www/html/index.php
<?php phpinfo(); ?>systemctl restart httpd
用测试主机访问服务器172.25.254.172/index.php
2、perl语言
cgi通用网关接口中主要使用的是perl语言,我们在虚拟机westosa共享位置 /var/www/html目录下编写index.cgi文件(文件内容为执行date命令显示系统当前时间)
cd /var/www/html/
vim index.cgi
cat index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print “hello,world.”;
print `date`;
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
chmod +x index.cgi ###给index.cgi文件赋予可执行权限
systemctl restart httpd
dnf install manual -y下载Apache的帮助手册
有关于CGI文件内容的提示
关于Apache配置文件里的修改提示
查询cgi-bin的安全上下文,并将index.cgi的安全上下文修改
测试:访问172.25.254.172/index.cgi
3、python语言
wsgi与cgi的功能类似,其主要使用的是python语言,我们在虚拟机westosa共享位置 /var/www/html目录下建立wsgi目录并书写wsgi的测试文件index.wsgi
dnf install python3-mod_wsgi
vim /var/www/html/index.wsgi
#编辑内容:
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']
跟cgi一样设置安全上下文
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/index.wsgi'
restorecon -RvvF /var/www/html/index.wsgi
0
vim /etc/httpd/conf.d/vhost
编辑内容:
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/index.wsgi
</VirtualHost>
systemctl restart httpd
需要安装包
在主机解析文件/etc/hosts 中添加解析域名wsgi.westos.org
在浏览器访问wsgi.westos.org
九、Apache的加密访问
1、安装https加密插件
dnf install mod_ssl.x86_64 -y
安装后出现ssl文件
浏览器访问172.25.254.172
此时安全证书不是我们自己的
2、生成自己的安全证书
openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/westos.org.key -x509 -days 365 -out /etc/httpd/westos.org.crt
编辑ssl.conf配置文件,将证书和key文件路径该为自己生成的证书和key的路径
vim /etc/httpd/conf.d/ssl.conf
注释这一行#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
添加新一行SSLCertificateFile /etc/httpd/westos.org.crt
注释这一行#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
添加新一行SSLCertificateKeyFile /etc/httpd/westos.org.key
systemctl restart httpd
测试:再次访问172.25.254.172,重新生成证书,查看信息
3、设置特定网页生成证书(比如一些需要登录的页面,用户与网页会进行交互,需要安全加密)强制将网页http转换为https
mkdir -p /var/www/virtual/westos.org/login
echo login.westos.org > /var/www/virtual/westos.org/login/index.html
vim /etc/httpd/conf.d/vhosts.conf
#添加内容:
<VirtualHost *:80>
ServerName login.westos.org
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
#
^(/.*)$ 是客户地址栏中输入的地址
%{HTTP_HOST}是客户主机
$1 是RewriteRule后跟的第一串字符即^(/.*)$即login.westos.org
整句的意思是讲:启动rewrite模块,将访问的域名请求,url地址内容不变,将http://变成https:
#
</VirtualHost>
<VirtualHost *:443>
ServerName login.westos.org
DocumentRoot "/var/www/westos.com/login"
CustomLog logs/news.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/westos.org.crt
SSLCertificateKeyFile /etc/httpd/westos.org.key
</VirtualHost>
再测试主机上配置域名解析
测试:访问login.westos.org,地址会转换为https开头
或者访问http://login.westos.org,地址也会转换为https开头
十、Apache+Squid代理
1、squid正向代理
实验需要一台能上网的服务器主机,一台能通过主机上午的代理主机westosa,一台不能上网的客户主机westosb
- 正向代理
正向代理就是:客户端虽然无法直接直接访问某台服务器,但可以通过一台可以访问该服务器的代理主机去访问。
1、服务器设置
设置能上网的服务器为路由器
firewall-cmd --add-masquerade
对代理主机进行设定连接到服务器主机的网关,并编辑DNS解析文件,使其可以上网
2、代理端设置
代理端关闭httpd服务
在代理端安装squid服务
dnf install squid -y
systemctl enable --now squid.service
firewall-cmd --permanent --add-service=squid
firewall-cmd --permanent --add-service=httpd
squid配置文件设置
vim /etc/squid/squid.conf
正向代理端虽然没有httpd服务,但是要允许客户端http访问
缓存文件设置,代理端从服务器上访问的信息保存在这里,再由客户端访问
重启服务后
在客户端上打开浏览器设定代理访问地址和端口
在客户端打开的浏览器可以看到百度,其实是100访问的网络,存放在缓存文件中,再由客户端通过squid服务访问到。实际客户端并不能上网
2、squid反向代理
先还原正向代理在浏览器中的设置
- 反向代理原理:把westosb作为代理,westosa是主服务器,用户访问服务器端时,显示的是代理端中的内容
1、代理端:
dnf install httpd -y
systemctl rnable --now httpd
cd /var/www/html
vim index.html
firewall-cmd --permanent --add-sevice=http
firewall-cmd --reload
#编辑:hello dyh
2、服务端:
vim /etc/squid/squid.conf
#修改内容:
http_port 80 vhost vport
cache_peer 172.25.254.200 parent 80 0 proxy-only
systemctl restart squid.service
systemctl-cmd --permanent --add-service=http
firewall-cmd --reload
测试:主机访问服务器ip 172.25.254.172
看到的是172.25.254.200代理端的地址默认发布文件内容