目录
Apache http协议
1、Apache的作用
在web被访问时通常使用http://的方式
“ http:// ” 超文本传输协议
Apache nginx stgw jfe Tengine
2、Apache的安装启动
dnf install httpd -y
firewall-cmd --permanent --add-service=http #防火墙中永久开启http服务
firewall-cmd --permanent --add-service=https #永久开启https服务
firewall-cmd --reload #刷新火墙使设定生效
systemctl enable --now httpd #开启服务并设定服务位开机启动
vim /var/www/html/index.html #http默认发布文件
3、Apache的基本信息
服务名称: httpd
配置文件: /etc/httpd/conf/httpd.conf ##主配置文件
/etc/httpd/conf.d/*.conf ##子配置文件
默认发布目录: /var/www/html
默认发布文件: index.html (可以添加多个)
默认端口: 80 #http (查看端口等信息:netstat -antlupe | grep http)
443 #https
用户: apache
日志: /etc/httpd/logs
4、Apache的基本配置
1.Apache端口修改
vim /etc/httpd/conf/httpd.conf #配置文件
Listen 80
firewall-cmd --permanent --add-port=80/tcp #火墙永久添加http端口
firewall-cmd --reload
systemctl restart httpd
http://172.25.254.120:80
2.默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html #westos.html文件在前 默认优先访问
systemctl restart httpd
3.默认发布目录
mkdir /westos_apache
vim /westos_apache/index.html[root@westosa ~]# ls -Zd /var/www/html/ #查看默认发布目录的安全上下文
system_u:object_r:httpd_sys_content_t:s0 /var/www/html/semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?' #更改安全上下文
restorecon -RvvF /westos_apache/ #刷新并详细显过程vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos_apache" #更改默认发布目录为/westos_apache,默认发布该文件夹中的index.html文件
<Directory "/westos_apache">
Require all granted #允许所有人登录 (denied不允许登录)
</Directory>systemctl restart httpd #重启服务
5、Apache的访问控制
1.基于客户端ip的访问控制
ip白名单#
<Directory "/var/www/html">
Order Deny,Allow 先Deny,再Allow
Allow from 172.25.254.20 允许20号主机访问
Deny from All 所有人都不允许
</Directory>
ip黑名单#
<Directory "/var/www/html">
Order Allow,Deny 先Allow再Deny
Allow from All 允许所有人访问
Deny from 172.25.254.20 20号不允许
</Directory>
2.基于用户认证
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/htpasswdfile ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin1 admin2 ##允许通过的认证用户
Require valid-user ##允许所有用户通过认证(指定用户和所有用户2选1)
</Directory>htpasswd -cm /etc/httpd/htpasswdfile admin ##生成认证文件
注意:
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
密码错误报错如下:
6、Apache的虚拟主机
mkdir -p /var/www/vhost/westos.org/{news,wenku}
echo "wenku 123" > /var/www/vhost/westos.org/wenku/index.html
echo "news 123" > /var/www/vhost/westos.org/news/index.htmlvim /etc/httpd/conf/httpd.conf #设置默认发布目录
DocumentRoot "/var/www/vhost"vim /etc/httpd/conf.d/vhost.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wenku.westos.org
DocumentRoot /var/www/vhost/westos.org/news
CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.org
DocumentRoot /var/www/vhost/westos.org/news
CustomLog logs/news.log combined
</VirtualHost>测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.20 news.westos.org wenku.westos.orgping news.westos.org
firefox: news.westos.org
7、Apache的语言支持
LAMP Linux+Apahce+Mysql+PHP+Perl+Python
#php#
vim /var/www/html/index.php
<?php
phpinfo();
?>dnf install php -y
systemctl restart httpd
firefox: 172.25.254.220/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`;chmod +x index.cgi # 给执行权限
./index.cgi #在shell中查看发布文件是否能显示时间
[root@westosb cgidir]# ls -Zd /var/www/cgi-bin/ #更改安全上下文及刷新
system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/
[root@westosb cgidir]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgidir(/.*)?'
[root@westosb cgidir]# restorecon -RvvF /var/www/html/cgidir/vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>firefox http://172.25.254.220/cgidir/index.cgi
#wsgi#
书写wsgi的测试文件
vim /var/www/html/wsgi/index.wsgi (python脚本缩进对齐很重要)
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']chmod +x /var/www/html/wsgi/index.wsgi
dnf install python3-mod_wsgi
systemctl restart httpdvim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.220 wsgi.westos.org
8、Apache的加密访问
##安装加密插件
dnf install mod_ssl -ymkdir /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.crtmkdir /var/www/vhost/westos.org/login
vim /var/www/vhost/westos.org/login/index.html
login.westos.orgvim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
ServerName login.westos.org
RewriteEngine On
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost><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>^(/.*)$ ##客户地址栏中输入的地址
%{HTTP_HOST} ##客户主机
$1 ##RewriteRule后面跟的第一串字符的值systemctl restart httpd
主机:vim /etc/hosts
172.25.254.220 music.westos.org news.westos.org map.westos.org wsgi.westos.org login.westos.org
##command 方法2
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
9、 Squid+Apache
#squid 正向代理#
上网主机:
配置ip可以上网 加网关和DNS
安装squid
vim /etc/squid/squid.conf
systemctl restart squidfirewall-cmd --permanent --add-service=squid
firewall-cmd --reload不能上网主机:
配置ip不能上网
下载火狐firefox
更改浏览器配置:Preference->General->最下面Settings->
✅ Manual proxy configuration
Http Proxy 172.25.254.220 Port 3128
✅ Use this proxy server for all protocols测试: ping www.baidu.com 不通
在浏览器中访问www.baidu.com可以
#squid 反向代理#
上网主机(代理主机) ##squid,没有数据负责缓存
vim /etc/squid/squid.conf
http_port 80 vhost vport ##vhost 支持虚拟域名 vport 支持虚拟端口
cache_peer 172.25.254.120 parent 80 0 proxy-only #当172.25.254.220的80端口被访问,会从172.25.254.120的80端口缓存数据
systemctl restart squid.servicefirewall-cmd --permanent --add-service=http
firewall-cmd --reload不能上网主机(资源机) ##Apache服务器
安装httpd
火墙添加http
echo 120号的资源 > /var/www/html/index.html 添加网页资源测试:
真机中 firefox http:/172.25.254.220
访问看到的时172.25.254.120上的数据