This little guide is meant to help people to install and set up the Amazon Web Services SDK for PHP in few steps. I’m going to try to explain every single detail to help new people working with Amazon Web Services.
To do so, I’m going to launch an Amazon EC2 instance with Ubuntu 12.04. To begin with we need to go the AWS Management Console, select the Amazon EC2 tab and click in ‘Launch instance’. We’ll follow ‘Classic Wizard’ and click Continue. Now we have to select an Ubuntu 12.04 (Precise Pangolin) instance. Since it is going to be difficult to find it among all AMIs, the easiest way is to check the AMI identifier in the Ubuntu website: http://uec-images.ubuntu.com/releases/precise/release/ . For this guide I’m going to select this AMI: ami-ac9943c5 (32-bit, ebs, us-east-1).
To find by AMI ID we click in ‘Community AMIs’ and we fill the Search field with the AMI ID we have selected. Something like this should appear:
We need to select the second one. In the next step we can select how many instances we want to launch and which type. For this example we will launch one t1.micro instance. Click continue. Click continue again. Give a name to the instance and click continue. Now you can select an existent keypair to connect to the instance or create a new one, it’s up to you. In the next window you have to select the security group for that instance (firewall). It is mandatory that the group allows SSH (port TCP 22) and HTTP (port TCP 80) connections at least, so if you need to create a new security group, do it with those rules. Click continue. Review all the parameters selected and if you are done click on Launch.
Perfect! Now we have an Ubuntu 12.04 running over AWS. So let’s start with the installation and configuration of the AWS SDK for PHP. We need to connect to our instance. We are going to do it using SSH (directly from terminal if you are on Mac or Ubuntu, or via Putty or any similar program if you are on Windows). We are not using the terminal provided by the AWS Management Console. To connect using SSH we need the public DNS of our instance. We can get this URL in the instances list in the Amazon EC2 tab, just selecting our instance and checking the information below.
It should be similar to: ec2-107-21-164-34.compute-1.amazonaws.com. Let’s open a Terminal (or Putty) and browse to the folder where we have our keypair. Once there, we execute:
ssh -i keypair_name.pem username@public_dns
For my example, mine would be:
ssh -i myKeypair.pem firstname.lastname@example.org
If everything is right, you will connect to the instance. However, not everything works at the first time. If you cannot connect, the most common issues are:
- Keypair permissions: the .pem file must have a specific permissions to be used. Those are that just the owner can read and write. So set them accordingly. If you are using Ubuntu or Mac execute: chmod 600 keypair.pem
- Security group port closed: ensure that your security group allows SSH connections (port TCP 22).
Once you are connected, you are in a completely new and clear Ubuntu. So, we need to install the following:
- Apache: it’s the webserver, needed to run PHP scripts.
- PHP5: the engine of PHP.
- Curl for PHP: a tool for transferring files that is used by the AWS SDK for PHP.
- Pear: a PHP application repository.
- AWSSDKforPHP: the SDK itself.
First of all, we should update the package list:
sudo apt-get update
And now let’s install what we need:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install php5-curl
sudo apt-get install php-pear
sudo /etc/init.d/apache2 restart
Everything, except the SDK, should be installed right now. Let’s test it. Browse to /var/www . Remove the file index.html (sudo rm index.html) and run sudo pico index.php . Pico or nano is a terminal text editor. Write ‘<?php phpinfo(); ?>’ and press Ctrl + X, then Y, and press Enter. Now if we go to http://ec2-107-21-164-34.compute-1.amazonaws.com (your public DNS address) in any browser we should see this:
Nice! We are almost done. Let’s install and configure the SDK. We are going to do it using PEAR ( http://pear.amazonwebservices.com/ ), so let’s run:
sudo pear channel-discover pear.amazonwebservices.com
sudo pear install aws/sdk
Installation done. Let’s configure it. Navigate to /usr/share/php/AWSSDKforPHP . Once there, run sudo pico config-sample.inc.php . This file contains all the information to connect with Amazon when you are doing requests. You need to fill up key (that it’s also called public key) and secret key. You can get them from https://aws-portal.amazon.com/gp/aws/securityCredentials. When you are done, press Ctrl + X, press Y, and now rename the file to config.inc.php and press Enter and Y. Last step is to test if the SDK works. The SDK comes with some test files, so let’s use them! Without changing the directory, execute:
sudo cp -r _samples /var/www
sudo /etc/init.d/apache2 restart
Now navigate to /var/www/_samples/ and execute sudo pico cli-s3_get_urls_for_uplods.php and replace “require_once ‘../sdk.class.php’;” for “require_once ‘AWSSDKforPHP/sdk.class.php’;” . Finally, go to any browser and go to: http://ec2-107-21-164-34.compute-1.amazonaws.com/_samples/cli-s3_get_urls_for_uploads.php (using your public DNS address) and we should see:
Congratulations! You have installed successfully the AWS SDK for PHP in Ubuntu 12.04. Now you have uploaded 4 test files to a bucket in your S3 account, go check it! Each time you want to use the SDK you just need to add ”require_once ‘AWSSDKforPHP/sdk.class.php’;” in your PHP code. I hope this guide helps people with installation and configuration problems. If you have any doubts, comment!