Posted on

Mod_rewrite makes me feel dumb

The combination of regular expressions and voodoo just makes me want to HULK SMASH my desk into little itty bitty pieces.

I am reading this example, and right away I had a question, which the author doesn’t explain in well enough detail to make me understand.
http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

Why in the heck do the “/user/joe/x” and “profile/joe/x” don’t match?I wish there was a way I could run a debug on this component, and watch the pattern matching tree for myself.

Because that Nettuts tutorial has comments closed, and I am just steaming at the ears at how much inane filler there is.
I always get obsessive when it comes to knowing some little detail.

Posted on

MacPorts LAMP Stack

I stole this from another website:

Install MacPorts

After installing Xcode you can download and install MacPorts. The best way to install it is to use the DMG installer on the MacPorts site. On the other hand if you want to compile it from source then follow these steps:

Open up the Terminal

svn co http://svn.macports.org/repository/macports/trunk/base/
cd base
./configure
make
sudo make install

I recommend whichever way you install macports you should run this command to make sure everything is up to date:

sudo /opt/local/bin/port -v selfupdate

If nothing as gone wrong so far then you should have MacPorts fully setup. Only one more thing to do before installing PHP/MySQL and Apache

Update your system paths

Just so I don’t have to keep on typing /opt/local/bin all the time i’m going add that directory to my PATH. This information can be stored in your .profile which runs every time you start a new terminal. If you don’t have a .profile file use these steps to create one:

cd ~
echo 'export PATH=/opt/local/bin:/opt/local/sbin:$PATH' >> .profile

and that’s that. If you open a new terminal window (yes do that now) then those directories will be searched for terminal commands before the ones in the default path.

Setting up the LAMP stack

Enable SSL – if you want to!

Before installing PHP, i’m going to make sure that the version of curl installed is compiled with SSL enabled. If you don’t need or care about SSL then you can safely skip this step.

sudo port install curl +ssl

You should note that regardless if you run the command above first or one of the ones below then MacPorts will download a whole bunch or ports that are required to install that port or one of its dependencies. Also these commands may take sometime, so give it a while or go make a cup a tea.

Install MySQL

This one is fairly simple, well kind of…

sudo port install mysql5-server

Note: a lot of blogs recommend installing just the mysql5 port, but if you want the option to have MySQL start automatically at startup you’ll want to install the server. The mysql5-server port installs the mysql5 port anyway, so you can’t lose!

If you want to run MySQL at startup then run this command after the port finishes installing

sudo port load mysql5-server

Finally to make sure that the database if fully setup you must run this command (before starting up any mysql process!)

sudo -u _mysql mysql_install_db5

And make sure you pay attention to its instructions! Personally I didn’t do anything here as this is a development setup for me, so I prefer to keep my settings loose. Although if you want MySQL commands in your terminals path (like port) then run these commands and start a new terminal:

cd ~
echo 'export PATH=/opt/local/lib/mysql5/bin:$PATH' >> .profile

Create /opt/local/etc/mysql5/my.cnf, add the following to it and save

[mysqld_safe] socket = /tmp/mysql.sock

Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:

sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock

Install PHP & Apache

Ok we’re finally installing PHP with MySQL support and Apache (FastCGI is just being installed separately for use with some debugging and profiling tools).

sudo port install php5 +apache2 +pear +fastcgi php5-mysql +mysqlnd

Like MySQL if you want Apache to start automatically when you start your computer then run this command after the port installs (as shown while the ports are installing)

sudo port load apache2

Configure PHP

To customize php, copy /opt/local/etc/php5/php.ini-development (if this is a development server) or /opt/local/etc/php5/php.ini-production (if this is a production server) to /opt/local/etc/php5/php.ini and then make changes (see below).

i.e. for development:

cd /opt/local/etc/php5/
sudo cp php.ini-development php.ini

i.e. for production:

cd /opt/local/etc/php5/
sudo cp php.ini-production php.ini

To customize php edit (using your favourite editor) /opt/local/etc/php5/php.ini to configure it properly, make sure you set the correct timezone (for myself it’s this):

date.timezone = Europe/London

Configure Apache

If this is your first install, you need to activate PHP in your web server.To enable PHP in Apache, run

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

Open /opt/local/apache2/conf/httpd.conf in your favourite text editor (it’s vi for me, but feel free to use pico, emacs or whatever!).

sudo vi /opt/local/apache2/conf/httpd.conf

If you want to change the default MacPorts Apache document root to match default Apple’s Apache (personally I didn’t, but I found this information on another blog – so it might be useful to some), change:

DocumentRoot "/opt/local/apache2/htdocs"

to:

DocumentRoot "/Library/WebServer/Documents"

If you changed the DocumentRoot, change the Directory directive from:

<Directory "/opt/local/apache2/htdocs">

to

<Directory "/Library/WebServer/Documents">

For PHP to work correctly you must add index.php to the dir_module directive:

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the <IfModule mime_module> block. Without this, all you’ll see is the text of your PHP scripts

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

And finally, to enable user directories, uncomment:

Include conf/extra/httpd-userdir.conf

But if you are going to use virtual hosts (as described below) then uncomment this line in addition to (or instead of) the line above:

Include conf/extra/httpd-vhosts.conf

Save and close the httpd.conf file.

Advanced Post-Configuration

Configure Apache Virtual Hosts

Now unless you just want to access your new web server using http://localhost you might want to set up some virtual hosts for Apache. I personally only need one virtual host atm i.e. http://testing.example.com. Virtual hosts need to be configured in two places, the hosts file (/etc/hosts) and the Apache virtual hosts file (/opt/local/apache2/conf/extra/httpd-vhosts.conf).

Firstly add this line to /etc/hosts after the localhost has been defined i.e.

127.0.0.1    localhost
127.0.0.1    testing.example.com

Then you must edit the virtual hosts file /opt/local/apache2/conf/extra/httpd-vhosts.conf and add this configuration

<VirtualHost *:80>
    ServerAdmin webmaster@testing.example.com
    DocumentRoot "/opt/local/apache2/vhosts/testing.example.com"
    ServerName testing.example.com
    ServerAlias www.testing.example.com
    ErrorLog "logs/testing.example.com-error_log"
    CustomLog "logs/testing.example.com-access_log" common
</VirtualHost>

The document root can be anything you want as long as apache has permissions to read that directory i.e. in this case you can:

sudo mkdir -p /opt/local/apache2/vhosts/testing.example.com
cd /opt/local/apache2/vhosts/
sudo chmod -R 755 testing.example.com

You can do this process for as many virtual hosts as you want. Also I personally keep all my websites stored in my user directory – yes bad on a server, but this is my personal development machine. Finally don’t forget to comment out or remove the example vhosts from http-vhost.conf or you’ll get warnings while starting up Apache, they are harmless to leave in, but it’s probably for the best to remove any potential errors

Installing extra PHP modules

There are plenty of extra PHP modules available from MacPorts, you can get a list of them by using this command. Although you won’t need any of the MySQL ones as you’ve already compiled it into PHP.

port search php5-

Here is the list of PHP5 ports I installed

sudo port install php5-openssl php5-curl php5-gd php5-iconv php5-http php5-mcrypt php5-xdebug

Updating MacPorts

MacPorts is simple to update, every few weeks I just run the command below to check for any outdated ports (i’ll also update this post with any changes to this process I discover)

sudo port outdated

to update all installed ports to the latests and greatest

sudo port upgrade outdated

That’s It

Now if you restart your computer and add code into the document root of your website (in this case http://testing.example.com) then you should have a fully working web server with PHP and MySQL. Ok it wasn’t easy, but for me it’s the best solution over the longer term

Posted on

iTerm2 keyboard shortcut for end of line and beginning of line

So its not that easy to switch to a brand new computing platform and interface at age 31.
Previously I used my Microsoft Elite keyboard as a shield between me and the MacBook Pro.
Today I Learned: The function key and arrows serve to replace the HOME, and END keys. 🙂

Also I was able to configure my iTerm2 to easily get me to the beginning and end of line.

I’m still new at this, and mapping Alt+Left Arrow and Alt+Right Arrow didn’t seem to work at first.
So I mapped Ctrl A and Ctrl Z to go the beginning and end of line.
Simply open the preferences, hit the key tab, and the plus sign to add your own mapping.
Then hit the keys you want mapped, and from the drop down select “Send Escape Sequence”
[H gets you the beginning, and [F gets you to the end 🙂

Screen Shot attached.

iTerm2 Key Preferences Image
Manipulate the Terminal’s keyboard mapping.

Posted on

CanJS is really the new hot thing in JavaScript

So its official.
I was at a Hare Krishna Temple in Silicon Valley. And while I was relaxing a bit, I overheard another pair of engineerings talking.
I couldn’t help but eavesdrop.

To my surprise, they were talking about CanJS. One of the engineers was RAVING about it to the other one. Finally I had to inject myself into the conversation and inquire as to where they worked.

And again to my surprise, they didn’t work at a large company, but a startup.
Bitovi is a name I am hearing more and more while I am up here in Silicon Valley!

Why? The simple answer, it makes things easier. You just have to write way less code. About 20% less.
It also organizes your code into better more logical, and therefore readable, structure.

So yes, thank you Brian Moschel, for JavaScriptMVC and for CanJS.

They are the IT thing in I.T. this year of our lord, 2013.

Posted on

PhantomJS + Jasmine vs Selenium Web Driver

Recently I started using phantomjs, which is a headless browser based on web-kit, for automated JavaScript testing.

Now when I was playing around with Selenium @ ABC Family, I really liked how the web driver started a browser instance and executed the test suite within it. This means Selenium is actually a better, or closer match, in terms of automated testing, because the browser is not headless. Although I don’t know all the internals of Selenium, that was my first impression.

But the positive thing about using the grunt, jasmine, phatomjs combo to run unit tests, is that we can start a jasmine server, which lets you check your code in many other browsers. That means you are not limited by the Selenium browser library of supported browsers. You can actually pick up your phone, or tablet, and point the browser to the test server, and see how your code executes on that particular system ( Device, OS, Browser). True this is not something that can be used with 100% automation on its own, but it does give you the freedom to experiment and see the behavior of code in a large variant of systems. This means that with services like DeviceAnywhere, you maybe able to cover and automate the testing of all kinds of strange fringe devices.

Something else that is interesting is that in Selenium, you can’t really hook, or spyOn member methods. While a lot of the tests written in jasmine, can be executed similarly with Selenium, because they just check for a class that has been added or removed from a DOM element, jasmine provides more integration with the code base.

The classes are loaded, along with a html fixture, and then executed. This is how the server works, by creating a #sandbox div where it loads the html fixtures for each test, loading the javascript into the page, instantiating the class, and then begins execution of the test suite. Now the opposite argument is, again, this is not how the site would be like in the wild. Other components would live on the page. So Selenium gives a more accurate assessment of how the code actually works on the end user’s system, since it loads the entire site, via a client side browser.

Now as a Computer Scientist, Java vs JavaScript argument is mute to me when it comes to choosing a “platform”. Because ultimately its like comparing apples to oranges, when you really look at the language structure and what they are designed to accomplish. Two different tools for different jobs. As a front end developer, who wants everything to be easy, there is definitely a benefit to having a unified language for creating build tools, server side applications, and user interfaces. So at some shops, where ROI is important, it’s a good idea to keep the tools all in the skill set of the human resources currently on staff. For UI people, this means JavaScript > Java. This is a good argument for using tools like grunt, and phantomjs, and jasmine, since they are all JavaScript based, they empower the new kingmakers (Open Source Developers).

Which is actually still not a big argument against Selenium Web Driver, because Java is very easy to install, you are not going to be making many changes to the driver itself, and the interface for running the Selenium Web Driver could potentially still be written in JavaScript.

Therefore the argument could be made that Selenium and Jasmine don’t have to be mutually exclusive, while it would bloat the build time to include both systems, a separate box, and process, could be used, to test with both, or the one that is missing in the build process.

While its too soon for me to say, “Dude, Selenium is old news.” I can say that all this merits more experimentation and testing. A very exciting time for Computer Scientists indeed. So much Brain Candy!

Posted on

Real Web Developers don’t do “Builds”

I don’t want to wait for some MAVEN command to execute.
I just want to refresh the browser!

So this is where Charles Proxy comes to the rescue!
The “Map to Local” feature allows you to quickly map your resources to a live implementation on a production, or development environment.

Really great feature, that saves a lot of time. Even the uploading to a generic hosting step can be skipped when doing changes to static resources. So this tool is good for Enterprises and SMBs.

Usually I would completely avoid extraneous non open source solutions. The KISS principle is something I apply not just to my coding, but to my workflow as well. Unfortunately, I don’t always get to decide what platform and workflow structure I have to interact with. And this where tools like Fiddler and Charles become indispensable in preserving my sanity.

While on the Mac environment, I am currently using Charles Proxy, Fiddler2 also provides this feature, hidden in the AutoResponder tab. Simply activate “Map URLs to local files”. And Fiddler should be able to run on Linux / Mac, although I haven’t tried it yet.

Check it out http://www.fiddler2.com/fiddler/help/video/default.asp

Posted on

Mobile Web

Today I had the pleasure of meeting Maximiliano Firtman. An amazing speaker who kept my attention the entire time.
He really validated my perspective on mobile web. That even though it’s a complete fuster cluck when it comes to devices, screens, features, and may be an exercise in complete futility because as soon as your done coding everything is going to change anyway, you still can’t idly sit back and do nothing.

Though many of my co-workers were joking that his talk made them depressed about the current state of the mobile web, I really found it enjoyable, because he echoed back many of my own viewpoints regarding Responsive Web Design, system architecture, and even how job duties or roles should be defined. And since great minds think alike, I recommend you check out his books. They will reveal lots of great resources. And illuminate niches in capturing users, and getting a better ROI, when it comes to creating a mobile version of your website.

Spoiler alert, there is no easy way to go about creating a mobile version of your site. And as craftsmen, we have to first painstakingly measure, and remeasure, before we put our tools to work. Keep in mind, this gentleman has been doing web programming since 1995, and has been a subject matter expert in mobile web design since 2000, during the days of WML, and WAP.

Posted on

Started a Happiness Journal

I had to go ahead and start a happiness journal.
It seems that our brains have the ability to be wired, or predisposed, to judge events in a positive, nuetral, or negative context.
Basically we can scan our day to day interactions with the world, seeking one of those three modalities to dominate.
The author of “The Happiness Advantage” claims that we can rewire our brain into a positive mode, and increase our performance by almost a third, by keeping a happiness journal.

Seems like a fun idea.

Posted on

CanJS, Mustache, Grunt, Jasmine: its a lot to take in.

Out of the frying pan and into the fire.

I have been tasked with doing Behavior Driven Development.
Which means I have to pick up Mustache, CanJS, Jasmine super fast.
Its not easy by any means.
The meatier functions I want to create coverage for, are firing errors.
I believe I am missing some can.view() calls, or other Mustache related data bindings.
The super easy function that I created coverage for, still shows as not being covered in the report.
So double wammy there.

When I worked at ABC Family I did setup PHPUnit for CodeIgniter, via CIUnit.
But then I took my R&D and handed it off to the unit testers, to actually generate the coverage report.
So my experience, for even backend unit testing, is very limited.

Considering just how rushed that project was, I am not surprised that I was quickly assigned to work on front / back end development, and off of unit test coverage.

Posted on

Reality is a Computer Simulation

Regardless if the external universe around is strictly materialistic, or not, what we perceive to be real is a complete illusion.

This is a fact, because neuroscience has uncovered the reality of how our brain functions. Even what the eyes perceive are a snapshot of the past, because light needs to travel to the cones, and the brain needs to interpret the electrochemical input.
And this process of interpretation is the subject of many movies such as the Matrix.

“The Father and I are one” Yeheshua proclaimed. But what is I, and what is The Father? More constructs of the mind? For everything exists within this mindspace, just like everything in existence must exist inside the confines of existence itself.
Or as Morgan Freeman has said it on the Colbert Report, “If nothing exists outside the universe, then what is the universe expanding into?”

In truth, we are all inside a simulation, hologram or not, the universe can be easily seen as a giant process of information exchange, a computation. And “we” are a simulation as well, just our senses are interpreted into a illusionary concept to allow us to function within the world, so is the concept of Self, nothing more than an illusion.

And we create our simulations.  And as we evolve, soon we will give birth to entire universes within ourselves!
This Iboga induced vision, is a beautiful explanation of the concept of infinity.

And this talk on elightenment and the self will also help unravel the mystery.

Oṃ bhūr bhuvaḥ svaḥ
tát savitúr váreṇ(i)yaṃ
bhárgo devásya dhīmahi
dhíyo yó naḥ pracodáyāt

 

If the ego is mere illusion, then what is true self awareness? In the movie Pi, Max Cohen finds the 216 digit number, the name of God, that allowed Euclid, his computer, to gain consciousnesses. What will happen to the hardware of our system, once we begin to process the ultimate computation? Once we are no longer wasting cycles running the ego?