VirtualHost在CentOS 7上的Apache 2.4.6下无法运行

Rey*_*rPM 7 php linux centos apache2 apache2.4

我正在尝试在CentOS 7上的Apache 2.4.6中设置一些VH,但由于它不起作用而没有成功.这是我现在尝试过的:

  • 因为in /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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 重新加载/重新启动Apache服务(同时尝试):

    service httpd reload|restart
    
    Run Code Online (Sandbox Code Playgroud)
  • 在Windows端编辑文件C:\Windows\system32\drivers\etc\hosts并添加以下行:

    192.168.3.131  webserver localhost devserver development # this is the IP of Apache Server
    
    Run Code Online (Sandbox Code Playgroud)
  • 打开浏览器并尝试:http://webserver,http://devserver两者都进入默认的Apache页面,因此VH不起作用.

  • /var/www/html/index.php用这行代码放下一个文件<?php phpinfo(); ?>只是为了知道哪些模块是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 
    
    Run Code Online (Sandbox Code Playgroud)

显然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>
Run Code Online (Sandbox Code Playgroud)

但是我得到了一个403 Forbidden

被禁止

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

这里失败了什么?

小智 6

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

要禁用SELinux,您需要:

0)[可选]破解外壳并成为root ...或享受闪亮的超级乐趣,配置sudo可以让您执行“ root stuff”项目。大概。

su -l
Run Code Online (Sandbox Code Playgroud)

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

sestatus
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

3)编辑/etc/selinux/config文件。更改SELINUX=enforcingSELINUX=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
Run Code Online (Sandbox Code Playgroud)

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

setenforce 0
Run Code Online (Sandbox Code Playgroud)

5)sestatus再次检查:

sestatus
Run Code Online (Sandbox Code Playgroud)

如果一切都按预期进行,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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

6.5)在这里停止阅读。还是不。我是留言板帖子,不是您的妈妈。

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

7)努力重新启用selinux。首先查看selinux日志以查看一些很棒的字母汤:

tail -f /var/log/audit/audit.log
Run Code Online (Sandbox Code Playgroud)

8)惊讶于功能的深度,疯狂的数量不正确的实用程序以及构成SELinux的丑陋UX混乱。在潜水之前,您可能应该穿上大男孩的裤子,喝一整杯咖啡。以下是一些信息:


Rig*_*lly 3

有几件事可能会给您带来问题:-

NameVirtualHost *:80
Run Code Online (Sandbox Code Playgroud)

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

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

net stop dnscache
net start dnscache
Run Code Online (Sandbox Code Playgroud)

最后,在您的虚拟主机定义中,它将有助于告诉 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>
Run Code Online (Sandbox Code Playgroud)

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

另外,我不确定 unix 上的 Apache 将其默认文档根目录放在哪里,但这可能是一个将您的 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>
Run Code Online (Sandbox Code Playgroud)

然后在每个目录中放入一个简单的 html 文件,其中写着“Hello from Servername”,并更改每个文件中的服务器名称,以便您知道您已访问正确的服务器。

RE: 更新 test1.php

Allow from all
Run Code Online (Sandbox Code Playgroud)

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

即使那样也应该是

Order Allow,Deny
Allow from all
Run Code Online (Sandbox Code Playgroud)

所以使用 Apache 2.4语法

Require all granted
Run Code Online (Sandbox Code Playgroud)

如果你想采取懒惰的路线并允许来自宇宙的访问。