Tag Archives: configuration

Switching coffee and PHP testing

Whoa, long time since my last post.

Anyways, what has happened recently?

I’ve switched jobs and are now working at a small IR firm in Denmark, which is located on Amager.
We do webcasts / Quarterly reports for firms in Denmark.

Saying “We do” is a bit off though.
I don’t, that is. I help out if we are missing a technician, else i’m creating webapps to help the business complete daily tasks faster.

And how is that going?
Well, I’ve completed a few systems already, and within one month the first system was deployed to the wild. It was fun to create a whole new system, especially because I am on my own.
Roughly speaking, i’m the only developer there, so I don’t get a lot of sparing on the code i’m writing. Only the designs and the general “look and feel” of an app.
It’s fun, it’s different and I like the pace.

An other thing i’ve been working on, is moving the old and new sites to new virtualized servers.
I’ve been managing a few servers for the last 6 years, but not anything on this scale. But it’s fun. It takes a little time from the programming though, but after it’s configured, it mostly runs smoothly.

I developed an SSO system and deployed it to the wild a few months ago. So far i’ve only had minor problems with it, and all things are running great.

And yes, I code in PHP. It was by choice actually. On my last jobs I’ve coded Bash, Perl, C, C++, then Java and then Ruby on Rails and on the new job I was given a free choice.

So I chose PHP.

“Why?” you might ask, when there’s soo much hype about Ruby on Rails these past few years. Without starting a flame wall in the comments, let’s just say, that while Ruby is a beautiful language, I just couldn’t get accustomed to the Rails frame work. So many rules, so many conventions. So little time. (The testing was nice though.. but meh)
I might return to Rails someday, but for now, it’s PHP controlling the battle.

The webapps i’ve created at my new job are all created using my small github project called php-mvc-base, which basically is just a structure I use to get started on a PHP project. It gives me “nice urls”, but other than that, the rules are pretty basic.

The structure outlines as follows:

/app
/app/controllers
/app/models
/app/views
/public/images
/public/javascripts
/public/stylesheets

And all controllers (ofc) go in the app/controllers folder.
So let’s say, that we want a new controller at the URL http://localhost/my_projects/ there is basically two things that can be done:
* Create a folder in the app/controllers folder, called my_projects. Place an index.php file there, do the controller code.
* Create a my_projects.php file in the app/controllers folder. Do the controller code.
Both options give the same path.

Simple and clean (well, at least in my world. And I use the folder approach btw, if you wanted to know).

(You can find the project at http://github.com/jimmiw/php-mvc-base along with a simple example.)

I’ve also released a few javascripts to CodeCanyon and I’ve had a few purchases already. It’s nice knowing people can use your stuff. I mostly coded them because I had a problem they could solve, but releasing them there, made the code so much better. This was mostly due to the javascript approval team on CodeCanyon (Thanks Jeremy McPeak for the patience and help).

You can see my profile on CodeCanyon here: http://codecanyon.net/user/jimmiw

I’ve also taken over a small project with a designer friend of mine called Janus C.
The project is familielivet and is a danish page the centers around the family. It’s free to use and hopefully easy to understand and use for all ages.
I’ve not actually released any code to the system yet, but we a doing some design changes and a total rewrite of the codebase, and it will hopefully be released soon.

After switching from Ruby to PHP I missed the easy testing that Ruby offered. Thank god for phpunit btw, this lovely tool simply makes testing fun again. Be sure to check it out when you are writing tests for your webapps (As you should be!).

On an interesting side note, Rails offers the ease of different environments for you to use.
E.g. Test, Staging and Production.
I actually found a simple way of doing this with PHP as well, but it requires that you have access to the apache configuration files (which you at least have on your development machine).

Roughly speaking, all you need to do, is to add a variable to your apache config, and then use the $_SERVER variable in PHP to test what environment your are currently working on.
Nice and simple.
A small example is this (taken from my development machine):
Just add the following piece of code to the bottom of your httpd.conf file
SetEnv APPLICATION_ENV "development"

And in PHP you can do the following when initializing the database connection:

<?php
  // development machine
  if($_SERVER['APPLICATION_ENV'] == "development") {
    mysql_connect(HOST, USER, PASSWORD);
    mysql_select_db(DATABASENAME);
  }
  // add as many environments as you want
  // the ELSE part is used for production. On shared hosts you cannot edit the
  // httpd.conf file, so if nothing is set, assume it's Production ;)
  else {
    mysql_connect(PRODUCTION_HOST, PRODUCTION_USER, PRODUCTION_PASSWORD);
    mysql_select_db(PRODUCTION_DATABASENAME);
  }
?>

I have a “unit” environment as well, which I use when testing database models using phpunit tests. This makes it easier to wipe database tables when starting tests.

And I switched from black coffee to Lattes.
No idea why, but after drinking black coffee for about 10years, they suddenly seem a bit boring…

Fixing symlinks in Mac OS 10 + built in Apache

The other night I was fiddling about with my macbook, trying to setup a “MySQL-Apache-PHP” server on my system. Digging a bit about the system and googling a bit, it seems that the system comes with a built in apache server and php installation.

I thought to myself, why not use the built in server instead of installing a MAMP package?

Anyways, seconds went by and I found the option in the system preferences, turned on “web sharing” and excitedly headed on to localhost. It worked perfectly, now I just needed to set it up as it was on my linux desktop.

The apache’s httpd.conf file is located in the folder: /private/etc/apache2/httpd.conf
(I used textmate to edit the file.)

What I wanted to do was, since I’m the only user on this laptop, I wanted my Sites folder to have a www “folder”, which was a symlink to a folder in my Documents folder. The symlink would then point to the webapp I was currently coding.

This worked on my linux desktop, so it should also work here.

The first thing I did was to fix the DocumentRoot so it now points directly to my /Users/username/Sites/www folder (or actually the symlink, which we’ll create now).
The I added PHP support by removing the comments from line 116, containing:

LoadModule php5_module         libexec/apache2/libphp5.so

Then I went into my Sites folder and created a symlink to a folder in the Documents folder called “current_project” (yes yes, secret stuff here). This is done using the following command:

ln -s ~/Documents/current_project ~/Sites/www

After that, just reboot you apache server (you can use the Web Sharing option in the System Preferences. Just uncheck the web sharing checkbox and recheck it again).

An here my problems began. I got a lot of warnings in my apache error log (/private/var/log/apache2/error_log) saying:

Symbolic link not allowed or link target not accessible: /Users/username/Sites/www

After googling for a while with no answers, I thought : “why not disable the userdir module? I’m not going to use the localhost/~username anyways”

This turned out to do the trick.

The solution was simply to disable the userdir module in apache.
Comment out line 112 (LoadModule userdir_module libexec/apache2/mod_userdir.so) and line 465 (Include /private/etc/apache2/extra/httpd-userdir.conf)
Restart the apache server (web sharing thingy in the System preferences interface) and you server can now access the symlink.

I hope this will help you on your way, when coding using a Mac.

Small update: (2010-06-02 at 13:15)
Remember to right click the Documents folder and choose “Get Info”. Unfold “Sharing & Permissions” and set “Everyone” to “Read only”, else you will get a permission denied