Deploying a Laravel application to AWS Elastic Beanstalk
Deploying a Laravel application to AWS Elastic Beanstalk involves several steps to set up the environment, configure the application, and deploy it. Here's a step-by-step guide:
Prerequisites:
- AWS Account: You need an AWS account.
- AWS CLI: Install the AWS Command Line Interface (CLI) on your local machine.
- Elastic Beanstalk CLI (EB CLI): Install the Elastic Beanstalk Command Line Interface.
- Git: Make sure Git is installed for version control.
- Composer: Laravel uses Composer for dependency management, so ensure it’s installed.
- Laravel Project: A working Laravel project on your local machine.
Step 1: Prepare Your Laravel Application
Set up your
.env
file:- Ensure your
.env
file is configured for production. In particular, update your database and cache configurations to use Amazon RDS, S3, or any other AWS service you plan to use.
- Ensure your
Install Dependencies:
- Run
composer install
to install your project dependencies.
- Run
Generate Application Key:
- If you haven’t already, generate the Laravel application key:
Set Up Storage Links:
- Ensure your storage is correctly linked to the public directory:
Migrate Database:
- Run migrations to set up your database schema. Make sure your production database is set in the
.env
file.
- Run migrations to set up your database schema. Make sure your production database is set in the
Optimize Laravel for Production:
- Run the following commands to optimize your Laravel application for production:
Step 2: Create Elastic Beanstalk Environment
Install AWS CLI: If you haven't installed AWS CLI, you can install it by following the instructions here: AWS CLI Installation.
After installation, configure your AWS CLI:
Provide your AWS Access Key, Secret Key, region, and output format.
Install Elastic Beanstalk CLI: The EB CLI is used to interact with AWS Elastic Beanstalk. Install it by following the instructions in the official EB CLI documentation.
Create a new Elastic Beanstalk application: Navigate to your Laravel project directory in the terminal and initialize Elastic Beanstalk:
This command will prompt you to:
- Choose your region (e.g.,
us-east-1
). - Select the application platform. Choose PHP since Laravel is a PHP application.
- Set up an Elastic Beanstalk environment (you can choose to create a new one).
- Choose your region (e.g.,
Configure Environment: Elastic Beanstalk will automatically detect your PHP environment, but you may need to adjust the configuration, especially if you're using an RDS database or specific storage options (e.g., Amazon S3).
Step 3: Configure Elastic Beanstalk for Laravel
Add a
.ebextensions
directory: Elastic Beanstalk allows you to customize your environment using configuration files stored in the.ebextensions
directory.Create a directory called
.ebextensions
in your Laravel project if it doesn’t exist, and add a configuration file namednginx.conf
for handling file uploads, etc.Example
nginx.conf
:This file sets the maximum upload size for files.
Create
Procfile
for Elastic Beanstalk: Create aProcfile
at the root of your Laravel project. TheProcfile
tells Elastic Beanstalk how to run your application.Add the following line to the
Procfile
:This line configures Elastic Beanstalk to serve your application using Apache.
Step 4: Deploy Your Application
Create an Elastic Beanstalk Environment: After configuring your project, create a new environment using the following command:
Choose an environment name (e.g.,
laravel-env
) and select the environment type (typically Load balanced for production or Single instance for development).Deploy Your Application: Once your environment is set up, deploy your Laravel application with:
This command will package your application, upload it to Elastic Beanstalk, and start the application.
Open Your Application: After deployment, you can open your application in the browser with:
This will open your Laravel application hosted on Elastic Beanstalk.
Step 5: Configure RDS and S3 (Optional)
If you're using Amazon RDS for your database or Amazon S3 for file storage, you need to configure these services.
RDS Database:
- In your
.env
file, set the following RDS variables:
- In your
S3 Storage:
- In your
.env
file, configure S3 storage:
Ensure your Laravel application is configured to use S3 for file uploads by updating
config/filesystems.php
.- In your
Step 6: Monitor and Manage Your Application
You can manage your Elastic Beanstalk application using the AWS Management Console or the EB CLI.
Use the following command to view logs:
To update the application, push changes to your Git repository, then deploy again with:
Conclusion
You’ve successfully deployed your Laravel application to AWS Elastic Beanstalk! Elastic Beanstalk provides a scalable, easy-to-manage environment for hosting web applications, while Laravel allows you to quickly build powerful, feature-rich applications.