我试图在 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://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
显然 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
Forbidden
You don't have permission to access /index.php on this server.
这里有什么问题?
最佳答案
详述jap1968的帖子,CentOS 7 附带了 SELinux 的痛点,将对接级别设置为 enforcing
。当完全正常的服务配置静默失败时,这会导致各种困惑 (Apache)。
要禁用 SELinux,您需要:
0) [可选] 打开一个 shell 并成为 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。如果您的虚拟主机的域名解析到您正在使用的服务器,您将看到 Shiny 的新虚拟主机:
# 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 的功能深度、大量命名不当的实用程序以及丑陋的用户体验困惑。在潜入水中之前,您可能应该穿上大男孩裤并喝一整壶咖啡。这里有一些信息:
关于php - VirtualHost 在 CentOS 7 上的 Apache 2.4.6 下不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25608659/