2011-12-25

PHP Fog Quickstart

For a while, I've been trying to find a good hosting service to use for testing out PHP apps and ideas I have. My criteria was to find a service that would be like Heroku for PHP apps: simple to get started, pre-configured with access to a database, scalable on demand, and no-hassle deployment (able to integrate with continuous deployment.)

Funnily enough, if you do a Google search for "heroku php" the first thing that comes up is PHP Fog. They provide hosting for up to 3 projects for free on shared hosting. They also offer MongoDB and application monitoring as add-ons.

Being it was Christmas and all, I decided to give myself a present and sign up. I was very surprised by how easy it was to get up and running. I managed to build a simple "echo" service in about 8 minutes, following roughly these steps:

After logging in, click "Launch New App". This brings you to a screen where you can choose from several pre-built applications (popular CMS or blogging platforms) or skeleton projects built on the most popular PHP frameworks.

For my purposes, I chose "Custom App". This will build an application containing exactly one "Hello world" PHP file. Enter a password for the MySQL database that will be set up, and a subdomain. The subdomain must be unique among all PHP Fog apps, and the setup won't continue until it is. After your app is set up, it can be given a custom domain name from $5 per month.

Click "Create App". This will bring you to your project's management page. From here, you can find everything you need to start developing your app. The git repository address for the app is displayed, as are the database credentials, helpfully wrapped in a mysql_connect function call.

Eventually, the "Status" light will turn green, indicating that your app is available. Clicking "View Live Site" brings you to your application, live to the world. Not much there. Time to change that.

Follow the instructions to add an SSH key to PHP Fog, which will give you access to write to your application's git repo. Then use the given git address to clone the repo to your development environment. Also, grab the Silex microframework to start building the app:
> git clone git@git01.phpfog.com:myapp.phpfogapp
> cd myapp.phpfogapp
> wget http://silex.sensiolabs.org/get/silex.phar
Edit index.php to provide a simple endpoint which will greet users:
<?php
require_once __DIR__.'/silex.phar';

$app = new Silex\Application();

$app->get('/', function() use($app) {
    return 'Checkout <a href="/hello/world">Hello...</a>';
});

$app->get('/hello/{name}', function($name) use($app) {
    return 'Hello '.$app->escape($name);
});

$app->run();
Also, create an .htaccess file that will redirect all traffic to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Add the files to repository, commit them, then push the master branch to make the changes live:
> git add index.php .htaccess silex.phar
> git commit
> git push origin master
Reload your site to see the changes. Follow the "Hello" link, and play with the URL to see the results.

After the holidays, I hope to actually build something interesting on the platform.

6 comments:

  1. Hi, this is Lucas, founder and CEO of PHP Fog.

    Happy holidays and great post, loved seeing your reaction and looking forward to what you build next. Let me know if you have any more questions or comments!

    ReplyDelete
  2. Hey Lucas,
    Seems pretty slick, so far. I already have a couple ideas for some REST APIs I've been building in my head. Also, I was pondering whether I really wanted to purchase a custom domain just to have SSL. On a lark, I tried https://myapp.phpfogapp.com

    It just worked.  Little touches like that are nice.  Thanks!

    ReplyDelete
  3. hi, I try to go through the sample and deploy to my phpfog project.
    However, it always render some error:
    Fatal error: require(): Failed opening required 'phar:///var/fog/apps/app30490/onepagenews.phpfogapp.com/silex.phar/src/Silex/Application.php' (include_path='.:/usr/share/php:/usr/share/php/libzend-framework-php') in phar:///var/fog/apps/app30490/onepagenews.phpfogapp.com/silex.phar/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php on line 229 

    any advice?
    thanks

    ReplyDelete
  4. Same here, something changed recently?

    ReplyDelete
  5. I just tried it with the most recent version of Silex and it worked for me. It seems like the autoloader built into thePHAR is not working. If you try and run the example on your local system, does it work? Can you run just a simple script that requires the PHAR and creates a new Silex\Application?

    ReplyDelete
  6. Yap, it works fine in my local system but fetal error on phpfog

    Fatal error: require(): Failed opening required 'phar:///var/fog/apps/app30490/onepagenews.phpfogapp.com/silex.phar/vendor/.composer/ClassLoader.php' (include_path='.:/usr/share/php:/usr/share/php/libzend-framework-php') in phar:///var/fog/apps/app30490/onepagenews.phpfogapp.com/silex.phar/vendor/.composer/autoload.php on line 5 

    ReplyDelete