Monday, March 19, 2012

Installing SVN on Shared Hosting with PHP exec()

Using PHP's wonderful exec() I have been able to compile, link and install svn on my shared hosting (not VPS). I won't say which hosting provider I use in case they find out and disable svn on me. exec() allowed me to get around the fact that I didn't have ssh access. So how do you do it?

Created a method to wrap exec so I could see the output in a nicely formatted way (useful for debugging):

function run($cmd) {
$output;


echo "running ". $cmd . "</br>";
exec($cmd.' 2>&1', &$output);
echo "<pre><code>";
print_r( $output );
echo "</code></pre";
}


Then I called this method with commands I wanted to execute as if I was logged into my hosting via ssh. e.g. calling run('ls') will run "ls" on the server and echo the output as a HTML response.


I following the steps from a helpful post (see credit below) to do the actual install. Firstly, you need to download the subversion source and dependencies:
run('wget http://subversion.tigris.org/downloads/subversion-1.4.6.tar.gz');
run('wget http://subversion.tigris.org/downloads/subversion-deps-1.4.6.tar.gz');


Using the server to download the tar files is usually much faster than uploading the files yourself. Then you need to extract the files:
run('tar -xzvf subversion-1.4.6.tar.gz');
run('tar -xzvf subversion-deps-1.4.6.tar.gz');

Next you'll need to run the configure script which came with the subversion source (see following code snippet). I replaced $HOME with a directory I had access to. The prefix is effectively the install location for svn. If you leave the $HOME variable in the command you will be installing it to the directory the $HOME variable contains. If it is empty you'll be trying to install to the root of the system which won't work because you won't have permissions.
run('./subversion-1.4.6/configure --prefix=$HOME --without-berkeley-db \ --with-ssl --with-editor=/usr/bin/vim \ --without-apxs --without-apache');

Finally run make and make install:
run('make')
run('make install')

The make commands will compile, link and install svn to the prefix directory. The svn binary is stored under the prefix/bin. So to run a svn command you can do:
run('./$HOST/bin/svn <args>')

Credit: http://joemaller.com/881/how-to-install-subversion-on-a-shared-host/