How to add a virtual host in Scotch Box
Sometimes you want to host multiple sites in a single virtual Scotch Box public_html folder, or any other Vagrant+VirtualBox setup.
Say you have a folder called drupal in public_html, you can always access the web site via http://192.168.33.10/drupal, but this is not optimal, for example for paths to images, if they rely on starting the path from root.
A possible solution is adding an extra Virtual Host to your Scotch Box.
Open your /etc/hosts file on your own machine (not Scotch Box) with
sudo vi /etc/hosts
... and add this
192.168.33.10 dev_site.local
This will make your browser ask Scotch Box, which is at 192.168.33.10, for dev_site.local.
On Scotch Box, open the default Virtual host file for Apache, 000-default.conf
sudo vi /etc/apache2/sites-available/000-default.conf
... assuming your site is in /var/www/public/dev_site, add this below the existing entry
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/public/d7.local ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ServerName d7.local <Directory "/var/www/public/d7.local"> Options Indexes FollowSymLinks AllowOverride all Require all granted </Directory> </VirtualHost>
This tells Scotch to guide requests for dev_site.local to the folder /var/www/public/dev_site.
Restart the Apache server to make the update take effect.
sudo systemctl restart apache2.service
If you are using Drupal, add this to your settings.php file:
$base_url = 'http://dev_site.local';
clear cache and generate a one-time log-in link
drush cc all drush uli
You should now be able to access your site at http://d7.local
To automate, you can copy paste this into your terminal:
sudo -i echo " <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/public/d7.local ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ServerName d7.local <Directory "/var/www/public/d7.local"> Options Indexes FollowSymLinks AllowOverride all Require all granted </Directory> </VirtualHost> " >> /etc/apache2/sites-available/000-default.conf systemctl restart apache2.service exit