Apache的管理及优化web

一.Apache的作用

在web被访问时通常使用http://的方式

http://                 超文本传输协议

http://  超文本传输协议提供软件

Apache

ngix

stgw

jfe

Tengine

二.Apache的安装

dnf install httpd.x86_64 -y

三.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                            刷新火墙使设定生效

四.Apache的基本信息

服务名称:    httpd

配置文件:    /etc/httpd/conf/httpd.conf     主配置文件

                      /etc/httpd/conf.d/*.conf       子配置文件

默认发布目录:/var/www/html

默认发布文件:index.html    修改167行

默认端口    :80    #http

                      443   #https

用户        :Apache日志        :/etc/httpd/logs
/etc/hosts

五.Apache的基本配置

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

2.默认发布文件

vim /etc/httpd/conf/httpd.conf

DirectoryIndex westos.html index.html

systemctl restart httpd

3.默认发布目录

vim /etc/httpd/conf/httpd.conf

DocumentRoot "/westos/html"

<Directory "/westos/html"

                   Require all granted

</Directory>

semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'

restorecon -RvvF /westos/

systemctl restart httpd

firefox http://172.25.254.20

六.Apache的访问控制

实验素材

mkdir /var/www/html/westos

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

<h1>westos's page</h1>

firefox http://172.25.254.20/westos

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

ip白名单

vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/westos">

Order Deny,Allow

Allow from 172.25.254.10

 Deny from All

</Directory>

ip黑名单

<Directory "/var/www/html/westos">

Order Allow,Deny

Allow from All

Deny from 172.25.254.10

</Directory>

2.基于用户认证

vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/westos">

AuthUserfile /etc/httpd/htpasswdfile           指定认证文件

AuthName "Please input your name and passwd"   认证提示

AuthType basic                                 认证类型

Require user admin                             允许通过的认证用户   二选一

 Require valid-user                             允许所有用户通过认证 二选一

</Directory>

htpasswd -cm /etc/httpd/htpasswdfile admin          生成认证文件

注意: 

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

七.Apache的虚拟主机

mkdir /var/www/virtual/westos.com/{news,music}

echo news > /var/www/virtual/westos.com/news/index.html

echo music > /var/www/virtual/westos.com/music/index.html

echo westos > /var/www/html/index.html

vim /etc/httpd/Vhost.conf   ##修改发布目录的配置文件

<VirtualHost _default_:80>
     
          DocumentRoot "/var/www/html" 
     
          CustomLog logs/default.log combined 

</VirtualHost>
 

<VirtualHost *:80> 
     
          ServerName music.westos.com 
     
          DocumentRoot "/var/www/westos.com/music" 
     
           CustomLog logs/music.log combined 

</VirtualHost>
 

<VirtualHost *:80> 
     
           ServerName news.westos.com 
     
          DocumentRoot "/var/www/westos.com/news" 
     
           CustomLog logs/news.log combined 

</VirtualHost>

systemctl restart httpd

测试:在浏览器所有主机中

vim /etc/hosts

172.25.254.20 www.westos.com news.westos.com music.westos.com

firefox访问

八.Apache的语言支持

php 

vim /var/www/html/index.php

<?php

       phpinfo();

?>

dhf search php

dnf install php -y

systemctl restart httpd

firefox http://172.25.254.20/index.php

cgi

mkdir /var/www/html/cgidir

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

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print `date`

semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgidir(/.*)?'

restorecon -RvvF /var/www/html/cgidir

chmod +x index.cgi

./index.cgi

vim /etc/httpd/conf.d/vhosts.conf

<Directory "/var/www/html/cgidir/">

           Options +ExecCGI

           AddHandler cgi-script .cgi

</Directory>

systemctl restart httpd

firefox http://172.25.254.20/cgidir/index.cgi

九.Apache的加密方式

安装加密插件

dnf install mod_ssl -y

生成证书    

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 

vim /etc/httpd/conf.d/ssl.conf

SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt

SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key

ll /etc/pki/tls/private/www.westos.com.key

chmod 644 /etc/pki/tls/private/www.westos.com.key

systemctl restart httpd

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

<VirtualHost *.80>

        ServerName login.westos.com

        RewriteEngine on

        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1

</VirtualHost>

<VirtualHost *.443>

          ServerName login.westos.com

          DocumentRoot "/www.westos.com/login"

           CustomLog logs/login.log combined

            SSLEngine on

            SSLCertficateFile /etc/pki/tls/certs/www.westos.com.crt

            SSLCertficateKeyFile /etc/pki/tls/private/www.westos.com.key

</VirtualHost>

systemctl restart httpd

^(/.*)$     客户地址栏中输入的地址

%{HTTP_HOST}     客户主机

$1          RewriteRule后面跟的第一串字符的值 

 十.squid 正向代理

1.实验环境:

单网卡主机设定ip不能上网

双网卡主机设定ip1为仅主机模式,设定ip2为桥接模式

2.实验效果:

让单网卡主机不能上网但浏览器可以访问互联网页

3.操作步骤:

在双网卡主机172.25.254.30中 

dnf install squid -y 

vim /etc/squid/squid.conf 

59 http_access allow all                         #允许任何人

65 cache_dir ufs /var/spool/squid 100 16 256     #缓存文件地址

systemctl restart squid 

firewall-cmd --permanent --add-port=3128/tcp     

firewall-cmd --reload

4.测试:

在单网卡主机中访问172.25.254.30

十一.squid反向代理

1.实验环境:

172.25.254.20        #Apache服务器

172.25.254.30        #squid,没有数据负责缓存

2.修改配置文件:

vim /etc/squid/squid.conf 

http_port 80 vhost vport  ##vhost 支持虚拟域名 vport 支持虚拟端口 

#当172.25.254.30的80端口被访问会从172.25.254.20的80端口缓存数据 

cache_peer 172.25.254.20 parent  80      0       proxy-only


 
systemctl restart squid

3.测试:

在客户主机172.25.254.10中访问squid主机172.25.254.30

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值