How to Deploy Your First App to PHP Cloud

There are an increasing number of Platform as a Service options available for developers, so it’s no surprise that Zend have joined the party. PHP Cloud is a public preview at the moment, but it already offers key features such as: MySQL access, source code management via Git, deployment to other cloud services, and SFTP access.

In this article, I’ll show you how to set up a CakePHP installation in an application container within PHP Cloud. Along the way, you’ll see how to access MySQL, deploy code, and use SFTP from your FTP client to access the application container.

First, get a zend.com account and a phpcloud.com invite

You will need to create a zend.com account (it’s free) if you don’t have one already. Then you will need to request an invite. Usually, Zend will send you an invite within 24 hours.

Access Keys

Once you have received your invite and created an account, the first thing to do is set up the access keys (from the menu on the left after you have logged in). If you have an RSA pair already, you can upload your existing public key. If not, or if you want to create one just for PHP Cloud, you can use the option to generate a new key pair:

generate a key pair

You can then download the .pem file (the private key) and store it safely on your machine.

Create a container

To create a container, click on my containers from the menu on the left after you have logged in. You will be presented with a form asking for basic information about the container you want to set up:

creating a container

Notice that you can also set up the container to be able to send emails too. You will then be asked to wait for a moment while your container is provisioned:

provisioning

That’s all there is to creating a container.

A word about SSH/Shell access

There is no provision for shell access at the time of writing. There is some limited SSH access. You can access your MySQL instance, or Zend Studio for remote debugging. There does seem to be a problem with it though, I ran into a lot of problems connecting over SSH, mostly to do with the:

Connection timed out during banner exchange

Bear in mind that this is a preview build at the moment, so there are bound to be teething problems. We can work around that issue for now.

Database Access

The other way we can get access is via the phpMyAdmin installation that has been made available. From the application container, click on management -> database management:

database management

That will provide a link to phpMyAdmin:

database management

Now you can work as normal with MySQL, importing data, creating tables and so on. To be able to connect to your database via your code, you will need the following:

  • Database host: mycontainer-db.my.phpcloud.com
  • Database port: 3306
  • Database schema name: mycontainer
  • Database user: mycontainer
  • Database password: your container password

Git Access

You can work with your source directly via Git:
Git
The button will provide a url to the repository that you can clone:

cd ~/sites
git clone https://mycontainer@mycontainer.my.phpcloud.com/git/container-root.git myproject

Obviously you will need to change your directory to where you want to store your code, and also add the container name that you have used in place of mycontainer.

SFTP Access

If you use an FTP client that supports STFP, then you can access your container that way too. You will need the following information:

  • User name: mycontainer
  • Host name: mycontainer.my.phpcloud.com
  • Port: 22 (this is the default SFTP port and usually there is no need to specify this)
  • RSA Private Key: the path to your private key file

The private key path should point to the .pem file you created earlier, or your exisitng private key.

Once you have connected, you will be presented with a file structure like this:

SFTP

Your code is intended to go in the public folder. You will see that I have added the files and folders that belong to a CakePHP installation.

You can work via Git, or you can work by uploading via SFTP, the choice is yours.

Trying it out – a simple CakePHP Application

We’ll install CakePHP, and build a simple blog application, just so that we can prove things work on the Zend Cloud.

The normal installation of CakePHP requires that you change permissions on certain folders and files. Since PHPCloud has no shell access, you will have to access the installation via SFTP.

Download a fresh copy of the stable version of CakePHP unzip it, and then upoad it to your container using the SFTP method described above. Ensure that you upload all files; CakePHP has some hidden files that you will need to be sure to upload too.

1. Change permissions – app/tmp

Next, change the permissions on the app/tmp folder to 777 (read and write) using your preferred (S)FTP client. Cake uses this folder to store session data, logs, cache, and test results.

2. Update app/config/core.php

Scroll down to around line 182, and change the security salt to one of your own. Also, change the cipherSeed value to a random numeric string.

3. Add your database settings

The database settings that you used for phpMyAdmin, should be added to app/config/database.php (you’ll need to re-name the file from database.php.default).

4. Test your site

Access your site from the by clicking the link in your application container:

CakePHP installed

And with any luck, you will see something like the screenshot above.

4. Add a table to the database

Log into your phpMyAdmin instance in the container, click on the database name on the left of the window, and then copy and paste the following SQL code into the text-box in the SQL tab. Click the “Go” button, and your table will be created:

/* First, create our posts table: */
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
    VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title,body,created)
    VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title,body,created)
    VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());

create a table

At this stage, you can revert to using Git now, since the file permissions issues have been resolved.

Clone your repository

I’m going to assume that you have Git installed, and a working knowledge of how to use it. Back in your container, you can find the clone url here:

Git clone url

Open up your terminal/console and change directory to where you would like to store your project. Then clone the repository:

git clone https://your_container.my.phpcloud.com/git/container-root.git myproject

Time for some code

We’ll start by creating code to handle blog posts.

1. Create a post model

Create a new file in app/Model called Post.php and add the following code:

<?php
class Post extends AppModel {
    public $name = 'Post';
}

2. Create a Posts Controller

We’ll create the common outline for our controller:

<?php
class PostsController extends AppController {
    public $name = 'Posts';
    public $helpers = array('Html', 'Form');
}

You’ll notice that we have loaded some common helpers too. Next, we’ll add an action to fetch all of the entries in our posts table:

public function index() {
        $this->set('posts', $this->Post->find('all'));
    }

3. Add a view file

Create a new file in app/views/Posts named index.ctp and add the following code:

<!-- File: /app/View/Posts/index.ctp -->

<h1>Blog posts</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php 
                echo $this->Html->link($post['Post']['title'],
                    array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); 
            ?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>

</table>

4. Checking progress

The seems to be an issue with the way that PHP Cloud handles .htaccess files at the moment. To get things working, go to app/config/core.php and do as advised here:

/**
 * To configure CakePHP *not* to use mod_rewrite and to
 * use CakePHP pretty URLs, remove these .htaccess
 * files:
 *
 * /.htaccess
 * /app/.htaccess
 * /app/webroot/.htaccess
 *
 * And uncomment the App.baseUrl below:
 */
    Configure::write('App.baseUrl', env('SCRIPT_NAME'));

You can now visit your site at: http://my-container.my.phpcloud.com/index.php/posts/index:

CakePHP up and running

5. Adding a view action

You will notice that clicking the links causes an error. That’s because we don’t have a view action in the posts controller, and an associated view file.

So in app/Controller/PostsController.php add the required action:

public function view($id = null) {
        $this->Post->id = $id;
        $this->set('post', $this->Post->read());
}

Then, in app/View/Posts add view.ctp:

<!-- File: /app/View/Posts/view.ctp -->
<h1><?php echo $post['Post']['title']?></h1>
<p><small>Created: <?php echo $post['Post']['created']?></small></p>
<p><?php echo $post['Post']['body']?></p>

Now you can click the links to view the individual posts.

Finally…

So you have seen how to get a simple application up and running on PHP Cloud. There are still some issues, but once they are resolved, this will undoubtably be a strong cloud hosting solution.

The ability to log in via SFTP is important, but the missing shell access is unfortunate. However, with phpMyAdmin, Git, and the ability to deploy to other cloud services, PHP Cloud already has a lot to offer.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Siavash Aghili

    Thank you Andy

  • Ali Khani

    Thanks, Nice post

  • sandeep

    Hi Andy,
    I started working on this, i followed your steps.
    I am able to access my container with SFTP ( Win Scp).
    I had Changed permissions for app/tmp to 777, updated Core.php and Cofigured database.php as shown:

    class DATABASE_CONFIG {

    public $default = array(
    ‘datasource’ => ‘Database/Mysql’,
    ‘persistent’ => false,
    ‘host’ => ‘sitesasi-db.my.phpcloud.com’,
    ‘port’ => ’3306′,
    ‘login’ => ‘sitesasi’,
    ‘password’ => ‘Site@123′,
    ‘database’ => ‘sitesasi’,
    ‘prefix’ => ”,
    //’encoding’ => ‘utf8′,
    );

    public $test = array(

    and when i am testing my application

    i am getting 500 Internal Server Error
    http://sitesasi.my.phpcloud.com/
    Apache/2.2.14 (Ubuntu) Server at sitesasi.my.phpcloud.com Port 80

    Kindly guide me in this regard.
    Thanks for the post.