Wednesday, May 21, 2008

The joys of setting up a development environment on Linux

I spent a large portion of the day today setting up a proper development environment. My job has needed one for some time, but us developers are notoriously overworked. Luckily, my current project (a new module added into the client section) gave me a perfect excuse to build up a development environment. I have had plans for some time to set up a proper CVS repository, local Apache servers, and make good use of our development server.

In the 14 months that I have spent at my current job, one thing that I've learned is that attempting to make changes to pages in a live environment is a pain in the ass. I have about a two minute window where I can have glitches and errors streaming on the page before I get a phone call or special visitor complaining about a downed site. Even from day one, I knew this was a bad idea, but only recently have I really been able to do anything about it, and here's what I did:

First off, a local apache build is essential for a PHP developer. I run Kubuntu 8.04 and through a single command, I got my entire server set up and ready to go. Open up a terminal and type the following to set up Apache2, PHP5, MySQL 5 and PHPMyAdmin:

sudo apt-get install php5 php5-mysql phpmyadmin apache2 mysql-server-5.0

If you want some additional PHP modules, you can throw in a few more for good measure. I added php5-xsl, php5-gd and php5-curl. Once this processes, typing 'localhost' into the browser URL bar will get you to view your /var/www directory as a website. This is the first critical step to a proper development environment.

Next up, to get phpmyadmin properly viewable, edit the /etc/apache2/apache2.conf file and add a single line at the end:

Include /etc/phpmyadmin/apache.conf

This will allow phpmyadmin to become accessible at http://localhost/phpmyadmin/.

So now that you have a local apache server, you can begin making sites in a development environment. However, most sites require database information to operate, so you have to sync up your live mysql server with your local mysql server. This can be done manually through export/import of sql files, or you can set up an automated synch tool. Personally, I set up a crontab and a series of SOAP calls that authenticates itself to my remote server, gets the database's table list, and then goes through each table, gets the structure, deletes the local table, rebuilds the local table, gets the table data, and populates the table. This operates every night so I don't even have to worry about my data being matched up.

The next step, one that is not critical, but I find to be useful, is to set up your hosts file to access the site in an easy fashion. This is especially useful if your site uses a lot of absolute paths for it's links and relocating from "http://www.domain.com" to "http://localhost/domain/" would cause linking issues.

If you want to set it up so that typing in "http://localdomain/" redirects to /var/www/domain, follow these steps:

Edit the /etc/hosts file and add a new entry. You should assign a new IP address for each entry, and the text field will be what you want to type in. For this example, add in "localdomain" to the entries listed at the top.

Next, edit /etc/apache2/sites-available/default to add in the new virtualhost. At the end of the page, add the following lines:

<VirtualHost *>
ServerName localdomain
DocumentRoot /var/www/domain
</VirtualHost>

The first step, editing the hosts file, adds an entry on what the local machine may be referred to. Adding in 'localdomain' tells the local hosts file that when that site is attempting to be accessed, redirect to the local server.

The second step, editing the site file, tells Apache what to do if it is asked to go to localdomain.

The final step is to restart the apache server and then test the site.

Now that you've done all of this, you can safely build and tinker with a development version of a site, rather than try to toy with the live version that people are using. Combine this with a proper CVS setup, and you can have a team of developers working in a coordinated, organized environment with well managed projects.

No comments: