Deploying Laravel on AWS Elastic Beanstalk
When it comes to hosting Laravel applications, AWS Elastic Beanstalk stands out as a reliable, scalable, and fully managed deployment service. It eliminates the complexity of infrastructure management, allowing developers to focus on building their applications instead of worrying about servers.
In this blog, you’ll learn how to deploy a Laravel application on AWS Elastic Beanstalk, understand best practices, and discover how to optimize it for performance and cost efficiency.
Why Choose AWS Elastic Beanstalk for Laravel Deployment
Deploying Laravel on Elastic Beanstalk offers a blend of flexibility and automation. It handles provisioning, load balancing, scaling, and monitoring automatically, allowing you to deploy complex applications without deep infrastructure expertise.
Key Benefits
-
Ease of deployment: Deploy your Laravel app using a simple zip file or through the AWS CLI.
-
Scalability: Automatically adjusts resources based on your application’s demand.
-
Built-in load balancing: Ensures even traffic distribution across multiple servers.
-
Integration with AWS services: Works seamlessly with RDS, S3, CloudFront, and more.
-
Cost efficiency: You only pay for the resources you use.
For developers who want enterprise-level hosting without managing servers, Elastic Beanstalk is one of the best choices.
Prerequisites
Before getting started, make sure you have the following:
-
An AWS account
-
Composer and PHP 8.2+ installed locally
-
The AWS CLI configured with your credentials
-
A working Laravel project ready for deployment
Step 1: Preparing Your Laravel Application
Before deployment, your Laravel project must be production-ready.
a. Environment Configuration
Update your .env file with production values. You’ll later set environment variables directly in Elastic Beanstalk for better security.
b. Application Optimization
Run Laravel’s built-in optimization commands to enhance performance:
These commands compile configuration and route files, reducing load time in production.
c. File Permissions
Ensure that the storage and bootstrap/cache directories are writable. This is essential for Laravel to function correctly on AWS.
Step 2: Creating an AWS Elastic Beanstalk Environment
Once your application is ready, it’s time to set up an environment in AWS.
a. Create an Application
-
Log in to your AWS Management Console.
-
Navigate to Elastic Beanstalk.
-
Click Create Application.
-
Give it a name (e.g., laravel-app).
b. Choose a Platform
Select PHP as your platform. Elastic Beanstalk will automatically detect and configure the appropriate runtime environment.
c. Configure Environment
Elastic Beanstalk allows you to choose between Web Server Environment (for Laravel apps) and Worker Environment (for background jobs).
Choose Web Server Environment for your primary app deployment.
You can either launch a new EC2 instance or use existing resources.
Step 3: Packaging Your Laravel Application
Elastic Beanstalk requires a deployable ZIP package of your Laravel project.
Exclude unnecessary files such as:
-
node_modules -
.gitfolder -
testsdirectory (optional)
Create a zip of your application containing all necessary files and folders, ensuring the index.php inside the public folder is accessible as the entry point.
Step 4: Configuring the Document Root
By default, Elastic Beanstalk expects the application’s root directory to contain the main index.php file. However, Laravel’s entry point is located inside the public directory.
To fix this, create a file named .ebextensions/laravel.config in your project root with the following configuration:
This ensures Elastic Beanstalk serves your Laravel application from the correct folder.
Step 5: Deploying Your Application
There are two main ways to deploy your Laravel app: via the AWS Management Console or the AWS CLI.
a. Deploy Using AWS Console
-
Go to your Elastic Beanstalk Application Dashboard.
-
Click Upload and Deploy.
-
Choose your zip file and click Deploy.
Elastic Beanstalk will automatically provision EC2 instances, load balancers, and storage based on your configuration.
b. Deploy Using AWS CLI
If you prefer command-line deployment, navigate to your project folder and run:
This method is faster for future updates and integrates well with CI/CD pipelines.
Step 6: Setting Environment Variables
Instead of hardcoding credentials in your .env file, set them securely in Elastic Beanstalk.
-
Open your Elastic Beanstalk Environment Dashboard.
-
Go to Configuration → Software.
-
Add environment variables like:
-
APP_ENV=production -
APP_KEY=your-app-key -
DB_HOST=your-database-endpoint -
DB_DATABASE=your-database-name -
DB_USERNAME=your-username -
DB_PASSWORD=your-password
-
This keeps your credentials safe and allows easy configuration changes.
Step 7: Connecting to a Database (RDS Integration)
AWS RDS (Relational Database Service) is ideal for hosting Laravel’s MySQL or PostgreSQL databases.
a. Create an RDS Instance
-
Navigate to RDS in AWS Console.
-
Create a new database (MySQL or PostgreSQL).
-
Choose the same region as your Elastic Beanstalk app.
-
Configure username, password, and database name.
b. Link RDS to Your Application
Update your Elastic Beanstalk environment variables with RDS credentials. Laravel will automatically connect to the RDS database via the .env values.
Step 8: Managing Storage and File Uploads
Laravel applications often require file uploads (e.g., images, PDFs). Instead of storing them locally on EC2, use Amazon S3.
a. Configure S3 Disk
In your config/filesystems.php, set the default disk to S3 and provide credentials in your environment variables.
This ensures all uploads go directly to S3, improving scalability and reliability.
Step 9: Enabling HTTPS with AWS Certificate Manager
Security is a priority in production environments. To enable HTTPS:
-
Go to AWS Certificate Manager (ACM).
-
Request a new SSL certificate for your domain.
-
Attach the certificate to your Elastic Beanstalk load balancer.
Now your Laravel application will be accessible securely over HTTPS.
Step 10: Monitoring and Scaling
Elastic Beanstalk includes built-in monitoring and scaling tools.
a. Monitoring
You can view metrics like CPU usage, latency, and request counts directly from the AWS console.
b. Auto Scaling
Elastic Beanstalk automatically scales your application based on traffic. You can configure minimum and maximum instance counts to control cost and performance.
Step 11: Managing Logs and Errors
Logs are essential for debugging. Elastic Beanstalk provides access to application and server logs directly from the console.
You can also integrate CloudWatch Logs for advanced monitoring and automated alerts.
Best Practices for Laravel on Elastic Beanstalk
-
Use queues: Offload heavy background jobs to SQS workers.
-
Cache aggressively: Utilize Redis or Memcached for caching sessions and queries.
-
Optimize database queries: Avoid N+1 problems using Eloquent’s eager loading.
-
Regular backups: Schedule automated RDS backups.
-
Use environment variables: Never store credentials directly in your code.
Common Deployment Issues and Fixes
1. White Screen or 500 Error
Usually caused by incorrect document root or missing .env configuration. Double-check the document_root setting and ensure your .env is correctly loaded.
2. Storage Permission Denied
Set proper permissions for storage and bootstrap/cache directories before zipping your project.
3. Missing PHP Extensions
Elastic Beanstalk’s default PHP platform includes common extensions, but if you need more, you can install them using .ebextensions.
Conclusion
Deploying Laravel on AWS Elastic Beanstalk offers an elegant mix of automation, scalability, and reliability. You can go from a local Laravel project to a globally accessible, production-ready application in a matter of minutes — without managing complex infrastructure.
By following these steps and best practices, your Laravel app will be optimized, secure, and ready to scale as your user base grows.
If you’re looking for a deployment solution that balances simplicity with power, AWS Elastic Beanstalk is the perfect fit for modern Laravel applications.