一、Apache的作用
1、在web被访问时通常使用http://的方式
http:// ##超文本传输协议
2、http:// 超文本传输协议提供软件
Apache
nginx
stgw
jfe
Tengine
3、实验一
dnf install httpd.x86_64 -y
firewall-cmd --permanent --add-service=http ##在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https ##在火墙中永久开启https访问
firewall-cmd --reload ##刷新火墙使设定生效
vim /var/www/html/index.html
二、Apache的安装
1、安装apache
dnf install httpd.x86_64 -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的基本配置
重点是指向:DirectoryIndex
1、Apache端口修改
vim /etc/httpd/conf/httpd.conf
Listen 8080 //编辑配置文件,修改端口为8080
firewall-cmd --permanent --add-port=8080/tcp //防火墙加8080端口
firewall-cmd --reload //重新加载防火墙
systemctl restart httpd //重启服务
netstat -antlupe | grep httpd //查看端口有没有加进去
http://172.25.254.100:8080
2、默认发布文件
vim /var/www/html/westos.html //编辑另外一个页面
内容
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html //编辑它的默认指向页面
systemctl restart httpd //重启服务
http://172.25.254.100:8080
3、默认发布目录
mkdir /westos_apache
vim /westos_apache/index.html
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?' //修改安全上下文和httpd一致
restorecon -RvvF /westos_apache/ //重新加载安全上下文
vim /etc/httpd/conf/httpd.conf //修改配置文件
DocumentRoot "/westos_apache"
<Directory "/westos_apache">
Require all granted
</Directory> //修改配置文件
systemctl restart httpd
http://172.25.254.170:8080
六、Apache的访问控制
实验素材:
mkdir /var/www/html/westos //制作一个目录
vim /var/www/html/westos/index.html //编辑
<h1>westosdir's page</h1> //index.html的内容
访问:
http://172.25.254.241/westos //就能看到刚才index里面的内容
1、基于客户端ip的访问控制
mkdir /var/www/html/westos //创建一个目录
vim /etc/httpd/conf/httpd.conf //修改配置文件
DocumentRoot "/var/www/html" //修改默认的加载路径
(1)ip白名单
<Directory "/var/www/html/westos"> //限制的目录
Order Deny,Allow
Allow from 172.25.254.41 //允许一个,拒绝所有
Deny from All
</Directory>
(2)ip黑名单
<Directory "/var/www/html/westos">
Order Allow,Deny
Allow from All //允许所有,拒绝一个
Deny from 172.25.254.41
</Directory>
2、基于用户认证
(1)加密文件的生成
mkdir /var/www/html/westos //创建加密的文件
vim /var/www/html/westos/index.html //编辑加载的页面
在这个路径下面/etc/httpd/:
htpasswd -cm .htpasswd admin //创建用户,并生成其加密的文件
(2)服务的配置
vim /etc/httpd/conf/httpd.conf //编辑配置文件
180行:
DirectoryIndex index.html //默认加载的文件
120行:
DocumentRoot "/var/www/html" //路径
<Directory "/var/www/html/westos"> //加密的目录
AuthUserfile /etc/httpd/.htpasswd //指定认证文件
AuthName "Please input your name and password" //认证提示语
AuthType basic //认证类型
Require user admin //允许通过的认证用户 2选1
Require valid-user //允许所有用户通过认证 2选1
</Directory>
systemctl restart httpd //重启服务
(3)测试
http://172.25.254.241/westos/
可以看到之前在此路径下编辑的文件,但是需要账户密码登陆,若不需要,则需要ctrl+shift+delete,清除所有的缓存信息,重新进入。
(4) 注意
htpasswd -cm /etc/httpd/htpasswdfile admin //生成认证文件
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
七、Apache的虚拟主机
1、 创建文件目录(服务器)
mkdir -p /var/www/vhost/westos.org/{news,music,hkk}
echo news.westos.org> /var/www/vhost/westos.org/news/index.html
echo music.westos.org> /var/www/vhost/westos.org/music/index.html
echo hkk.westos.org> /var/www/vhost/westos.org/hkk/index.html
2、编辑配置文件(服务器)
vim /etc/httpd/conf.d/vhost.conf //编辑虚拟主机的配置文件
<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName music.westos.org
DocumentRoot /var/www/vhost/westos.org/music
CustomLog logs/music.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.org
DocumentRoot /var/www/vhost/news.org/news
CustomLog logs/news.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName hkk.westos.org
DocumentRoot /var/www/vhost/hkk.org/hkk
CustomLog logs/hkk.log combined
</VirtualHost>
3、手动解析域名
浏览器在哪里就在哪里编辑
vim /etc/hosts
172.25.254.241 westos.westos.org news.westos.org music.westos.org hkk.westos.org //服务器ip
4、授权
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/">
AllowOverride None
# Allow open access:
Require all granted
</Directory> //给目录授权
5、重启并测试
systemctl restart httpd
westos.westos.org
news.westos.org
music.westos.org
hkk.westos.org //分别访问,用浏览器
八、Apache的语言支持
1、php相关
(1)编辑
vim /var/www/html/index.php
<?php
phpinfo();
?>
(2)安装
dnf install php -y
systemctl restart httpd
(3)访问
http://172.25.254.241/index.php
2、cgi相关
(1)文件相关
mkdir /var/www/html/cgidir //路径目录
vim /var/www/html/cgidir/index.cgi //编辑cgi代码
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print `date`;
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgidir/index.cgi(/.*)?' //更改安全安上下文信息和httpd保持一致
restorecon /var/www/html/cgidir/index.cgi
chmod +x /var/www/html/cgidir/index.cgi //给文件加执行权限
(2)配置文件相关
vim /etc/httpd/conf.d/vhost.conf //编辑配置信息(虚拟机)
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
(3)测试
firefox http://172.25.254.241/cgidir/index.cgi
3、wsgi
(1)书写wsgi的测试文件
vim /var/www/html/wsgi/index.wsgi //内容:
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']
(2)安装
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>
(3)测试
http://wsgi.westos.org/ //解析
九、Apache的加密访问
1、安装加密插件
dnf install mod_ssl -y
2、生成证书
(1)command 1
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 #生成证书
x509 证书格式
-req 请求
-in 加载签证名称
-signkey /etc/pki/tls/private/www.westos.com.key
*/
(2)command 2
mkdir /etc/httpd/tls
openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/westos.org.key -x509 -days 365 -out /etc/httpd/tls/westos.org.crt
3、给login.westos.org加上https锁
vim /etc/httpd/conf.d/vhost.conf //配置文件编辑
<VirtualHost *:443> //内容!
ServerName login.westos.org
DocumentRoot "/var/www/vhost/westos.org/login"
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/tls/westos.org.crt
SSLCertificateKeyFile /etc/httpd/tls/westos.org.key
</VirtualHost>
4、给login.westos.org各种输入格式加上https
无论怎么输入login.westos.org都会有https保护
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName login.westos.org
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
systemctl restart httpd
5、给浏览器所在主机加上手动域名解析
vim /etc/hosts
login.westos.org
6、给其他服务的各种输入加上https保护
(1)加上加密锁
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:443>
ServerName hkk.westos.org
DocumentRoot "/var/www/vhost/westos.org/hkk"
CustomLog logs/hkk.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/tls/westos.org.crt
SSLCertificateKeyFile /etc/httpd/tls/westos.org.key
</VirtualHost>
(2)加上配置信息
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName hkk.westos.org
DocumentRoot /var/www/vhost/westos.org/hkk
CustomLog logs/hkk.log combined
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
7、测试这个时候不论怎么输入hkk.westos.org和login.westos.org,都会有https的保护
这个时候不论怎么输入hkk.westos.org和login.westos.org,都会有https的保护
这个时候不论怎么输入hkk.westos.org和login.westos.org,都会有https的保护
8、代码含义
^(/.*)$ ##客户地址栏中输入的地址
%{HTTP_HOST} ##客户主机
$1 ##RewriteRule后面跟的第一串字符的值
十、Squid+Apache
1、squid 正向代理
(1)实验环境
1、有网络的代理主机 172.25.254.41
2、无网络的虚拟机 172.25.254.241
(2)实验步骤
1、代理主机
dnf install squid -y //安装代理服务
vim /etc/squid/squid.conf //编辑配置文件
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256 //内容
systemctl restart squid //重启代理服务
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
firewall-cmd --list-all //防火墙相关
2、虚拟机
shell:
dnf install firefox -y
ping baidu.com
vim /etc/sysconfig/network-scripts/ifcfg-ens3
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=172.25.254.241
PREFIX=24
DEFROUTE=yes
NAME=ens3
DEVICE=ens3
ONBOOT=yes
GATEWAY=172.25.254.250
DNS=114.114.114.114
浏览器:
在firefox中右上角三道杠 perference,network-setting manual-proxy 勾选use proxy for all 然后输入主机的ip,和端口3128。
3、测试
在虚拟机中 ping www.baidu.com 不通 在浏览器中访问www.baidu.com可以
2、squid反向代理
(1)实验环境
172.25.254.241 ##Apache服务器
172.25.254.41 ##squid,没有数据负责缓存
(2)实验步骤
1、172.25.254.41
vim /etc/squid/squid.conf
http_port 80 vhost vport //vhost 支持虚拟域名 vport 支持虚拟端口
cache_peer 172.25.254.241 parent 80 0 proxy-only
//#当172.25.254.41的80端口被访问会从172.25.254.241的80端口缓存数据
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
systemctl restart squid
2、172.25.254.241
dnf install httpd.x86_64 -y //装http服务
vim /var/www/html/index.html //编辑默认发布页内容
172.25.254.241
systemctl enable --now httpd //启用httpd服务
systemctl start firewalld.service //启用防火墙
firewall-cmd --permanent --add-service=http //防火墙添加http
firewall-cmd --reload //防火墙重新加载
3、测试
在代理主机,访问172.25.254.241和172.25.254.41都可以看到172.25.254.241上面的默认发布页。
当需要在客户端访问代理端时,需要在浏览器把代理的端口改成80。