mac下配置Apache虚拟域名方案,以及遇到的坑

  1、 配置Apache虚拟域名

   1.执行    sudo vi /etc/apache2/httpd.conf 开始配置httpd.conf 的文件;

      //配置listen 80端口(默认配置),此处可以修改监听端口,例如Listen 81

      

     2.打开相应 LoadModule

LoadModule userdir_module libexec/apache2/mod_authn_core.so   //默认已经打开
LoadModule php5_module libexec/apache2/mod_authz_host.so      //默认已经打开,供基于主机名、IP地址、请求特征的访问控制
LoadModule userdir_module libexec/apache2/mod_userdir.so //允许用户从自己的主目录中提供页面(使用"/~username")     
LoadModule php5_module libexec/apache2/libphp5.so      //php模块

    3.配置Document路径执行你的工程目录文件(重要)    

DocumentRoot "/Users/mac0011/Desktop/apatcl"
<Directory "/Users/mac0011/Desktop/apatcl">
    #
    # 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 FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted                        
</Directory>
 1.设置AllowOverride None 改为 AllowOverride All;

2. Apache2.2版本写法 Order allow,deny allow from all Apache2.4版本写法,注意只需要这一句话就可以了 Require all granted 

    

    4.打开虚拟域名配置文件,把#Include /private/etc/apache2/extra/httpd-vhosts.conf中的#去掉  

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

      

    5.配置httpd-vhosts.conf文件  执行 sudo vi /etc/apache2/extra/httpd-vhosts.conf

     

NameVirtualHost 127.0.0.1:80

<Directory "/Users/mac0011/Desktop/apatcl">
    #
    # 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 FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
#    Order allow,deny
    Require all granted
</Directory>

<VirtualHost 127.0.0.1:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/Users/mac0011/Desktop/apatcl"
    ServerName hailong.com
    ErrorLog "/Users/mac0011/Desktop/apatcl/error_log"
    CustomLog "/Users/mac0011/Desktop/apatcl/access_log" common
#      <Directory "/Users/mac0011/Desktop/apatcl">
#        Options Indexes FollowSymLinks Multiviews
#        AllowOverride All
#        Require all granted
#      </Directory>
</VirtualHost>

 

1.NameVirtualHost 127.0.0.1:80,使用NameVirtualHost 配置具体的域名ip;  127.0.0.1指本机ip即回调地址;

2.如果配置的多个虚拟域名为同一工程目录下,可以将<Directory></Directory>提出来,放到上面同一配置,也可以单独为每一个虚拟域名 单独配置

3.<Directory></Directory>中添加Require all granted.Apache 2.2版本的参考上面的写法;

 

    6.配置hosts文件 执行sudo vi /etc/hosts

    

   

    7.重启Apache,sudo apachectl restart

 

    8.其他:

       httpd -S  可以定位一下,apache配置过程中的出现的配置问题

       OK!!至此,完成apache虚拟域名的配置

 

  

 2、 遇到的坑,关于403拒绝访问的解决方案

    1.apache2.2和apache2.4配置不同处理问题

       Apache2.2版本写法  
            Order allow,deny  
            allow from all  
        Apache2.4版本写法,注意只需要这一句话就可以了
            Require all granted   
本次在配置时遇到的主要问题就是,楼主在apache2.4版本时单纯的把allow from all替换成了 Require all granted,而没有吧 order allow deny给屏蔽掉,导致访问的时候一直出现403的错误;主要还是太粗心了;


   2.配置多个虚拟域名时,尽量不要采用通配符 *,使用
NameVirtualHost为这个基于域名的虚拟主机指定IP地址集
  也在/etc/hosts上分别为两个虚拟主机指向了本机IP127.0.0.1,但是第一个虚拟主机总是会覆盖第二个甚至覆盖localhost,效果就是不管访问那个域名最后都会指向dummy-host1.example.com域名的应用,
最后弄了好久才发现要在同一ip上配多个基于域名的虚拟主机需要这样做,必须用NameVirtualHost指令为这个基于域名的虚拟主机指定IP地址集,而且每一个虚拟主机不能使用*通配符,必须指定ip

   

     3.工程目录权限的问题(记住是每一级的目录都要设置)

                  chmod 755或者chmod 777或者chmod o+x  对每一个工程设置好读写权限;

                   

  4、 虚拟目录

    【这个问题我没遇到过,因为我没这样写过,网上资料这么写,可作为参考】

    如果设置的是虚拟目录,那么你需要在httpd.conf中定义一个虚拟目录,而且像极了如下的片段:

    Alias /folder "/usr/local/folder"                       
    <Directory "/usr/local/folder">                             
      Options FollowSymLinks                                
      AllowOverride None                                  
      Order deny,allow                                   
      Deny from all                                     
      Allow from 127.0.0.1 192.168.1.1                       
    </Directory>   

如果是这一种情况,而且你写得类似我上面的代码,三处folder都是一样一样的,那绝对会是403!怎么解决呢,就是把跟在Alias后面斜杠后面的那串改了,改成什么,不要和虚拟目录的文件夹同名就好,然后我就可以用改过后的虚拟目录访问了,当然,改文件夹也行,只要你不怕麻烦,只要Alias后面的虚拟目录定义字符(红色)和实际文件夹名(黑色)不相同就OK。

   5、selinux的问题

     如果依然是403,那就是selinux在作怪了,于是,你可以把你的目录进行一下selinux权限设置。

      今天我遇到的就是这个问题了。

      #chcon -R -t httpd_sys_content_t /usr/local/site#chcon -R -t httpd_sys_content_t /usr/local/site/test

    网上资料说不过,这一步大多不会发生。但我的问题确实是,可能跟系统有关,具体原理还不是很懂。

  6、wsgi的问题

  我的虚拟主机配置为:

  <VirtualHost *:80>
   WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgiAlias /static/ /srv/lxyproject/collectedstatic/ServerName 10.1.101.31
     #ServerName example.com#ServerAlias www.example.com
    <Directory /srv/lxyproject/collectedstatic>  
    Options Indexes  FollowSymLinks   
     AllowOverride None    
    Require all granted
    </Directory>
    <Directory /srv/lxyproject/wsgi/>    
    Allow from all
    </Directory>
    ErrorLog   /etc/httpd/logs/lxyproject.error.logLogLevel warn
  </VirtualHost>

  我访问

                              

   log报错:

  client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi

  解决办法:

  修改<Directory /srv/lxyproject/wsgi/>中Allow from all为:Require all granted

     这个问题是因为版本的原因,

  我的httpd版本为:

  [root@yl-web conf.d]# rpm -qa |grep httpdhttpd-devel-2.4.6-31.el7.centos.x86_64httpd-tools-2.4.6-31.el7.centos.x86_64httpd-2.4.6-31.el7.centos.x86_64

  而2.3以下版本用Allow from all,2.3及以上版本为Require all granted。

  <Directory /home/aettool/aet/apache>  <IfVersion < 2.3 >   Order allow,deny   Allow from all  </IfVersion>  <IfVersion >= 2.3>   Require all granted  </IfVersion></Directory>

   参考:http://stackoverflow.com/questions/17766595/403-forbidden-error-with-django-and-mod-wsgi

 

  7.添加多个类型的文件

    修改前

    <IfModule dir_module>

        DirectoryIndex index.html

    </IfModule>

    修改后:

    <IfModule dir_module>

      DirectoryIndex index.php index.php3 index.html index.htm

    <IfModule dir_module>

 

 

 3、 相关参考博客      

  https://wiki.apache.org/httpd/ClientDeniedByServerConfiguration

  http://www.cnblogs.com/AloneSword/archive/2013/03/01/2939564.html

  https://discussions.apple.com/docs/DOC-3083                

  http://www.th7.cn/system/lin/201507/122784.shtml          //apache httpd服务器403 forbidden的问题

  http://blog.csdn.net/yanzi1225627/article/details/45075265  //如何将apache的这个默认目录更改到用户目录下。 

  http://www.cnblogs.com/wenqiangwu/archive/2013/09/12/3317030.html   //linux查看及修改文件权限以及相关

 

 

      

posted on 2017-01-18 10:14  lolDragon  阅读(1653)  评论(0编辑  收藏  举报

导航