Showing posts with label mac. Show all posts
Showing posts with label mac. Show all posts

Thursday, July 1, 2010

How to get SubVersioN on Mac OS X 10.6?

Even if you work on a project on your own it's worth having version control system in place. It helps to keep things tidy and gives you time machine possibilities for working with your code (I am not talking about the trademarked machine). I like SVN which is mature and seems to have it all. Let's have a look how to install that beast on our lovely Macs.

Download SVN for free

First good news - SVN comes with a nice Mac-like installer and is completely free and published under Apache Licence. You can download it from CollabNet website. It requires free registration which at first seems a bit dodgy but CollabNet is the company responsible for creating SVN and it's a good source.

Install SVN on Mac OS X 10.6

Once you have downloaded the package it's time to install it. The installer looks all pretty and takes no longer than just a few moments. It installs the whole svn bunch (server and client) in /opt/subversion directory. The only downside is that you don't get any fancy application like you do for MAMP to control it. After you've finished the installation it all happens in the command line... but cheer up, it's not that scary. You will get it up and running in no time.

Set up evironment

SVN gives you complete freedom of the location of your repositories. It goes like this. You create a directory wherever you wish and tell your svn server to feel at home in there. That directory is then your svn server root directory. Your SVN server will create your repositories in there.

Let's get started then. Since all the svn related programs are located in /opt/subversion/bin you have to specify the path every time you need to run them. It's not handy at all, we would prefer not to worry about where they are. That's not a biggy. Fire up your Terminal (it's that monitor icon in /Applications/Utilities) and edit the file .profile in your home directory. I, for one, like vim but it might be slightly depressing experience at first if you are not familiar with it. In that case use pico, it's really straighforward

$ pico .profile
and add the following line at the end of the file:
export PATH=/opt/subversion/bin:$PATH
The .profile file is read every time you create new shell, so next time you open your Terminal the change will be applied. Because we don't want to close the Terminal just yet only to reopen it let's cheat a tiny bit:
$ . .profile

Well done! Let's check if it works

$ svn
Type 'svn help' for usage.
If you got similar output you are ready to proceed.

Set up SVN repository

Now it's time to create root directory for the SVN server. You can name it whatever you like and create it even in your home directory if you wish. SVN is not fussy at all. Let's do that then and let's give it meaningful name "svnrepos".

$ mkdir svnrepos
And finally it's time to launch your SVN server. By the way it's not a typo, there is no 'r' at the end:
$ svnserve -d -r svnrepos
There are no repositories. All we have is just SVN server ready for action. Let's give him something to look after. If you want to create a test repository to play with it for a while, just do this:
$ svnadmin create svnrepos/test

Set up privileges

Congratulations! You have just created your repository. Good thing is you don't need to worry about having to do some magic tricks later on to get rid of the test repository whenever you feel like it. Simply delete the directory svnrepost/test and it's gone forever. For now though, it's accesible through the URL svn://localhost/test but you cannot do much with it unless you grant yourself the privileges.

SVN server keeps access control lists per repository, so you need to dive into config directory of your newly created repository and conquer! Sounds like a big job but it isn't. All it takes is adding one line in one file and removing one comment in another one. Go to svnserve/test/conf and edit the file passwd first. Here you will add your preferred username and password. It's easier when you choose your Mac username. For example, my Mac username is "jacek" and my password for the test repository is "test", so I added a line jacek = test.

Next edit the file svnserve.conf and remove comment from the line saying password-db = passwd.

Believe it or not, that's it! Now you can access your repository through your favourite SVN client and do whater SVN clients do. One thing you need to remember is to run

svnserve -d -r svnrepos
every time you start your Mac. Of course it's quite easy to make it happen automagically but that's for another story.

Thanks for reading. I hope you found this guide useful, it worked for you and made you a bit happier. Please don't think twice before you leave your comment.

Jacek

Sunday, February 7, 2010

PHP: Cross Platform Testing Lab on Mac How To

Taking PHP development seriously, it is not very uncommon practice to do cross-platform tests of own code. With virtual machines it is dead easy to test your PHP code on three popular systems (Mac OS X, Linux and Windows) using only one physical machine. To make it even more pleasant all systems can share PHP code from one location hence there is no need to copy every time you want to test. And here comes the cream: you can get fully-blown eye-candy virtualizer for free.

VirtualBox is good and is free!

Yes, that is correct. VirtualBox is free and made by SUN. It is good piece of software, very reliable and easy to use. You can get VirtualBox for Windows, Mac or Linux.

VirtualBox performance on Mac Mini

I used VirtualBox to create Vrtual Cross-Platform PHP Lab on my Mac Mini run by Snow Leopard (10.6.2). My Mac was armed with 1.83GHz Intel Core 2 Duo CPU and 2BG of RAM. I have created one virtual machine for Windows XP SP2 and one for Debian 5.03 "lenny". Each virtual machine was limited to 256MB of memory and 2GB of hard drive space.

Running both virtual boxes with AMPs on them along with MAMP on the hosting Mac did not visibly affect overall host system responsiveness. I could have Chrome, Firefox, Aptana, Preview, iTunes, Skype and Activity Monitor running concurrently and smoothly. Only Spaces (9 desktops) were a bit more choppy than usual. Each of the virtual boxes was also responsive.

Both guest systems were visible and accessible over the local network. They could easily obtain IP addresses from DHCP server existing within the network. All the network communication was also reliable except for situations when one of the boxes (either real or virtual one) generated heavy traffic on the network interface. Downloading 100MB from Internet at 200kB/s was enough to cause connection disruption.

Set up Mac OS X host system

If you are PHP developer working on Mac, probably you already have half of the work done. Nevertheless assuming you are starting from scratch the following steps need to be accomplished:
  1. Download MAMP (Mac OS X AMP) and install it.
  2. Download VirtualBox for Mac and install it.
Both packages come with nice Macish installers and therefore the whole process is really straightforward.

Create WAMP virtual machine on your Mac

Now it's time to create your Windows virtual machine and set it up. I used Windows XP because it is relatively lightweight and fast compared to alternatives. VirtualBox is not fussy though and allows you to prepare virtual machine for many different operating systems including quite a few versions of Windows. Here is a step by step guide which tells you how to create a virtual machine, install Windows (XP) on it and configure WAMP to access PHP projects stored in the host filesystem:
Create new virtual machine for Windows.
Simply go to Machine -> New. Then just follow the guide, it takes about 2 minutes. There are no difficult questions and if you want to know more - the help is really good. I have created Virtual Machine called "virtual-winxp" with 256MB of RAM and 2GB of new virtual hard drive being dynamically expanding storage. The network adapter I have chosen was bridged adapter. The latter is important if you want to have access to LAN and Internet from your the guest OS and be able to access the guest OS from your Mac.
Install your preferred version of Windows.
My Win XP installation process was real time. When installer announced 30 minutes left - it was 30 indeed minutes. Network name of my Win XP was again "virtual-winxp".
Download WAMP and install it
Nothing I could think of to add here.
Install VirtualBox Guest Additions
Go to Devices -> CD/DVD Devices and choose VBoxGuestAdditions.iso. You can do so even when the guest OS is running. Virtual CD will appear in your drive and you will know what to do. VirtualBox Guest Additions are essential if you want to share your PHP code with the host OS.
Configure shared folder
That would allow you to share your PHP code with the host OS. Go to Devices -> Shared Folders... and click "Add shared folder" icon - the one with "+" sign. Then choose your projects folder from Mac, say /Applications/MAMP/htdocs/projects and name it, say "projects". Now that folder will be accessible from Windows as pseudo network drive \\vboxsrv\projects. It is probably a good idea to make it permanent if you don't want to repeat this step every time you start your guest OS.
Configure your WAMP to use projects folder
Let's assume you want to access your PHP projects through your WAMP as http://virtual-winxp/projects. Go to your Windows, click on the WAMP icon in system tray, choose Apache -> Alias directories -> Add an alias. When the command window appears, type in the alias name "projects".
On the second screen enter the real directory name being "//vboxsrv/projects". Forward slashes are not a mistake, Apache for Windows likes it like that. Your WAMP should restart and when you go to http://localhost/projects you should see your Mac projects directory.
Make your WAMP visible for your Mac
If you prefer to test results of your PHP on your Mac (who wouldn't) rather than on the guest OS, it might be a good idea, to make your WAMP visible in the local network. All you need to do is click on your WAMP icon in system tray, choose "Put Online" and wait till the Apache restarts.

Create LAMP virtual machine on your Mac

Preparing Linux virtual box was even easier because there was no clicking involved! Ok, in fact I was installing Debian without GUI, so I needed to amend a couple of files manually. No rocket science though, just some config files.
Create new virtual machine for your Linux
Similar procedure as for virtual machine for Windows, only different name ("virtual-debian" in my case). Settings exactly the same: 256MB RAM, 2GB virtual HDD expanding storage and bridged network adapter. Please remember, that the bridged network adapter is important if you want to access LAN and Internet from your guest OS and also access the guest OS from your Mac.
Install your preferred distribution of Linux
I have chosen Debian because it has very good package management and I like it. The hostname I set for it was again "virtual-debian".
Install nano
As mentioned before, you might need to change a few config files. If you are not familiar with vim it is probably a good idea to install more friendly console editor. When logged in as root run:
#aptitude install nano
Install LAMP for Debian
Just follow the link, it's all in there.
Prepare for installing VirtualBox Guest Additions
These are installed as a kernel modules which are being compiled for you by the installer. That means that you need to install kernel headers beforehand. First of all find out what kernel you are running:
#uname -r
I got result like this:
2.6.26-2-amd64
Once you know the version it's time to install headers. Simply repeat the version at the end of linux-headers package:
#aptitude install linux-headers-2.6.26-2-amd64
The installer might ask you a few questions, for example about installing additional packages. It's safe to answer "yes" to all of them.
Install VirtualBox Guest Additions
Again, go to Devices -> CD/DVD Devices and choose VBoxGuestAdditions.iso. You can do so even when the guest OS is running. Virtual CD will become ready for you to mount and access it:
#mount /media/cdrom
Then you need to copy installer to your hard drive:
#cd /media/cdrom
#cp VBoxLinuxAdditions-amd64.run /~

and run it:
#cd
#./VBoxLinuxAdditions-amd64.run

Configure shared folder
Same as with Windows virtual box. That would allow you to share your PHP code with the host OS. Go to Devices -> Shared Folders... and click "Add shared folder" icon - the one with "+" sign. Then choose your projects folder from Mac, say /Applications/MAMP/htdocs/projects and name it, say "projects". Now that folder will be available to mount from your Linux (Debian) as... projects (read next point for explanation). It is probably a good idea to make it permanent if you don't want to repeat this step every time you start your guest OS.
Configure your Debian to mount shared folder on startup
Now everything is set up and ready we only need to mount projects folder from your Mac on your Linux virtual box. Assuming you want to access your projects folder as http://virtual-debian/projects and you don't want to mess around with Apache config too much (at least at this point) simple create the mount point and mount:
#cd /var/www/
#mkdir projects
#mount -t vboxsf projects projects

Now inside projects directory you should see contents of your Mac projects directory. To make this mount occur every time you start your Linux, you need to add a line to /etc/fstab config file. Open the file:
#nano /etc/fstab
and add the following line:
projects    /var/www/projects    vboxsf    ro    0    0
Save the file and you are sorted. From now on, whenever you boot Linux the folder will be mounted for you and ready to use. And don't worry about the mount error - it's because default Linux mount happens before the vboxadd service is loaded.

Setup Fixed IP Addresses for your Virtual Farm

My virtual boxes get IP addresses from DHCP server running within my network. Their IPs used to change every now and then and I must admit I found it quite annoying. To avoid that problem I have assigned IP addresses statically to both virtual boxes on my DHCP server. Now they still receive the whole information package (DNS, gateway and so on) but their IPs are same every time.

Access your virtual boxes by names

If you prefer to access your virtual boxes via their names (like virtual-winxp) rather then IPs you may want to add them to your /etc/hosts file on your Mac. It's dead easy and takes no time. Simply run your Terminal.app (it's in /Applications/Utilities directory) and switch to root:
$su -
here you enter your password, and then edit the /etc/hosts file:
#nano /etc/hosts
Here you need to add one line for each virtual box. First IP then the name you like, for example:
192.168.0.11    virtual-debian
192.168.0.12    virtual-winxp
Save it and job done.

Thanks for reading. I hope you found this guide useful, it worked for you and made you a bit happier. Please don't think twice before you leave your comment.