Linux – Apache2 virtualhost not working (Debian)

apache-2.2debianlinuxvirtualhost

I have a local server at home running debian 8. I access this server with its ip address given from the router. Now I wanted to try to work with virtualhosts to make my server a bit better. I created two virtualhosts blog.com and mysite.com. But my problem is that they are both not working. Everytime I enter them in the browser nothing happens. I have already tried several solutions here on stack overflow or with google. Nothing helped.

Any advice?

What I have done:

First: I have set up two further directories besides the default one for the sites and changed the directory of the default.

1) for blog.com: /var/www/blog.com/html

2) for mysite.com: /var/www/mysite.com/html

3) changed the default: /var/www/html to /var/www/default/html


Second: Then I created in every html folder a index.html file


Third: I created two further .conf files in the /etc/apache2/sites-available directory and changed the default.conf DocumentRoot

blog.com.conf

<VirtualHost *:80>
    ServerName blog.com
    ServerAlias www.blog.com
    ServerAdmin info@blog.com
    DocumentRoot /var/www/blog.com/html/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

mysite.com.conf

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    ServerAdmin info@mysite.com
    DocumentRoot /var/www/mysite.com/html/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Changed default.conf DocumentRoot to:

default.conf

<VirtualHost *:80>
    ServerAdmin info@default.com
    DocumentRoot /var/www/default/html/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Fourth: Enabled my two new sites

sudo a2ensite blog.com.conf

sudo a2ensite mysite.com.conf


Fifth: I restarted apache

sudo service apache2 restart

Also did reload

sudo service apache2 reload

sudo /etc/init.d/apache2 restart


Sixth: I edited my hosts file

127.0.0.1 localhost
ip blog.com
ip mysite.com

Best Answer

For the name based virtual hosting to work, you have to make sure that the hostnames you are trying to access the sites, resolve to the server ip address. That means, in your case, blog.com and mysite.com should resolve to the server local ip address. These are supposed to be done by configuring nameservers accordingly.

Another way to achieve this locally is to add the entries to the local pc's hosts file so that it resolves correctly. As you have put the entries in your servers hosts file, your pc is not aware of it and can't resolve the names and no request for the sites are reaching anywhere.

Have a look at Name-based Virtual Hosting and Microsoft TCP/IP Host Name Resolution Order

Related Topic