Ubuntu – Setting up vhost on VirtualBox with Ubuntu 16.04

Apache2servervirtualbox

I am pretty new to development and this is why I am stuck at such a "beginners" problem.

I want to have a directory under /var/www/mysite with a index.html and access it via web browser on my PC (Win10 64 Bit) by typing mysite.example into it.

I don't want to publish anything into the internet with that. I only want to test and develop a site which later might be hosted.

I have created a VirtualMachine using Oracle VirtualBox and installed Ubuntu16.04 (64-Bit, also enabled Virtualization at my i5 2500 in Bios so I get these 64-Bit displayed).

Then I installed apache2, mysql and php.

ip addr show gave me an address: 192.168.178.31.

So if I type into my browser http://192.168.178.31/ I get to the apache2 default page – It works!

I created the directory /var/www/mysite and put a simple index.html for testing purpouse into it. I said sudo chown -R 775 to /var/www/.

Now I need to create a vhost so I can access to this index.html in this specific directory by typing in a specific string into my browser. But it just won't work!

I went to /etc/apache2/sites-available/ and created mysite.conf.

This is the content:

<VirtualHost *80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mysite
ServerAlias mysite.example
</VirtualHost>

I did a2ensite mysite.conf, sudo service apache2 reload and restart

So there I am. If I type mysite.example into my browser I only get errors.

Site not reached. DNS-Server for mysite.example not found and so on (ERR_NAME_NOT_RESOLVED).

Best Answer

Please note that if you are using mysite.local instead of mysite.org* (or other popular extension) you may type http:// in front of it every time, because the browser may not recognise .local as a domain extension.

  • .google, .dev, .foo, .page, .app, .chrome domain extensions are not good idea for similar purposes since December 2017, read more... and more...

If you want to access mysite.local and www.mysite.local from the local machine, edit /etc/hosts and bind these two domain names to the loopback interface - 127.0.0.1:

127.0.0.1    localhost mysite.local www.mysite.local
  • Add this line somewhere inside of /etc/hosts.

  • Note that, in this context the local 'machine' is the Virtual Machine with Ubuntu.

If you want to access mysite.local through another computer from your LAN, edit its host file and bind the two domain names to the servers IP address:

192.168.178.31    mysite.local www.mysite.local

Another option is to create your local DNS:


Here is a basic virtual host configuration applicable for your case:

<VirtualHost *:80>

        ServerName mysite.local
        ServerAlias www.mysite.local

        ServerAdmin webmaster@mysite.local

        DocumentRoot /var/www/mysite

        <Directory /var/www/mysite>
                Options Indexes FollowSymLinks
                AuthType None
                AllowOverride All
                Order allow,deny
                Allow from all
                Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/mysite.local-error.log
        CustomLog ${APACHE_LOG_DIR}/mysite.local-access.log combined

</VirtualHost>
  • In the beginning put some basic content in /var/www/mysite. The function phpinfo(); is a good idea:

      echo '<?php phpinfo(); ?>' | sudo tee /var/www/mysite/index.php
    

Restart Apache and try to access http://mysite.local via the browser.


Read also:

Related Question