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.
- Apple MacBook A1342, or a variant of it
- Apple Mac OS X installation DVD that came with the laptop
- Single-disc Windows 7 64-bit installation DVD
- Internet connection (either via LAN or broadband modem)
- 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.
- 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.
- Install Windows 7 as you would normally. Select BOOTCAMP as the partition to install Windows 7 on.
- 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.
- Install any additional drivers and software as required.
- 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?
- 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.
- I can take the VM with me and run it on another computer if need be.
- I don’t have to install server software directly on my base operating system. This keeps it lean and clean for gaming and such 😀
- Newest stable release of Oracle VirtualBox
- 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.
- A preferably fast and reliable internet connection.
- Install Oracle VirtualBox. Just click next next next done. Simple as that.
- Fire up Oracle VirtualBox and click File –> Preferences.
- Set your Default Machine Folder, or leave it as it is if you prefer to do so. Click OK.
- Specify a name for your new virtual machine, set Operating System to Linux and Version to Ubuntu (64 bit). Click Next.
- 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.
- 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.
- Click Next, then click Create. After the hard drive dialog disappears, click Create again.
- 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:
- Select English and then select Install Ubuntu Server.
- Select your language and location, and configure the keyboard if necessary. I use English (U.S.).
- Specify your hostname.
- Set up your partitions. To keep it simple, I use guided partitioning using the entire disk.
- Set up your user name, password, and home folder encryption (if you want to).
- When asked for a proxy configuration, leave it blank. The installer should start downloading stuff using apt.
- Choose your automatic security update setting.
- In the software selection, select LAMP Server and Mail Server. Select Internet Site for the mail server setting.
- 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:
- Run sudo apt-get update to update the repository to the latest source.
- Run sudo apt-get dist-upgrade to upgrade all installed packages to the latest version.
- 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.
- Run sudo mount /dev/cdrom /media/cdrom to mount the VBoxGuestAdditions media.
- Run cd /media/cdrom to switch directory to the mounted media.
- 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:
- 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.
- Run mkdir /home/[your_username]/public_html to create your own public html directory. This will be the mount point of the shared folder.
- Run id [your_username] to find out your uid and gid. Take note of this.
- Run cd /etc/init.d to change to the startup script directory.
- Run sudo pico vboxshare-automount to open pico and create a new file called vboxshare-automount.
- 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.
- Run sudo chmod +x vboxshare-automount to make the file executable.
- Run sudo update-rc.d vboxshare-automount defaults to make the script run on every startup.
- 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:
- Shutdown your virtual box if it’s not already off.
- Go to Settings –> Network –> Adapter 1. It should be set to NAT by default.
- Click on Advanced and then Port Forwarding.
- Click on the add icon, then set rule name to http, protocol is TCP, host port 8080, and guest port 80.
- Start up your virtual box.
- Fire up your browser and go to http://localhost:8080. You should see the default apache2 page saying that it works.
- Now go to the virtual box’s console and run sudo a2enmod userdir to enable per-user directory service.
- 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.
- Run sudo /etc/init.d/apache2 restart to restart the apache server.
- On your host machine, create a new file info.php in the root of the shared folder. Fill it with the usual phpinfo(); code.
- 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.