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

时间:2014-05-21 14:17:24

标签: apache ubuntu virtualhost

我的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(即localhost / html / tmpnet)访问它们,它可以正常工作,只是在尝试使用虚拟主机时没有。

我的主机文件是:

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 个答案:

答案 0 :(得分:7)

这可能有点晚了,不确定你是否找到了解决方案,但这就是我所做的。在花了很多时间修改我的配置文件并调整我的vhost后,我决定使用a2dissite 000 ...默认站点并重新加载apache。一旦我这样做,重定向就像epxected一样工作。当你在服务器上有多个站点时,我不知道这将如何展开,如果它将默认为第一个加载的vhost或...我总是认为apache必须有一个默认主机,我可以这是错误的。

答案 1 :(得分:6)

我不仅要禁用默认值:

sudo a2dissite 000-default.conf

但我也不得不重新加载apache作为sudo - 没有sudo它没有正确地重新加载

sudo service apache2 reload

答案 2 :(得分:4)

我遇到了这个问题,事实证明我必须禁用默认的虚拟主机。

sudo a2dissite 000-default.conf

答案 3 :(得分:1)

你还在面对这个问题吗?

您是否查看了位于/var/log/apache2/error.log的错误日志。错误日志是不言自明的,可以很好地解决问题。

我的机器上的错误显示了这一点(错误陈述的后半部分)

  

.htaccess:无效的命令&#39; RewriteEngine&#39;,可能拼写错误或   由未包含在服务器配置中的模块定义

在我的情况下,Apache模块重写未启用。我启用了模块,并且虚拟主机设置恢复了活跃。

此外,我发现您使用的语句为“AllowOverride All&#39;表示您正在尝试使用重写模块。使用命令sudo a2enmod rewrite启用模块&#39;重写&#39;如果还没有,然后重新启动apache2 sudo /etc/init.d/apache2 restart

看看这是否有帮助!

答案 4 :(得分:0)

如果要从精确(12.04)升级,则需要将 .conf 扩展名添加到虚拟主机配置文件中

例如,以前你有这个配置

/etc/apache2/sites-available/project

然后你需要添加 .conf 扩展名,

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

当然,您必须在已启用网站的文件夹中创建新的符号链接, cd 到已启用网站的文件夹和

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

重启apache,你就完成了。

答案 5 :(得分:0)

我正在测试相同的设置:

  • Apache 2.4.7 with mod_rewrite(sudo a2enable rewrite)

  • Ubuntu 14.04 LTS

在使用基本LAMP(see this link)进行全新安装时,我在站点可用时使用以下内容(使用来自站点的软链接)

<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>

一切只适用于该代码。 (注意需要全部授予这是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版本,并且在文档中没有多少说法。

所以我猜这个版本还有一些bug,我不鼓励在生产服务器上使用它。

答案 6 :(得分:-1)

嗯,重点是:

IncludeOptional sites-enabled/*.conf

这意味着将包含文件名末尾带有.conf的虚拟主机文件。

重命名它们,或更改指令。

侨, Gelma