目录
### 1.Apache的作用 ###
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache 百度 ##nginx 网易、火狐、爱奇艺、360 ##stgw 腾讯 ##jfe 京东 ##Tengine 阿里、新浪、字节跳动
curl -I baidu.com
:显示网页的头信息
### 2.Apache的安装 ###
dnf search httpd
dnf install httpd.x86_64 -y
### 3.Apache的启用 ###
systemctl enable --now httpd:开启httpd服务并设定开机自启动
firewall-cmd --permanent --add-service=http:在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https:在火墙中永久开启https访问
firewall-cmd --reload:刷新火墙使设定生效
firewall-cmd --list-all:查看火墙信息
### 4.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
### 5.Apache的基本配置 ###
1.Apache端口修改
vim /etc/httpd/conf/httpd.conf
Listen 8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
http://172.25.254.125:8080
2.默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
3.默认发布目录
mkdir /var/www/westos
mv /var/www/westos /var
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/westos"
Require all granted
systemctl restart httpd
firefox http://172.25.254.125
### 6.Apache的访问控制 ###
实验素材#
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
westosdir's page
firefox http://172.25.254.125/westos
1.基于客户端ip的访问控制
ip白名单
Order Deny,Allow
Allow from 172.25.254.25
Deny from All
ip黑名单
Order Allow,Deny
Allow from All
Deny from 172.25.254.25
2.基于用户认证
vim /etc/httpd/conf/httpd.conf
AuthUserfile /etc/httpd/.htauthfile ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许通过的认证用户 2选1
Require valid-user ##允许所有用户通过认证 2选1
htpasswd -cm /etc/httpd/.htauthfile admin ##生成认证文件
注意:
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
### 7.Apache的虚拟主机 ###
vim /etc/httpd/conf/httpd.conf:还原文件
mkdir -p /var/www/virtual/westos.org/{linux,luck}:创建存放目录
vim /var/www/westos.org/linux/index.html:创建linux.westos.org主页
vim /var/www/westos.org/luck/index.html:创建luck.westos.org主页
vim /etc/httpd/conf.d/vhosts.conf:创建并编辑虚拟主机配置文件<VirtualHost _default_:80>
DocumentRoot /var/www/html
Customlog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName linux.westos.org
DocumentRoot /var/www/westos.org/linux
Customlog logs/linux.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName luck.westos.org
DocumentRoot /var/www/westos.org/luck
Customlog logs/luck.log combined
</VirtualHost>systemctl restart httpd:重启服务
vim /etc/hosts:设定客户端解析(在浏览器所在的主机中添加)
172.25.254.125 www.westos.org linux.westos.org luck.westos.org
### 8.Apache的语言支持 ###
php
vim /var/www/html/index.php
<?php
phpinfo();
?>
dnf install php -y
systemctl restart httpd
firefox http://172.25.254.125/index.php
cgi
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/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/cgidir">
Options ExecCGI
AddHandler cgi-script .cgi
Directoryindex index.cgi
</Directory>
http://172.25.254.125/cgidir/index.cgi
wsgi
vim /var/www/html/wsgi/index.wsgi
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos']
dnf install python3-mod_wsgi
systemctl restart httpd
vim /etc/httpd/conf.d/vhost
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
### 9.Apache的加密访问 ###
安装加密插件
dnf install mod_ssl -y
生成证书
x509 证书格式
-req 请求
-in 加载签证名称
-signkey /etc/pki/tls/private/www.westos.com.key
openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/westos.org.key -x509 -days 365 -out /etc/httpd/westos.org.crt
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
systemctl restart httpd ^(/.*)$ ##客户地址栏中输入的地址
%{HTTP_HOST} ##客户主机
$1 ##RewriteRule后面跟的第一串字符的值
###10.http转https###
加密
###11.squid代理###
正向代理
当被缓存的页面被第二次访问的时候,浏览器将直接从本地代理服务器那里获取请求数据而不再向原web站点请求数据。这样就节省了宝贵的网络带宽,而且提高了访问速度。
反向代理
如果互联网用户请求的页面在代理服务器上有缓冲的话,代理服务器直接将缓冲内容发送给用户。如果没有缓冲则先向WEB服务器发出请求,取回数据,本地缓存后再发给用户。这种方式通过降低了WEB服务器的请求数从而降低了WEB服务器的负载。
实验准备(3台主机)
一个可以访问到Apache页面的主机
一个有squid服务,但没有httpd服务的主机
一台需要从node1获取数据的主机