<4>apache的管理及优化web


1.Apache 的作用

在 web 被访问时通常使用 http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine

2.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

3.Apache 的基本配置

安装:
[root@node1~]# dnf install httpd.x86_64 -y
[root@node1~]# firewall-cmd --permanent --add-service=http
[root@node1~]# firewall-cmd reload
[root@node1~]# systemctl enable --now httpd

1.Apache 端口修改
[root@node1~]# vim /etc/httpd/conf/httpd.conf
其中34行/etc/httpd是阿帕奇的根
更改Listen 8080  #默认端口是80,改完要改回去,restart
此时去网页访问8080是不行的
[root@node1~]# firewall-cmd --permanent --add-port=8080/tcp
[root@node1~]# firewall-cmd --reload
此时去网页访问172.25.254.100:8080是行的

#2.默认发布文件##
[root@node1~]#vim test.html
Hello test
此时访问172.25.254.100/test.html #跳转index.html测试页
[root@node1~]# vim /etc/httpd/conf/httpd.conf 
DirectoryIndex test.html index.html  #更改,添加test.html,可以添加多个,按顺序访问,若都不存在就、,就访问index.html测试页。 之后再改回来默认的,刷新
[root@node1~]# systemctl restart httpd
此时访问172.25.254.100/test.html #此时能访问172.25.254.100

[root@node1~]# mkdir /westos_web
[root@node1~]# vim /westos_web/index.html
[root@node1~]# vim /etc/httpd/conf/httpd.conf 
121行改为DocumentRoot “/westos_web”
[root@node1~]# systemctl restart httpd
此时访问172.25.254.100/test.html #此时不能访问172.25.254.100
当打不开阿帕奇的时候查看cat /var/log/messages;当阿帕奇不能访问的时候cat /etc/httpd/logs/error_log

#3.默认发布目录
[root@node1~]# vim /etc/httpd/conf/httpd.conf  
121行改为DocumentRoot “/westos_web”   #%之后改回来注释掉
	添加	Require all granted
[root@node1~]# systemctl restart httpd
此时访问172.25.254.100/index.html #此时能访问
%操作完成

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

4.Apache 的访问控制

%操作:Apache 的访问控制黑白名单
[root@node1~]# vim /etc/httpd/conf/httpd.conf
编辑内容:121 DocumentRoot “/westos_web”   
	Require all granted
改为:
DocumentRoot “/var/www/html”
#DocumentRoot “/westos_web”
<Directory “/var/www/html/westos”>
	Other Deny,Allow  #先读取Deny拒绝所有,再读Allow刷新允许172.25.254.250,此时只允许172.25.254.250访问。
	Allow from 172.25.254.250
	Deny from all
</ Directory >
[root@node1~]# systemctl restart httpd
此时网页只允许172.25.254.250访问172.25.254.100/westos/。

若Other Allow,Deny  #先读取Allow接受所有,再读Deny刷新拒绝72.25.254.250,此时只拒绝172.25.254.250访问。
	Allow from all
	Deny from 172.25.254.250
</ Directory >

 [root@node1~]# systemctl restart httpd
此时网页只拒绝172.25.254.250访问172.25.254.100/westos/。其他用户均可访问172.25.254.100/westos/

%操作完成

在这里插入图片描述10.4.17.241可以访问
在这里插入图片描述
在这里插入图片描述

%操作:用户认证
(虚拟机没有的话在真机里找)
[root@node1~]#  vim /etc/httpd/conf/httpd.conf
把121行DocumentRoot “/var/www/html”
#DocumentRoot “/westos_web”
把<Directory “/var/www/html/westos”>
	Other Deny,Allow
	Allow from 172.25.254.250
	Deny from all
</ Directory >
改为
<Directory “/var/www/html/westos”>
	
</ Directory >

[root@node1~]# cd /etc/httpd/
[root@node1 httpd]# ls
[root@node1 httpd]# htpasswd -cm /etc/httpd/.htpasswd admin ##生成认证文件,c创建,m指定用户.注意:当/etc/httpd/.htpasswd 存在那么在添加用户时不要加-c 参数,否则会覆盖源文件内容
[root@node1~]#  vim /etc/httpd/conf.d/httpd.conf
把124行添加
<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 >
[root@node1~]# systemctl restart httpd
[root@node1~]# vim /etc/hosts
更改:172.25.254.100  www.westos.org  linux.westos.org lee.westos.org  login.westos.org  #172.25.254.100为自己的ip
用网页访问www.westos.org/westos/
输入:name:lee
      密码:westos

%操作完毕

在这里插入图片描述在这里插入图片描述

7.Apache 的虚拟主机

小实验展示:
%操作:
[root@node1~]# cd /var/www/html/
[root@node1~]# mkdir westos
[root@node1~]# cd westos/
[root@node1~]# vim index.html
westos’s page
在该主机的网页访问172.25.254.100/westos/  就会显示westos’s page

在lee的用户内操作
[root@node1~]# vim /etc/hosts
添加:172.25.254.100 www.westos.org  linux.westos.org lee.westos.org  login.westos.org

在node1里操作:
[root@node1~]# vim /etc/httpd/conf/httpd.conf  
121# DocumentRoot “/westos_web”   
	#Require all granted
[root@node1~]# systemctl restart httpd
%操作完毕
测试:
在浏览器所在主机中
vim /etc/hosts
192.168.0.11 www.westos.com wenku.westos.ocm news.westos.com
firefox http://www.westos.com
firefox http://wenku.westos.com
firefox http://news.westos.com



%操作:Apache 的虚拟主机
[root@node1~]# mkdir -p /var/www/virutal/westos.org/{linux,lee}
[root@node1~]# vim /var/www/ virutal/westos.org/linux/index.html
linux.westos.org
[root@node1~]# vim /var/www/ virutal/westos.org/lee/index.html 
lee.westos.org
[root@node1~]# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost _default_:80>  #默认的
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>   #lee的
ServerName lee.westos.org 
DocumentRoot  /var/www/ virutal/westos.org/lee
CustomLog logs/lee.log combined
</VirtualHost>
<VirtualHost *:80>  #linux的
ServerName linux.westos.org
DocumentRoot  /var/www/ virutal/westos.org/linux
CustomLog logs/linux.log combined
</VirtualHost>

[root@node1~]# systemctl restart httpd

测试:
在浏览器所在主机lee中
vim /etc/hosts
添加:172.25.254.100 www.westos.org  linux.westos.org lee.westos.org  login.westos.org
  #172.25.254.100访问的主机ip
在该主机的网页访问
http://www.westos.com  #访问出来的界面是默认的hello westos
http://linux.westos.com  #访问出来的界面是linux.westos.org
http://lee.westos.com  #访问出来的界面是 lee.westos.org
%用一个阿帕奇发布多个站点
如果网页打不开就用tail /var/log/messages查看错误进行修改
%操作结束

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

8.Apache 的语言支持

%操作:Apache 的语言支持 
1,php语言
[root@node1~]# cd /var/www/html/
[root@node1/html]# ls
[root@node1/html]# vim index.php
编辑内容:
<?php
Phpinfo();
?>

[root@node1~]# dnf install php -y
[root@node1~]# systemctl restart httpd
网页访问www.westos.org/index.php  可以访问
[root@node1~]# dnf install httpd-manual –y   #阿帕奇apache的说明手册,可以网页访问www.westos.org/manual/

2,cgi语言,作用是让apache利用cgi这个插件来读取代码的最后执行结果。Apache是发布,不具备读取脚本结果的,所以要用cgi来读取代码的最后执行结果,
[root@node1~]#cd  /var/www/html/
[root@node1/html]# mkdir cgi-scripts
[root@node1/html]# vim /var/www/html/ cgi-scripts /index.cgi
编辑内容
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;

[root@node1/html]# vim /etc/httpd/conf.d/vhost.conf
添加内容
<Directory "/var/www/html/ cgi-scripts ">
Options +ExecCGI
AddHandler cgi-script  .cgi
</Directory>
[root@node1/html]# cd cgi-scripts
[root@node1/ cgi-scripts]# ll
[root@node1/ cgi-scripts]# chmod +x index.cgi
[root@node1/ cgi-scripts]# systemctl restart httpd
网页访问 www.westos.org/cgi-scripts/index.cgi  #显示的是脚本中命令date执行过后的结果,并不是脚本编写内容

3,wsgi 语言
[root@node1~]#cd /var/www/html/
[root@node1/html]# mkdir wsgi-scripts
[root@node1/html]# cd wsgi-scripts
[root@node1/ wsgi-scripts]# ls
[root@node1/ wsgi-scripts]# vim /var/www/html/ wsgi-scripts /index.wsgi
编辑内容
def application(env, westos):
	westos( ‘200 ok’ , [(‘Content-Type’, ’text/html’)])
	return[b’hello wsgi!’]

[root@node1/ wsgi-scripts]# chmod +x index.wsgi
[root@node1/ wsgi-scripts]# python3 index.wsgi
[root@node1/ wsgi-scripts]# vim /etc/httpd/conf.d/vhosts.conf
在后面添加:
<VritualHost  *:80>
ServerName wsgi.westos.org
WSGIScriptAlias /  /var/www/html/ wsgi.westos/index.wsgi
</ VritualHost >

[root@node1/ wsgi-scripts]# systemctl restart httpd
%要做解析/etc/hosts,要在虚拟机的网页访问 www.westos.org/wsgi-scripts/index.cgi  #显示的是脚本编写内容:hello wsgi!
%操作完成

在这里插入图片描述php语言
在这里插入图片描述

扫描二维码关注公众号,回复: 12439765 查看本文章

在这里插入图片描述在这里插入图片描述
cgi语言

在这里插入图片描述
在这里插入图片描述

wsgi语言
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

9.Apache 的加密访问

%操作Apache 的加密访问,在虚拟机里操作
[root@node1~]# dnf install mod_ssl -y ##安装加密插件
[root@node1~]# cd /etc/httpd/conf.d/
[root@node1/conf.d]# ls   #ssl.conf是主配置文件
[root@node1/conf.d]# systemctl restart httpd
[root@node1/conf.d]# firewall-cmd --permanent --add-servuce=https
[root@node1/conf.d]# firewall-cmd --reload
网页访问www.westos.org  证书有感叹号,删除三个

%生成证书
[root@node1~]# openssl genrsa -out /mnt/www.westos.org.key 2048 #生成私钥,genrsa加密方式,-out后跟输出位置,长度不能小于2048
[root@node1~]# openssl req -new -key /mnt/www.westos.org.key -out /mnt/www.westos.org.csr   ##生成证书签名文件, csr请求证书,下面要填证书内容
Country:CN
State:Shanxi
Locality:xi’an
Organization Name:westos
Organization Unit Name :linux
Common Name:www.westos.org
Email:[email protected]
A challenge password[]:   #直接回车,密码不能写,不然就不能开机启动了
An optional company[]:   #不用管,直接回车

[root@node1~]# ls /mnt/   #有了文件www.westos.org.csr
[root@node1~]# openssl x509 -req -days 365 -in \ /mnt/www.westos.org.csr -signkey /mnt/www.westos.org.key  -out /mnt/www.westos.org.crt #生成证书, -signkey签的文件,-out生成的文件;x509 证书格式;-req 请求;-in 加载签证名称;-signkey /mnt/www.westos.org.key签证
[root@node1~]# cp /mnt/www.westos.org.*  /etc/httpd/
[root@node1~]# cd /etc/httpd
[root@node1~]# ls
[root@node1~]# cd  /etc/httpd/conf.d/
[root@node1/conf.d]# ls
[root@node1/conf.d]# vim ssl.conf
编辑内容:
更改86行为 SSLCertificateFile /etc/httpd/www.westos.org.crt
更改94行为: SSLCertificateFile  /etc/httpd/www.westos.org.key

[root@node1/conf.d]# systemctl restart httpd
网页访问www.westos.org 可以查看认证信息

只有重要的网站才会加密
[root@node1/conf.d]# mkdir /var/www/virtual/westos.org/login
[root@node1/conf.d]# vim /var/www/virtual/westos.org/login/index.html
编辑内容:
login.westos.org

[root@node1/conf.d]# vim /etc/httpd/conf.d/vhost.conf
后面添加:(参考ssl.conf)
<VirtualHost *:80>
ServerName login.westos.org
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/www.westos.org.crt/
SSLCertificateKeyFile /etc/httpd/www.westos.org.key

ServerName login.westos.com
DocumentRoot /var/www/virutal/westos.org/login"
CustomLog logs/login.log combined
</VirtualHost>


[root@node1/conf.d]# systemctl restart httpd

网页访问https://login.westos.org  #界面是login.westos.org
%操作完成

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qiao_qing/article/details/110143563
今日推荐