ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第三本书第三章Apache的管理及优化

2021-11-11 19:03:09  阅读:158  来源: 互联网

标签:本书 httpd www 第三章 westos var html Apache org


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

2.安装Apache
 dnf search 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 --reload  #刷新火墙使设定生效
 vim /var/www/html/index.html  #修改默认测试页
   hello world

 检测:172.25.254.117==显示是hello world

 4.Apache的基本信息:
1)服务名称:httpd
2)配置文件:
  /etc/httpd/conf/httpd.conf  ##主配置文件
  /etc/httpd/conf.d/*.conf    ##子配置文件
3)默认发布目录: /var/www/html
4)默认发布文件: index.html
5)默认端口:80  #http。   【443 #https 】
6)用户: apache
7)日志: /etc/httpd/logs

5.Apache的基本配置
1)Apache端口修改
vim /etc/httpd/conf/httpd.conf #修改Apache的主配置文件
  Listen 8080  #默认端口改为8080(45行左右)
systemctl restart httpd   #重启服务
firewall-cmd --permanent --add-port=8080/tcp   #添加并永久打开一个端口到tcp区域
firewall-cmd --reload  #更新火墙


*检测 http://172.25.254.117:8080[之前的端口80,就不能访问了]


==本次实验做完把端口号还原成默认== 

2)默认发布文件

 cd /var/www/html
 vim test.html
   hello test
vim /etc/httpd/conf/httpd.conf
    DirectoryIndex test.html index.html
systemctl restart httpd


*检测 http://172.25.254.117==显示应该是hello test


3)默认发布目录
mkdir /westos/html -p


ls -Zd /var/www/html #查看/var/www/html的安全上下文
ls -Zd /westos/html #查看/westos/html目录的安全上下文
semanage fcontext -a -t httpd_sys_content_t '/westos/html(/.*)?'   #永久修改/westos/html目录的安全上下文
restorecon -RvvF /westos/html/ #刷新
systemctl restart httpd  #重启服务


vim /westos/html/index.html
      /westos/html 's page
vim /etc/httpd/conf/httpd.conf
       *注释掉DocumentRoot "/var/www/html"
       DocumentRoot "/westos/html"
       <Directory "/westos/html">
            Require all granted
       </Directory>
systemctl restart httpd


*检测 http://172.25.254.117==显示应该是/westos/html 's page


**实验完成后恢复环境==取消注释DocumentRoot "/var/www/html"
注意:一定要把新建的/westos/html目录的安全上下文改成跟/var/www/html目录一样,不然访问出来的页面是系统默认的一整页的英文

6.Apache的访问控制
实验素材:

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


检测:访问172.25.254.117/westos===出现页面是:/var/www/html/westos page


1)基于客户端ip的访问控制
ip白名单

vim /etc/httpd/conf/httpd.conf
     DocumentRoot "/var/www/html"
    <Directory "/var/www/html/westos">
          Order Deny,Allow  #先读Deny在读Allow
          Allow from 172.25.254.17  #只允许此ip主机访问,一定要写真实主机的ip
          Deny from all 
    </Directory>
systemctl restart httpd


检测:172.25.254.117/westos/==出现的页面是:/var/www/html/westos page


注意:读的顺序是谁在前先读谁,在写ip的时候一定要写真实主机的ip
ip黑名单

vim /etc/httpd/conf/httpd.conf
   DocumentRoot "/var/www/html"
   <Directory "/var/www/html/westos">
        Order Allow,Deny  #先读Allow
        Allow from 172.25.254.17  
         Deny from all #最后读deny结果是任何ip都不能访问
    </Directory>
systemctl restart httpd


##注意:此实验做完删掉刚才添加的黑白名单
 

2)基于用户认证
cd /etc/httpd/
ls
htpasswd -cm .htauthfile admin  #生成认证,密码123
htpasswd -m .htauthfile lee  #生成认证,密码123
cat .htauthfile  #查看
vim /etc/httpd/conf/httpd.conf  
    <Directory "/var/www/html/westos">
         AuthUserfile /etc/httpd/.htauthfile
         AuthName "Please input username and passwd !!!"
         AuthType basic
   #     Require user lee  #指定lee用户可以访问。那么admin就不可以访问了
         Require valid-user  #指定所有用户都可以访问
   </Directory>

 systemctl restart httpd


注意:当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容

 检测:

7.Apache的虚拟主机
真机中:
    vim /etc/hosts  #设置客户端解析(在浏览器所在的主机中添加)
       172.25.254.117 www.westos.org  linux.westos.org  luck.westos.org


虚拟机中:

  mkdir -p /var/www/westos.org/{linux,luck} #创建存放的目录
   echo linux > /var/www/westos.org/linux/index.html #把linux导入文件
   echo luck > /var/www/westos.org/luck/index.html #把luck导入文件,注意路径
   cat /var/www/westos.org/luck/index.html  #查看文件内容
   cat /var/www/westos.org/linux/index.html 
   cd /etc/httpd/conf.d/  #切换目录
   vim vhost.conf #注意编写此文件时的绝对路径
     <VirtualHost _default_:80>
       DocumentRoot /var/www/html
       CustomLog logs/default.log combined
     </VirtualHost>

 

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

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

 systemctl restart httpd


 *检测www.westos.org===显示内容是hello world
     linux.westos.org ===显示内容是linux
     luck.westos.org ====显示内容是luck

 

 

 

8.Apache的语言支持
1)php
 

cd /var/www/html/
mkdir /var/www/html/php  #建立php目录,注意建立的路径
dnf install php -y  #安装php
systemctl restart httpd  #重启服务【安装完后必须重启程序!!!】
cd php/
vim index.php  #创建php程序,注意路径
  <?php
     phpinfo();
  ?>
systemctl restart httpd


检测:172.25.254.117/php/index.php

2)cgi(perl)
 

mkdir /var/www/html/cgi  #创建cgi目录,一定要注意路径
cd /var/www/html/cgi
vim index.cgi  #编写cgi程序
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print `date`;
perl index.cgi  #执行程序
   Content-type: text/html
   Fri Nov  5 09:58:07 CST 2021
chmod +x /var/www/html/cgi/index.cgi   #增加可执行的权限
vim /etc/httpd/conf.d/vhost.conf   
   <Directory "/var/www/html/cgi">   #路径一定要写正确
      Options +ExecCGI
      AddHandler cgi-script .cgi
      Directoryindex index.cgi
   </Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' #永久修改该目录的安全上下文
restorecon -RvvF /var/www/html/cgi  #刷新


检测:172.25.254.117/cgi/

 

3)wsgi(python)
 

mkdir /var/www/html/wsgi #建立存放目录
vim /var/www/html/wsgi/index.wsgi  #编写swgi程序。内容一定对齐,python对格式要求很严谨
   def application(env,westos):
       westos('200 ok',[('Content-Type', 'text/html')])
       return [b'hello westos']
dnf install python3-mod_wsgi -y   #下载安装
systemctl restart httpd
vim /etc/httpd/conf.d/vhost.conf #编写虚拟机主配置文件
    <VirtualHost *:80>
         ServerName wsgi.westos.org  #服务名字
         WSGIScriptAlias / /var/www/html/wsgi/index.wsgi  #路径写正确
    </VirtualHost>
systemctl restart httpd

 

在主机中:vim /etc/hosts  #一定要是超级用户
          172.25.254.117  wsgi.westos.org


检测:wsgi.westos.org

 

9.Apache的加密访问
 

dnf install mod_ssl -y  #安装加密插件
systemctl restart httpd  #每次下载完之后一定要重启服务
mkdir /etc/httpd/tls  
openssl  req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/www.westos.org.key -x509 -days 365 --out /etc/httpd/tls/www.westos.org.crt【x509 证书格式;-req 请求;-in 加载签证名称】  #生成证书、私钥【不能小于2048】、证书签名文件
vim /etc/httpd/conf.d/ssl.conf  #编写配置文件【指定证书和密钥文件,路径一定要对】
  #85和93行注释掉,然后复制下来改为:
  SSLCertificateFile /etc/httpd/tls/www.westos.org.crt #指定证书。85行内容,路径一定要写正确
  SSLCertificateKeyFile /etc/httpd/tls/www.westos.org.key  #指定密钥文件。93行,路径一定要正确
systemctl restart httpd  #重启服务
mkdir /var/www/westos.org/login  #创建存放的目录 
echo login\'s page > /var/www/westos.org/login/index.html 把login\'s page导入到路径文件中
cat /var/www/westos.org/login/index.html  #查看文件内容
  login's page
vim /etc/httpd/conf.d/vhost.conf  #编写虚拟机主配置文件
   <VirtualHost *:80>
      ServerName login.westos.org
      RewriteEngine on
      RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1  #【^(/.*)$ ##客户地址栏中输入的地址;%{HTTP_HOST} ##客户主机;$1  ##RewriteRule后面跟的第一串字符的值】
   </VirtualHost>

   <VirtualHost *:443> #443是超文本加密传输协议
       ServerName login.westos.org
       DocumentRoot "/var/www/westos.org/login"
       SSLEngine on
       SSLCertificateFile /etc/httpd/tls/www.westos.org.crt
       SSLCertificateKeyFile /etc/httpd/tls/www.westos.org.key
   </VirtualHost>
systemctl restart httpd


在主机中:vim /etc/hosts  #一定要是超级用户
          172.25.254.117  login.westos.org


检测:访问login.westos.org,会自动变为加密的地址 

 

 

 

 

10.squid

 squid 正向代理

正向代理:当被缓存的页面被第二次访问的时候,浏览器将直接从本地代理服务器哪里获取请求数据而不再向原web站点请求数据,这样节省了网络宽带同时也提高了访问速度


需要2台主机,一台主机可以上网(squid代理),一台主机不能上网,不能上网的主机通过可以上网的主机去访问网页
实验效果:让单网卡主机不能上网但浏览器可以访问互联网页
操作:

在双网卡主机nodea中:【一定要确保软件仓库是否搭建成功】

nmcli connection show 
nmcli connection delete Wired\ connection\ 1
cd /etc/sysconfig/network-scripts/
vim ifcfg-ens3   #配置网络ip地址
   DEVICE=ens3
   ONBOOT=yes
   BOOTPROTO=none
   IPADDR=172.25.254.170
   NETMASK=255.255.255.0
   NAME=ens3
   DNS1=114.114.114.114
   GATEWAY=172.25.254.70   
nmcli connection reload
nmcli connection up ens3
nmcli connection show
dnf install squid -y  #下载squid
vim /etc/squid/squid.conf   #修改主配置文件
   第59行改为 http_access allow all
   第65行取消注释 
systemctl start squid   #开启squid服务
firewall-cmd --permanent --add-service=squid    #在火墙中永久添加squid服务
firewall-cmd --reload   #刷新火墙
firewall-cmd --add-masquerade  #打开地址伪装

 

 在单网卡主机nodeb中:【一定要注意路径】

nmcli connection show
nmcli connection delete Wired\ connection\ 1
cd /etc/sysconfig/network-scripts/   #配置网络文件
vim ifcfg-ens3
   DEVICE=ens3
   ONBOOT=yes
   BOOTPROTO=none
   IPADDR=172.25.254.200
   NETMASK=255.255.255.0
   NAME=ens3
nmcli connection reload
nmcli connection up ens3
nmcli connection show
dnf install firefox -y
ping www.baidu.com  #ping不通

   打开火狐,在火狐中设定

   

squid反向代理 

在nodeb中
 

dnf install httpd -y  #下载软件
systemctl start httpd   #开启httpd服务
firewall-cmd --add-service=http    #在火墙中永久设定http
echo 172.25.254.200 > /var/www/html/index.html   把172.25.254.200导入/var/www/html/index.html文件中

在nodea中

vim /etc/squid/squid.conf  #在主配置文件中添加
   http_port 80 vhost vport
   cache_peer 172.25.254.200 parent 80  0 proxy-only
systemctl restart squid.service    #重启squid服务
firewall-cmd --add-service=http    #在火墙中永久添加http

 在火狐上访问

 

标签:本书,httpd,www,第三章,westos,var,html,Apache,org
来源: https://blog.csdn.net/m0_62341392/article/details/121177273

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有