Deploying a Laravel project from your local development environment to AWS can seem daunting, but with the right steps and configurations, it becomes manageable. This comprehensive guide will walk you through every step, from setting up your AWS environment to configuring your Laravel application for deployment.
Introduction
Deploying a Laravel project to AWS involves several steps, including setting up an EC2 instance, configuring a database with RDS, transferring your files, setting up a web server, and more. This guide will cover each step in detail to help you successfully deploy your Laravel application.
Prerequisites
Before we begin, ensure you have the following:
- An AWS account
- Basic understanding of AWS services
- A locally developed Laravel project
- SSH client (e.g., PuTTY for Windows or Terminal for macOS/Linux)
Setting Up AWS
Creating an IAM User
- Login to AWS Management Console: Go to AWS Management Console.
- Navigate to IAM: In the services menu, find and select “IAM” (Identity and Access Management).
- Create a New User:
- Click on “Users” and then “Add user”.
- Provide a username (e.g.,
laravel-deploy
). - Select “Programmatic access” for access type.
- Set Permissions:
- Click “Attach existing policies directly”.
- Search for
AdministratorAccess
and select it.
- Review and Create:
- Review the settings and click “Create user”.
- Note the
Access key ID
andSecret access key
(you’ll need these later).
Setting Up EC2
- Launch an EC2 Instance:
- Go to “EC2” in the AWS Management Console.
- Click “Launch Instance”.
- Choose an Amazon Machine Image (AMI):
- Select
Amazon Linux 2 AMI
(preferred for its simplicity and compatibility).
- Select
- Choose an Instance Type:
- Select
t2.micro
(sufficient for small to medium Laravel applications).
- Select
- Configure Instance Details:
- Leave the default settings unless you have specific requirements.
- Add Storage:
- The default 8 GB is usually sufficient, but you can increase it if needed.
- Add Tags:
- Add a tag with Key
Name
and ValueLaravelServer
.
- Add a tag with Key
- Configure Security Group:
- Create a new security group or select an existing one.
- Add the following rules:
- SSH (port 22) for your IP address.
- HTTP (port 80) for all IP addresses.
- HTTPS (port 443) for all IP addresses.
- Review and Launch:
- Review the settings and click “Launch”.
- Create a new key pair, download it (e.g.,
laravel-key.pem
), and keep it secure.
- Connect to Your Instance:
- Use the
.pem
file to SSH into your instance:sh ssh -i "laravel-key.pem" ec2-user@your-ec2-public-ip
- Use the
Setting Up RDS
- Create an RDS Instance:
- Go to “RDS” in the AWS Management Console.
- Click “Create database”.
- Choose a Database Creation Method:
- Select “Standard Create”.
- Engine Options:
- Choose “MySQL”.
- DB Instance Class:
- Select
db.t2.micro
.
- Select
- Settings:
- Provide a DB instance identifier (e.g.,
laravel-db
). - Set a master username and password.
- Provide a DB instance identifier (e.g.,
- Connectivity:
- Ensure your EC2 instance can access this RDS instance. Configure the security group accordingly.
- Additional Configuration:
- Set the initial database name (e.g.,
laravel
).
- Set the initial database name (e.g.,
- Create Database:
- Review the settings and click “Create database”.
Configuring Your Local Laravel Project
Database Configuration
Update your .env
file with your RDS database credentials:
DB_CONNECTION=mysql
DB_HOST=your-rds-endpoint
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=your-master-username
DB_PASSWORD=your-master-password
Other Environment Variables
Set other necessary environment variables in your .env
file, such as APP_ENV
, APP_DEBUG
, APP_URL
, etc.
Deploying Your Project
Transferring Files
- Zip Your Project: Exclude the
vendor
andnode_modules
directories to reduce file size.sh zip -r laravel-project.zip . -x "vendor/*" -x "node_modules/*"
- Transfer the Zip File: Use
scp
or an SFTP client to transfer the zip file to your EC2 instance.sh scp -i "laravel-key.pem" laravel-project.zip ec2-user@your-ec2-public-ip:/home/ec2-user
Setting Up Environment Variables
After transferring your files, SSH into your EC2 instance and unzip the project:
ssh -i "laravel-key.pem" ec2-user@your-ec2-public-ip
unzip laravel-project.zip -d /var/www/html
Installing Dependencies
Navigate to your project directory and install the PHP dependencies:
cd /var/www/html
sudo yum install php-cli php-zip unzip -y
composer install --optimize-autoloader --no-dev
Setting Up Web Server
- Install Apache and PHP:
sudo yum install httpd php php-mysqlnd -y sudo systemctl start httpd sudo systemctl enable httpd
- Configure Apache:
Create a new virtual host file for your Laravel project:sudo nano /etc/httpd/conf.d/laravel.conf
Add the following configuration:<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/public <Directory /var/www/html/public> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- Restart Apache:
sh sudo systemctl restart httpd
Laravel Configuration
- Generate Application Key:
php artisan key:generate
- Run Migrations:
php artisan migrate --force
- Set Permissions:
sh sudo chown -R apache:apache /var/www/html sudo chmod -R 775 /var/www/html/storage sudo chmod -R 775 /var/www/html/bootstrap/cache
Additional Configurations
Setting Up S3 for File Storage
- Create an S3 Bucket:
- Go to “S3” in the AWS Management Console.
- Create a new bucket (e.g.,
laravel-storage
).
- Configure Laravel to Use S3:
Update your.env
file with your S3 credentials:FILESYSTEM_DRIVER=s3 AWS_ACCESS_KEY_ID=your-access-key-id AWS_SECRET_ACCESS_KEY=your-secret-access-key AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET=laravel-storage
Update theconfig/filesystems.php
file:'s3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), ],
Configuring SSL
- Install Certbot:
sudo yum install -y certbot python2-certbot-apache
- **
Obtain SSL Certificate**:sh sudo certbot --apache -d your-domain.com -d www.your-domain.com
- Auto-Renewal:
sudo crontab -e
Add the following line to renew the certificate automatically:0 0 * * * /usr/bin/certbot renew --quiet
Setting Up CloudFront
- Create a CloudFront Distribution:
- Go to “CloudFront” in the AWS Management Console.
- Create a new distribution.
- Set the origin domain name to your S3 bucket (e.g.,
laravel-storage.s3.amazonaws.com
).
- Configure Distribution Settings:
- Enable “Redirect HTTP to HTTPS”.
- Set the “Default Root Object” to
index.html
.
- Update Your Laravel Configuration:
Update your.env
file to include your CloudFront URL:env AWS_URL=https://your-cloudfront-url.cloudfront.net
Monitoring and Maintenance
Setting Up CloudWatch
- Enable Detailed Monitoring:
- Go to “EC2” in the AWS Management Console.
- Select your instance and enable detailed monitoring.
- Create Alarms:
- Go to “CloudWatch” in the AWS Management Console.
- Create alarms for CPU usage, disk space, etc.
Backups
- Automate RDS Backups:
- Go to “RDS” in the AWS Management Console.
- Enable automated backups for your RDS instance.
- Backup S3 Data:
- Enable versioning on your S3 bucket.
- Set up lifecycle rules to manage old versions and backups.
Conclusion
Deploying a Laravel project to AWS involves multiple steps, including setting up an EC2 instance, configuring RDS, transferring files, setting up the web server, and making additional configurations for storage and security. By following this comprehensive guide, you can ensure a smooth deployment process and maintain a robust and scalable Laravel application on AWS.