Adding multiple domains to vagrant using Apache2 min read

In

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

Log in to your guest machine using ssh.

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

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 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.


Run the following commandsudo a2ensite your_new_conf.conf

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

    6 responses

    1. 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.

      1. Hi, P137,

        I assumed that’s already done!

    2. Andrew Grigorkin Avatar
      Andrew Grigorkin

      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

    3. Andrew Grigorkin Avatar
      Andrew Grigorkin

      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.

      1. Hi Andrew,

        Thanks for your reply! I’ve updated the post!

    4. […] de carpetashttps://www.vagrantup.com/docs/synced-folders/basic_usagePara habilitar mas de un sitiohttps://peshmerge.io/adding-multiple-domains-to-vagrant-using-apache/Crear un entorno […]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

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