Apache管理及优化
1.Apache 的作用
在 web 被访问时通常使用 http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine
curl -I www.baidu.com 查看百度的
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 |
报错 | 403:权限限制 |
—转载
错误:
https://blog.csdn.net/github_38524608/article/details/79127386
5.Apache 的基本配置
5.1.Apache 端口修改
vim /etc/httpd/conf/httpd.conf
Listen 1111
firewall-cmd --permanent --add-port=1111/tcp
firewall-cmd --reload
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 1111
systemctl restart httpd
在配置文件中修改的端口如果不在服务使用的几个端口中,则无法重启服务,需要添加端口,及在火墙中添加所要使用的端口
访问的时候需要加端口,/var/www/html/index.html 文件内容
5.2默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
访问的时候一般默认访问的文件是/var/www/html/index.html,可以修改主配置文件/etc/httpd/conf/httpd.conf,进行访问的顺序设定
将westos.html写在前面,所以访问apache时默认访问的文件为westos.html文件
编辑完主配置文件,重启服务httpd,可以去访问服务器,
当删掉/var/www/html/westos.com文件,默认访问文件就变成/var/www/html/index.html文件
5.3.默认发布目录
1.修改httpd服务的主配置文件/etc/httpd/conf/httpd.conf,将所建目录进行编写及授权;
2.更改所建目录的安全上下文,使其与httpd的默认发布目录/var/www/html/一致
3.重启httpd服务
4.测试,查看访问的默认发布目录是否改变
6.Apache 的访问控制
实验素材
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
westos
firefox http://服务端主机ip/westos6.1.基于客户端 ip 的访问控制
ip黑名单
vim /etc/http/conf/httpd.conf
systemctl restart httpd
ip白名单
vim /etc/http/conf/httpd.conf
systemctl restart httpd
6.2.基于用户认证
htpasswd -cm /etc/httpd/htpasswdfile admin ##生成认证文件
注意:
当/etc/httpd/htpasswdfile 存在那么在添加用户时不要加-c 参数否则会覆盖源文件内容
1.建立认证文件
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>
3.访问设定的/var/www/html/westos,需要用户认证
第一次访问的时候需要用户认证,之后就不需要了,有之前的缓存
可以清掉历史缓存,再次访问的时候需要用户认证
清除历史
7.Apache 的虚拟主机
一个ip建立多个站点
1.
建立实验素材
2.vim /etc/httpd/Vhost.conf
##在httpd的子配置文件中配置虚拟主机
systemctl restart httpd
3.
测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.211 www.westos.com bbs.westos.com news.westos.com tieba.westos.com
测试效果
firefox http://www.westos.com ##可以访问到默认信息
firefox http://wenku.westos.com ##访问到上文设置信息
firefox http://news.westos.com
8.Apache 的语言支持
dnf install manual
##安装manaul手册,去访问,查看一些apache的语言支持,如何写
8.1php
php默认发布测试页
1.vim /var/www/html/index.php
<?php
phpinfo();
?>
dnf install php -y
systemctl restart httpd
firefox http://172.25.254.211/index.php
8.2perl----cgi
公共(Common)。无须考虑客户机和服务器所运行的操作系统平台,只要二者的网关程序遵循同一数据传输协议,即可进行数据交互。
网关(Gateway)。CGI 可被用来作为 HTTP 服务器与其他第三方应用程序之间的“连接件”或“中件(Middle Ware) "。
接口(Interface)。CGI 使用标准通讯机制,为其它应用程序与 HTTP 服务器提供数据传输接口。
1. ###建立目录
2
2.vim /var/www/html/cgi/index.cgi
##在建立的cgi目录下编辑文件
#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date
; ### 访问cgi文件时,会看到命令执行的效果
chmod +x /var/www/html/cgi/index.cgi
3.
vim /etc/httpd/conf.d/vhost.conf
##编辑配置文件
<Directory “/var/www/html/cgi”>
Options +ExecCGI
AddHandler cgi-script .cgi
4.
firefox http://192.168.0.11/cgidir/index.cgi
测试
8.3wsgi-----python
dnf install python3-mod_wsgi.x86_64 -y
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi-scripts/index.wsgi
[root@localhost html]# cd wsgi-scripts/
[root@localhost wsgi-scripts]# ls
index.wsgi
[root@localhost wsgi-scripts]# cat index.wsgi
def application(env, westos):
westos( ‘200 ok’,[(‘Content-Type’, ‘text/html’)])
return [b’hello wsgi’]
chmod +x /var/www/html/wsgi-scripts/index.wsgi
加解析,访问
wsgi.westos.org
hello wsgi
9.Apache 的加密访问
http -----> https
9.1apache的加密访问设定
1.安装加密插件
dnf install mod_ssl -y
2.生成证书
1.openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048 #
生成私钥
2.openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.csr
##生成证书签名文件
3.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 | 加载签证名称 |
4.
vim /etc/httpd/conf.d/ssl.conf 编辑加密插件的主配置文件
``
重启httpd
3.网页中https:server ip访问测试
9.2自动转成https加密形式访问形式
2.vim /etc/httpd/conf.d/vhost.conf
systemctl restart httpd
^(/.*)$ | 客户地址栏中输入的地址 |
---|---|
%{HTTP_HOST} | 客户主机 |
$1 | RewriteRule 后面跟的第一串字符的值 |
client
vim /etc/hosts
172.25.254.211 log.westos.com
##做好解析
访问log.westos.com
10.Squid+Apache
10.1 什么是squid?
用户去访问squid主机,squid主机去访问最终信息,(用户不能直接去访问某些数据,需要squid去访问,再传递给用户,类似于翻墙)
squid是一种用来缓冲Internet数据的软件。它是这样实现其功能的,接受来自人们需要下载的目标(object)的请求并适当地处理这些请求。也就是说,如果一个人想下载一web页面,他请求Squid为他取得这个页面。Squid随之连接到远程服务器(比如:http://squid.nlanr.net/)并向这个页面发出请求。然后,Squid显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时,Squid可以简单地从磁盘中读到它,那样数据迅即就会传输到客户机上。当前的Squid可以处理HTTP,FTP,GOPHER,SSL和WAIS等协议。但它不能处理如POP,NNTP,RealAudio以及其它类型的东西。
10.2squid 正向代理
实验环境:
cache主机ip :172.25.254.211
测试主机ip:172.25.254.11
实验效果
让测试主机11不能上网但浏览器可以访问互联网页
操作:
1.在211中主机:server端中
211主机将网关设置成250,可以上网的主机,dns1=114.114.114.114 其可以上网,访问www.baidu.com
dnf install squid -y ###安装squid
vim /etc/squid/squid.conf ###编辑主配置文件
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid ##重启squid
==2.在测试主机11中选择(浏览器界面)
NetWork Proxy 172.25.254.211 3128
选择reference
ip是cache主机的ip
测试:
在单网卡主机11中
ping www.baidu.com 不通
在浏览器中访问 www.baidu.com 可以
ping www.baidu.com不通
但可以在网页中访问www.baidu.com
10.3squid 反向代理
实验环境:
172.25.254.111
##Apache 服务器
172.25.254.211
##squid,没有数据负责缓存
1.vim /etc/squid/squid.conf
http_port 80 vhost vport
##编辑内容
##vhost 支持虚拟域名
##vport 支持虚拟端口
#当 172.25.254.211 的 80 端口被访问会从 172.25.254.111的 80 端口缓存数据
cache_peer 172.25.254.111 parent 80 0 proxy-only
##编辑的文件内容
systemctl restart squid
##重启squid
测试
firefox http:/172.25.254.211
访问看到的时 172.25.254.111 上的数据
注意:在做测试的时候需要将之前的设定进行重新更改改为设定的80端口