Monday, May 17, 2010

Local development with GIT and Plesk

I'm loving GIT. But, what I really wanted to do was to create a development environment locally that pushed changes to a server which was therefore always up to date.

So, I've got an Ubuntu (lucid) machine locally and an Ubuntu server running Plesk 9.x remotely.

Make sure that git is installed on both systems
sudo apt-get install git-core
Make sure that you setup Plesk to give the user shell access (bin/bash) in Web Hosting Settings.

Follow this helpful todo.

Easy! When you are working remotely to setup the git repro, you will need to be root - so remember to change the httpdocs and httpdocs.git directory permissions back to "username:psaserv" and do it recursively:
chown -R username:psaserv httpdocs
chown -R username:psaserv httpdocs.git
Passwordless login and git clone

It's worth while setting up passwordless login:
ssh-copy-id -i .ssh/id_rsa.pub username@domainname.tld
Then, the command to get the git repro locally becomes:
git clone ssh://username@domainname.tld/var/www/vhosts/domainname.tld/httpdocs.git
Setup apache

You'll need to make sure the apache2, php5 are installed:
sudo apt-get install apache2 php5
You need to setup apache by editing the conf file: /etc/apache2/sites-available/default. If you are going to run multiple local sites, you'll have to declare them here. For each site, add the following:
<VirtualHost *:80>
ServerName localservername
DocumentRoot /home/sites/location/httpdocs/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/sites/location/httpdocs/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
You also need to edit /etc/hosts to provide a route to your local server. Add a line:
127.0.0.1 localserver

Setup database forwarding

We could just setup mysql or other database locally. However, then you need to try and keep local and remote databases synced. If you are just reading, it's much simpler to setup an ssh tunnel to your remote server.

While the ssh for the git clone was as a plesk user of the particular website, this ssh should be performed as a user of the server i.e. it should log you in a you, not as a website. You need to make sure that mysql is NOT running locally:
sudo stop mysql
And then, type
ssh -L3306:localhost:3306 remoteserver.tld
For ease, if you save this command to a file and put it in /usr/local/bin and then "chmod +x" it, you can simply type the name of the file (e.g. connect) and the tunnel will be set up.

Connecting to the tunnelled mysql db via PHP

The other piece of glue is to realise that you can't connect to the database as localhost, you have to use 127.0.0.1, e.g.:
mysql_connect('127.0.0.1', $username, $password);
All in all, a pretty successful development environment.


No comments: