2.1 Apache的管理及优化web

Apache的作用

http:超文本传输协议(共享协议),运输在TCP协议之上
http是由软件提供:curl -I www.qq.com
Apache是提供超文本传输协议的软件
(Apache提供http服务,Apache的软件名称就是http)
同时,还有,nginx,stgw,jfe,Tengine

Apache的安装和启用

  1. 安装并开启Apache
dnf search http(搜索软件)
dnf Install httpd.x86_64 -y 
systemctl enable--now  httpd
  1. 永久设定防火墙策略
firewall-cmd --permanent --add-service=http
#刷新火墙,火墙策略生效
firewall-cmd --reload
#list中出现有http,表示apache被防火墙允许,http服务在火墙策略中对外开放
firewall-cmd --list-all

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

Apache的基本配置

服务名称:httpd
配置文件:rpm -qc httpd

  • 主配置文件:/etc/httpd/conf/httpd.conf
  • 子配置文件:/etc/httpd/conf.d/*.conf
    在这里插入图片描述

默认端口:netstat -antlupe | grep httpd

  • http:80
  • https:443 (加密端口)
    在这里插入图片描述

默认发布目录:/var/www/html
默认发布文件:index.html

注意!

  • 关闭selinux
vim   /etc/sysconfig/selinux
#SELINUX=disabled
reboot

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

  • 更改目录,更改索引文件,更改端口。都要重启服务生效
systemctl restart httpd

更改端口

#编辑主配置文件
vim   /etc/httpd/conf/httpd.conf
#/listen  (45行),改成8080,selinux不会对8080端口有影响)
systemctl restart httpd
netstat  -antlupe  |  grep  httpd
firewall-cmd  --add-port=8080/tcp
firewall-cmd  --list-all

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

更改默认发布文件

默认发布目录:/var/www/html
默认发布文件:index.html

1)在默认发布目录/var/www/html的文件的名称需要修改成默认发布文件名称

vim   /var/www/html/index.html

2)不想默认文件名字,可以修改主配置文件

vim   /etc/httpd/conf/httpd.conf
#/index(167行,修改默认发布文件指令)
#DirectoryIndex  westos  index.html   
#2个默认发布文件,先看前面的文件
#前面的默认文件都没有,就是默认测试页
systemctl  restart  httpd

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

更改默认发布目录

1)建立默认发布目录

mkdir  /var/www/westos
vim /var/www/westos/index.html

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

2)修改主配置文件

vim   /etc/httpd/conf/httpd.conf
#/Doc(122行)
#
#修改默认发布目录的参数:/var/www/westos
#还需要写入以下内容,进行授权
#<Directory  "/var//www/westos">
#   Require  all  granted
#</Directory>
#默认发布目录可以被访问
systemctl  restart httpd

Q1:更改默认发布目录后,默认发布文件是从该目录中读吗?
A1:yes
Q2:那目录中没有文件呢?
A2:默认访问测试页
在这里插入图片描述在这里插入图片描述

访问控制

基于IP的访问控制

编辑发布文件

mkdir  /var/www/html/westos
vim  /var/www/html/westos/index.html

在这里插入图片描述在这里插入图片描述编辑主配置文件

vim   /etc/httpd/conf/httpd.conf

【注意访问顺序】

DocumentRoot “/var/www/html"
#默认发布目录可以被访问
<Directory  "/var/www/html/westos">
  Order  Allow,Deny                
  Allow  from  All               
  Deny  from  192.168.0.117 (不允许117访问)
</Directory>
#先读allow,后读deny,deny会覆盖allow中的信息
#允许所有人,但是禁止117主机访问
DocumentRoot “/var/www/html"
#默认发布目录可以被访问
<Directory  "/var/www/html/westos">
  Order  Deny,Allow                
  Allow  from   192.168.0.117           
  Deny  from  all 
</Directory>
#先读deny,后读allow。allow会覆盖deny中的信息
#禁止除了117主机的所有人

在这里插入图片描述测试结果:
在这里插入图片描述在这里插入图片描述

基于用户的访问控制

1)建立认证文件

#admin用户存在与否无所谓
htpasswd  -cm  /etc/httpd/.htpasswd  admin
ls  -l  /etc/httpd/.htpasswd
cat  /etc/httpd/.htpasswd
#再次建立时,要把c去掉,否则,之前的认证信息会被删除
htpasswd  -m  /etc/httpd/.htpasswd  yao
cat  /etc/httpd/.htpasswd

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

2)编辑主配置文件

DocumentRoot “/var/www/html"
#默认发布目录可以被访问
<Directory  "/var/www/html/westos">
   AuthUserFile  /etc/httpd/.htpasswd               //指定认证文件
   AuthName  "Please  input  username  and  passwd" //指定认证提示
   AuthType    basic                                //指定认证类型
   Require user  admin                              //指定认证用户:只允许admin
#Require valid-user                                 //指定认证用户:允许认证文件中的所有人
</Directory>

Require valid-user
在这里插入图片描述

systemctl restart httpd

在这里插入图片描述测试结果:
在这里插入图片描述在这里插入图片描述第二次访问,密码会被记住,不需要再去键入密码
可以【ctrl+shift+del】——> everything
在这里插入图片描述 Require user admin
在这里插入图片描述在这里插入图片描述测试结果:
在这里插入图片描述

Apache的虚拟机主机

一般的网页都有很多子网站
Apache不能发布多个测试页

如果有多个页面要发布,需要搭建虚拟主机

用谁测试,就在谁里面写【117】

  1. 创建多个发布目录和对应的发布文件
mkdir -p  /var/www/westso.org/{linux,shell,python}
echo linux.westos.org > /var/www/westos.org/linux/index.html
echo linux.westos.org > /var/www/westos.org/shell/index.html
echo linux.westos.org > /var/www/westos.org/python/index.html
echo linux hello,linux! > /var/www/westos.org/index.html

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

  1. 浏览器所在的主机中写本地解析
vim  /etc/hosts
192.168.0.117    Linux.westos.org shell.westos.org python.westos.org www.westos.org

在这里插入图片描述测试结果:
在这里插入图片描述

  1. 搭建虚拟主机
#进入子配置目录
#指定子配置文件
cd  /etc/httpd/conf.d
vim /etc/httpd/conf.d/vhost.conf

在这里插入图片描述在 /etc/httpd/conf.d/vhost.conf 文件中写入:

#Apache默认主机
<VirtualHost _default_:80>
  DocumentRoot   /var/www/html             //默认发布目录
  CustomLog  logs/default.log combined     //日志,相对路径(绝对路径是/etc/httpd/logs/default.log combined)
</VirtualHost>
#
#Apache虚拟机主机
<VirtualHost *:80>
  ServerName   linux.westos.org            //虚拟主机域名
  DocumentRoot   /var/www/westos.org/linux //虚拟主机默认发布目录
  CustomLog   logs/linux.log   combined    //虚拟主机日志(combined:混合型日志)
</VirtualHost>

在这里插入图片描述

  1. 重启服务
systemctl  restart  httpd

在这里插入图片描述

  1. 测试结果
    在这里插入图片描述firefox: shell.westos.org ,www.westos.org
    在这里插入图片描述在这里插入图片描述

Apache语言的支持

Apache只能共享静态页面text

#安装Apache帮助手册

dnf   install  httpd-manual  -y
systemctl   start   httpd

在这里插入图片描述

运行脚本

php

1)编写默认发布文件

vim  /var/www/html/index.php

在这里插入图片描述写入内容如下:

<?php
	phpinfo();
?>

在这里插入图片描述

2)安装php

dnf   install  php  -y
systemctl restart  httpd

在这里插入图片描述

3)测试
firefox:192.168.0.117/index.php
在这里插入图片描述

cgi:perl语言

cgi页面:(通用网关接口)脚本必须要去执行

1)编写默认发布文件

mkdir   /var/www/html/cgi
vim   /var/www/html/cgi/index.cgi

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

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "hello, world.";

在这里插入图片描述

perl   index.cgi

在这里插入图片描述

2)设定默认发布文件的权限

chmod   a+x  index.cgi

在这里插入图片描述

3)重启服务

systemctl  restart  httpd

4)测试

firefox:192.168.0.117/cgi/index.cgi
注意!这时的页面只显示出代码,没有执行结果
在这里插入图片描述

5)指定子配置文件

#进入子配置目录,指定子配置文件
cd  /etc/httpd/conf.d
vim vhost.conf

在这里插入图片描述

写入:

<Directory /var/www/html/cgi>
Options +ExecCGI                 //执行cgi程序
AddHandler  cgi-script  .cgi     //参数触发器
</Directory>

在这里插入图片描述

6)重启服务

systemctl  restart  httpd

7)测试
firefox:192.168.0.117/cgi/index.cgi
注意!这时,页面显示是执行结果,成功。
在这里插入图片描述

wsgi:python语言

1)编写默认发布文件

mkdir   /var/www/html/wsgi
vim  /var/www/html/wsgi/index.wsgi

在这里插入图片描述

写入:【python】

def  aplication(env, westos):
       westos('200 ok',[('Content-Type','text/html')])
       return [b'hello westos!']

在这里插入图片描述

2)设定默认发布文件的权限

chmod  a+x  index.wsgi

在这里插入图片描述

3)安装python(默认系统里没有安装wsgi)

dnf  search wsgi
dnf  install   python-3

在这里插入图片描述

4)指定子配置文件

vim  /etc/httpd/conf.d/vhost.conf

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

<VirtualHost  *:80>
servername  wsgi.westos.org
WSGIScriptsAlias  / /var/www/html/wsgi/index.wsgi
<VirtualHost/>

在这里插入图片描述

5)本地解析

vim   /etc/hosts

写入:

192.168.0.117    wsgi.westos.org

在这里插入图片描述6)测试
Firefox:wsgi.westos.org
在这里插入图片描述Q:为什么wsgi需要本地域名解析?
A:这是Apache的设定方式

Apache的加密访问

http:80端口
https:443端口

Apache上有加密用的锁和证书
锁交给主机,【加密:密码,账户】——> 服务器上有钥匙
https://去解密

设定支持https

1)安装加密插件

dnf  install  mod_ssl  -y
ls   /etc/httpd/conf.d

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

2)火墙策略

firewall-cmd  --permanent  --add-service=https
firewall-cmd  --reload
firewall-cmd  --list-all

在这里插入图片描述

3)在网页上删除下载的锁
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述4)生成密钥key

mkdir -p /etc/httpd/webkey
openssl  genrsa  -out  /etc/httpd/webkry/www.westos.org.key 2048
ls    /etc/httpd/webkey/www.westos.org.key

在这里插入图片描述

5)生成key后,需要签证,编写数字证书

openssl  req  -new  -key  /etc/httpd/webkey/www.westos.org.key  -out  /etc/httpd/webkey/www.westos.org.csr
#国家CN
#省shannxi
#城市xi‘an
#公司
#部门
#域名
#邮箱
#密码:回车
#密码:回车

在这里插入图片描述

6)模仿CA机构,生成数字证书

openssl  x509  -req  -days  365  -in   /etc/httpd/webkey/www.westos.org.csr   -signkey  /etc/httpd/webkey/www.westos.org.key    -out  /etc/httpd/webkey/www.westos.org.crt

在这里插入图片描述

7)编辑默认发布文件

mkdir   /var/www/westos.org/login  -p
echo  login.westos.org >  /var/www/westos.org/login/index.html

在这里插入图片描述

8)指定子配置文件

vim  /etc/httpd/conf.d/vhost.conf

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

<VirtualHost*:443>
   ServerName    login.westos.org
   DocumentRoot  /var/www/westos.org/login
   CustomLog     logs/login.log  combined
   SSLEngine     on
   SSLCertificateFile     /etc/httpd/webkey/www.westos.org.crt
   SSLCertificateKeyFile  /etc/httpd/webkey/www.westos.org.key
</ViirtualHost>

在这里插入图片描述

9)编写本地解析

vim  /etc/hosts

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

192.168.0.117  login.westos.org

在这里插入图片描述

10)重启服务

systemctl restart  httpd

11)测试
Firefox:login.westos.org
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

自动从80端口转到443端口

1)编辑主配置文件

vim  /etc/httpd/conf.d/vhost.conf

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

<VirtualHost *:80>
   ServerName     login.westos.org
   RewriteEngine  On
   RewriteRule    ^(/.*)$  https://%{HTTP_HOST}$1
</VirtualHost>

先指定客户主机(它在地址局域网写入的所有信息)
$1:将 ^(/.*)$ 的内容添加到https://{}后面
在这里插入图片描述2)重启服务

systemctl   restart  httpd  

3)测试
在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47133613/article/details/113571298
2.1
今日推荐