簡體   English   中英

VirtualHost在CentOS 7上的Apache 2.4.6下無法運行

[英]VirtualHost is not working under Apache 2.4.6 on CentOS 7

我正在嘗試在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下放置一個文件,用<?php phpinfo(); ?> <?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 

顯然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。

什么失敗了?

為了詳細闡述jap1968的帖子, CentOS 7帶來了SELinuxenforcing的對接級別的痛苦。 當完全正常的服務配置無聲地失敗( Apache )時,這會導致各種混淆。

要禁用SELinux,您需要:

0) [可選]破解打開一個shell並成為root ...或者享受一個閃亮的新的,超級有趣的,配置sudo讓你做“root stuffs”項目。 大概。

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 如果您的vhost的域名正在解析為您正在處理的服務器,您將看到閃亮的新虛擬主機:

# 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混亂。 你可能應該穿上你的大男孩褲子,然后在潛入之前喝一整罐咖啡。這里有一些信息:

SELinux也要小心。 默認配置將阻止httpd訪問您的虛擬主機目錄。 您需要設置適當的上下文:

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

另一種選擇就是禁用SELinux。

可能會給您帶來問題的一些事情: -

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

另外我不確定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>

然后在每個目錄中放入一個簡單的html文件,說“來自Servername的Hello”並更改每個文件中的servername,這樣你就知道你已經到了正確的服務器。

RE:更新test1.php

Allow from all

除非您已加載LoadModule access_compat_module modules/mod_access_compat.so ,否則也不是有效的Apache 2.4語法

即便如此,它應該是

Order Allow,Deny
Allow from all

所以使用Apache 2.4語法

Require all granted

如果您想采用惰性路徑並允許從Universe訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM