Cogito ergo sum

Adding multiple domains to vagrant using Apache2 min read

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

Data Science student and a software engineer whose interests span multiple fields.

6 comments

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

  • If you aren’t using the Scotch box and you’re making your own Vagrant box from scratch instead, don’t forget to put (or rather uncomment) the following line:
    `config.vm.network “private_network”, ip: “192.168.33.10”`
    in your Vagrantfile.

  • VirtualHost settings are incorrect. The directive must be closed. The correct version:

    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

    Options Indexes FollowSymLinks
    AllowOverride all
    Require all granted

  • To: webmaster

    Hi. Please refer to my previous comment which I posted a couple of mins ago. There I tried to point your attention to the fact that there is an error in how you posted the VirtualHost configuration. I meant that you need to close the Directory directive at the end. After I posted my previous comment, I noticed that the directives were interpreted as HTML tags and were cut out.

    Now you can correct your post. Please, delete both of my comments.

    Cheers.

By Peshmerge Morad
Cogito ergo sum