zoukankan      html  css  js  c++  java
  • linux apache2.4 配置虚拟主机

    1. 端口监听

    apache的端口监听设置,是指设定apache这个软件针对当前服务器的哪些端口提供服务。通常浏览器提供的都是web请求,但有些请求不在apache的服务范围之内(端口不符)

    1.1 设置监听多端口

    直接在httpd.conf中添加监听的端口即可。

    #
    # Listen: Allows you to bind Apache to specific IP addresses and/or
    # ports, instead of the default. See also the <VirtualHost>
    # directive.
    #
    # Change this to Listen on specific IP addresses as shown below to
    # prevent Apache from glomming onto all bound IP addresses.
    #
    #Listen 12.34.56.78:80
    Listen 80
    #添加要监听的端口
    Listen 8080

    2. 主机配置

    一个主机,最核心的就是两件事:

    1. 主机(站点)的名字:ServerName “主机名” 
      • 主机(站点)的实际文件夹位置:DocumentRoot “站点的实际完整路径”

    apache的作用其实就是一个”转换”的角色:将当前电脑中的某个文件夹,对外以某个域名(站点)的方式展现出来。换句话说:站点的本质就是一个文件夹。

    #
    # ServerName gives the name and port that the server uses to identify itself.
    # This can often be determined automatically, but we recommend you specify
    # it explicitly to prevent problems during startup.
    #
    # If your host doesn't have a registered DNS name, enter its IP address here.
    #
    #ServerName localhost:8080
    ServerName localhost
    
    #
    # Deny access to the entirety of your server's filesystem. You must
    # explicitly permit access to web content directories in other
    # <Directory> blocks below.
    #
    <Directory />
        AllowOverride none
        Require all denied
    </Directory>
    
    #
    # Note that from this point forward you must specifically allow
    # particular features to be enabled - so if something's not working as
    # you might expect, make sure that you have specifically enabled it
    # below.
    #
    
    #
    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    #修改默认的配置,设置为E:/www为站点根目录
    DocumentRoot "E:/www"
    <Directory "E:/www">
        #
        # Possible values for the Options directive are "None", "All",
        # or any combination of:
        #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        #
        # Note that "MultiViews" must be named *explicitly* --- "Options All"
        # doesn't give it to you.
        #
        # The Options directive is both complicated and important.  Please see
        # http://httpd.apache.org/docs/2.4/mod/core.html#options
        # for more information.
        #用于显示设定“可显示文件列表”(当无可显示网页的时候)
        Options Indexes FollowSymLinks
    
        #
        # AllowOverride controls what directives may be placed in .htaccess files.
        # It can be "All", "None", or any combination of the keywords:
        #   Options FileInfo AuthConfig Limit
        # 允许.htaccess文件覆盖设置
        AllowOverride All
    
        #
        # Controls who can get stuff from this server.
        # 控制谁能访问这台服务器,这是apache2.4中的用法
        # 原来版本为
        # Order deny,allow
        # Allow from all
    
        Require all granted
    </Directory>

    当一个请求通过域名解析进入到当前apache并端口配置成功后,apache就会开始”提供服务”。

    1. 在站点设置中找到ServerName项,看是否匹配请求中的主机名 
      • 如果找到,则在对应的目录(DocumentRoot配置项)中找相应的文件
      • 如果找到,则返回该文件(或调用php语言模块执行后返回)
      • 如果第2步没有找到对应请求中的主机名,那么将第一个主机当作准备提供服务的主机

    因此:只要ip解析和端口能够进入当前服务器并匹配apache端口设定,则apache一定会提供服务,即使主机名不匹配

    当修改主机根目录后访问可能会造成Forbidden,You don't have permission to access / on this server.这是由于文件夹的访问是有权限的,初始的站点所对应的文件夹,安装的时候已经设置好权限了。现在改了,需要进行设置。


    3. 配置文件夹访问权限

    权限中部分设置见2中的代码内容。以下内容apache可以设定“默认网页”以提供给用户

    #
    # DirectoryIndex: sets the file that Apache will serve if a directory
    # is requested.
    #
    <IfModule dir_module>
        #这里是对全局(所有站点,所有文件夹)都起作用
        DirectoryIndex index.html index.php index.htm
    </IfModule>

            此时,对于没有指定要访问明确的网页请求,会按顺序从前往后找这些文件,找到后就在返回给用户浏览器中显示出来。但是如果没有找到,此时,Options中的Indexes发挥作用:显示该文件夹中所有文件和文件夹 

    我们可以将DirectoryIndex设置项单独放在站点文件夹区域设置中,则只对该单独的站点或文件夹起作用

    <Directory "E:/www">
        #用于显示设定“可显示文件列表”(当无可显示网页的时候)
        Options Indexes FollowSymLinks
    
        # 允许.htaccess文件覆盖设置
        AllowOverride All
    
        # 控制谁能访问这台服务器,这是apache2.4中的用法
        # 原来版本为
        # Order deny,allow
        # Allow from all
        Require all granted
    
        #设置默认显示页面,只对该文件夹(及其子文件夹)有效
        DirectoryIndex phpinfo.php
    </Directory>

    4. 主机别名设置

    在应用中,通常用一下两种方式来访问同一个站点: 
    http://www.chris.com 
    http://chris.com

    此时,就相当于”2个站点(主机名)”但访问的是同一个内容

    这需要使用主机别名来实现,在多站点设置中会使用到

    ServerAlias 别名1 别名2 别名3

    当然,如果在本机上访问,记得配置对应的hosts文件,否则仍然无效

    5. 目录别名设置

    目录别名也叫虚拟目录,对于实际存在的目录,可以直接按正常的文件夹访问层级关系来访问,但是对于不存在的目录,我们可以通过配置项,做到对外看起来存在一样:

    比如:http://www.study.com/soft/ 站点中不存在该目录,正常访问会出现NOT FOUND,但是可以通过设置让该地址访问

    <IfModule alias_module>
        #Alias /soft "真实路径"
        Alias /soft "E:/www/study/study/"
    </IfModule>

    设置之后会出现Forbidden,解决方法和2中一样。


    6. 文件夹访问控制的文件控制方式

    通常,我们在config配置文件中,使用Directory配置项,目的是用来控制文件夹的访问权限。 
    但我们也可以使用一个独立的文件来控制某文件夹的访问权限。 
    该文件名必须是: .htaccess

    注意:

    1. 只有后缀和点号(无文件名部分) 
      • 该文件必须放在要被控制访问权限的文件夹中(不同的文件夹可以放不同的该文件)
      • 其“上级文件夹”(通常是Directory设定中的文件夹)必须使用如下代码允许.htaccess发挥作用: 
        AllowOverride All
      • .htaccess文件中出现代码,几乎可以跟Directory设定中出现的代码一样。
      • 如果.htaccess文件有效,则其设置会覆盖其上级设置。
      • 此.htaccess文件修改后可以立即发挥作用,无需重启apache

    7. 多站点设置

    7.1 在http.conf中打开多站点配置模块

    # Virtual hosts
    #Include conf/extra/httpd-vhosts.conf

    将其前面的分号去掉改为:

    # Virtual hosts
    Include conf/extra/httpd-vhosts.conf
    如果在httpd.conf中找不到改行,自己添加也行。

    7.2 在httpd-vhosts.conf文件中修改默认站点根目录

    这是对于apache2.4来说,如果你原先httpd.conf中的根目录修改了,那么这里也要改,因为打开多站点功能后该设置会覆盖httpd.conf文件中的部分设置。否则可能会出现Forbidden。

    #下一行为默认设置,端口默认为80,必须为httpd.conf中你监听的端口
    <VirtualHost _default_:80>
    #下一行为默认设置,由于我的默认站点根目录修改了,所以在这里也要将它改掉,否则会出现Forbidden
    #DocumentRoot "${SRVROOT}/htdocs"
    DocumentRoot "E:/www"
    #ServerName www.example.com:80
    </VirtualHost>

    7.3 配置站点

    你可以按照它给的例子

    
    #<VirtualHost *:80>
    #    ServerAdmin webmaster@dummy-host.example.com
    #    DocumentRoot "${SRVROOT}/docs/dummy-host.example.com"
    #    ServerName dummy-host.example.com
    #    ServerAlias www.dummy-host.example.com
    #    ErrorLog "logs/dummy-host.example.com-error.log"
    #    CustomLog "logs/dummy-host.example.com-access.log" common
    #</VirtualHost>

    以下是我的设置:

    #下一行"*:80"在httpd.conf的配置文件中必须监听该端口
    <VirtualHost *:80>
        #设置主机名
        ServerName www.study.com
        #设置主机别名,即用该别名也可以访问(前提是域名解析正确)
        ServerAlias study.com
        #设置该站点根目录
        DocumentRoot "E:/www/study"
        #设置文件夹访问控制,其路径要和上一行的DocumentRoot一样,
        <Directory "E:/www/study">
            #用于显示设定“可显示文件列表”(当无可显示网页的时候)
            Options Indexes
            #启用文件夹访问控制的文件.htaccess设置
            AllowOverride All
            #请求控制
            Require all granted
            #默认打开的页面设置
            DirectoryIndex index.php index.html
        </Directory>
    </VirtualHost>
    
    #···依照上面的方法,可以设置多个站点
  • 相关阅读:
    文件夹打开对话框
    文件打开对话框
    HOOK函数(二)——全局HOOK
    HOOK函数(一)——进程内HOOK
    抓包
    List 访问
    坑爹的EL 表达式。
    tomcat 虚拟目录的安全问题
    框架
    程序员相关词汇
  • 原文地址:https://www.cnblogs.com/xiaochongzi/p/8186327.html
Copyright © 2011-2022 走看看