Ubuntu配置apache2.4配置虚拟主机遇到的问题

这篇日志发布时间已经超过一年,许多内容可能已经失效,请读者酌情参考。

update:

偶然看到了 apache的更新说明,直接贴个地址过来吧。

http://httpd.apache.org/docs/2.4/upgrading.html


最近想把web开发目录从/var/www/转移到/home/user/webroot目录,并配置基于域名的apache2虚拟主机,结果遇到各种403错误,非常郁闷。最终在Scrat同学的帮助下,终于找到问题所在,于是在此记录。

我使用的apache版本是Server version: Apache/2.4.6 (Ubuntu),这是ubuntu安装的默认版本,低版本如apache2.2可能不存在这个问题(因为以前用2.2就没遇到这样的情况)。

0x01 Ubuntu配置apache2的前置知识

使用apt-get安装的apache2与直接编译安装版本略有不同,其配置文件不在是httpd.conf,而是/etc/apache2/apache2.conf。

而其虚拟主机的位置的配置文件也不在是vhost之类的,而是sites-avilable/xxx.conf。默认的localhost为000-default.conf,按照这个配置文件,配置一份demo.conf,就完成了虚拟主机的配置工作。

配置参考http://wiki.ubuntu.org.cn/Apache虚拟主机指南,这是Ubuntu的中文维基百科。推荐配置如下:

<VirtualHost *:80>
ServerName edunuke.example.com
ServerAdmin edunuke@mail.example.com
DocumentRoot "/var/www/edunuke/"
ErrorLog "/var/log/apache2/edunuke_errors.log"
CustomLog "/var/log/apache2/edunuke_accesses.log" common    
</VirtualHost>

可以看到推荐的配置是/var/www/的子目录。

按照000-default.conf的配置,填写完整的配置如下:

<VirtualHost *:80>
        ServerName demo
 
        ServerAdmin xbzbing@163.com
        DocumentRoot /home/user/webroot
        <Directory /home/user/webroot>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/demo_error.log
        CustomLog ${APACHE_LOG_DIR}/demo_access.log combined
</VirtualHost>

很多时候的403错误都是<Directory>标签没有配置Allow from all,因为默认是Deny from all。

(注意,这里的Allow from all的配置项目属于2.2.x及以下版本才有,2.4.x已经不需要这个配置了)

然后使用命令a2ensite demo来使demo启用,再使用service apache2 reload来使配置生效。

别忘了修改/etc/hosts文件,使demo指向127.0.0.1。

原理上说到这里已经配置完毕,应该可以直接访问了,但是,一大波403正在靠近。。。。。

0x02问题来了

如果按照维基百科的配置,将目录选择在/var/www/目录或者子目录的话,完全无压力可以跑。但是换到/home/user/webroot之类的地方就出现403错误。

看错误日志,具体信息为:

AH00035: access to / denied (filesystem path '/home/user/webroot') because search permissions are missing on a component of the path

这说明在/home/user/webroot的路径上,存在一个目录权限设置有问题,导致拒绝访问。

1、修改/home/user/权限。

因为默认情况下该目录是700,apache2是无法访问的。

修改为711即可。

2、修改/home/user/webroot权限。

chmod 755 -R /home/user/webroot/

让webroot及其子目录拥有755权限,将一些特殊目录设置777,如assets目录,runtime目录等,这个根据程序修改。

权限修改完毕,请求依然是403。

AH01630: client denied by server configuration: /home/user/webroot/

看来是服务器配置问题啊。可是在网上搜了不少文章,都没有提到这个事情。

悲剧折腾了两天,重装,换目录也不行,哪怕换到/var/webroot/也还是不行,一一对比了/var/www和/home/user/webroot的权限设定,也没有问题啊,很纠结。

这时候叫来了Scrat同学,经过一阵瞎折腾,在修改了/etc/apache2/apache2.conf文件后的一个配置后居然可以了。

<Directory />
        #Options FollowSymLinks
        #AllowOverride None
        #Require all denied
        #我也忘记当时他改了什么,大概是把demo.conf中的<Directory>复制过来了
</Directory>

(PS这不是正确的方法,正确的方法见后续)

注释掉了这里的东西,写入了不知道从哪里找来的几行配置,居然访问通过了。

说实话,我觉得apache通过a2ensite/a2dissite这类命令来控制虚拟主机,是架构上先进的体现,没想到配置虚拟主机还得修改apache2文件。。。。郁闷。。

晚上回去我又仔细看了看这个配置文件,发现是自己傻逼了。

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>
 
<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

注释已经说了,这是防止访问根目录以外的目录做的特殊安全设定,默认情况下除了/usr/share和/var/www外,其他目录都apache服务器都不能访问。要使/home/user/webroot可以访问,就要增加一个类似的访问请求项目:

<Directory /home/user/webroot>
        AllowOverride None
        Require all granted
</Directory>

OK,这下达到原先的目标了。

但是这还没完,难道增加一个虚拟主机就得修改一次apache2.conf文件?真不能通过a2enssite无缝增加一个虚拟主机?

答案是肯定的,其实还是demo.conf配置问题,需要增加一个配置项目:Require all granted。

完整的配置如下:

<VirtualHost *:80>
        ServerName demo
 
        ServerAdmin xbzbing#163.com
        DocumentRoot /home/user/webroot
        <Directory /home/user/webroot>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/demo_error.log
        CustomLog ${APACHE_LOG_DIR}/demo_access.log combined
</VirtualHost>

至此,Apache2.4.6的虚拟主机已经可以正常工作了,终于可以愉快的玩耍了j_0028.gif

留言交流

fish
fish 2014-04-03 09:22 回复
略高端啊犀利擦!
疯狂的dabing
疯狂的dabing 2014-04-03 12:30 回复
回复 fish : 鱼子来访! icon_cool.gif
yellowkingdom
yellowkingdom 2014-06-18 21:01 回复
非常感谢 真的解决的问题!!
疯狂的dabing
疯狂的dabing 2014-06-19 14:53 回复
icon_smile.gif apache 2.4的配置确实比2.2多了很多坑。。。
初学者
初学者 2014-07-31 21:35 回复
大虾,请您百忙中补全很重要的几句命令,不要省略,实在是关键。如何建立和配置demo.conf,然后启用它,使demo指向127.0.0.1的具体代码如何书写。具体的命令请指点。谢谢您了。
疯狂的dabing
疯狂的dabing 2014-08-02 16:15 回复
回复 初学者 : 你好,这篇文章介绍的是apache 2.4.x的简单配置,主要针对2.4版本出现的一些403错误进行介绍。其他配置信息可以参考文中提到的wiki地址:http://wiki.ubuntu.org.cn/Apache虚拟主机指南
ChaelesMu
ChaelesMu 2014-12-24 16:28 回复
非常感谢,最后发现只设置Directory 就可以正常访问
lsTMango
lsTMango 2015-05-05 16:55 回复
卧槽,百度这个怎么没把这个帖子放到第一位
点击换图