Ubuntu 14.04:Apache 2.4.7虚拟主机无法工作/重定向

6

我在我的Ubuntu 14.04机器上安装了Apache 2.4.7,并且我的一些虚拟主机与我不一致。我正在尝试运行5个虚拟主机,其中3个可以正常工作,而另外2个则不能。这两个不起作用的.conf文件如下:

002-tmpnet.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/tmpnet
    ServerName  tmpnet
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/tmpnet/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        #Order allow,deny
        #allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

003-tmpcom.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/tmpcom
    ServerName  tmpcom
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/tmpcom>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        #Order allow,deny
        #allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

这两个都返回内容。
500 Internal server errors

当我试图访问它们时,如果我通过本地主机(即localhost/html/tmpnet)访问它们,它可以完美地工作,但是在尝试使用虚拟主机时就无法正常工作。

我的hosts文件内容如下:

127.0.0.1   localhost
127.0.1.1   Eagle
127.0.1.1   tmpcom
127.0.1.1   tmpbiz
127.0.1.1   tmporg
127.0.1.1   tmpnet
127.0.1.1   thatsmybrick

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我的 apache2.conf 文件如下:

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups on
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride All
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

我已经卡在这个问题上很久了,真的非常需要帮助。任何指向正确方向的建议都将是惊人的。

谢谢

7个回答

7
这可能有点晚了,不确定您是否已经找到解决方案,但这是我所做的。在花费了很多时间修改我的配置文件和调整我的虚拟主机之后,我决定a2dissite 000...默认站点并重新加载apache。一旦我这样做了,重定向就按照预期工作了。我不知道当您在服务器上有多个站点时,会发生什么情况,如果它会默认为第一个加载的虚拟主机或者...我一直认为apache必须有一个默认主机,但我可能错了。

谢谢!我运行了 a2dissite 000-default.conf 命令,并将 .conf 添加到我的现有文件中,它起作用了! - skovy

6

我不仅需要禁用默认值:

sudo a2dissite 000-default.conf

但是我还必须以sudo身份重新加载apache - 如果没有sudo,它将无法正确重新加载。

sudo service apache2 reload

4
我遇到了这个问题,结果发现我需要禁用默认虚拟主机。
sudo a2dissite 000-default.conf

1

你还在面临这个问题吗?

你看过位于 /var/log/apache2/error.log 的错误日志了吗?错误日志很明显,可以很好地指导解决问题。

我的机器上显示的错误是这样的(错误语句的后半部分)

.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

在我的情况下,Apache 模块 rewrite 没有启用。我启用了该模块,然后虚拟主机设置就生效了。

此外,我发现你正在使用语句 "AllowOverride All",这表明你正在尝试使用重写模块。如果还没有启用该模块,请使用命令 sudo a2enmod rewrite 启用该模块,然后重新启动你的 apache2 sudo /etc/init.d/apache2 restart

看看这是否有帮助!


0
如果您正在从Precise(12.04)升级,则需要将.conf扩展名添加到您的虚拟主机配置文件中。
例如,以前您有这个配置。
/etc/apache2/sites-available/project

然后你需要给它添加.conf扩展名。

/etc/apache2/sites-available/project.conf

当然,您必须在sites-enabled文件夹中创建一个新的符号链接,cd到site-enabled文件夹并执行以下操作:

sudo ln -s ../sites-available/project.conf 

重启Apache,问题解决。


0

我正在测试相同的设置:

  • Apache 2.4.7与mod_rewrite(sudo a2enable rewrite)

  • Ubuntu 14.04 LTS

在基本的LAMP干净安装(请参见此链接)中,我在sites-available中使用以下内容(通过从sites-enabled建立软链接)

<VirtualHost *:80>
  ServerName test.cubeinspire.com
  DocumentRoot /home/test/public_html  
  CustomLog /home/test/log/access_log combined
  ErrorLog /home/test/log/error_log
  <Directory /home/test/public_html>
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

只有使用那段代码才能正常工作。(请注意 Require all granted 是 Apache 2.4.7 的新要求)

当我添加以下代码时:

<VirtualHost *:80>
  ServerName redirectme.cubeinspire.com
  Options +FollowSymLinks
  RewriteEngine on
  Redirect 301 / http://test.cubeinspire.com/
<Directory "/home/test/public_html">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>

当我访问redirectme.cubeinspire.com时,我陷入了301重定向循环。

这是一个非常简单的代码,在以前的Apache版本上运行良好,并且文档中没有太多说明。

因此,我猜测在那个版本中仍然存在一些错误,我不建议在生产服务器上使用它。


-1

好的,重点在于:

IncludeOptional sites-enabled/*.conf

这意味着它将包括文件名以.conf结尾的虚拟主机文件。

重命名它们或更改指令。

再见, Gelma


我认为你没有读清楚问题。他的文件确实命名为.conf,但仍然无法加载。 - Machavity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接