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}/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
Leave a Reply