How to Set Up and Deploy Laravel Applications on AWS
May 20, 2023 08:42 · 1390 words · 7 minute read
Introduction
This guide will walk you through the process of setting up and deploying Laravel (a PHP framework) applications on AWS (Amazon Web Services). It covers the steps for creating and configuring the necessary AWS services, building CI/CD pipelines, and deploying the applications to EC2 instances and using S3 buckets for storage assets.
Please refer to the following sections for detailed instructions:
- Introduction
- Key Terms, Tools, and Services
- Step-by-Step Guide
- Step 1: Set Up an S3 Bucket for Your Laravel Application
- Step 2: Create a Laravel API
- Step 3: Set up an EC2 Instance
- Step 4: Install PHP, Composer, and Nginx on EC2
- Step 5: Install and Configure MySQL (If Needed)
- Step 6: Clone and Configure Your Laravel Application
- Step 7: Configure Nginx for Your Laravel Application
- Step 8: Set up AWS Code Pipeline for Laravel
- Step 9: Update Your Laravel Application on local machine
- Step 10: Monitor and Scale Your Applications
- Step 11: Security Best Practices
Key Terms, Tools, and Services
AWS (Amazon Web Services): AWS is a cloud services platform by Amazon that provides a wide range of cloud services such as computing power, database storage, content delivery, and other functionalities.
EC2 (Elastic Compute Cloud): EC2 is a service provided by AWS that offers secure, resizable compute capacity in the cloud. It’s designed to make web-scale cloud computing easier for developers.
Laravel: Laravel is a popular open-source PHP framework used for web application development following the MVC (Model View Controller) architectural pattern.
S3 (Simple Storage Service): Amazon S3 is a scalable object storage service by AWS. It’s designed for storing and retrieving any amount of data at any time, from anywhere on the web.
AWS CodePipeline: AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates.
AWS CodeBuild: AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy.
Nginx: Nginx is a popular open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache.
MySQL: MySQL is a widely used open-source relational database management system.
Step-by-Step Guide
Step 1: Set Up an S3 Bucket for Your Laravel Application
- Go to the S3 section of the AWS Management Console.
- Click on “Create bucket” and provide a unique name for your bucket.
- Configure the bucket settings as per your requirements.
- Click on “Create bucket” to create the bucket.
- Copy the bucket name and region for later use.
Step 2: Create a Laravel API
In your local machine, create a new Laravel project:
composer create-project --prefer-dist laravel/laravel test-api
Navigate to the new Laravel application directory: cd test-api
Configure the s3 bucket connection in the
.env.example
file.
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_DEFAULT_REGION=your_default_region
AWS_BUCKET=your_bucket_name
- copy the .env.example file to .env:
cp .env.example .env
- Configure the database connection (if needed) and the s3 bucket connection in the
.env
file. - Run:
composer install
php artisan key:generate
php artisan migrate
php artisan serve
- Test the application by visiting http://localhost:8000 in your browser.
Step 3: Set up an EC2 Instance
- Log into your AWS Management Console and select EC2 from the list of services.
- Click on “Launch Instance” and select an Amazon Machine Image (AMI). For Laravel, you can use an Amazon Linux 2 AMI.
- Choose an instance type based on your needs and click “Next: Configure Instance Details”.
- Configure instance details as per your requirements and click “Next: Add Storage”.
- Add storage as per your needs and click “Next: Add Tags”.
- Add tags if required and click “Next: Configure Security Group”.
- Configure a security group. Make sure to open ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
- Review your instance configuration and click “Launch”.
- You’ll be prompted to create a new key pair or use an existing one. This is important for connecting to your instance. Download the key pair and keep it safe.
Step 4: Install PHP, Composer, and Nginx on EC2
Connect to your EC2 instance using SSH.
ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-ip
Update the package lists for upgrades and new package installations:
sudo apt update
Install PHP:
sudo apt install -y php
Install Composer:
sudo apt install -y composer
Install Nginx:
sudo apt install -y nginx
Step 5: Install and Configure MySQL (If Needed)
- Install MySQL:
sudo apt install -y mysql-server
- Secure your MySQL installation:
sudo mysql_secure_installation
- Follow the prompts to set a root password and secure your installation.
Step 6: Clone and Configure Your Laravel Application
- cd
/var/www/
- Clone your Laravel application repository to your EC2 instance.
1.1. Make sure to add your ssh key to your github account.
cat ~/.ssh/id_rsa.pub
1.2. If you’re using GitHub, you can clone your repository using SSH:git clone {your-repository-ssh-url}
- Navigate to the Laravel application directory:
cd /path/to/laravel
- Install the Laravel dependencies using Composer:
composer install
- Copy the
.env.example
file to.env
:cp .env.example .env
- Generate the application key for Laravel:
php artisan key:generate
- Configure the database connection in the
.env
file using the credentials you set up in Step 3. - Configure the s3 bucket connection in the
.env
file.
Step 7: Configure Nginx for Your Laravel Application
- Open the Nginx configuration file for editing:
sudo nano /etc/nginx/sites-available/default
- Update the configuration file with the following content:
server {
listen 80;
server_name your-domain.com;
root /path/to/laravel/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
- Save the file and exit the editor.
- Restart Nginx to apply the changes:
sudo systemctl restart nginx
- Open your domain in your browser to test your Laravel application using ec2 public ip.
http://your-instance-public-ip
Step 8: Set up AWS Code Pipeline for Laravel
- Go to the AWS Management Console and open the AWS CodePipeline console.
- Click “Create pipeline”.
- Enter a name for your pipeline and click “Next”.
- For “Source provider”, select “GitHub” and connect your GitHub account. Then, select your Laravel repository and the branch you want to use.
- For “Build provider”, choose AWS CodeBuild. Click “Create project” and configure it to use the buildspec.yml file in your repository. Here’s a sample buildspec.yml for Laravel:
version: 0.2
phases:
install:
runtime-versions:
php: 8.2
commands:
- echo Installing source code...
- composer install
pre_build:
commands:
- echo Pre-build steps...
- php artisan migrate --force
build:
commands:
- echo Build started on `date`
- echo Compiling the Laravel code...
- php artisan optimize
- composer dump-autoload
post_build:
commands:
- echo Build completed on `date`
- For “Deploy provider”, choose “Amazon EC2”. Configure it to deploy your Laravel application to your EC2 instance.
- Review your pipeline configuration and click “Create pipeline”.
Step 9: Update Your Laravel Application on local machine
- Open the routes/api.php file.
- Add the following code to create a new API endpoint:
use Illuminate\Support\Facades\Storage;
Route::get('/message', function () {
$url = Storage::disk('s3')->url('filename.txt');
return response()->json(['message' => 'This is from Laravel API', 'fileUrl' => $url]);
});
- Run the following command to start the Laravel development server:
php artisan serve
- Open the URL in your browser to test the API endpoint
http://localhost:8000/api/message
- You should see the following response:
{
"message": "This is from Laravel API",
"fileUrl": "https://your-bucket-name.s3.your-region.amazonaws.com/filename.txt"
}
- Last but not least, commit your changes and push them to your GitHub repository.
- Go to the AWS CodePipeline console and open your pipeline.
- Now you can see the pipeline in action. It will automatically deploy your Laravel application to your EC2 instance.
Step 10: Monitor and Scale Your Applications
- Set up monitoring for your EC2 instances and S3 buckets using AWS CloudWatch.
- Configure alarms and notifications for important metrics.
- Monitor the performance and health of your applications using CloudWatch dashboards and logs.
- Implement auto-scaling to handle increasing traffic and demand.
Step 11: Security Best Practices
- Use SSL/TLS certificates to secure your applications. You can use AWS Certificate Manager to provision and manage certificates.
- Regularly update your software and apply security patches to keep your applications and infrastructure secure.
- Follow AWS security best practices for EC2 instances, S3 buckets, and other services you use.
- Implement proper access controls and permissions to restrict unauthorized access to your applications and resources.
Remember to continuously monitor and improve your applications, keep them up to date with the latest security patches, and follow best practices for development and operations.
If you have any questions or encounter any issues, please feel free to reach out to me on Twitter
Happy deploying!