Friday, January 27, 2012

Get started with Symfony in Amazon Cloud

We recently released BitNami LAMP WAMP and MAMP Stacks with Symfony 2.0.9, a popular development framework (the *AMP Stacks also bundle other frameworks like ZendFramework, CodeIgniter and CakePHP). In this post, we will walk you step by step through how to start a project with Symfony in the Amazon Cloud using a BitNami LAMP Stack AMI.

First, you will need an Amazon acount to access the AWS Console. You can find how to create your account here (note that they have a free tier which provides you with a free micro instance for a year). Once you login to the AWS Console, go to the EC2 tab and launch a new Instance. You can locate the BitNami LAMP Stack image by searching in the Community AMIs tab for "lampstack-5.3.9-0", which is the latest version at the time of this writing.


AWS Community AMIs

Another option is to launch directly from the BitNami website. You can select your preferred region (United States, Europe or Asia) and the architecture (32 or 64 bits) and click on the appropriate link. Then, you can set different parameters from the AWS Console: availability zone, ssh key, the image type (micro, small, medium) and the security group. Once the machine is started, access it by typing the public DNS name in a browser. You should see something similar to the screenshot below.

LAMPStack welcome page


The next step involves connecting to the machine. You can download the private SSH key from your Amazon account. If you want to connect to the machine from Windows using the popular tool Putty, you will need to convert your private key to the .PPK format. Take a look at this tutorial if you are not sure how to perform this conversion.

On Linux or OS X, you can open a Terminal and run the following command:

$ ssh -i /path/to/your/private/key.pem bitnami@ec2-xx-xx-xx-amazonaws.com

You will see a welcome message similar to:

BitNami welcome message



All the required files for the Symfony framework are in the "/opt/bitnami" folder:

ctlscript.sh: It is the main script to start and stop the servers.
frameworks/symfony: The Symfony 2 framework files.
apache2: The Apache server files.
php: The PHP language files.
mysql: The MySQL database files.

One of the advantages of this structure is that you can install the BitNami LAMP Stack on your own desktop machine, develop your application locally and then migrate the full directory directly to the cloud.

You can edit the files online with a command line editor (nano, vim, emacs) but many people prefer to edit files with a graphical editor locally. Using an SFTP client like FileZilla, you can edit the files from your computer. If you need help, see our how-to for configuration instructions.

In the first place, you can check the requirements:

$ cd /opt/bitnami/frameworks/symfony/app
$ php check.php

You will see a message similar to:

********************************
*                              *
*  Symfony requirements check  *
*                              *
********************************

php.ini used by PHP: /opt/bitnami/php/etc/php.ini

** WARNING **
*  The PHP CLI can use a different php.ini file
*  than the one used with your web server.
*  If this is the case, please ALSO launch this
*  utility from your web server.
** WARNING **

** Mandatory requirements **

  OK        Checking that PHP version is at least 5.3.2 (5.3.9 installed)
  OK        Checking that the "date.timezone" setting is set
  OK        Checking that app/cache/ directory is writable
  OK        Checking that the app/logs/ directory is writable
  OK        Checking that the json_encode() is available
  OK        Checking that the SQLite3 or PDO_SQLite extension is available
  OK        Checking that the session_start() is available
  OK        Checking that the ctype_alpha() is available
  OK        Checking that the token_get_all() is available
  OK        Checking that the APC version is at least 3.0.17

** Optional checks **

  OK        Checking that the PHP-XML module is installed
  ...


A simple way to start learning Symfony is via the Quick Tour that you can access via web. To enable it, you should uncomment the following line that you can find in the Apache configuration file /opt/bitnami/apache2/conf/httpd.conf:

Include "/opt/bitnami/frameworks/symfony/conf/symfony.conf"

and restart the Apache server:

$ ./ctlscript.sh restart apache

The Symfony welcome application is only accessible from localhost. In this case you can create a SSH tunnel to access the application. You can check how to access phpMyAdmin or Symfony application here.

Symfony welcome page

That's all! You can configure the database settings in the parameters.ini file or you be able to change the configuration via web.