Cogito ergo sum

Adding multiple domains to vagrant using Apache

Three weeks ago I was working locally on my Linux machine on a php project. Beside that project I wanted to work on another project, it was also a web application. I was searching for a way to be able to host both projects on the same Vagrant machine. I didn’t want to add a new Vagrant machine for just that small project. It’s also insane if I have to add a new Vagrant for each application.

I was and I am still using Scotch Box, which is an awesome Vagrant machine preloaded with tons of tools for web developers.

I have finally decided, after long search on the internet, to do that by editing the hosts file on Linux machine (Host OS) and editing the virtualhosts file on the Scotch Box (Guest OS).

On the guest machine

  1. Log in into your guest machine using ssh.
  2. Create a new .conf file inside
     /etc/apache2/sites-available/

    or you can copy the default apache configuration file using the following command.

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your_new_conf.conf
  3. Open the newly created config file and edit it using vim or nano
    sudo vim /etc/apache2/sites-available/your_new_conf.conf

    Copy the following code and paste into in the config file:

    <VirtualHost *:80>
       ServerAdmin webmaster@example.com
       ServerName  example.com
       ServerAlias example.com
       DocumentRoot /var/www/public/your_directory/
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
       <Directory "/var/www/public/your_directory">
       Options Indexes FollowSymLinks
       AllowOverride all
       Require all granted
    </VirtualHost>
    

    Replace example.com with your chosen domain name. In addition, you have to specify the DocumentRoot and Directory. You have to point them to a folder where you host your web application files.

  4. Run the following command   sudo a2ensite your_new_conf.conf
  5. Run the following command  sudo service apache2 restart

On the host machine

On Linux

On Linux, edit hosts file  sudo vim etc/hosts

On Windows

On Windows, edit the hosts file located in C:\Windows\System32\drivers\etc\hostsusing any editor.

Add the following lines based on how many domains do you want to map to the working directory

 192.168.33.10 example.com

The ip-address is the preserved ip-address for your Vagrant box (guest machine). You can add as many as you want, but remember you have to follow the steps (1 to 5) for each new domain on your Vagrant box .

References:

https://httpd.apache.org/docs/current/vhosts/examples.html
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
https://stackoverflow.com/questions/36704850/how-to-vagrant-scotchbox-subdomain-virtualhost/46083678#46083678

About the author

Peshmerge Morad

A IT-student and a programmer based in the Netherlands whose interests span multiple fields.

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By Peshmerge Morad
Cogito ergo sum