What is Laravel?
Laravel is a free and open source PHP framework that provides a set of tools and resources for building PHP applications. It is known for its ease of use, provides extensive features and a complete ecosystem of packages and extensions. For example, it provides useful functions like token based authentication, unit testing, and file upload out of the box
Laravel provides powerful database tools including an Object Relational Mapper (ORM) called Eloquent and built-in mechanisms for database migration and seed generation. The command-line tool Artisan allows developers to bootstrap new models, multiple controllers, and other pre-built application components.
Why Choose AWS For Your Web Application?
AWS has a range of services and features that can help you build, host, and manage web applications:
- AWS offers a set of serverless features including AWS Lambda, AWS Step Functions, Amazon API Gateway, and AWS Fargate, allowing developers to offload simple event-based tasks from their applications.
- AWS developer tools support DevOps practices and enable infrastructure and deployment automation. AWS supports elastic and continuous delivery from build to commit to release.
- AWS provides a cloud-based development environment using AWS Cloud9.
Pricing for Amazon services depends on the service you use. You can calculate this price in several ways, but the most important is the time you use it—in general, you only pay for what you use. In addition Amazon charges for storage space, and ingress/egress bandwidth into or out of Amazon, or between Amazon regions. You can use an Amazon calculator to estimate total costs for all resources when deploying your app to AWS.
Quick Tutorial: Deploying a Laravel Application to Elastic Beanstalk
Step 1: Launch an Elastic Beanstalk Environment
To create an environment for your Laravel app:
- Open the Elastic Beanstalk console using this link (contains appropriate configuration for the tutorial).
- Under Platform, select PHP.
- Under Application code, select Sample application. Give your app a unique name.
- Click Review and launch, then Create app.
Step 2: Install Laravel and Create a Sample Web Application
Use Composer to install Laravel and dependencies, and create a default project:
~$ composer create-project --prefer-dist laravel/laravel eb-laravel
Step 3: Deploy the Application to AWS
To deploy the application via Elastic Beanstalk:
- Create an Elastic Beanstalk source bundle with all the files in the default Laravel project.
- You can use this command to create a source bundle called laravel-default.zip, excluding the vendor folder which is not necessary for application deployment:
zip ../laravel-default.zip -r * .[^.]* -x "vendor/*"
- Open the Elastic Beanstalk console and select your Amazon region in the Regions selector.
- In the sidebar, click Environments and choose then choose the name of the app you created in step 1 above.
- Click Upload and deploy, select the source bundle in the local file system, and click Deploy.
- Wait for the deployment to complete and click the website URL to open the website in your browser.
You should now see this message:
Forbidden. You don’t have permission to access / on this server
This is normal, it is because Elastic Beanstalk serves the root of your server, while the index.php file is in the public folder.
Step 4: Configure Composer Settings to Serve Public Folder
To serve the Laravel application as the root of the web server:
- Open the Elastic Beanstalk console, select the Amazon Region, click Environments and choose the name of the app you created in step 1.
- From the navigation pane, select Configuration > Software configuration > Edit.
- Under Document Root, type /public and click Apply.
- Click the URL again to open the site in the browser, it should work now:
Image Source: AWS
Step 5: Add a Database
Launch a Relational Database Service (RDS) MySQL database within the Elastic Beanstalk environment:
- Open the Elastic Beanstalk console, select Amazon Region, click Environments and select your app.
- From the navigation pane, select Configuration > Database configuration > Edit.
- Under Engine, select mysql.
- Type a username and password—these will be the credentials of the database. They will be passed to your application via environment variables.
- Click Apply.
It will take about ten minutes for the database to be provisioned.
Step 5: Deploy Updated Application
To update your Elastic Beanstalk environment with the database:
- Create a new source bundle using the same command as in step 3 above.
- Open the Elastic Beanstalk console, select Amazon Region, click Environments, and choose the name of your app.
- From the navigation pane, select Upload and Deploy > Browse, locate your new bundle in the file system, and click Deploy.
- When the deployment finishes, refresh the web page and you should see the connection to the new database:
In this article, I explained how AWS supports the Laravel framework and showed how to deploy your first Laravel application to the cloud in 5 steps:
- Launch an Elastic Beanstalk environment in your AWS account.
- Install Laravel on the local machine and create a sample web application.
- Create a source bundle containing your application and deploy it via Elastic Beanstalk.
- Configure Laravel composer settings to serve the public folder.
- Add a MySQL Database within the Elastic Beanstalk environment.
- Deploy the updated application with a database connection.
That’s it! You now know how to set up an entire Laravel application stack on AWS with just a few clicks.