Apache的管理及优化

###### 1.Apache的作用 ######

Apache  :用超文本协议共享文件的软件
html:超文本语言 设置静态页面
在web被访问时通常使用http://的方式
http://                ##超文本传输协议

超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine

###### 2.Apache的安装 ######


dng search http   ----搜索apache服务
httpd.x86_64 : Apache HTTP Server   ----下载这个即可

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
用户:apache
日志:/etc/httpd/logs

###### 5.Apache的基本配置 ######

实验:#1.Apache端口修改#

1)安装apache及启用

#dnf install httpd  -------下载httpd服务
#systemctl enable --now httpd  --##开启服务并设定服务位开机启动
#firewall-cmd --permanent --add-service=http  ---##在火墙中永久开启http访问-明文
#firewall-cmd --permanent --add-service=https ---加密访问
#firewall-cmd --reload             -----##刷新火墙使设定生效

测试:直接在火狐里面输入 172.25.254.173

 2)编辑默认发布文件内容

#cd /var/www/html          ----默认发布目录
#vim index.html            -----默认发布文件
--------
hello westos
--------
火狐 显示 "hello westos"

测试:火狐界面

 3)修改端口为8080  ---注意只能修改为8080

#vim /etc/httpd/conf/httpd.conf      -----主配置文件
------
Listen 8080                   ----这个端口只能改为8080   默认为80:

-------
注意修改端口后要重新开启火墙
#firewall-cmd --permanent --add-port=8080/tcp
#firewall-cmd --reload
#systemctl restart httpd
      ---重启生效

4)测试:火狐输入172.25.254.173  ---访问不到 因为端口已经改变

           输入172.25.254.173:8080   --显示以下界面 说明端口修改成功

 5)端口恢复为默认值80

#vim /etc/httpd/conf/httpd.conf
------------
Listen 80                ---改为默认80
--------
##systemctl restart httpd       ---重启生效

实验#2.修改默认发布文件##
将test.html设定为默认发布文件

1)编辑test.html内容 创建实验材料

#cd /var/www/html
#vim test.html    
------------
hello test
-------------
#cat test.html
--------
hello test
-------
#cat index.html
----------
hello westos
-------

测试:火狐输入 172.25.254.173/test.html  =-----显示 “ hello test" 但此时还不是默认发布文件

 2)编辑配置文件 设定test.html为默认发布文件

#vim /etc/httpd/conf/httpd.conf----编辑配置文件 设定默认发布文件
----------
DirectoryIndex test.html index.html         
----------

注:“先发布test.html的内容 如果没有 
发布index.html的内容 如果还没有就出现默认界面”

#systemctl restart httpd 

 3)测试:火狐输入 172.25.254.173 --- 显示 “hello test"

说明test.html已经成功设定为默认发布文件

 4)恢复默认发布文件

#vim /etc/httpd/conf/httpd.conf  ---还原index.html为默认发布文件 
#systemctl restart httpd
#rm -fr test.html      删除test.html

测试:火狐界面   显示“hello westos”    ---表明恢复成功

实验:#3.修改默认发布目录
将 /www/westos设定为默认发布目录

1)创建实验素材

#mkdir /www/westos -p
#cd /www/westos/
#vim index.html
--------
/www/westos 's page
--------
#getenforce            ----selinux  一定要为 “enforcing"状态
#semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'  --修改安全上下文
#restorecon -RvvF /www/     ----刷新

2)编辑配置文件--修改默认发布目录

#vim /etc/httpd/conf/httpd.conf  --修改配置文件
--------
DocumentRoot "/www/westos"

<Directory “/www/westos">
.....
.......
....
......
 Require all granted              ---授权
--------
#systemctl restart httpd

 3)测试:火狐访问 172.25.254.127  ---- 显示 “/www/westos 's page"
   表明默认发布目录 设定成功

4)目录恢复原始默认

#vim /etc/httpd/conf/httpd.conf  ---恢复原始默认值
-----
注释
----
#systemctl restart httpd

###### 6.Apache的访问控制 ######

##1.基于客户端ip的访问控制#

实验:黑名单访问

1)建立实验素材

#cd /var/www/html
#ls
#mkdir westos         ---建立实验素材
#ls
#cd westos/
#vim index.html
----------
/var/www/html/westos
---------

2)设置黑名单

#vim /etc/httpd/conf/httpd.conf      ---设置白名单
----------

<Directory "/var/www/html/westos">
       Order allow,deny
       Allow from all
       Deny from 172.25.254.73      
</Directory>
---先允许在拒绝---允许所有人访问 然后拒绝27访问  后面会覆盖前面
----------------
#systemctl restart httpd

3)测试

火狐测试 ---172.25.254.173/westos
73被拒绝访问 其他人可以

实验:##白名单访问###

1)修改配置文件

#vim /etc/httpd/conf/httpd.conf
------------------------
<Directory "/var/www/html/westos">
       Order deny,allow
       Allow from 172.25.254.27
       Deny from all     -
</Directory>
------------------------
#systemctl restart httpd

2)测试: 火狐测试 --只有172.25.254.73可以访问-172.25.254.173/westos

 注意:黑白名单设置不安全 改成响应ip即可访问 我们可以进行用户认证

#2.基于用户认证#

实验:用户认证

1)建立用户

#htpasswd -cm /etc/httpd/.htpasswd lee  ----建立用户lee
New password:
Re-type new password:
Adding password for user lee
#cat /etc/httpd/.htpasswd
lee:$apr1$Gfeiu..X$VNrd1vcpYDRhBhTPMBlDF0

#htpasswd -m /etc/httpd/.htpasswd admin  
  --建立apache用户admin 
-m 不会覆盖/etc/httpd/.htpasswd内容 -c会

 

 2)修改配置文件

#vim /etc/httpd/conf/httpd.conf
-------------------
<Directory "/var/www/html/westos">
  AuthUserFile "/etc/httpd/.htpasswd"
  AuthName "Please input username and password !!"
  AuthType basic
#  Require user lee
 Require valid-user
</Directory>
--------------------------:
#systemctl restart httpd

 3)测试:火狐测试 172.25.254.173/westos 显示要输入用户名和密码

 ###### 7.Apache的虚拟主机 ######
一个ip只能打开一个测试点  不能打开多个 如何解决? 创建虚拟机

实验:虚拟主机

1)主机创建测试文件

---------73-------测试
#vim /etc/hosts                ---主机建立测试文件
-------------------

172.25.254.173 utility.example.com www.westos.org linux.westos.org luck.westos.org
--------------

 2)虚拟机建立实验素材

-------127---------
#mkdir -p /var/www/virtual/westos.org/{linux,luck}  ---建立实验素材
#echo linux.westos.org > /var/www/virtual/westos.org/linux/index.html
#echo luck.westos.org > /var/www/virtual/westos.org/luck/index.html

3)修改配置文件

#vim /etc/httpd/conf.d/vhosts.conf  ----编辑配置文件
----------------------------------
<VirtualHost _default_:80>
 DocumentRoot /var/www/html
 CustomLog logs/default.log combined
</VirtualHost>

<VirtualHost *:80>
  ServerName linux.westos.org
  DocumentRoot /var/www/virtual/westos.org/linux
  CustomLog logs/linux.log combined
 </VirtualHost>

<VirtualHost *:80>
  ServerName luck.westos.org
  DocumentRoot /var/www/virtual/westos.org/luck
  CustomLog logs/luck.log combined
 </VirtualHost>
-----------------------------------
如果代码输入错误 显示
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.
#systemctl status httpd.service ---可以查看错误
#systemctl restart httpd

 注意:别忘了在主配置文件中修改权限

#vim /etc/httpd/conf/httpd.conf

 4)测试:

-----------主机73--------测试------
火狐----“linux.westos.org"  显示---linux.westos.org
---“luck.westos.org"-->luck.westos.org
==="www.westos.org"--->hello westos

 

######8.Apache的语言支持 ######

实验:php服务

1)下载安装php

#dnf install php -y
#systemctl restart httpd

2)编辑php文件

#cd /var/www/html
#ls
#vim index.php
--------------
<?php
phpinfo();
?>
---------------

3)测试:火狐访问 172.25.254.173/index.php    ----显示php服务界面

 实验:cgi 服务

1)下载http服务手册并打开

#dnf install httpd-manual -y        
在火狐中访问 172.25.254.127/manual/  
#systemctl restart httpd

 2)编辑index.cgi文件

#cd /var/www/html
#mkdir cgi
#cd cgi/
#vim index.cgi
-------
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
--------------
#semanage fcontext -l | grep cgi-bin   ----看看cgi的安全上下文怎么写
#semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'     ----设定安全上下文
#restorecon -RvvF /var/www/html/cgi/   --刷新

 

 

 3)修改配置文件

#vim /etc/httpd/conf.d/vhost.conf
---------------------------------
<Directory "/var/www/html/cgi">
Options ExecCGI
AddHandler cgi-script .cgi
</Directory>chmod +x
------------------------
#chmod +x index.cgi
#systemctl restart httpd

 

 4)测试:火狐 输入 172.25.254.173/cgi/index.cgi
显示 动态 真实时间 显示

 实验:wsgi---支持python语言实现

1)下载安装wsgi服务

#dnf search wsgi
#dnf install python3-mod_wsgi.x86_64 -y
#mkdir /var/www/html/wsgi
#semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/wsgi(/.*)?'           ---修改安全上下文
#restorecon -RvvF /var/www/html/wsgi

2)编辑index.wsgi文件

#vim /var/www/html/wsgi/index.wsgi
--------------
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos linux web']
-------------------

 

 3)修改配置文件

#vim /etc/httpd/conf.d/vhosts.conf
-------
<VirtualHost *:80>
  ServerName wsgi.westos.org
  WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
---------
#systemctl restart httpd

 4)主机解析

#vim /etc/hosts

172.25.254.127 utility.example.com www.westos.org
 linux.westos.org luck.westos.org wsgi.westos.org 
login.westos.orgs
------------------

 5)测试:火狐 输入“wsgi.westos.org" ====显示=====“hello westos linux web”


###### 9.Apache的加密访问 ######

1.下载https 安装加密软件
dnf install mod_ssl -y
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

##方法二  ---常用
openssl req --newkey rsa:2048 \
-nodes -sha256 -keyout /etc/httpd/westos.org.key \
-x509 -days 365 -out /etc/httpd/westos.org.crt

实验:下载https 并设置加密证书


1)下载https

#dnf search http   -----注意不是https
#dnf install mod_ssl.x86_64 -y

2)生成加密证书并查看

#cd /etc/httpd/conf.d/
#openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/westos.org.key -x509 -days 365 -out /etc/httpd/westos.org.crt    ---生成证书
编写信息
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shannxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:www.westos.org
Email Address []:linux@westos.org
------------------
#ls /etc/httpd/westos.org.key
/etc/httpd/westos.org.key
#ls /etc/httpd/westos.org.crt   ---查看证书是否生成
/etc/httpd/westos.org.crt

3)编辑配置文件

3.编写配置文件
#cd /etc/httpd/conf.d/
#vim ssl.conf
-------------
SSLCertificateKeyFile /etc/httpd/westos.org.crt

SSLCertificateKeyFile /etc/httpd/westos.org.key
-------------------
#systemctl restart httpd

4)测试
在主机的火狐输入“https://172.25.254.173" 显示 “hello westos"

点击“锁”查看证书可看到详细信息

 

 http访问百度时 会自动跳转https加密界面 ? 怎么做到呢
只有和企业交互数据才要强制加密  普通浏览不需要加密

实验:http-->https


1)建立实验材料

#mkdir /var/www/virtual/westos.org/login -p   ----建立目录
#echo login.westos.org >  /var/www/virtual/westos.org/login/index.html   -----写进配置文件
#cat /var/www/virtual/westos.org/login/index.html
---------
login.westos.org
---------

2)编辑配置文件

#vim /etc/httpd/conf.d/vhosts.conf   ----编写配置文件
-------
<VirtualHost _default_:80>
  DocumentRoot /var/www/html
  CustomLog logs/default.log combined
</VirtualHost>


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


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

-------------------------------------------
#systemctl restart httpd   ------重启


注意: RewriteEngine on     ---重写       
  RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 --
----^(/.*)$---用户输入的内容   
----https://%{HTTP_HOST}$1 --把用户输入的内容加密
1---指”RewriteRule“后面的第一串字符

 

 3)修改主配置文件

 4)主机解析

5)测试: 在主机火狐输入“:http://login.westos.org" --页面自动跳转

为https://login.westos.org/ 会有安全提示 点进去显示“login.westos.org"

###### 10.Squid+Apache #######

浏览器访问的端口都是 “80”

客户做的:正向代理=翻墙 ----为了访问不能访问的东西
企业做的:反向代理--为了使用户访问速度更快

实验:正向代理
代理服务端:172.25.254.100(nodea)
测试客户端:172.25.27  (真机)
资源储存端:172.25.254.250 (可以上网)

1.服务端网络配置

-----------虚拟机--------100------------
#ip route add default via 172.25.254.250
#echo nameserver 114.114.114.114 > /etc/resolv.conf
#ping www.baidu.com   ---可以ping通 配置成功


2.安装squid代理

#dnf install squid -y
#dnf remove httpd -y  ---去除httpd的影响

3.修改配置文件

#vim /etc/squid/squid.conf
-----------
59 http_access allow all
65 cache_dir ufs /var/spppl/squid 100 16 256   (去掉注释即可)
---------
#systemctl restart squid   ---重启
#firewall-cmd --add-service=squid   ---开启火墙允许服务

 3.客户端代理设置

 

 4.客户端测试 :可使用www.baidu.com访问百度   但ping不通www.baidu.com

 5.总结

正向代理是 用户(27)无法上网 但可以通过代理服务端(100)去访问(250)

27请求访问www.baidu.com-->代理服务端请求到250下载资源--->缓存成功-->27可以访问百度

实验:反向代理


西安用户   西安腾讯服务器   深圳腾讯服务器
如果西安用户想访问深圳腾讯服务器 但是速度过慢 所以西安腾讯服务器可进行“反向代理” 让用户更快速度访问深圳服务器

实验环境:
将正向代理网页设置还原

172.25.254.27            ##Apache服务器
172.25.254.127             ##squid,没有数据负责缓存


1.编写配置文件

代理服务端---127

#vim /etc/squid/squid.conf
-------------
http_access allow all

62 http_port 80 vhost vport
63 cache_peer 172.25.254.27 parent 80 0 proxy-only
---------------------
#systemctl restart squid
#firewall-cmd --permanent --add-service=http

 2.测试
firefox  http:/172.25.254.127
访问看到的时172.25.254.27上的数据

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值