Apache虚拟主机配置详细教程

Posted 宝山的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache虚拟主机配置详细教程相关的知识,希望对你有一定的参考价值。

文章目录

Apache 虚拟主机企业应用

  • 企业真实环境中,一台WEB服务器发布单个网站会非常浪费资源,所以一台WEB服务器上会发布多个网站,少则3-5个,多则2-30。

  • 在一台服务器上发布多哥网站,也称之为部署多个虚拟主机,WEB虚拟主机配置方法有三种:

    • 基于 单个 IP地址 多个 Socket端口
    • 基于 单个 IP地址 一个端口 不同域名
    • 基于 多个 IP地址 一个端口
  • 其中基于同 一个端口不同域名的方式 在企业中得到广泛的使用和应用,如下为 基于一个端口不同域名,在一台Apache WEB服务器上部署多个网站。

部署一个端口不同域名

  • 1、创建虚拟主机配置文件httpd-vhosts.conf

    • 该文件默认已存在,只需要去掉httpd.conf配置文件中#即可
  • `创建网站主机目录

    [root@localhost ~]# mkdir -p /usr/local/apache/htdocs/love1,love2
    
    
    
  • 创建网页内容

     [root@localhost ~]# cd /usr/local/apache/htdocs/
     [root@localhost htdocs]# ls
     love1  love2
     [root@localhost htdocs]# cd love1
     [root@localhost love1]# ls
     love1.zip
     [root@localhost love1]# unzip love1.zip 
     Archive:  love1.zip
     creating: css/
     inflating: css/theme.css           
     creating: images/
     inflating: images/1.gif            
     inflating: images/1.mp3            
     inflating: images/2.jpg            
     inflating: images/3.jpg            
     inflating: images/emoji_bixin.jpg  
     inflating: images/emoji_kelian.jpg  
     inflating: images/music_note_big.png  
     inflating: index.html              
     creating: js/
     inflating: js/jquery-2.0.3.min.js  
     inflating: js/jweixin-1.2.0.js     
     inflating: js/start_onlyyou.js     
     inflating: js/theme_common.js      
     inflating: js/typed.min.js         
    [root@localhost love1]# cd ../love2
    [root@localhost love2]# ls 
    love2.zip
    [root@localhost love2]# unzip love2.zip 
    Archive:  love2.zip
     creating: images/
     inflating: images/1.jpg            
     inflating: index.html              
     creating: js/
     inflating: js/yiqi.js              
     creating: style/
     inflating: style/main.css          
    [root@localhost love2]# 
    
    
  • 修改配置文件

    [root@localhost love1]# pwd
    /usr/local/apache/htdocs/love1
    [root@localhost love1]# vim ../../conf/extra/httpd-vhosts.conf 
    [root@localhost love1]# cat ../../conf/extra/httpd-vhosts.conf 
    <VirtualHost *:80>
      ServerAdmin rivers@163.com
      DocumentRoot "/usr/local/apache/htdocs/love1"
      ServerName www.love1.lan1.com
      ServerAlias www.biaobai.com
      ErrorLog "love1.com-error_log"
      CustomLog "logs/love1.com-access_log" common
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerAdmin rivers@163.co
      DocumentRoot "/usr/local/apache/htdocs/love2"
      ServerName www.love2.lan2.com
      ErrorLog "logs/love2.com-error_log"
      CustomLog "logs/love2.com-access_log" common
    </VirtualHost>
    [root@localhost love1]# 
    
  • 重启服务

    [root@localhost love1]# /usr/local/apache/bin/apachectl stop
    [root@localhost love1]# /usr/local/apache/bin/apachectl start  
    
  • 测试:

  • 输入:www.love1.lan1.com 访问

  • 输入:www.love2.lan2.com

调试worker工作模式

<IfModule worker.c>
# Worker MPM引擎配置段;
StartServers        8
#默认启动Apache 工作进程数
MaxClients         2000	
#每秒支持的最大客户端并发;
MinSpareThreads     25	
#最小空闲线程数;
MaxSpareThreads     60			
#最小空闲线程数;
ThreadsPerChild     40	
#每个进程启动的线程数;
MaxRequestsPerChild  0
#每个进程能处理的最大请求数,0表示无限制;
</IfModule>

进行压力测试

  • 测试工具 ab
  • 常用参数:
    • -c 并发多少次
    • -n 请求次数
    [root@localhost apache]# ./bin/ab -c 1000 -n 10000 http://www.love2.lan2.com/index.html
    This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.love2.lan2.com (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests
    
    
    Server Software:        Apache/2.4.46
    Server Hostname:        www.love2.lan2.com
    Server Port:            80
    
    Document Path:          /index.html
    Document Length:        585 bytes
    
    Concurrency Level:      1000
    Time taken for tests:   1.908 seconds
    Complete requests:      10000
    Failed requests:        0
    Total transferred:      8310000 bytes
    HTML transferred:       5850000 bytes
    Requests per second:    5240.58 [#/sec] (mean)
    Time per request:       190.819 [ms] (mean)
    Time per request:       0.191 [ms] (mean, across all concurrent requests)
    Transfer rate:          4252.85 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0   51 212.4      1    1009
    Processing:    11   53 175.6     16    1618
    Waiting:        8   53 175.6     15    1618
    Total:         12  105 288.7     17    1723
    
    Percentage of the requests served within a certain time (ms)
      50%     17
      66%     19
      75%     23
      80%     31
      90%    203
      95%   1019
      98%   1219
      99%   1419
     100%   1723 (longest request)
    [root@localhost apache]# ./bin/ab -c 1000 -n 50000 http://www.love2.lan2.com/index.html
    This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.love2.lan2.com (be patient)
    Completed 5000 requests
    Completed 10000 requests
    Completed 15000 requests
    Completed 20000 requests
    Completed 25000 requests
    Completed 30000 requests
    Completed 35000 requests
    Completed 40000 requests
    Completed 45000 requests
    apr_socket_recv: Connection reset by peer (104)
    Total of 49944 requests completed
    [root@localhost apache]# 
    
    

权限设置

  • 两种权限设置方法

    <VirtualHost *:80>
    ServerAdmin rivers@163.com
    DocumentRoot "/usr/local/apache/htdocs/love1"
    ServerName www.love1.lan1.com
    ServerAlias www.biaobai.com
    ErrorLog "love1.com-error_log"
    CustomLog "logs/love1.com-access_log" common
        <Directory /usr/local/apache/htdocs/love1>
                AllowOverride ALL
                Options Indexes FollowSymLinks
                Order allow,deny
                Allow from 192.168.10.10
        </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
    ServerAdmin rivers@163.co
    DocumentRoot "/usr/local/apache/htdocs/love2"
    ServerName www.love2.lan2.com
    ErrorLog "logs/love2.com-error_log"
    CustomLog "logs/love2.com-access_log" common
        <Directory /usr/local/apache/htdocs/love2>
                <RequireAll>
                  Require all granted
                  Require not ip 192.168.10.10
                </RequireAll>
        </Directory>
    
  • 测试结果
    [root@localhost extra]# curl http://www.love2.lan2.com/index.html
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>LOVE</title>
    <link href="style/main.css" rel="stylesheet" >
    </head>
    <style>
    	
    </style>
    <body>
    	<div class="box" align="center">
    		<p>我观察你很久了</p>
    		<h1>小姐姐做我对象好不好?</h1>
    		<img src="images/1.jpg">
    		<div class="bottom">
    			<div class="left" id="hao"></div>
    			<div class="right" id="buhao">不好</div>
    		</div>
    	</div>
    </body>
    	<script type="text/javascript" src="js/yiqi.js"></script>
    </html>
    [root@localhost extra]# 
    
    
    [root@localhost ~]# curl http://www.love2.lan2.com/index.html
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don't have permission to access this resource.</p>
    </body></html>
    [root@localhost ~]# 
    

Apache 配置文件详解

  • Apache 的配置文件 是Apache WEB难点,读者需要掌握配置文件中每个参数的含义,理解日常运维中遇到的故障,并解决问题。
 ServerTokens OS            服务器的版本和操作系统而已,不需要改动他,如果不在乎你的系统信息被远程用户查询到,则可以将这个项目注释掉(不建议)
 
 ServerRoot  "/etc/httpd"   服务器设置的最顶层目录,有点类似于chroot那种感觉。包括logs , modules等的数据都应该要放置在此目录下面(如果这些配置没有声明成绝对路径的话)
 
 PidFile run/httpd.pid      放置PID的文件,可方便apache软件的管理。只有相对路径考虑
 
 ServerRoot                 设置值,所以文件在/etc/httpd/run/httpd.pid
 
 Timeout 60                 不论接收或发送,当持续连接等待超过60秒则该次连接就中断一般来说,此数值在300秒左右即可,不需要修改这个原始值
 
 KeepAlive Off              这里最好把默认值“Off”修改为“On”。这里表示是否允许持续性的连接,也就是一个TCP连接可以具有多个文件资料传送的要求
 
 MaxKeepAliveRequests 100    可以将默认的100改成500或更高与上一个设置的值KeepAlive有关,当KeepAlive的值设置为On的时候,这个数值可以决定。该次连接能够传输的最大传输数量。为了提高效率则可以改大一点。0代表不限制
 
 KeepAliveTimeout 65  在KeepAlive设置为“On”的情况下,该次连接在最后一次传输后等待延迟的秒数,当超过该秒数的时候该连接中断。保持默认值15即可,如果设置的值太高(等待时间较长),在较忙碌的系统上面将会有较多的Apache程序占用资源,可能有效率方面的问题。
 
 
 <IfModule prefork.c>
 StartServers      8				启动Apache的时候,唤醒几个PID来处理服务的。#Apache使用了进程预派生的技术来处理请求,大大提高了响应速度,
 MinSpareServers   5				最小预备使用的PID数量
 MaxSpareServers   20		    最大预备使用的PID数量
 ServerLimit       4096			服务器的限制
 MaxClients        4096			最多可以有多少个客户端同时连接到Apache
 Maxrequestsper Child 4000       每个进程能处理的最大请求数
 Loadmodule mod version.so       静态加载 apache相关模块
 erverAdminsupport@ifed.net      管理员邯箱,网站异常,错误信息会发生至该邯箱
 <IfModule>

Apache Rewrite 规则讲解