

# QuickStart: Deploy a PHP application to Elastic Beanstalk
<a name="php-quickstart"></a>

In the following tutorial, you'll learn how to create and deploy a sample PHP application to an Amazon Elastic Beanstalk environment using the EB CLI.

**Not for production use**  
Examples are intended for demonstration only. Do not use example applications in production.

**Topics**
+ [Your Amazon account](#php-quickstart-aws-account)
+ [Prerequisites](#php-quickstart-prereq)
+ [Step 1: Create a PHP application](#php-quickstart-create-app)
+ [Step 2: Run your application locally](#php-quickstart-run-local)
+ [Step 3: Initialize and deploy your PHP application](#php-quickstart-deploy)
+ [Step 4: Browse your cloud application](#php-quickstart-run-eb-ap)
+ [Step 5: Update and redeploy your application](#php-quickstart-run-eb-ap)
+ [Clean up](#php-quickstart-cleanup)
+ [Next steps](#php-quickstart-next-steps)

## Your Amazon account
<a name="php-quickstart-aws-account"></a>

If you're not already an Amazon customer, you need to create an Amazon account to use Elastic Beanstalk.

### Create an Amazon account
<a name="php-quickstart-aws-account-procedure"></a>

#### Sign up for an Amazon Web Services account
<a name="sign-up-for-aws"></a>

If you do not have an Amazon Web Services account, use the following procedure to create one.

**To sign up for Amazon Web Services**

1. Open [http://www.amazonaws.cn/](http://www.amazonaws.cn/) and choose **Sign Up**.

1. Follow the on-screen instructions.

Amazon sends you a confirmation email after the sign-up process is complete. At any time, you can view your current account activity and manage your account by going to [http://www.amazonaws.cn/](http://www.amazonaws.cn/) and choosing **My Account**.

#### Secure IAM users
<a name="secure-an-admin"></a>

After you sign up for an Amazon Web Services account, safeguard your administrative user by turning on multi-factor authentication (MFA). For instructions, see [Enable a virtual MFA device for an IAM user (console)](https://docs.amazonaws.cn/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html#enable-virt-mfa-for-iam-user) in the *IAM User Guide*.

To give other users access to your Amazon Web Services account resources, create IAM users. To secure your IAM users, turn on MFA and only give the IAM users the permissions needed to perform their tasks.

For more information about creating and securing IAM users, see the following topics in the *IAM User Guide*: 
+ [Creating an IAM user in your Amazon Web Services account](https://docs.amazonaws.cn//IAM/latest/UserGuide/id_users_create.html)
+ [Access management for Amazon resources](https://docs.amazonaws.cn/IAM/latest/UserGuide/access.html)
+ [Example IAM identity-based policies](https://docs.amazonaws.cn/IAM/latest/UserGuide/access_policies_examples.html)

## Prerequisites
<a name="php-quickstart-prereq"></a>
+ Elastic Beanstalk Command Line Interface - For installation, see [Install EB CLI with setup script (recommended)](eb-cli3.md#eb-cli3-install).
+ PHP - Install PHP on your local machine by following [Installation and Configuration](https://www.php.net/manual/en/install.php) instructions on the PHP website.

## Step 1: Create a PHP application
<a name="php-quickstart-create-app"></a>

For this quick start, you will create a *Hello World* PHP application.

Create a project directory.

```
~$ mkdir eb-php
~$ cd eb-php
```

Next, create an `index.php` file in the project directory and add the following code.

**Example `index.php`**  

```
<?php
  echo "Hello from a PHP application running in Elastic Beanstalk!";
?>
```

## Step 2: Run your application locally
<a name="php-quickstart-run-local"></a>

Use the following command to run your application locally.

```
~$ php -S localhost:5000
```

Open a browser to [http://localhost:5000](http://localhost:5000).

You should see your hello message in the browser and log messages in your terminal.

Stop the local server by entering `Control+c`, so you can deploy the Elastic Beanstalk.

## Step 3: Initialize and deploy your PHP application
<a name="php-quickstart-deploy"></a>

Next, you will deploy your application to an *environment* using the Elastic Beanstalk console or the EB CLI. For this tutorial, you'll use the EB CLI with the interactive option to initialize an environment. 

**To initialize your environment and create an environment**

1. Run the following **init** command.

   ```
   ~$ eb init -i
   ```

   The init command creates an application interactively. The application name will default to the local folder which is `eb-php`.

   For all prompts, except SSH access, accept the defaults to create an environment with the latest PHP platform version. For troubleshooting instances, you can set up SSH access by re-running the `eb init -i`command at a later time, or connect using Amazon EC2 Instance Connect or Session Manager.

1. Create an environment and deploy your application

   Run the following command to create an environment named `blue-env`.

   ```
   ~$ eb create blue-env
   ```

   When you run the **eb create** command for the first time, Elastic Beanstalk automatically builds a zip file of your application, called a *source bundle*. Next, Elastic Beanstalk creates an environment with one or more Amazon EC2 instances, and then deploys the application into the environment.

   Deploying your application to Elastic Beanstalk might take up to five minutes.

## Step 4: Browse your cloud application
<a name="php-quickstart-run-eb-ap"></a>

When the process to create your environment completes, your application should be running and listening for requests on port 5000. Connect to your application with the following command:

```
~$ eb open
```

The `eb open` command opens a browser tab to a custom subdomain created for your application.

## Step 5: Update and redeploy your application
<a name="php-quickstart-run-eb-ap"></a>

After you have created an application and deployed to an environment, you can deploy a new version of the application or a different application at any time. Deploying a new application version is faster because it doesn't require provisioning or restarting Amazon EC2 instances.

Update your PHP code to include the REQUEST\_TIME value from the server environment:

```
<?php
  echo "Hello from a PHP application running in Elastic Beanstalk!";
  
  $timestamp = $_SERVER['REQUEST_TIME'];
  echo '<br/>Request time: ' . date('Y/m/d H:i:s', $timestamp);
?>
```

Redeploy your PHP code to Elastic Beanstalk with the following command:

```
~$ eb deploy
```

When you run **eb deploy**, the EB CLI bundles up the contents of your project directory and deploys it to your environment.

After the deploy finishes, refresh the page or reconnect to your application with `eb open`. You should see your updates. If not, troubleshoot by running your local server again to verify your changes.

****Congratulations\!****  
You've created, deployed, and updated a PHP application with Elastic Beanstalk\!

## Clean up
<a name="php-quickstart-cleanup"></a>

After you finish working with the demo code, you can terminate your environment. Elastic Beanstalk deletes all related Amazon resources, such as [Amazon EC2 instances](using-features.managing.ec2.md), [database instances](using-features.managing.db.md), [load balancers](using-features.managing.elb.md), security groups, and [alarms](using-features.alarms.md#using-features.alarms.title). 

Removing resources does not delete the Elastic Beanstalk application, so you can create new environments for your application at any time.

**To terminate your Elastic Beanstalk environment from the console**

1. Open the [Elastic Beanstalk console](https://console.amazonaws.cn/elasticbeanstalk), and in the **Regions** list, select your Amazon Web Services Region.

1. In the navigation pane, choose **Environments**, and then choose the name of your environment from the list.

1. Choose **Actions**, and then choose **Terminate environment**.

1. Use the on-screen dialog box to confirm environment termination.

Alternatively, you can terminate your environment with the EB CLI with the following command:

```
~$ eb terminate
```

## Next steps
<a name="php-quickstart-next-steps"></a>

You can explore your application environment using the Elastic Beanstalk console. For more info, see [Explore your environment](GettingStarted.md#GettingStarted.Explore).

For advanced examples using PHP, see [Advanced examples for PHP in Elastic Beanstalk](php-samples.md).