Apache的管理和web优化
1.Apache的作用
在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
2.Apache的安装
dnf install httpd.x86_64 -y
3.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 ##刷新火墙使设定生效
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端口修改
[root@d ~]# vim /etc/httpd/conf/httpd.conf
45 #Listen 80
46 Listen 8080
[root@d ~]# firewall-cmd --permanent --add-port=8080/tcp
[root@d ~]# firewall-cmd --reload
[root@d ~]# systemctl restart httpd
[root@d ~]# ss -antlupe | grep 8080
# 在网页内输入ip后发现网页报错,但在ip:8080就可以正常显示,apache的ip默认端口为80。
2.默认发布文件
[root@d html]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex test.html index.html
[root@d html]# vim test.html
[root@d html]# systemctl restart httpd
3.默认发布目录
[root@d www]# mkdir westos
[root@d www]# vim /etc/httpd/conf/httpd.conf
123 #DocumentRoot "/var/www/html"
124 DocumentRoot "/var/www/westos"
125 <Directory "/var/www/westos">
126 Require all granted
127 </Directory>
[root@d westos]# vim index.html
[root@d westos]# systemctl restart httpd
6.Apache的访问控制
1.基于客户端ip的访问控制
[root@d westos]# vim /etc/httpd/conf/httpd.conf
125 <Directory "/var/www/html/westos">
126 Order Deny,Allow
127 Allow from 真机ip
128 Deny from all
129 </Directory>
# 只允许自己的真机访问发布目录里的内容,否定其他用户
[root@d westos]# systemctl restart httpd
[root@d westos]# vim /etc/httpd/conf/httpd.conf
125 <Directory "/var/www/html/westos">
126 Order Allow,Deny
127 Allow from all
128 Deny from 真机ip
129 </Directory>
# 只允许其他用户访问,自己真机访问被拒绝
[root@d westos]# systemctl restart httpd
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 admin ##允许通过的认证用户 2选1
Require valid-user ##允许所有用户通过认证 2选1
</Directory>
htpasswd -cm /etc/httpd/htpasswdfile admin ##生成认证文件
注意:当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
7.Apache的虚拟主机
[root@d html]# echo default > index.html
[root@d www]# mkdir -p web.com/{a,b}
[root@d a]# vim index.html
a.page
[root@d b]# vim index.html
b.page
[root@d conf.d]# vim web.conf
1 <VirtualHost _default_:80>
2 DocumentRoot "/var/www/html"
3 CustomLog logs/default.log combined
4 </VirtualHost>
5
6 <VirtualHost *:80>
7 ServerName a.web.com
8 DocumentRoot "/var/www/web.com/a"
9 CustomLog logs/a.log combined
10 </VirtualHost>
11
12 <VirtualHost *:80>
13 ServerName b.web.com
14 DocumentRoot "/var/www/web.com/b"
15 CustomLog logs/b.log combined
16 </VirtualHost>
[root@d conf.d]# systemctl restart httpd
在浏览器所在主机中
vim /etc/hosts
虚拟机ip-100 www.web.com a.web.ocm b.web.com
# 通过输入域名可以解析到ip查看到发布的内容
8.Apache的语言支持
dnf install httpd-manual -y 安装后可以查看apache的一些文件。
#php#
dnf install php -y
[root@d html]# vim index.php
<?php
phpinfo();
?>
[root@d html]# systemctl restart httpd
#cgi# perl
[root@d html]# mkdir cgidir
[root@d cgidir]# vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[root@d cgidir]# chmod +x index.cgi
[root@d cgidir]# vim /etc/httpd/conf.d/web.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
[root@d cgidir]# systemctl restart httpd
firefox http://ip/cgidir/index.cgi
#wsgi# python
[root@d cgidir]# dnf install python3-mod_wsgi.x86_64 -y
[root@d html]# mkdir wsgidir
[root@d html]# vim /etc/httpd/conf.d/web.conf
23 <VirtualHost *:80>
24 ServerName wsgi.web.com
25 WSGIScriptAlias / /var/www/html/wsgidir/index.wsgi
26 </VirtualHost>
[root@d wsgidir]# vim index.wsgi
1 def application(env, westos):
2 westos( '200 ok', [('Content-Type', 'text/html')])
3 return [b'hello wsgi!']
[root@d wsgidir]# chmod +x index.wsgi
[root@d wsgidir]# systemctl restart httpd
9.Apache的加密访问
##安装加密插件 http变为https的加密网址
[root@d wsgidir]# dnf install mod_ssl -y
[root@d httpd]# openssl genrsa -out www.web.com.key 2048 #生成私钥
[root@d httpd]# openssl req -new -key www.web.com.key -out www.web.com.csr
##生成证书签名文件
[root@d httpd]# openssl x509 -req -days 365 -in www.web.com.csr -signkey www.web.com.key -out www.web.com.crt
#生成证书x509 证书格式-req 请求-in 加载签证名称-signkey
[root@d login]# vim /etc/httpd/conf.d/web.conf
28 <VirtualHost *:80>
29 ServerName login.web.com
30 RewriteEngine on
31 RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
32 </VirtualHost>
33
34 <VirtualHost *:443>
35 ServerName login.web.com
36 DocumentRoot /var/www/web.com/login
37 CustomLog logs/login.log combined
38 SSLEngine on
39 SSLCertificateFile /etc/httpd/www.web.com.crt
40 SSLCertificateKeyFile /etc/httpd/www.web.com.key
41 </VirtualHost>
[root@d web.com]# mkdir login
[root@d login]# vim index.html
[root@songtao westos]# vim /etc/hosts
[root@d login]# firewall-cmd --list-all
[root@d login]# systemctl restart httpd
10.Squid+Apache
linux的虚拟机上网
1.在虚拟机里的[root@d ~]# vim /etc/resolv.conf 添加真机能上网的解析地址nameserver ip。
2.[root@d ~]# vim /etc/sysconfig/network在这个文件里添加全局网关,网关为能上网主机的ip。或者添加临时网关ip route add default via ip。
squid 正向代理
在能上网的主机里操作
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
在虚拟机里进行测试
squid反向代理
把自己机子的apache去除
[root@d squid]# dnf remove httpd
[root@d squid]# vim /etc/squid/squid.conf
62 http_port 80 vhost vport
63 cache_peer 172.25.254.113 parent 80 0 proxy-only ##有apache的主机113
[root@d squid]# systemctl restart squid
测试:
firefox 被删除apache的ip
访问看到的时172.25.254.113上的数据。