How I Setup My PHP Dev Env: Part 1

This is a post long overdue. Originally started as a favor to a good friend of mine, this now has also turned into my personal quest to document my efforts to set up my PHP Development Environment on my MacBook.

DISCLAIMER: This is some geeky stuff, you have been warned.

So, without further ado, let’s get to it!

Section 1: The Operating System

I have an Apple MacBook A1342, so the documentation pertains to that specific hardware. If you have another type or brand of computer, or if you choose another operating system, the following instructions should not apply to you. You should go ahead and install your operating system as you would normally.

Prerequisites:

  1. Apple MacBook A1342, or a variant of it
  2. Apple Mac OS X installation DVD that came with the laptop
  3. Single-disc Windows 7 64-bit installation DVD
  4. Internet connection (either via LAN or broadband modem)

Steps:

  1. Fire up your MacBook and let it enter Mac OS X as usual. Find Bootcamp Assistant in your Applications folder and open it. Set up your BOOTCAMP partition. As I’ve done this 2 years ago, I don’t need to do it again, and I can’t remember the exact steps, so please go visit Apple’s support site if you need more detailed information.
  2. Insert your Windows 7 64-bit installation DVD and restart your MacBook. Hold down the Option key while booting up to show the boot options, and select the DVD.
  3. Install Windows 7 as you would normally. Select BOOTCAMP as the partition to install Windows 7 on.
  4. At this point you should be able to log into Windows 7. Now, insert your Mac OS X installation DVD, find Bootcamp for Windows, and install it.
  5. Install any additional drivers and software as required.
  6. Voila! Your base operating system is now ready.

Section 2: The Virtual Machine

I choose to use a virtual machine to host my PHP and MySQL server. Why?

  1. I can emulate the deployment server environment as closely as possible without having to compromise my development environment. This way I can only install server software as necessary, perform server configuration as if I was doing it on the real server, and reinstall the entire server if necessary without having to install my base operating system.
  2. I can take the VM with me and run it on another computer if need be.
  3. I don’t have to install server software directly on my base operating system. This keeps it lean and clean for gaming and such 😀

Prerequisites:

  1. Newest stable release of Oracle VirtualBox
  2. Installation DVD (or disk image file) of a guest operating system of your choice, or preferably an image of it. I tend to like the latest server version, so I use Ubuntu 11.10 Oneiric Ocelot Server Edition (64-bit), which is downloadable from the Ubuntu website.
  3. A preferably fast and reliable internet connection.

Steps:

  1. Install Oracle VirtualBox. Just click next next next done. Simple as that.
  2. Fire up Oracle VirtualBox and click File –> Preferences.
  3. Set your Default Machine Folder, or leave it as it is if you prefer to do so. Click OK.
  4. Specify a name for your new virtual machine, set Operating System to Linux and Version to Ubuntu (64 bit). Click Next.
  5. Set the RAM allocated for the virtual machine. Usually 512MB is enough for servers, but you’d probably want to use more if you intend to install the desktop version of Ubuntu. Click Next.
  6. Create a new virtual hard drive. Leave all options as it is and click Next to bring up the virtual hard drive dialog. Select VDI, Dynamically Allocated, and set the size to your liking. I use 20GB.
  7. Click Next, then click Create. After the hard drive dialog disappears, click Create again.
  8. Insert your Ubuntu installation DVD or set the disk image file in Settings –> Storage –> IDE Controller and start your virtual machine.

At this point you should be booted into the Ubuntu installation DVD. Let’s move on:

  1. Select English and then select Install Ubuntu Server.
  2. Select your language and location, and configure the keyboard if necessary. I use English (U.S.).
  3. Specify your hostname.
  4. Set up your partitions. To keep it simple, I use guided partitioning using the entire disk.
  5. Set up your user name, password, and home folder encryption (if you want to).
  6. When asked for a proxy configuration, leave it blank. The installer should start downloading stuff using apt.
  7. Choose your automatic security update setting.
  8. In the software selection, select LAMP Server and Mail Server. Select Internet Site for the mail server setting.
  9. It’s safe to install GRUB as recommended, go ahead and do it.

Section 3: VirtualBox Guest Additions

Proceed until the virtual machine restarts, then shut it down. Remove the Ubuntu installation DVD (image) from the (virtual) drive and insert the VBoxGuestAdditions.iso image from C:Program FilesOracleVirtualBox. Now it’s time to play with the command line:

  1. Run sudo apt-get update to update the repository to the latest source.
  2. Run sudo apt-get dist-upgrade to upgrade all installed packages to the latest version.
  3. Run sudo apt-get install dkms to install Dynamic Kernel Module Support (DKMS), which will be used to install VirtualBox guest additions on your Linux machine.
  4. Run sudo mount /dev/cdrom /media/cdrom to mount the VBoxGuestAdditions media.
  5. Run cd /media/cdrom to switch directory to the mounted media.
  6. Run sudo sh ./VBoxLinuxAdditions.run to install the VirtualBox Guest Additions.

Section 4: Shared Folders

Now that we have VirtualBox Guest Additions installed, we can start adding shared folders to the virtual box:

  1. Shutdown the virtual box, go to Settings –> Shared Folders, and create a shared folder referencing a directory of your choice on the host machine. Take note of the share name.
  2. Run mkdir /home/[your_username]/public_html to create your own public html directory. This will be the mount point of the shared folder.
  3. Run id [your_username] to find out your uid and gid. Take note of this.
  4. Run cd /etc/init.d to change to the startup script directory.
  5. Run sudo pico vboxshare-automount to open pico and create a new file called vboxshare-automount.
  6. Write mount -t vboxsf -o uid=[your_uid],gid=[your_gid] [your_shared_folder_name] /home/[your_user_name]/public_html in the file. Press Ctrl-X and then Y, enter, to save the file.
  7. Run sudo chmod +x vboxshare-automount to make the file executable.
  8. Run sudo update-rc.d vboxshare-automount defaults to make the script run on every startup.
  9. Restart your virtual box, then login and run cd public_html. If you run ls, you should see the contents of your shared folder there.

Section 5: Port Forwarding, User Directories, and PHP Parsing

Alright, we now have a shared folder that we can use to directly edit our PHP files. Now we need to enable our host machine to access the virtual box’s PHP server:

  1. Shutdown your virtual box if it’s not already off.
  2. Go to Settings –> Network –> Adapter 1. It should be set to NAT by default.
  3. Click on Advanced and then Port Forwarding.
  4. Click on the add icon, then set rule name to http, protocol is TCP, host port 8080, and guest port 80.
  5. Start up your virtual box.
  6. Fire up your browser and go to http://localhost:8080. You should see the default apache2 page saying that it works.
  7. Now go to the virtual box’s console and run sudo a2enmod userdir to enable per-user directory service.
  8. Run sudo pico /etc/apache2/mods-available/php5.conf to edit the PHP5 module configuration. Comment out the part between the <IfModule> tags to enable PHP parsing in home directories.
  9. Run sudo /etc/init.d/apache2 restart to restart the apache server.
  10. On your host machine, create a new file info.php in the root of the shared folder. Fill it with the usual phpinfo(); code.
  11. Direct your browser to localhost:8080/~[your_user_name]/info.php. You should see the PHP information page there.

That’s it. Now we have a working Apache/PHP server running on Ubuntu and accessible seamlessly through our Windows machine. Next we should check if we have phpMyAdmin and everything else we need to really start cracking. But I think I’ll save that for another post: Part 2.