QuickStart: Deploy a PHP application to Elastic Beanstalk
In the following tutorial, you'll learn how to create and deploy a sample PHP application to an AWS Elastic Beanstalk environment using the EB CLI.
Not for production use
Examples are intended for demonstration only. Do not use example applications in production.
Sections
Your AWS account
If you're not already an AWS customer, you need to create an AWS account to use Elastic Beanstalk.
Sign up for an AWS account
To get started with AWS, you need an AWS account. For information about creating an AWS account, see Getting started with an AWS account in the AWS Account Management Reference Guide.
Prerequisites
Elastic Beanstalk Command Line Interface - For installation, see Install EB CLI with setup script (recommended).
PHP - Install PHP on your local machine by following Installation and Configuration
instructions on the PHP website.
Step 1: Create a PHP application
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
Use the following command to run your application locally.
~$php -S localhost:5000
Open a browser to 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
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
-
Run the following init command.
~$eb init -iThe 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 -icommand at a later time, or connect using Amazon EC2 Instance Connect or Session Manager. -
Create an environment and deploy your application
Run the following command to create an environment named
blue-env.~$eb create blue-envWhen 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
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
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
After you finish working with the demo code, you can terminate your environment. Elastic Beanstalk deletes all related AWS resources, such as Amazon EC2 instances, database instances, load balancers, security groups, and alarms.
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
Open the Elastic Beanstalk console
, and in the Regions list, select your AWS Region. -
In the navigation pane, choose Environments, and then choose the name of your environment from the list.
-
Choose Actions, and then choose Terminate environment.
-
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
You can explore your application environment using the Elastic Beanstalk console. For more info, see Explore your environment.
For advanced examples using PHP, see Advanced examples for PHP in Elastic Beanstalk.