Monday 20 February 2012

Setting up Git to manage a website project

In the server:

This is where your code lives, in your DocumentRoot:
$ cd /var/www
$ mkdir mysite

This is the remote repository:
$ cd /home/user
$ mkdir mysite.git && mysite.git
$ git init --bare
Initialized empty Git repository in /home/user/mysite.git/
$ vi mysite.git/hooks/post-receive
~
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
~
~
~

And in the local machines:

$ cd /var/www
$ mkdir mysite && cd mysite
$ git init
Initialized empty Git repository in /var/www/mysite/.git/

$ git remote add web ssh://<user>@<server_url>/home/user/mysite.git
$ git push web +master:refs/heads/master

$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -m "first commit"
$ git push web

Then:
Another user could clone the remote repository:
git clone ssh://<user>@<server_url>/home/user/mysite.git


Monday 13 February 2012

Accessing MySQL from PHP error

I was getting this weird error on Yii:
CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 111

If you are accessing MySQL from PHP and both reside on the same server you must use localhost as the server name on the MySQL configuration file. Like this one I had on my Yii application.

On my Yii config/main.php I changed this:
'connectionString' => 'mysql:host=<host_url>;dbname=<db_name>',

For this:
'connectionString' => 'mysql:host=localhost;dbname=<db_name>',

This post saved my life: MySQL Forums