How to Deploy Locally Developed Laravel Projects to AWS: A Step-by-Step Guide

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

  1. Login to AWS Management Console: Go to AWS Management Console.
  2. Navigate to IAM: In the services menu, find and select “IAM” (Identity and Access Management).
  3. Create a New User:
    • Click on “Users” and then “Add user”.
    • Provide a username (e.g., laravel-deploy).
    • Select “Programmatic access” for access type.
  4. Set Permissions:
    • Click “Attach existing policies directly”.
    • Search for AdministratorAccess and select it.
  5. Review and Create:
    • Review the settings and click “Create user”.
    • Note the Access key ID and Secret access key (you’ll need these later).
See also  Dealing with Legacy Databases in Laravel

Setting Up EC2

  1. Launch an EC2 Instance:
    • Go to “EC2” in the AWS Management Console.
    • Click “Launch Instance”.
  2. Choose an Amazon Machine Image (AMI):
    • Select Amazon Linux 2 AMI (preferred for its simplicity and compatibility).
  3. Choose an Instance Type:
    • Select t2.micro (sufficient for small to medium Laravel applications).
  4. Configure Instance Details:
    • Leave the default settings unless you have specific requirements.
  5. Add Storage:
    • The default 8 GB is usually sufficient, but you can increase it if needed.
  6. Add Tags:
    • Add a tag with Key Name and Value LaravelServer.
  7. 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.
  8. 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.
  9. 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

Setting Up RDS

  1. Create an RDS Instance:
    • Go to “RDS” in the AWS Management Console.
    • Click “Create database”.
  2. Choose a Database Creation Method:
    • Select “Standard Create”.
  3. Engine Options:
    • Choose “MySQL”.
  4. DB Instance Class:
    • Select db.t2.micro.
  5. Settings:
    • Provide a DB instance identifier (e.g., laravel-db).
    • Set a master username and password.
  6. Connectivity:
    • Ensure your EC2 instance can access this RDS instance. Configure the security group accordingly.
  7. Additional Configuration:
    • Set the initial database name (e.g., laravel).
  8. 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.

See also  Caching in Laravel: A Comprehensive Guide

Deploying Your Project

Transferring Files

  1. Zip Your Project: Exclude the vendor and node_modules directories to reduce file size.
    sh zip -r laravel-project.zip . -x "vendor/*" -x "node_modules/*"
  2. 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

  1. Install Apache and PHP: sudo yum install httpd php php-mysqlnd -y sudo systemctl start httpd sudo systemctl enable httpd
  2. 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>
  3. Restart Apache:
    sh sudo systemctl restart httpd

Laravel Configuration

  1. Generate Application Key: php artisan key:generate
  2. Run Migrations: php artisan migrate --force
  3. 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

  1. Create an S3 Bucket:
    • Go to “S3” in the AWS Management Console.
    • Create a new bucket (e.g., laravel-storage).
  2. 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 the config/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

  1. Install Certbot: sudo yum install -y certbot python2-certbot-apache
  2. **
See also  Part 5: Data Management and Storage Optimization

Obtain SSL Certificate**:
sh sudo certbot --apache -d your-domain.com -d www.your-domain.com

  1. Auto-Renewal: sudo crontab -e Add the following line to renew the certificate automatically: 0 0 * * * /usr/bin/certbot renew --quiet

Setting Up CloudFront

  1. 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).
  2. Configure Distribution Settings:
    • Enable “Redirect HTTP to HTTPS”.
    • Set the “Default Root Object” to index.html.
  3. 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

  1. Enable Detailed Monitoring:
    • Go to “EC2” in the AWS Management Console.
    • Select your instance and enable detailed monitoring.
  2. Create Alarms:
    • Go to “CloudWatch” in the AWS Management Console.
    • Create alarms for CPU usage, disk space, etc.

Backups

  1. Automate RDS Backups:
    • Go to “RDS” in the AWS Management Console.
    • Enable automated backups for your RDS instance.
  2. 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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.