If you havent already set up your virtual machine, you may want to check out our guide on setting up an Ubuntu LAMP server running on VirtualBox.
This short guide will show you how to set up multiple websites to run from your development server and have the websites shared from your Windows host machine. This will allow you to work on your files and view them in your preferred browser on your host machine before uploading them to your live server.
Setting Up Available Sites
The default location for your websites on the Apache server is in /var/www/. You should create a folder for each of your websites using the Terminal like so:
sudo mkdir /var/www/newsite sudo mkdir /var/www/newsite2 sudo mkdir /var/www/newsite3
For each website you then need to set up a virtual host configuration file. Copy the default configuration file and edit it for your new website as outlined in the Ubuntu server guide:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/newsite
Edit the new file:
sudo gedit /etc/apache2/sites-available/newsite
The file should look like this:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory <"/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined Alias /doc/ "usr/share/doc/" <Directory "usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </VirtualHost>
You can delete this section:
<Directory /> Options FollowSymLinks AllowOverride None </Directory>
In between the ServerAdmin and DocumentRoot lines define a ServerName for your new site, also change the DocumentRoot to match the folder for the site:
ServerAdmin webmaster@localhost ServerName newsite DocumentRoot /var/www/newsite
Change Directory /var/www to:
Next if you are going to want to use an .htaccess file with your website you need to change the AllowOverride None on the Directory /var/www/newsite to:
Save and close your new configuration file. Then you need to disable the default site:
sudo a2dissite default
And enable your new website:
sudo a2ensite newsite
If you need modrewrite to be enabled enter the following (otherwise you will see server errors):
sudo a2enmod rewrite
Restart Apache to apply the new settings:
sudo /etc/init.d/apache2 restart
Remember to create a virtual configuration file for each website you want on the server, then you can use a2ensite and a2dissite to switch between websites.
Setting Up Shared Folders From The Windows Host
Shut down your virtual machine, then in VirtualBox select your virtual machine and open up the settings. Go to the 'Shared Folders' options and create a share for each website you have on your Windows host machine. Set the path for each one and assign a unique folder name, the folder name will be the share name for that path. Once done start your virtual machine.
To link your shared folder with the Apache website folder you need to mount the folder like so, where 'newsite' is the share name and /var/www/newsite is its path on Apache:
sudo mount -t vboxsf newsite /var/www/newsite
If you load your server now on the Windows 7 host you can see your website working. Changes made while working in Windows can be seen after quick save and a refresh of your browser.
Note: You will need to mount the drive again if you shut down or restart Ubuntu.
Setting a Dev URL using the hosts file
If you set up your virtual machine to use a static IP as defined in my previous guide Ubuntu LAMP server running on VirtualBox, then you can use the windows host file to point the static IP to a short URL like http://dev/.
To do this open your windows host file in:
Add the following line at the bottom where the IP address is your static IP and 'dev' can be whatever you wish:
You can then open http://dev/ in your browser (and bookmark) to see your currently active website.