Skip to main content


In this guide, you will learn how to use Amazon Web Services ElastiCache with PHP applications. We’ll do this by deploying a basic PHP website visitor counter with Elastic Beanstalk. We are using Elastic Beanstalk to effortlessly deploy and host our code, instead of focusing on setting up infrastructure. We will be using ElastiCache as our hosted cache service, which will be powered by Redis.


What is elasticache?


ElastiCache allows you to manage and scale cache using Memcached or Redis as the storage engine.


What are the benefits of using ElastiCache?

Many applications today are powered by or use data in some way. Unfortunately, the database can often be a bottleneck and causes an application to slow down and give a bad user experience. Some benefits of using an in-memory cache such as Redis:

  • It’s incredibly fast
  • Less resources are used
  • Features such as data expiration


ElastiCache VS self-managed Redis

We’ll be focusing on using Redis as our ElastiCache engine, but it is possible to host your own Redis instance / services if you prefer. Let’s break it down.




Configuring ElastiCache

Let’s get started by setting up ElastiCache in Amazon Web Services.

  1. We first need to log into our AWS dashboard
  2. Click on the “services” dropdown
  3. Click on “ElastiCache” found under the “Database” section
  4. On the left hand side menu, click the “Redis” link
  5. You are now in the Redis section of the EC dashboard, click on the blue “Create” button:




We now need to create our cluster / instance. You should see a form on the screen, similar to the one below:



Make sure you have “Redis” selected for the ‘cluster engine’. Let’s fill out the form:

  • Set the name field: test-ec-redis
  • Set the description field to something helpful. For example, “A test Redis instance to learn ElastiCache”
  • We can leave the engine version compatibility set to 3.2.4
  • We can leave the port set to 6379
  • We can leave the parameter group set to default.redis3.2
  • Let’s set the node type to cache.t2.micro as that is part of AWS’s free tier
  • Let’s set the number of replicas to None for now
  • We can ignore the advanced settings for now. Continue by pressing the blue “Create” button


AWS will now create and launch the instance:




It may take a few minutes, but once it’s created you will have successfully launched an ElastiCache instance running Redis as its database engine. Congratulations! You should see the status change to “available”:




You can now click on the blue “test-ec-redis” name to learn more information about that specific node:




Please Note: copy and save the “Endpoint” URL as we will be using this later in our PHP application code.


Basic security settings for ElastiCache

Currently ElastiCache is locked down and cannot be accessed. If we tried to connect to the Endpoint URL from above, then we would receive “Connection timed out” errors. To handle this, we need to edit the security group that we have used to set up the ElastiCache instance.

In the information, you should see a section called “Security Group(s).” here’s an example of mine:

Security Group(s): sg-a57782cc (VPC)(active)

In my case, I need to update the ‘sg-a577782cc’ security group to accommodate incoming connections for ElastiCache. To edit this, do the following.


  1. Go to your AWS dashboard and click into the EC2 section.
  2. On the left hand side menu you should see a section titled “NETWORK & SECURITY”, click on the “Security Groups” option.
  3. You should now see the ‘Security Groups’ dashboard and see a list of available security groups. You now need to find the security group that is being used by your ElastiCache instance.
  4. Select your security group and click on the “Inbound” tab found at the bottom of the screen.
  5. Click on the “Edit” button and we can now add in a rule to allow access into your EC instance by clicking the “Add Rule” button.
  6. Change the type to “Custom TCP Rule”, set the port range to the port we used in our set up earlier (the default us 6379) and set the ‘source’ to “Anywhere”.
  7. Click the blue “Save” button.

Congratulations, you have successfully created a Redis server powered by ElastiCache that allows incoming connections.


Please Note: It is worth looking into security groups and permissions with regards to ElastiCache. We don’t want anyone to connect and run commands on our Redis instance. We’ve kept it simple for this guide, but don’t use these security settings in a production environment without understanding how they work.

Effortless PHP deployment using Elastic Beanstalk

Our website counter application is simple and only consists of two scripts. We’ll be using the Predis package for PHP, which allows us to connect and interact with Redis in ElastiCache. To get Predis, we need to create a ‘composer.json’ file that Beanstalk will see and install.


Here’s our ‘composer.json’ file:


"require": {
"predis/predis": "^1.1"


And here is our PHP application, ‘index.php’:


require 'vendor/predis/predis/autoload.php';


$client = new Predis\Client([
‘scheme’ => ‘tcp’,
‘host’ => getenv(“EC_HOST”),
‘port’ => 6379,

$value = $client->incr(‘website_visitors’);
echo “Total website visitors: “ . $value . “.”;


The PHP application loads the Predis package, connects to ElastiCache using the host URL (which is saved inside an environment variable called EC_HOST), and then increments the key named “website_visitors.” The first time you run this file, the key will be created and a value of ‘1’ will be assigned to it. We now need to add these files to a zipped file (Beanstalk supports .zip or .war). I’ve called my folder ‘’.

Creating an Elastic Beanstalk environment

An environment is a list of settings that AWS uses to configure the elastic cloud instance where your code / files will be hosted.


To get started with EB, you’ll need to log into your AWS dashboard and select the “Services” drop down from the top menu. Under the “Compute” section, click on the service titled “Elastic Beanstalk.”.




We can now start the wizard by clicking the “Create New Application” link in the top right corner. You will see a popup that asks for an application name and description. The name is mandatory but the description field is not:




I’ll be using “linux-academy-demo” as my application name. When you are ready, click the blue “Create” button. You will now be asked to select an environment tier:




In this example, we will be selecting the “Web server environment” tier, which is perfect for our demo PHP application. When you are ready, click the blue “Select” button. Next we will need to provide some information about our application.

We need to set the “Platform” option to “PHP” and in the “Application code” section, select the “Upload your code” option. Then click the “Upload” button. You’ll need to find the .zip or .war folder that you created earlier and upload it.




You can upload this from your local machine or choose to use a public S3 URL. Click the blue “Upload” button after making your selection. To keep this guide relatively straightforward, we will ignore the “Configure more options” button for now, but you can use those settings to configure things like PHP version and EC2 instance type. By default, it uses the t2.micro option which falls within AWS’s free tier.

Let’s make our application live by clicking the “Create environment” button. You will see a new screen that updates in real time letting you know the status of your Beanstalk instance:




We now need to create our ‘EC_HOST’ environment variable. You can do this by going to the running EB instance dashboard and clicking on the “Configuration” menu option found on the left side of the page.

You will be presented with a screen containing multiple options. Click on the “cog” / “settings” icon next to the “Software Configuration” title:




Scroll down to the “Environment Properties” section and add the key (“EC_HOST”) and the value (ElastiCache Endpoint URL). Here’s an example of mine:




Click on the blue “Apply” button and EB will restart your application. If everything is successful, you should be given a live URL for your application. The URL is not easy to remember. For reference, here’s mine:

If I visit that URL, I see my live PHP application:




I’ve been running tests with my ElastiCache setup, so my key value is already set to 19. Refreshing the page causes PHP to increment the key. I will then see “Total website visitors: 20.” If other people visit this page, the counter will continue to increment.

Congratulations! You have created a PHP website visitor counter using ElastiCache Redis as the cache, and used Elastic Beanstalk to deploy and host it.

If you would like to stop this application, navigate to the environment using the menu on the left (in AWS). On the overview section, you should see the “Actions” drop down button. Click on that, then click the “Terminate Environment” option:




To confirm, click the red “Terminate” button that appears in the pop up window.

To stop our ElastiCache instance, we must follow a similar procedure. Navigate to the EC dashboard and click on the “Redis” option. You should see your instance here. Select it and click the “Delete” button at the top of the screen. A security pop up will appear asking you to confirm your account. Click the “Delete” button again, and the instance will stop and be deleted.





Comments are disabled for this guide.