【问题标题】:VirtualHost is not working under Apache 2.4.6 on CentOS 7VirtualHost 在 CentOS 7 上的 Apache 2.4.6 下无法运行
【发布时间】:2014-10-25 20:22:49
【问题描述】:

我正在尝试在 CentOS 7 上的 Apache 2.4.6 中设置一些 VH,但没有成功,因为它不起作用。这是我到目前为止所尝试的:

  • 因为在/etc/httpd/conf/httpd.conf 中是这一行Include conf.modules.d/*.conf 然后我在/etc/httpd/conf.d/vhost.conf 下创建一个文件并将其放入其中:

    NameVirtualHost *:80
    
    <VirtualHost *:80>
         ServerName webserver
         ServerAlias localhost devserver development
         DocumentRoot /var/www/html
    </VirtualHost>
    
  • 重新加载/重启 Apache 服务(都试过了):

    service httpd reload|restart
    
  • 在 Windows 端编辑文件 C:\Windows\system32\drivers\etc\hosts 并添加这一行:

    192.168.3.131  webserver localhost devserver development # this is the IP of Apache Server
    
  • 打开浏览器并尝试:http://webserverhttp://devserver 都进入默认的 Apache 页面,因此 VH 无法正常工作。

  • /var/www/html/index.php 下放置一个文件&lt;?php phpinfo(); ?&gt; 只是为了知道Apache 正在加载哪些模块,结果如下:

    core mod_so http_core mod_access_compat mod_actions mod_alias mod_allowmethods mod_auth_basic mod_auth_digest 
    mod_authn_anon mod_authn_core mod_authn_dbd mod_authn_dbm mod_authn_file mod_authn_socache mod_authz_core 
    mod_authz_dbd mod_authz_dbm mod_authz_groupfile mod_authz_host mod_authz_owner mod_authz_user mod_autoindex 
    mod_cache mod_cache_disk mod_data mod_dbd mod_deflate mod_dir mod_dumpio mod_echo mod_env mod_expires mod_ext_filter 
    mod_filter mod_headers mod_include mod_info mod_log_config mod_logio mod_mime_magic mod_mime mod_negotiation 
    mod_remoteip mod_reqtimeout mod_rewrite mod_setenvif mod_slotmem_plain mod_slotmem_shm mod_socache_dbm 
    mod_socache_memcache mod_socache_shmcb mod_status mod_substitute mod_suexec mod_unique_id mod_unixd mod_userdir 
    mod_version mod_vhost_alias mod_dav mod_dav_fs mod_dav_lock mod_lua prefork mod_proxy mod_lbmethod_bybusyness 
    mod_lbmethod_byrequests mod_lbmethod_bytraffic mod_lbmethod_heartbeat mod_proxy_ajp mod_proxy_balancer mod_proxy_connect 
    mod_proxy_express mod_proxy_fcgi mod_proxy_fdpass mod_proxy_ftp mod_proxy_http mod_proxy_scgi mod_systemd mod_cgi mod_php5 
    

显然 mod_vhost 已加载但无法正常工作,我错过了什么吗?有什么帮助或建议吗?也许我忘记了什么,但我阅读了 Apache 文档并没有找到有用的东西

更新:test1

我对 VH 定义做了一些更改,现在这就是我所拥有的:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName webserver
    #ServerAlias localhost devserver development

    <Directory "/var/www/html">
        Options FollowSymLinks Includes ExecCGI
        AllowOverride All
        Allow from all

        #Require local
        #Require 192.168.3.0/16
        #Require 192.168.1.0/16
    </Directory>
</VirtualHost>

但是我得到了一个403 Forbidden

禁止

您无权访问此服务器上的 /index.php。

这里出了什么问题?

【问题讨论】:

    标签: php linux centos apache2 apache2.4


    【解决方案1】:

    为了详细说明jap1968 的帖子,CentOS 7 附带了 SELinux 的痛苦级别设置为enforcing。当完全正常的服务配置静默失败时,这会导致各种混乱 (Apache)。

    要禁用 SELinux,您需要:

    0) [可选] 破解一个 shell 并成为 root ......或者享受一个闪亮的新的、超级有趣的配置 sudo 让你做“root 的东西” “ 项目。大概吧。

    su -l
    

    1) 获取SELinux 的当前状态。运行sestatus

    sestatus
    

    2) 如果 SELinux 导致脱发和过早衰老,您会得到如下结果:

    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   enforcing
    Mode from config file:          enforcing
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      28
    

    3) 编辑/etc/selinux/config 文件。将SELINUX=enforcing 更改为SELINUX=permissive。这样做会让您在下次重新启动时获得无尽的快乐。你最终会得到这样的结果:

    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    # SELINUX=enforcing
    # ===> VOODOO HERE <===
    SELINUX=permissive
    # ===> END VOODOO  <===
    #
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    

    4) 禁用 SELinux。您可以在此时重新启动,但告诉 SELinux 从折磨您中抽出时间更容易。运行 setenforce 以重置 SELinux 的强制级别以匹配 /etc/selinux/config 文件:

    setenforce 0
    

    5) 再次检查sestatus

    sestatus
    

    如果一切按预期进行,sestatus 将返回如下内容:

    SELinux status:                 enabled
    SELinuxfs mount:                /sys/fs/selinux
    SELinux root directory:         /etc/selinux
    Loaded policy name:             targeted
    Current mode:                   permissive
    Mode from config file:          permissive
    Policy MLS status:              enabled
    Policy deny_unknown status:     allowed
    Max kernel policy version:      28
    

    6) 重新启动 Apache。如果您的虚拟主机的域名解析到您正在使用的服务器,您将看到闪亮的新虚拟主机:

    # Restart apache:
    systemctl restart httpd.service
    
    # Be lazy by checking your virtual host from the command line:
    curl www.example.com/new-file-that-only-exists-in-your-new-vhost.txt
    

    6.5) 停止阅读这里。或者不要。我是留言板帖子,不是你妈妈。

    以下所有内容都超出了原始问题的范围,仅包括在内,因为您确实应该在启用 SELinux 的情况下运行

    7) 努力重新启用 selinux。从查看 selinux 日志开始,看看一些很棒的字母汤:

    tail -f /var/log/audit/audit.log
    

    8) SELinux 对功能的深度、大量名称不佳的实用程序以及丑陋的 UX 混乱感到惊讶。在你潜入水中之前,你可能应该穿上你的大男孩裤子,喝一整壶咖啡。这里有一些信息:

    【讨论】:

      【解决方案2】:

      也要小心 SELinux。默认配置将阻止 httpd 访问您的虚拟主机目录。您需要设置适当的上下文:

      # chcon -R -u system_u -r object_r -t httpd_sys_content_t <DocumentRoot>
      

      另一个选项就是禁用 SELinux。

      【讨论】:

        【解决方案3】:

        有几件事可能会导致您出现问题:-

        NameVirtualHost *:80
        

        不再是 Apache 2.4.x 的有效语法,您应该将其完全删除。

        在 Windows 端,一旦您更改了 HOSTS 文件,您需要重新加载 DNS Client service,因此要么重新启动,要么更好的是,使用“以管理员身份运行”启动命令窗口并执行以下操作:-

        net stop dnscache
        net start dnscache
        

        最后,在您的虚拟主机定义中,它将有助于告诉 apache 从哪里可以接受到此虚拟主机的连接,如下所示:-

        <VirtualHost *:80>
             ServerName webserver
             ServerAlias localhost devserver development
             DocumentRoot /var/www/html
            <Directory  "/var/www/html">
                AllowOverride All
        
                Require local
                Require ip 192.168.3
        
            </Directory>
        </VirtualHost>
        

        这将允许从运行 apache Require local 的机器和本地网络上的任何 IP 地址 Require ip 192.168.3 进行访问

        我也不确定 Apache 在 unix 上将其默认文档根放在哪里,但将您的 3 个域名区分为不同的目录可能是一个想法

        <VirtualHost *:80>
             ServerName localhost
             ServerAlias localhost
             DocumentRoot /var/www/html
            <Directory  "/var/www/html">
                AllowOverride All
        
                Require local
                Require ip 192.168.3
        
            </Directory>
        </VirtualHost>
        
        
        <VirtualHost *:80>
             ServerName webserver
             ServerAlias webserver
             DocumentRoot /var/www/html/webserver
            <Directory  "/var/www/html/webserver">
                AllowOverride All
        
                Require local
                Require ip 192.168.3
        
            </Directory>
        </VirtualHost>
        
        <VirtualHost *:80>
             ServerName development
             ServerAlias development
             DocumentRoot /var/www/html/development
            <Directory  "/var/www/html/development">
                AllowOverride All
        
                Require local
                Require ip 192.168.3
        
            </Directory>
        </VirtualHost>
        
        
        <VirtualHost *:80>
             ServerName devserver
             ServerAlias devserver
             DocumentRoot /var/www/html/devserver
            <Directory  "/var/www/html/devserver">
                AllowOverride All
        
                Require local
                Require ip 192.168.3
        
            </Directory>
        </VirtualHost>
        

        然后在每个目录中放置一个简单的 html 文件,上面写着“Hello from Servername”,并更改每个文件中的服务器名,这样你就知道你已经到了正确的服务器。

        RE:更新 test1.php

        Allow from all
        

        也不是有效的 Apache 2.4 语法,除非您已加载 LoadModule access_compat_module modules/mod_access_compat.so

        即使这样也应该是

        Order Allow,Deny
        Allow from all
        

        所以使用 Apache 2.4 语法

        Require all granted
        

        如果您想采用惰性路由并允许从宇宙访问。

        【讨论】:

        • 我删除了 ServerAlias 指令,只留下 ServerName 以供测试是否存在问题而不是问题。我删除了 NameVirtualHost 指令,这也不是问题。 CentOS 和其他基于 RHEL 的发行版中的 Apache 将 DocumentRoot 设置为 /var/www/html,因此在 VH 配置中是正确的。最后,在 Windows 中使用您留下的两个命令进行测试后,我也尝试了这个ipconfig /flushdns,但访问webserver 会将我带到默认的“欢迎”
        • 是的,但我的意思是,您已经将所有 3 个域名都指向了 /var/www/html,因此它们应该都转到同一个虚拟主机,这与您的默认 localhost 位置相同。那么你怎么知道虚拟主机定义是否有效?因此,如果您创建 3 个不同的 VHOST 都指向不同的目录,您将知道哪些实际有效,哪些无效。
        • 好吧,我的逻辑是,如果所有三个域都指向同一个地址,并且如果我调用 webserver 或 localhost 或 devserver,该地址是否有一个 index.php 文件,是否应该显示索引页面?也许我错了,现在我有另一个问题,我认为这是问题所在,这是我收到的消息Unknown Authz provider: 192.168.3
        • 我认为您需要在 Apache 中激活 mod_authz_host 才能使 Require ip 构造工作
        • 已加载 LoadModule authz_host_module modules/mod_authz_host.so :-\
        猜你喜欢
        • 2020-06-08
        • 1970-01-01
        • 2018-12-05
        • 2016-04-22
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 2015-06-08
        • 2015-06-13
        相关资源
        最近更新 更多