April 19, 2016

AWS PHP Guide for Developers

AWS
Zend Server

Earlier this month we hosted an AWS 101 webinar in which we did a live walk-through showing you how easy it is to get up and running with your PHP applications on the enterprise cloud platform AWS.

In this article, we'll walk you through getting started with AWS using a Zend PHP server. But first, let's clear up a common question.

Does AWS Support PHP?

Yes, there are many options for deploying PHP applications on AWS. These options ranging from using a minimal OS image to spin up an instance and then install LAMP stack on top of it, or use images that come with everything needed to run web application pre-installed.

Easy Steps to Host a PHP Website on AWS

One of the quickest ways to get your PHP app into an AWS machine is to use Zend Server AWS machine images (AMI) which comes with Zend Server pre-installed (but without MySQL server). If your application requires a MySQL server (and many PHP apps do), we recommend using AWS RDS which supplies MySQL as a service.

Aside from regular PHP and Apache stack, Zend Server also provides additional value with the following components:

  • Z-Ray: allows displaying all information about requests processed by PHP in real time
  • Deployment: automates deployment of PHP applications
  • Monitoring: centralized interface to PHP events
  • Data cache: PHP extension that allows caching data in memory or on disk
  • Page cache: cache full page that was generated by PHP
  • Job queue: background execution of requests to any URL
  • Session clustering: allows sharing of PHP sessions between cluster nodes

Zend Server images on AWS Marketplace provide a choice of the following options:

  • Operating system: RHEL7 or Ubuntu 14.04 (CentOS is on our roadmap)
  • PHP versions: 5.5 or 5.6 (there are a few images with PHP 5.3/5.4 as an LTS version and PHP 7 will be supported soon)
  • Zend Server editions: developer, professional, and enterprise

Zend Server for AWS PHP + MySQL

Ready to get started with AWS PHP + MySQL?

As a follow-on to the webinar, we have pulled together this step-by-step guide covering how to start a simple instance of Zend Server on AWS, add RDS MySQL service and connect to it, and launch WordPress using Zend Server deployment.

Step 0 - AWS Vocabulary and Security Basics

Let’s start with a simple vocabulary of common terms that are used when discussing AWS:

  • AWS (Amazon Web Services)
  • EC2 (Elastic Compute Cloud): an AWS service that provides computing resources
  • AMI (Amazon Machine Image): a filesystem image with some metadata like type of OS in image and type of virtualization to use when starting new server
  • Instance: a Virtual Machine running on EC2 using some AMI
  • Instance type: a combination of CPU, memory, storage, and networking capacity
  • RDS (Relational Database Service): an AWS service that provides many relational databases (supports, but not limited to, MySQL)

To help you understand the different AWS security settings, these security terms will become handy:

  • VPC (Virtual Private Networking): allows you to define one or more networks which is composed of one or more VPC Subnets
  • VPC Subnet: similar to regular network subnet
  • Security Group: a set of network rules. Resources on AWS can belong to one or more Security Groups.
  • Key pair: regular SSH key pair. Must be specified when starting EC2 Instance to allow SSH access.

Step 1 - Launching Zend Server on AWS

There are two ways to launch Zend Server on AWS. First is to use AWS Marketplace website (option A) and launch Zend Server from there. Second is to use Marketplace inside AWS Console (option B). We will cover both ways here.

Option A: Launch Zend Server using AWS Marketplace website

image blog zend server aws
  1. Open AWS Marketplace website
  2. Sign in or create AWS account by clicking relevant link on top of page
  3. Type ‘Zend Server’ in the search bar and press Enter
  4. In the search result locate Zend Server with relevant PHP version, edition, and operating system. For this guide please select 'PHP 5.6 - Zend Server Developer Edition (Ubuntu)'.
  5. On the product page click on "Continue" button located on the right
  6. Configure and customize the launch settings of Zend Server instance.
    • Version: version of Zend Server AMI to use. It’s recommended to always use latest version
    • Region: which region Zend Server instance should be started
    • Instance type:  how much CPU/memory/Storage you want for your instance
    • VPC settings: which VPC and subnet instance is started
    • Security group: security group for instance, you can select existing security group or let AWS create new security group with default rules for Zend Server (recommended)
    • Key pair: which SSH key pair should be installed on this instance
  7. Review the launch settings and click on "Accept Software Terms & Launch with 1-Click" button, After the first launch this button will be named "Launch with 1-Click".
  8. Login to AWS Console to see the new instance and get its’ details

Option B: Launch Zend Server using AWS Console

  1. Login to AWS Console
  2. Open EC2 Console
  3. On dashboard click on "Launch Instance"button
  4. In the wizard presented select AWS Marketplace tab on the left
  5. Enter 'Zend Server' in the search bar
  6. Select relevant Zend Server product with relevant PHP version, edition, and operating system and click Continue button in product summary that is shown. For this guide please select 'PHP 5.6 - Zend Server Developer Edition (Ubuntu)'.
  7. Select instance type
  8. On steps 3, 4, 5 various instance settings can be configured. For this guide, skip them by clicking “5. Tag Instance” link on top.
  9. Enter friendly name for your instance that will be used to identify it and click on "Review and Launch" button
  10. Review new instance settings and click "Launch"
  11. Either select an existing key pair (and acknowledge you have access to it) or create a new one and click "Launch Instances"
  12. Wait while the instance is created and then click on "View Instances" button at the bottom of a page Launch Status

Bootstrap Zend Server to get Zend Server ready for work

Once you have finished either option A or B, you need to make Zend Server ready for work. We have to bootstrap it using very simple wizard. To do this we open the Zend Server UI which is accessible using a web browser on port 10081 of the instance we just started.

We need to find out either the public DNS (what we use in this guide) or public IP of the instance. We will also need the instance ID to enter Zend Server UI. By default Zend Server UI is protected so you have to enter instance ID as login and password, but once you bootstrap Zend Server and set admin password you won’t have to use it again.

Follow these steps to find “Public DNS” and “Instance ID” of your instance:

  1. Open AWS Console
  2. Enter EC2 Console
  3. In the menu on the left click on “Instances” link
  4. Locate your instance in the list of instances and click on it
  5. On the bottom pane you will see information about the instance shown in 2 columns
    • “Public DNS” field is on top of right column
    • “Instance ID” field is on top of left column

Now that we have public DNS name of instance we can open our browser and enter the following address to access Zend Server UI: http://<Public DNS>:10081/

Once you open Zend Server UI you will first be asked to authenticate. Enter “Instance ID” you found earlier as both login and password.

You will then be presented a wizard that will guide you to setup Zend Server in a few very simple steps. Upon finishing wizard and successfully bootstrapping your Zend Server is fully ready to work.

Note: bootstrap process can be fully automated by providing user data to instance. 

Step 2 – Launching RDS for MySQL

We have Zend Server, which means that we have fully working and configured web server with PHP. To go on and launch WordPress, we are still need to add a MySQL database server. We, of course, could install and configure it on the same instance with Zend Server, but it’s much simpler to use AWS RDS. RDS provides many database services including MySQL. So let’s setup an RDS instance and configure Security Group to allow access to it:

  1. Open AWS Console
  2. Open RDS Console
  3. Click “Launch a DB Instance” button
  4. Select “MySQL” tab on the left and click “Select” button
  5. Select “MySQL” under Dev/Test section and click “Next Step” button
  6. Configure all options you see on screen (once you select some option, it’s description is presented on the right), for our example fill following details:
    • DB Engine Version: 5.6.27 or later
    • DB Instance Class: db.m3.medium
    • Multi-AZ Deployment: No
    • Storage Type: General Purpose (SSD)
    • Allocated Storage: 5GB
    • DB Instance Identifier: unique name for your DB Instance
    • Master Username: root
    • Master Password: your password>
    • Confirm Password: must be same as Master Password
  7. Click “Next Step” button
  8. Advanced Settings step - as in previous step, clicking any option shows its description on the right. For this guide change only the following settings:
    • VPC: select your default VPC
    • Subnet Group: select any subnet group
    • Publicly Accessible: yes
    • Availability Zone: No preference
    • VPC Security Group: select same Security Group as Security Group of EC2 instance you created (if you do not remember it, you can find it in EC2 console on details pane where you found “Instance ID” and “Public DNS” fields)
    • Database Name: leave empty
    • Backup Retention Period: if you do not want to backup this RDS, select 0
  9. Click “Launch DB Instance”

Now we have to wait till our new DB instance is launched and ready for use. While waiting we can configure our Security Group rules to allow communication between web server and MySQL DB server.

Configure Security Group

There are two options for configuration here: either allow communication only between web server and MySQL server (more suitable for production environments), or allow anyone to connect to MySQL DB server (more suitable for development environments). In this guide we will cover only second option.

Allow anyone to connect to MySQL server

  1. Open AWS Console
  2. Open EC2 Console
  3. Click on “Security Groups” link in the menu on the left
  4. Select relevant Security Group
  5. In the details pane on bottom click on tab “Inbound”
  6. Click on “Edit” button
  7. In the dialog shown click “Add Rule”
  8. Fll values of new rule as following:
    1. Type - Custom TCP Rule
    2. Protocol - TCP
    3. Port Range - 3306
    4. Source - Custom IP and enter 0.0.0.0/0 as IP
  9. Click “Save” button

To find MySQL server DNS see how to find endpoint (which is also MySQL DNS) in next step.

Step 3 - Deploy WordPress

Once RDS has finished starting (it can take 10 minutes or even more), you have to find its endpoint. For this follow these steps:

  1. Open AWS Console
  2. Open RDS Console
  3. Click on “Instances” link in the menu on the left
  4. Click on your RDS instance
  5. Locate “Endpoint” field and save it

Once we have MySQL endpoint we can deploy the WordPress application. For this do the following:

  1. Open Zend Server UI (reminder - http://<instance public DNS>:10081/)
  2. Login to Zend Server (if needed)
  3. Click on “Getting Started” menu on the left
  4. Click on WordPress icon located under “Deploy sample apps” section
  5. Wait till WordPress application is downloaded and click “Next” button
  6. Next you will be presented “README” section with details about WordPress package, you can read them and then press “Next”
  7. Here you can fill application details:
    • Display name: name that this application will have in applications list
    • Virtual Host: on which virtual host application should be deployed (leave “default server”)
    • Path: path under which application will be deployed (leave empty)
  8. Click “Next” to proceed
  9. Read EULA, check checkbox under it and click “Next”
  10. On next screen you will see prerequisites validation status
  11. Click “Next” to continue
  12. Now the important part - user parameters, fill them as following:
    • Site URL: must be http://<Instance Public DNS>/
    • Blog Name: fill as you like
    • Database host: must be set to endpoint of RDS instance
    • Database username: root
    • Database password: password you entered when you created RDS instance
    • Database name: name of database for WordPress (change only if needed)
    • WordPress admin username: username for admin user of WordPress
    • WordPress admin password: password for admin user of WordPress
    • WordPress admin email: email for admin user of WordPress
    • Table prefix: prefix for tables created in database (change only if needed)
  13. Click “Next” button
  14. Review deployment summary and click “Deploy”

Once you click “Deploy” application deployment will be started. It should take up to a few minutes to finish (but most of time it finishes in tens of seconds). Once the application status on applications list page changes to “Deployed”, you can access WordPress on http://<Instance Public IP>/

Using Z-Ray

Most interesting and impressive feature of Zend Server for developers is Z-Ray. It can show comprehensive information about how page was generated by PHP, what resources were used, timing of any function, and even more custom and specific information using plugins. Since we already deployed WordPress on our Zend Server, let’s see what Z-Ray can show us. Z-Ray is displayed as a bar at bottom of web page in browser (this bar is injected by Z-Ray PHP extension in Zend Server). So open your WordPress website and you will see a bar on bottom similar to this:

Image Blog Zray bar

Note that Z-Ray is enabled by default only in Zend Server Developer Edition, or when you choose “Development” profile during bootstrap process of Zend Server Professional Edition and Zend Server Enterprise Edition. Additionally you can configure Z-Ray to be shown only for requests coming from specific IP addresses or when URL has Z-Ray token. This is very useful if you want to use Z-Ray in production, but do not want to show it to anyone visiting your website.

Getting Started

You can start using Zend Server on AWS today. You can also start a free 30-day trial.

START FREE TRIAL

Additional Resources