Dupontta
Dupontta

Reputation: 69

Ubuntu 14.04: Apache 2.4.7 virtualhost not working/redirecting

I have Apache 2.4.7 installed on my Ubuntu 14.04 machine, and some of my virtualhosts are not agreeing with me. I have 5 virtual hosts I am trying to run; 3 of them work, 2 do not. The .conf files for the two that do not work are:

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>

Both of these return

500 Internal server errors

when trying to visit them. If I visit them through localhost (i.e. localhost/html/tmpnet) it works perfectly fine, just not when trying to use the virtual host.

My hosts file is:

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

and my apache2.conf is:

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

I have been stuck on this for a long while now, and I would really appreciate some help. Any pointing in the right direction would be amazing.

Thank you

Upvotes: 6

Views: 17474

Answers (7)

Cube Inspire
Cube Inspire

Reputation: 31

I'm testing the same setup:

  • Apache 2.4.7 with mod_rewrite (sudo a2enable rewrite)

  • Ubuntu 14.04 LTS

On a clean install with the basic LAMP (see this link) I use the following at sites-available (with soft link from 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>

All works great only with that code. (Notice the Require all granted that is a new requirement of Apache 2.4.7)

When I add the following code:

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

Then when I access redirectme.cubeinspire.com I fall into a 301 redirect loop. This is a quite simple code that was working on previous Apache versions and there is no much said at the documentation.

So I guess there is still some bugs on that version, I would discourage to use it on a production server.

Upvotes: 0

Mark Pruce
Mark Pruce

Reputation: 945

Not only did I have to disable the default:

sudo a2dissite 000-default.conf

but I also had to reload apache as sudo - without sudo it was not reloading properly

sudo service apache2 reload

Upvotes: 6

sanusi
sanusi

Reputation: 769

if you are upgrading from precise (12.04), then you need to add .conf extension to your virtual host config file

for example, previously you have this config

/etc/apache2/sites-available/project

then you need to add .conf extension to it,

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

and of course you have to create a new symlink in sites-enabled folder, cd to site-enabled folder and

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

restart apache and you're done.

Upvotes: 0

Andrea Gelmini
Andrea Gelmini

Reputation: 121

Well, the point is this:

IncludeOptional sites-enabled/*.conf

it means it will only includes virtual hosts files with .conf at the end of filename.

Rename them, or change the directive.

Ciao, Gelma

Upvotes: -1

Eddie Martinez
Eddie Martinez

Reputation: 13910

I was facing this issue, and it turned out I had to disable the default virtual host.

sudo a2dissite 000-default.conf

Upvotes: 4

Hatem Jaber
Hatem Jaber

Reputation: 2402

This may be a little late, not sure if you found a solution or not, but this is what i did. After spending many hours modifying my config files and tweaking my vhost, I decided to a2dissite 000... the default site and reloaded apache. Once I did this, the redirect was working as epxected. I don't know how this will unfold when you have more than one site on a server, if it will default to the first loaded vhost or ... I was always under the impression that apache had to have a default host, I could be wrong on that.

Upvotes: 7

saTya
saTya

Reputation: 11

Are you still facing the problem?

Did you look at the error log located at /var/log/apache2/error.log. The error log is self explanatory and will give a good lead to the problem.

The error on my machine showed this (latter part of the error statement)

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

In my case the Apache module rewrite was not enabled. I enabled the module and voila the virtual host settings came alive.

Also I see that you are using the statement 'AllowOverride All' which indicates you are trying to use the rewrite module. Enable the module 'rewrite', if not already, using the command sudo a2enmod rewrite and then restart you apache2 sudo /etc/init.d/apache2 restart

See if this helps!

Upvotes: 1

Related Questions