Posted by on in iDevelop, PHP | 13 comments

Sometime back I wrote a step-by-step guide for setting up PHP development environment in a virtual machine. This is a simplified (& less tiring) version of that.

You’ll need a few things to proceed:

  • A computer, ofcourse. I used a MacBook Pro running OS-X Lion.
  • Oracle’s VirtualBox VM software – get it from here.
  • A linux distro. We will use Ubuntu Server here. Current LTS version is 12.04. Download the 32 bit ISO from here. Remember to download 32 bit version, the 64 bit version is kinda sluggish on less than 4 GB of RAM. 32 bit version is good enough for running web development stack unless you have specific needs.

 

STEP 1: Install the Virtual Box and then fire it up. In the window that opens, click on the “New” icon in the toolbar.

 

STEP 2: When it asks for VM name & type, give a name to your VM & select Linux as Operating System & Ubuntu as version. Click Continue.

 

STEP 3: Allot 1 GB of RAM to the VM. If your computer has less than 8 GB of RAM then 512 MB to the VM should be fine. (You should consider increasing the RAM on your computer)

 

STEP 4: On the Virtual Disk Creation Wizard window, VDI should be selected by default. If it isn’t then select it & click Continue.

 

STEP 5: On the next screen titled “Virtual disk storage details”, select “Fixed Size” & click Continue.

 

STEP 6: On the next screen, allocate 10 GB to your virtual disk. If you don’t have much space on HDD then 5 GB should be fine.

 

STEP 7: Now you’ll be presented with a summary of the virtual disk & Virtual Box will set it up on your HDD. It’ll take a few minutes. You can put the coffee in the coffee maker if you want while Virtual Box is working. After its done, click the Create button & it’ll all be setup.

 

STEP 8: In the menu bar on top, go to Virtual Box -> Preferences and click the icon labelled “Network” in the window that opens. If you don’t see anything in the “Host-only Networks” box then click the first icon on the right which has a green plus (+) symbol, it’ll be the only one click-able. This will add a network adaptor for VMs to talk to the host OS, Mac OSX in my case. If the “Host-only Networks” box already has one or more items then you don’t need to do anything except move to next step. 🙂 Click OK to close this window.

 

STEP 9: So now that the VM has been created, we’ll proceed to install the Ubuntu Server operating system on it. In the Virtual Box manager window, your Ubuntu VM should already be selected, click start to launch it.

 

STEP 10: A new window will open and after the launch sequence, you’ll see the First Run Wizard window. Click Continue.

 

STEP 11: On the next window you’ll be asked to select installation media. Click the folder icon next to the drop down and browse to the folder where you downloaded the Ubuntu Server ISO. Select that ISO. Click Continue.

 

STEP 12: On the next window you’ll be asked to choose a language for the installation procedure. I chose English, you’re free to choose any other if you so wish. Hit the Enter key to proceed.

 

STEP 13: Now it’ll ask what you want to do. Default selection will be “Install Ubuntu Server”, hit Enter key to proceed. On the next screen it again asks for language, choose yours & hit Enter key.

 

STEP 14: Now it asks for keyboard type. If you know yours then select “No”, using your TAB key to move the selection & hit Enter key.

 

STEP 15: If you selected “No” in previous step then you’ll now see a list of keyboard options. Mine is “English (US)” so I selected that.

 

STEP 16: Now it’ll start loading up stuff. After a few seconds it’ll ask for hostname. Now this hostname is important as you’ll be using this to SSH into your VM and also to access its virtual disk for files. So enter a hostname & note it down somewhere. I gave it “ubuntu-vm” so that its easy to remember.

 

STEP 17: After that it’ll ask for username & password. Set those up & then it asks whether you want to have Ubuntu encrypt your home directory. We don’t need this here so select “No” & hit ENTER.

 

STEP 18: On the next screen it’ll ask about disk partitioning, select “Guided – use entire disk and setup LVM”.

 

STEP 19: On the next screen it’ll ask if you want to write partitioning scheme to disk. Select Yes to proceed. On the next screen it’ll ask the size of volume to be used, don’t change the default value & hit ENTER to proceed. On the next screen it shows the partitioning summary & will ask if you want to write the changes. Select “Yes” to proceed.

 

STEP 20: On the next screen it will show update options for operating system. Select “No automatic updates” & hit ENTER key.

 

STEP 21: Next on software installation screen, select OpenSSH server & LAMP server. Use arrow keys to move up and down & spacebar to select/unselect an option. Hit the ENTER key to proceed.

 

STEP 22: On the next screen it’ll ask you to set a root password for MySQL. Its a good idea to do so; if you don’t then that’s ok as well since this is just your development stack.

 

STEP 23: After several minutes the installation will be done and Ubuntu will reboot and present you with a login prompt. In the Devices menu on top, select Install Guest Additions. After its done, login using the username & password you chose during installation.

Open the network interfaces file via the following command:

  1. sudo nano /etc/network/interfaces

When you have the file open, add following to the end of the file:

text
/etc/network/interfaces

  1. auto eth1
  2. iface eth1 inet static
  3. address 192.168.56.101
  4. netmask 255.255.255.0

and then press ^X (Control + X ) and then Y and then press ENTER key to save the file.

Here we assigned a static IP to Ubuntu so that we won’t have to check on every boot what IP has been assigned to it. Virtual Box assigns IPs in range 192.168.56.1xx, so unless you have another VM using 192.168.56.101, you should be fine with this IP here.

Now shut down the VM using following command:

  1. sudo shutdown -h now

 

STEP 24: Now we need to setup Virtual Box network so that our VM can talk to host OS without any issues. For this we added the “Host-only network” in STEP 8 above. In the Virtual Box VM Manager, click on Settings to open our VM’s settings, then click the Network icon in toolbar and switch to Adapter 2 tab. Click the checkbox to enable it, select “Host-only Adapter” in the first drop down list. In the second drop down the adapter you added in STEP 8 should be selected by default.

Now click on the Shared Folders icon in the toolbar. Click on the folder with plus (+) icon on the right and select a folder that you wish to be mounted in the VM and the folder name which you wish to use for it in the VM. Click on the checkbox labelled “Auto-mount” so that this folder will be mounted automatically when the VM boots. Click Ok and again click Ok.

 

STEP 25: Now we need to add this VM’s IP to our hosts file so that we don’t have to use the IP every time we want to connect to it. Open the Terminal & run the following:

  1. sudo nano /private/etc/hosts

Add the following in the file:

text
/private/etc/hosts

  1. 192.168.56.101  <vm_hostname>
  2. 192.168.56.101  sandbox.dev

Replace <vm_hostname> with the hostname you gave when installing Ubuntu in STEP 16. Press ^X (Control + X ) and then Y and then press ENTER key to save the file. Use the following command to flush your local DNS cache:

  1. sudo killall -HUP mDNSResponder

 

STEP 26: Now that Ubuntu is installed & we have a hosts file entry on our host OS, we can SSH into Ubuntu VM to configure it further. Start the Ubuntu VM but don’t login there. In the terminal issue the following command:

  1. ssh <username>@<vm_hostname>

Replace <username> with the username you chose in Ubuntu installation in STEP 17. Replace <vm_hostname> with your hostname. You will be told that the server is not recognized & asked if you would like to continue. Select Yes and then enter the password you chose in STEP 17.

After you are logged in, update list of available software for our ubuntu and then update all installed packages using the following commands:

  1. sudo apt-get update
  2. sudo apt-get -y upgrade

 

STEP 27: Now we install the basic stuff like build-essential, module assistant, linux-headers, dkms, zip, unzip, make, curl, wget, libxml, openssl, Git and Subversion. Run the following commands:

  1. sudo apt-get -q -y install build-essential module-assistant linux-headers-$(uname -r) dkms zip unzip
  2. sudo apt-get -y install php5-dev
  3. sudo apt-get -q -y install gcc make wget cron libxml2 libxml2-dev libzip-dev libbz2-dev curl libcurl4-openssl-dev libcurl3 libcurl3-gnutls libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libmcrypt-dev libmcrypt4 libxslt1-dev libxml2-dev git subversion

After the installations are complete, we install Guest Additions. Run following commands:

  1. sudo mount /dev/cdrom /media/cdrom
  2. sudo sh /media/cdrom/VBoxLinuxAdditions.run --nox11

 

STEP 28: Now we will configure a few things. First, we’ll add our user and apache user to the virtual box group and enable apache rewrite module.

  1. sudo usermod -a -G vboxsf <username>
  2. sudo usermod -a -G vboxsf www-data
  3. sudo a2enmod rewrite

Replace <username> with the username you chose in Ubuntu installation in STEP 17.

After its done, issue the following command to open Apache configuration:

  1. sudo nano /etc/apache2/httpd.conf

and add the following line to it:

text
/etc/apache2/httpd.conf

  1. ServerName localhost

Press ^X (Control + X ) and then Y and then press ENTER key to save the file. Then run the following to add a site to apache:

  1. sudo nano /etc/apache2/sites-available/default

Add the following to the file:

apache
…che2/sites-available/default

  1. <VirtualHost *:80>
  2.     ServerName sandbox.dev
  3.     ServerAlias sandbox.dev
  4.     DocumentRoot /media/sf_<folder_name>/
  5.    
  6.     LogLevel info
  7.     ErrorLog ${APACHE_LOG_DIR}/sandbox.dev-error.log
  8.     CustomLog ${APACHE_LOG_DIR}/sandbox.dev-access.log combined
  9.    
  10.     RewriteEngine On
  11.     RewriteLog ${APACHE_LOG_DIR}/sandbox.dev-rewrite.log
  12.     RewriteLogLevel 0
  13. </VirtualHost>

Replace <folder_name> with the folder name you gave for your shared folder in STEP 24. Press ^X (Control + X ) and then Y and then press ENTER key to save the file.

Now we enable this site in Apache and restart it.

  1. sudo service apache2 restart

Now we enable error reporting in PHP. Open the PHP ini file using following command:

  1. sudo nano -w /etc/php5/apache2/php.ini

Now press ^W (Control + W) to search and type in “error_reporting =” and on the line where it defines error reporting, set it to:

ini
/etc/php5/apache2/php.ini

  1. error_reporting = E_ALL | E_STRICT

Now search for “display_errors =” and on the line where it defines display errors, set it to On. Then search for “log_errors =” and on the line where it defines log errors, make sure its set to On. Below that is the “log_errors_max_len”, set it to 0. After that, search for “error_log =” and add a new line after the comment where it shows error_log, and add following:

ini
/etc/php5/apache2/php.ini

  1. error_log = /media/sf_<folder_name>/php_errors.log

Replace <folder_name> with the folder name you gave for your shared folder in STEP 24. Press ^X (Control + X ) and then Y and then press ENTER key to save the file.

Now its time to configure MySQL. By default MySQL will only listen to connections from 127.0.0.1. We need to be able to connect from our host OS for easier debugging or to use with any GUI tools on host OS, so we’ll make it listen to connections from all IPs. Run the following command to open MySQL config:

  1. sudo nano -w /etc/mysql/my.cnf

Now press ^W (Control + W) to search and type in “bind-address” and on the line where it defines bind-address, set it to:

ini
/etc/mysql/my.cnf

  1. bind-address        = 0.0.0.0

Press ^X (Control + X ) and then Y and then press ENTER key to save the file. Now login to MySQL and run the following SQL:

  1. GRANT ALL ON *.* TO 'root'@'%';
  2. FLUSH PRIVILEGES;

Exit MySQL & restart it using the following command:

  1. sudo service mysql restart

 

STEP 29: Now we’ll create a folder with symlinks to Apache & PHP conf files etc. for easy access. Run the following commands:

  1. cd ~/
  2. mkdir _lamp && cd _lamp
  3. sudo ln -s /etc/apache2/httpd.conf
  4. sudo ln -s /etc/apache2/sites-available/default default-site
  5. sudo ln -s /etc/php5/apache2/php.ini
  6. sudo ln -s /media/sf_<folder_name> www

Replace <folder_name> with the folder name you gave for your shared folder in STEP 24. Now “~/_lamp” is the folder to go to whenever you need to edit Apache or PHP config files.

 

STEP 30: Now we’ll install Xdebug & configure it with PHP. Run the following commands:

  1. cd
  2. git clone git://github.com/derickr/xdebug.git
  3. cd xdebug*
  4. phpize
  5. ./configure --enable-xdebug
  6. make
  7. sudo make install

After the installation is done, which will take a bit, issue the following commands to open php.ini:

  1. cd ~/_lamp
  2. sudo nano php.ini

Use ^W (Control + W) to search for “date.timezone” and set it to your preferred time zone. You can look up list of supported time zones here. Above “date.timezone”, add the following:

ini
~/_lamp/php.ini

  1. [xdebug]
  2. zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so
  3. xdebug.remote_enable=1
  4. xdebug.remote_connect_back=1
  5. xdebug.remote_port=9000
  6. xdebug.var_display_max_children=-1
  7. xdebug.var_display_max_data=10000
  8. xdebug.var_display_max_depth=-1

Press ^X (Control + X ) and then Y and then press ENTER key to save the file. Now run the following command to restart Apache.

  1. sudo service apache2 restart

 

Now you can setup your websites/apps in Apache in the file at “~/_lamp/default-site”, keep your files in “~/_lamp/www”, import your databases in MySQL and start using the PHP development stack you just installed in your VM. One of the good things about having the webroot of the VM in a folder on the host OS is that if you use an IDE or have a favourite code editor then you can still use them all to write code and files will be saved in the VM’s webroot. :tup: And same goes for the Git/Subversion client you might already have on your host OS. 🙂