zoukankan      html  css  js  c++  java
  • 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查看及修改文件权限以及相关

  • 相关阅读:
    发布几个DNN模块自己写的,功能还不完善
    c#冒泡排序正解!
    74很多小学生在学习加法时,发现“进位”特别容易出错。你的任务是计算两个三位数在相加时需要多少次进位。你编制的程序应当可以连续处理多组数据,直到读到两个0(这是输入结束标记)。
    69 N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N1)······*2*1.现在你的任务是计算出N!的位数有多少(十进制)?首行输入n,表示有多少组测试数据(n<10)随后n行每行输入一组测试数据 N( 0 < N < 1000000 )
    96已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n1位的数。第一行为M,表示测试数据组数。接下来M行,每行包含一个测试数据。
    101输入两点坐标(X1,Y1),(X2,Y2)(0<=x1,x2,y1,y2<=1000),计算并输出两点间的距离第一行输入一个整数n(0<n<=1000),表示有n组测试数据;随后每组占一行由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开 对于每组输入数据,输出一行结果保留两位
    266 给定一行字符,逆序输出此行(空格.数字不输出)
    98描述 输入一个百分制的成绩M,将其转换成对应的等级,具体转换规则如下:90~100为A;80~89为B;70~79为C;60~69为D;0~59为E;输入 第一行是一个整数N,表示测试数据的组数(N<10)
    50爱摘苹果的小明小明家的院子里有一棵苹果树,每到秋天树上就会结出10个苹果。苹果成熟的时候,小明就会跑去摘苹果。小明有个30厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试。现在已知10个苹果到地面的高度,以及小明把手伸直的时候能够达到的最大高度请帮小明算一下她能够摘到的苹果的数
    100 小南刚学了二进制,他想知道一个数的二进制表示中有多少个1,你能帮他写一个程序来完成这个任务吗?
  • 原文地址:https://www.cnblogs.com/it-tsz/p/9399012.html
Copyright © 2011-2022 走看看