Serve more than one site using virtualhosts on mac OSX

There would be a need for this or that reason to serve more than one site from apache. And this is quite possible using configuration inside the apache

First thing first
go to the apache config, by default it should be

/etch/apache2/apache.conf

And search for

#Include /private/etc/apache2/extra/httpd-vhosts.conf

Which, as you can see, is commented. Hence uncomment that by deleting the # infront of it.

The above task will allow us to mess around the httpd-vhosts.conf file that is in the extras directory.

Lets assume we have a site names siteVH which is residing in the directory /www/sitevh
and lets assume we have another site called siteAnother residing in director /www/siteanother

Now the goal is to access those sites independently. So we will create two different virtual hosts for each one and be able to access them independently


    ServerAdmin youreamil@domain.com
    DocumentRoot "/www/siteVH"
    ServerName sitevh.com
    ServerAlias local.site
    ErrorLog "/private/var/log/apache2/sitevh.log"
    CustomLog "/private/var/log/apache2/sitevh.com-access_log" common
    
        AllowOverride All
        Order allow,deny
        Allow from all
        Options Indexes FollowSymLinks
    

And the we will have the same duplicate values for the other one as well.

Then update the /etc/hosts to let know our local “DNS” :) to know what to do

127.0.0.1 sitevh.com siteanother.com

This shall take care of the whole thing.
One more thing, restart apache

sudo apachectl restart

PDO not throwing any exceptions

If you want pdo to thow exception so that you can see what is going on kinda thing.. you can add the following parameter when you instantiate PDO just next to the password part of the pdo

array(PDO::ATTR_EMULATE_PREPARES => false, 
                                                                                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)

**Note: This is not the best way to handle exception by any means!!

Removing a decorator from Zend Form

Zend will ship its elements with tags of dd dt
But if you want to get only the element with out decorator being around it.
you can do:

        $this->addElement('text', 'you_element', array(
                ....
                'decorators' => array('viewHelper'),
                ....
        ));

But if you want to display errors along with the HTML format,
add errors in the decorators array

This should give you only the element that you can implement your own decorator around it

Debugging PHP app using xDebug and Eclipse Tutorial

This will be a tutorial on how to debug a PHP application using xDebug and eclipse.

1. Get the eclipse for php from Eclipse for php
A side note, if you are on any *nix OS, this might require you to update your java to 1.7 and above. If that is the case, download latest Java JDK from Oracle (1.8 at the time of writing) and do:

which java

in most cases, this will show /usr/bin/java which is a symlink to the actual java binary.
Follow the symlink using

ls -la 

That depending on the destro you have, would point to the link destination, there you can point to the java symlink to

ln -s /path/to/the/downloaded/jdk/bin/java

That should take care of the problem.
On windows, you need to updateh environment variable that you set for the JAVA_HOME. Googling on this shall give you the right direction.

2. Get xDebug on your system
Well, before this you might need the whole stack of apache, or any other php-approved webserver, and php itself. For that if you are on *nix you can use

sudo apt-get install lamp-server^

On windows, you can use wamp server and be done with it.

To install xdebug on *nix

sudo apt-get install php5-xdebug

Once you do that, restart your apache server

service apache2 restart

and do phpinfo(); you should be able to see the xdebug info there.

Now, pull your php.ini and add the following to it

[xdebug]
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

You can alternately add this info in the xdebug.ini if it it exists in the additional ini folder. This information would be available on the phpinfo();

Again restart your apache web server.

For testing purposes I have created a simple yet good php example on https://github.com/gullele/simple-php-calculator-example

Go to your eclipse Run>Run Configurations..
and do the following on respective tabs
For server tab:

server
For Debugger tab.

debugger

Now your eclipse is ready for debugging!
You can put a break point by double clicking on the area you would like the debugger to stop.
As you can see, I was able to see the value assigned on the $config variable that is was read from config.ini file.

debugprogress

Seasoned PHP programmer toolkits

PHP has grown a lot in the past decades and is becoming one of the emerging programming languages. There are still problems with it of course, phpsadness but I would say some of those problems can be easily avoided using a better programming standards.
I have compiled some basic tools/methodologies that we should at least know them to be a good PHP-abiding citizen. Some of the points are applicable to being a good programmer in general.

Version Control Systems
It is almost a must to know one basic versioning system. I would say it is good to have a knowledge of distributed versioning system. Here is a discussion regarding the central versus distributed versioning systems Difference between version systems
From the DVCS I have an experience on mercurial and git and I would say git is better.
Why using VCS? They become handy wheather we are working in team or solo programmer. Some examples:
-when the release becomes really creazy and we want to go back to the earlier solid version(checking out older revisions)
-To save the current working progress and be able to look on work another revision (stashing)
-To be able to search revisions based on comments of the commits we have
The above are some of the most commonest things we use day today without mentioning being able to work from different computers and other minor routines.

Dependency Manager
Composer has become a common inclusion on most of the frameworks for managing the depencies on the progect.
You know when you are working on a project it would be dependent on a lot of libraries. Some of them from third parties, some from github, some from PEAR… well how are we going to manage all these dependencies with out a headache? Composer!
Using composer.json file, you will be able to list all the dependencies you need for the project and it will cache it locally. So next time when you require the same dependencies it will be really fast to load it since it is loading from local computer not from remore githut repo or something.
What is the use apart from this? We will have a light weight application composed of the main business logic. All the third party stuffs will be loaded whenever they are needed using composer install command.
Other advantage is it will lock the dependencies using composer.lock file. By doing that we are assured that everytime when we are installing, it will using the lock file to fetch the dependencies.
You can get a lot of goodies that are prepared for composer from https://packagist.org/

Coding Standard
It is a vital part in coding in general – to have some coding standard. These days, lots of projects and even composer might require you to follow basic coding standards and for php, the defacto is ps-r one.
http://www.php-fig.org/psr/psr-0/
http://www.php-fig.org/psr/psr-1/
http://www.php-fig.org/psr/psr-2/
http://www.php-fig.org/psr/psr-3/
http://www.php-fig.org/psr/psr-4/

Caching
Caching is almost a must on most of high traffic sites. The concept is simple: it is faster to read the data from memory than from database/file. We do encounter a lot of data that do not change often like configs and hence it would make sense to put them on memory than reading them from database.
For this we can use Memcache/d http://memcached.org/
and Redis http://redis.io/
Both have driver for php and are widely used.
They allow object persistence through serialization so all the cost of reading from the database and constructing the object could be saved by tossing end-product object into the memory and use it.
*If you are working on Doctrine, it might not work well with those outside of its own caching mechanism. But that wont be the problem – just use its own!

Vagrant
I would say it is one of the best development tools. It is based on the concept of virtual machine. Can you imagine the problem you would face to setup a new machine for development, installing drivers for some of the packages, installing the language dependencies… with vagrant you would do all this only once. And you can use that same configuration again and gain. All you need to keep is some simple files containing your configuration.
Learn more on http://www.vagrantup.com/

IDE
This is totally personal. Some of us would be happy and productive by coding from vi or notepad others from eclipse.. the list can go on and on.
I have been on Eclipse, netbeans, vi, sublime..
By far I would say phpstorm is the best. http://www.jetbrains.com/phpstorm/ It would come with a month of free trial which will give you the chance to play with it.

No SQL Databases
We are all familiar with relational databases like mysql. With no-sql ones, the story is totally different. The are document based storage systems and they scale better horizontally.
To see it with and exmple, in a typical employee, department, company situation, we need three tables, assuming they are normilized in all forms, so to get the information of employeeA, there would be two joins involved. But in a no-sql database the data might look like, not exacly but, array (’employeeAId’=>array(‘company’=>companyInfoGoesHere, ‘department’=>DepartmentInfoGoesHere’));
You can see that selecting the database with single employeeId would give us an access to all the related data. Hence it scales horizontall.
As you can see, it might not be a solution for all cases. But it will definitely boost our performance with our old school relational database.
Take a look at http://www.mongodb.org/

I will write on
Debugging
Testing
Reviews
Workflows among others on my next blog.

Installing php-mysql-driver using mysqlnative driver

PHP has been providing us starting form 5.3 the mysqlnd [mysql native driver] that will liberate us from using the mysql client library. And it will be shipped along with the php
But if we remove the mysqllib for some reason and wanted to load it again..
This will be for linux fedora – I have done this on Amazon EC2 server

sudo yum shell
remove php-mysql
install php-mysqlnd
run
quit

you will see the new mysql ini files being added in /etc/php.d folder
Then restart httpd

sudo service restart httpd

undefined symbol: php_json_encode in Unknown on line 0

Am fighting to install my application on Amazon EC2.. I installed the mongo and php mongo driver then restarted apache to get the above error message.
The problem is loading precedence. Mongo, which is dependent on json.so, is being loaded ahead of the json.so hence crying over ‘Where Is My JSON.so ehiehiehi’
The fix would be to make sure we are loading json first before the mongo – actually this would be hte problem on while loading memcache as well..
On php.ini –
Right above the extension=mongo.so, add extension=json.so
Usually json.so will be loaded through its own ini file json.ini
On Redhat Linux it would be inside

/etc/php.d/

So, go to the json.ini and comment the extension=json.so line
restart httpd/apache and
DOne!

setting XDEBUG variable through cookies

We know how important xdebug in php is .. no post on that :)
when it is through the url, we will use the

XDEBUG_SESSION_START=somevariable

But, when you are using curl or some other client for using a post, cookie is a rescue

CURL_COOKIE = 'XDEBUG_SESSION=somevariable'

Just add the above as one of the curl options and you should be good.

react/zmq installation error on PHP 5.4 with composer

Last week I upgraded my PHP to 5.4 on my mac. It was not that bad, but I am starting to pay little by little.
I am using Ratchet for a websocket and for pushing I was using 0MQ.
My composer.json looks

....
    "require": 
        "cboden/Ratchet": "0.3.*",
		"react/zmq": "dev-master"
    }
...

Then I do composer install, it was complaining zmq is not found in the system.
I had zmq already but it is on the php5.3 default directory. And I can’t use the zmq.so file I compiled b/c i am using 5.4 now.
Here is the fix:
Go and grab the git clone from github

git clone git://github.com/mkoppanen/php-zmq.git

for further ways checkout this
Then

cd php-zmq

Then do the following

phpize

Make sure you run the correct phpize in this case. If you are upgrading from 5.3 and still you have the old version sitting around on your machine, phphize will take the older one by default.
My new php 5.4’s one is in /opt/local/bin/phpize54. So make sure you are running the phpize with the new one.
One easy thing to do would be to symlink the old phpize with the new one like:

sudo ln -s new/phpize/path old/phpize/location/phpize

Backing to the topic,
Then do the configure, being inside the php-zmq folder, as follows

./configure --with-php-config=/opt/local/bin/php-config54

make sure to replace the value of –with-php-config with your proper php-config path

if you don’t have the packageconfig, you will have an error requesting for that and you can install it from macport – if you are using mac as follows:

sudo port install pkgconfig

Just search for pkgconfig for others distros you have and it should not be that difficult ..
once you do that install it by

make && make install

Hollla..
the compiled file should be inside the modules directory.
After this, copy that file into the php executables folder and add extention=zmq.so into your php.ini

Now you can do the composer install and it should install it..