Planning to set-up a new development environment, but haven’t yet quite decided between MAMP and installing Apache and MySQL locally?
There is also 3rd option, run LAMP stack virtualized locally.
Benefits of running your XAMP (apache, MySQL & PHP) stack in VirtualBox has many benefits.
- Like having the same environment as in production.
- All libraries are not (easily) available to all platforms.
- Freeing resources is easy, just shutdown your virtualmachine.
- Download and install VirtualBox https://www.virtualbox.org/
- Download and install Ubuntu 12.04 Server http://www.ubuntu.com/download
We need guest additions to get access to local files in your host machine (folder sharing).
sudo apt-get install dkms build-essential linux-headers-`uname -r` sudo shutdown -r now
Click on VirtualBox menu: Devices -> Install Guest Additions…
sudo mkdir /media/cdrom sudo mount /dev/cdrom /media/cdrom cd /media/cdrom sudo ./VBoxLinuxAdditions.run --nox11 sudo shutdown -r now
(note: device may also be cdrom0 – cdromN)
Sharing folder with host
Apache (in Ubuntu) will need access to your local folder where you keep your Drupal Projects, something like projects or sites.
In VirtualBox, select your (new, just-installed?) server image and adjust its settings. From under Shared Folders, share the folder you are using for your drupal projects. Basically, if your Drupal sites are under /Users/Uname/Sites/%PROJECT, share the Sites folder. DO NOT check read-only, instead check auto-mount option. If there are issues in mounting, do it manually.
To get access to share in Linux, you need to add your username and www-data to vboxsf group by using
sudo usermod -a -G vboxsf www-data
Install and configure Apache and PHP
Start by downloading & configuring Apache and PHP
sudo apt-get install libapache2-mod-php5 php-pear php5 php5-cli php5-common php5-dev php5-gd php5-mysql php5-curl sudo apt-get install php-apc libapache2-mod-rpaf
Enable Apache modules and remove unneeded mods
Remove modules: cgi, autoindex, status; – a2dismod cgi autoindex statusEnable modules: Rewrite, expires;;- a2enmod rewrite expires
sudo apt-get install -y mysql-server mysql-client
Make sure mysql starts at reboot
character_set_server=utf8 collation_server=utf8_swedish_ci default-storage-engine=innodb
Restart MySQL and clear old index files
sudo /etc/init.d/mysql stop sudo rm /var/lib/mysql/ib* sudo /etc/init.d/mysql start
Stable IP for virtual machine (Ubuntu)
Stable IP is must-have for usability, you don’t want to adjust configuration every time your IP-address changes.
This can be achieved by creating global (in VirtualBox) network and using it in a virtual machine.
From VirtualBox’s (software) preferences go to the networking tab and create a new network.
After you have created network, go to settings of virtual machine (ubuntu server) and edit network settings.
Adapter 1: Host-only, select the network you just created.
Adapter 2: Since through adapter 1 won’t provide internet access, you can do it through another adapter. The configuration depends on your environment but tries either NAT or bridge.
Creating the first site
First, start by adding a new site to /etc/apache2/sites-available/default file or create a new file, you can use old configuration as a base.
Point directory to mounted folder in /media/MOUNT/folder (example /media/sf_Sites).
note: if your files are directly under mount-point, you will get 403 Forbidden error
Don’t forget to reload settings! (sudo service apache2 reload)
The second step is checking what IP-address is assigned to your VirtualBox machine. With the default configuration, it is probably 192.168.56.101. You can see your current IP-address by typing ifconfig in terminal and looking IP of eth0 interface.
You want alias for your site what you can use to access site from your local environment, for example, project.dev.
In Mac OS X or Linux add the following line to /etc/hosts (in Windows use equivalent configuration):
As Juan pointed out if you have issues with file permissions you can try out the solution from here, I solved the write permissions on Linux by adding a user to the same group with vboxfs.
But I think in the performance POW, it’s best to use NFS mount.