PHP: Setting up Amazon AWSSDKforPHP on Ubuntu 12.04 LTS

We recently moved to Amazon cloud services at my work place, and it has been both interesting and fun to say the least. I started changing some of our old Java
and PHP applications to use the AWS services such SQS, SES,S3 and etc, it has been fun and games with a lot of trial and errors and I am hoping to share all that I have learnt in that process.

For this post I will focus on getting the PHP environment ready for AWS.

1. PHP requirements

Your PHP environment needs to conform to the following requirements before you can start using the SDK:

You can get more details from this page: http://aws.amazon.com/articles/4261?_encoding=UTF8&jiveRedirect=1

2.  Download the AWSSDKforPHP

You can download the AWSSDKforPHP by following the link here and extract it to your local development directory e.g. /var/www/aws .

There are other options you can use to get the latest version of the sdk:

Once you have installed the SDK, e.g. /var/www/aws , navigate to http://localhost/aws/sdk-1.5.14/_compatibility_test/sdk_compatibility_test.php through your browser and make sure that your environment passes the test.

3. AWS Credentials

The SDK requires that you use your Amazon AWS credentials for using all the service it covers, you can get your accounts credentials from this link or you can register for a new account which is very easy, all they require is your credit card last I checked.

That is all for now folks, I will explore using AWS SQS service in the post – happy coding.

Installing PHP-AMQP on Ubuntu 12.04 LTS

This guide assumes the following:

  • You have installed Ubuntu 12.04 LTS.
  • You have a basic PHP5 installation.
  • You have Sudo access.
  • You know how to compile from source code (make or cmake), How to compile from source?

By the way, here is my installed version of PHP:

PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:20:55)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

[/ccN_bash]

1. Install build tools

Lets begin, first we need to install the necessary tools before we can compile the php-amqp extension. Please execute the following commands.

sudo apt-get install build-essential
sudo apt-get install php-pear
sudo apt-get install git

2. Compiling Rabbitmq C AMQP client library

We need to download the rabbitmq-c from github since the default one which comes with the ubuntu repository (librabbit-mq) has certain errors which makes the php-amqp extension fail to compile.

  • Navigate to the temp directory.
     cd /tmp
  • Clone rabbitmq-c github repository.
    git clone git://github.com/alanxz/rabbitmq-c.git
    cd rabbitmq-c
    git submodule init (Submodule 'codegen' (git://github.com/rabbitmq/rabbitmq-codegen.git) registered for path 'codegen')  
    git submodule update
  • Compile rabbitmq-c client.
    sudo autoreconf -i
    sudo ./configure
    sudo make
    sudo make install

3. Compile php-amqp extension.

First do a search for the latest version available,

pecl search amqp

My search returned the following results:

amqp    1.0.5 (stable)  Communicate with any AMQP compliant server

Install the latest version of php-amqp or you can browse http://pecl.php.net for a specific version you might want. I am gonna install the latest version 1.0.5.

sudo pecl install amqp-1.0.5

Once it’s done compiling, you need to add it to your php.ini file.

navigate to /etc/php5/conf.d/
sudo vi amqp.ini

Then add the following inside the file.

extension=amqp.so

Restart your apache, and you should see the amqp extension via phpinfo(), Alternatively you can use the CLI version of the php and your output will look something like this:

php -m
[PHP Modules]
amqp
bcmath
bz2
calendar
Core
ctype....

4. The End….

Installing RabbitMQ on Ubuntu 12.04 LTS

I struggled a bit to get the RabbitMQ installation working on my new Ubuntu 12.04 LTS, Below is a documented list of the steps I performed to get it to work and hopefully you will find them useful for your installation.

  • Navigate to http://www.rabbitmq.com/install-debian.html  , they have listed some useful steps to get you going and I will be referencing them on this post.
  • Add the following line to your /etc/apt/sources.list:
    deb http://www.rabbitmq.com/debian/ testing main
  • Add a repository signing key for the RabbitMQ repository.
    wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
    sudo apt-key add rabbitmq-signing-key-public.asc
  • Update your ubuntu repositories.
    sudo apt-get update
  • Install RabbitMQ server.
    sudo apt-get install rabbitmq-server
  • Install the management plugin, it is a grouping of the following plugins:
    • mochiweb
    • webmachine
    • rabbitmq_mochiweb
    • amqp_client
    • rabbitmq_management_agent
    • rabbitmq_management

    which will give you a nice web based interface to interact with the server.

    sudo rabbitmq-plugins list

    Your out put should contain:

     [ ] rabbitmq_management               2.x.x</strong></li>
    • Enable management plugin.
    sudo rabbitmq-plugins enable rabbitmq_management
    • Once you have enabled the management plugin, you should see the following collection of plugins being enabled/activated.
      • mochiweb
      • webmachine
      • rabbitmq_mochiweb
      • amqp_client
      • rabbitmq_management_agent
      • rabbitmq_management
    • Restart the server for the changes to take effect.
      sudo rabbitmqctl stop
    • Start the server with nohup.
      cd /tmp/
      sudo nohup rabbitmq-server start &amp;
    • It’s time to create users, we will create only one user for the sake of shortening this guide. Will create admin user.
      sudo rabbitmqctl add_user admin mypassword
    • Give new user administration privileges
      sudo rabbitmqctl set_user_tags admin administrator
    • Lets assign permissions to admin user                                                                    
      sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
    • Delete the default guest account                                                                                                    
      sudo rabbitmqctl delete_user guest 
    • Restart the server for the changes to take effect.                                                                             
      sudo rabbitmqctl stop 
    • Lastly, navigate to http://localhost:55672 to view the web interface for your RabbitMQ server
    • The End………

The site is backup again

The site has been down for sometime now, about two weeks to be exact. The cause of the down time is one which every developer knows and tends to neglect until it’s late.

My long trusted server of 5 years decided to die on me, The hard drive failed and since I neglected to take appropriate measures like doing BACKUPS frequently – I lost all the content of of this blog and many other useful content I had collected throughout the years. All gone , I prayed a bit and tried to convince the support person at hostgator to try the drive again but to no avail – it was all gone. A bit tough to swallow at first but I knew who the culprit was, and that is me. I have since remedied the situation and it will not happen again. I now have full backups and incremental backups so I am told, but I guess will only find out when we crash again.    

I apologize to all those who keep getting dead links when connecting to the site and I will give you one piece of advice for the future.

Do BACKUPS because you never know when you might need them. Assume the al qaeda has decided to target your site.

Lessons learned:

  1. Backup.
  2. Backup frequently.
  3. and be paranoid about backups.
  4. Backup your Backups if you can!!!!!