Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache 2.4 virtual host ignored

I'm setting up a new web server using Ubuntu 14.0, following the instructions here for creating virtual hosts, however my single additional virtual host always loads the default site (/var/www/html).

Here is my vhost configuration (I have replaced my domain with example.com, but it is otherwise identical):

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/example/app

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

I have made NO OTHER CHANGES to the Apache 2.4 configuration which is installed in Ubuntu 14.

EDIT: I am correctly enabling sites and restarting apache after making changes to this vhost.

When I visit example.com in my browser, I get the default site. What am I doing wrong? How do I get this virtual host to load the correct directory?

EDIT: The solution is a missing "sudo" in the restart command (https://stackoverflow.com/a/23713299/225682).

like image 923
xtfer Avatar asked Nov 27 '22 07:11

xtfer


1 Answers

6 years too late but maybe this will help someone. When debugging issues like this, first check that apache can see your virtualhost configuration: apachectl -S

You should see your virtualhost configuration in the output:

*:80                   example.com (/etc/httpd/conf.d/example.com.conf:2)

If you cannot see it, maybe the config file path is not included into the main configuration file or the file itself is missing a .conf extension.

If you see your VirtualHost in the output but you're still getting the default VirtualHost instead, check the global ServerName directive in the main configuration file. It should NOT match your VirtualHost ServerName.

Beware - if your global ServerName is not set, apache will obtain one using rDNS. So if your IP address' reverse DNS record matches example.com (same as your VirtualHost), your VirtualHost might be ignored. I believe this is a very common source of problems and I haven't found this on the forums yet. Check your IP's rDNS record with host command if you face this issue.

like image 155
Pavel Zak Avatar answered Dec 04 '22 04:12

Pavel Zak