Building a system for your Laravel application on AWS involves several steps, including configuring your VPC, launching EC2 instances, setting up MySQL and Redis, configuring security, and deploying the application. Here's a comprehensive guide to set up a full Laravel application system on AWS with a custom load balancer:
Step-by-Step Guide: Building a Laravel Application System on AWS
1. Set Up the VPC (Virtual Private Cloud)
The first step is to create a Virtual Private Cloud (VPC) where all your AWS resources will reside.
Create the VPC
Navigate to the VPC Dashboard:
- In the AWS Management Console, go to Services → VPC.
Create VPC:
- Click Create VPC.
- For IPv4 CIDR block, choose something like
10.0.0.0/16. - For IPv6 CIDR block, choose No IPv6 CIDR Block unless you need IPv6.
- Set Tenancy to Default (unless you need dedicated instances).
- Name your VPC (e.g.,
laravel-vpc).
Create Subnets:
- Create at least two subnets:
- Public Subnet (e.g.,
10.0.1.0/24) for your load balancer and web server (Laravel). - Private Subnet (e.g.,
10.0.2.0/24) for MySQL and Redis.
Create an Internet Gateway:
- Go to Internet Gateways and click Create Internet Gateway.
- Attach it to your VPC (
laravel-vpc).
Route Tables:
- Create and associate route tables for both public and private subnets:
- Public Route Table should have a route to the Internet Gateway.
- Private Route Table should have no route to the internet (for internal communication).
Navigate to the VPC Dashboard:
- In the AWS Management Console, go to Services → VPC.
Create VPC:
- Click Create VPC.
- For IPv4 CIDR block, choose something like
10.0.0.0/16. - For IPv6 CIDR block, choose No IPv6 CIDR Block unless you need IPv6.
- Set Tenancy to Default (unless you need dedicated instances).
- Name your VPC (e.g.,
laravel-vpc).
Create Subnets:
- Create at least two subnets:
- Public Subnet (e.g.,
10.0.1.0/24) for your load balancer and web server (Laravel). - Private Subnet (e.g.,
10.0.2.0/24) for MySQL and Redis.
- Public Subnet (e.g.,
Create an Internet Gateway:
- Go to Internet Gateways and click Create Internet Gateway.
- Attach it to your VPC (
laravel-vpc).
Route Tables:
- Create and associate route tables for both public and private subnets:
- Public Route Table should have a route to the Internet Gateway.
- Private Route Table should have no route to the internet (for internal communication).
2. Set Up EC2 Instances
You’ll need to launch several EC2 instances, including the load balancer, Laravel application instances, MySQL, and Redis.
Launch EC2 Instance for Load Balancer (Optional)
If you want to create a custom load balancer using EC2 (instead of AWS ALB):
Launch EC2 Instance for Load Balancer:
- Go to EC2 Dashboard → Launch Instance.
- Choose an AMI (e.g., Amazon Linux 2 or Ubuntu).
- Choose an instance type (e.g.,
t2.microfor testing). - Set security group rules to allow HTTP (80), HTTPS (443), and SSH (22) from trusted IPs.
Install and Configure Load Balancer Software (e.g., Nginx or HAProxy):
- SSH into the instance and install a reverse proxy like Nginx or HAProxy.
- Example for Nginx:
- Example for Nginx:
- Configure it to load balance between your backend Laravel instances.
- SSH into the instance and install a reverse proxy like Nginx or HAProxy.
Launch EC2 Instances for Laravel Application
Launch Two EC2 Instances for Laravel:
- These will run your Laravel application and handle incoming traffic.
- Follow the same steps as for the load balancer, but choose the public subnet for these instances.
- Set security group rules to allow HTTP (80) and HTTPS (443) from your load balancer's IP or security group.
Install Laravel Dependencies:
- SSH into the Laravel EC2 instances and install PHP, Composer, and other dependencies:
- Clone your Laravel project from your Git repository or upload the project files.
Configure Apache/Nginx:
- Set up Apache or Nginx to serve your Laravel app.
Configure Laravel Environment:
- Update the
.env file in Laravel with your MySQL and Redis configurations:
Launch Two EC2 Instances for Laravel:
- These will run your Laravel application and handle incoming traffic.
- Follow the same steps as for the load balancer, but choose the public subnet for these instances.
- Set security group rules to allow HTTP (80) and HTTPS (443) from your load balancer's IP or security group.
Install Laravel Dependencies:
- SSH into the Laravel EC2 instances and install PHP, Composer, and other dependencies:
- Clone your Laravel project from your Git repository or upload the project files.
Configure Apache/Nginx:
- Set up Apache or Nginx to serve your Laravel app.
Configure Laravel Environment:
- Update the
.envfile in Laravel with your MySQL and Redis configurations:
Launch EC2 Instance for MySQL Database
Launch EC2 Instance for MySQL:
- Choose an Ubuntu or Amazon Linux 2 AMI for MySQL.
- Assign it to the private subnet.
- Set security group rules to only allow access from your Laravel instances (on port 3306).
Install MySQL:
- SSH into the MySQL instance and install MySQL:
Create MySQL Database and User:
- Connect to MySQL:
- Create a database and user:
Launch EC2 Instance for MySQL:
- Choose an Ubuntu or Amazon Linux 2 AMI for MySQL.
- Assign it to the private subnet.
- Set security group rules to only allow access from your Laravel instances (on port 3306).
Install MySQL:
- SSH into the MySQL instance and install MySQL:
Create MySQL Database and User:
- Connect to MySQL:
- Create a database and user:
Launch EC2 Instance for Redis
Launch EC2 Instance for Redis:
- Launch another EC2 instance in the private subnet.
- Set security group rules to allow access to port 6379 only from the Laravel instances.
Install Redis:
- SSH into the Redis instance and install Redis:
Configure Redis:
- Edit the Redis configuration file (
/etc/redis/redis.conf) to bind Redis to the private IP and set it to protected mode:
Start Redis:
- Restart Redis to apply the changes:
Launch EC2 Instance for Redis:
- Launch another EC2 instance in the private subnet.
- Set security group rules to allow access to port 6379 only from the Laravel instances.
Install Redis:
- SSH into the Redis instance and install Redis:
Configure Redis:
- Edit the Redis configuration file (
/etc/redis/redis.conf) to bind Redis to the private IP and set it to protected mode:
Start Redis:
- Restart Redis to apply the changes:
3. Set Up Security Groups
Security Group for Load Balancer:
- Open HTTP (80) and HTTPS (443) for public access.
- Allow SSH (22) from trusted IPs for management.
Security Group for Laravel EC2 Instances:
- Allow HTTP (80) and HTTPS (443) from the load balancer’s security group.
- Allow MySQL (3306) and Redis (6379) only from the private subnet or specific IPs.
Security Group for MySQL and Redis EC2 Instances:
- Allow MySQL (3306) from Laravel EC2 instances.
- Allow Redis (6379) from Laravel EC2 instances.
Security Group for Load Balancer:
- Open HTTP (80) and HTTPS (443) for public access.
- Allow SSH (22) from trusted IPs for management.
Security Group for Laravel EC2 Instances:
- Allow HTTP (80) and HTTPS (443) from the load balancer’s security group.
- Allow MySQL (3306) and Redis (6379) only from the private subnet or specific IPs.
Security Group for MySQL and Redis EC2 Instances:
- Allow MySQL (3306) from Laravel EC2 instances.
- Allow Redis (6379) from Laravel EC2 instances.
4. Configure Load Balancer (Optional)
If you're using a custom EC2 instance for load balancing:
- Configure the Reverse Proxy (Nginx or HAProxy):
- Point the reverse proxy to your Laravel EC2 instances.
- Example for Nginx:
5. Final Testing and Deployment
Test Load Balancer:
- Access the public IP of your load balancer (if using EC2-based load balancer) or your VPC’s public IP to check if Laravel is working.
Test Database and Redis:
- Ensure Laravel is able to connect to MySQL and Redis using the configured private IP addresses.
Deploy SSL:
- If you want to serve your Laravel application over HTTPS, consider setting up SSL on your load balancer or web server (e.g., using Let’s Encrypt for a free SSL certificate).
Test Load Balancer:
- Access the public IP of your load balancer (if using EC2-based load balancer) or your VPC’s public IP to check if Laravel is working.
Test Database and Redis:
- Ensure Laravel is able to connect to MySQL and Redis using the configured private IP addresses.
Deploy SSL:
- If you want to serve your Laravel application over HTTPS, consider setting up SSL on your load balancer or web server (e.g., using Let’s Encrypt for a free SSL certificate).
6. Auto-Scaling and Monitoring
Set Up Auto Scaling:
- Use AWS Auto Scaling to scale your Laravel EC2 instances based on traffic.
- Set the desired capacity, minimum, and maximum instances in your Auto Scaling Group.
Set Up Monitoring:
- Use AWS CloudWatch to monitor the health and performance of your EC2 instances.
- Set up alarms for key metrics (e.g., CPU usage, memory, disk space).
Set Up Auto Scaling:
- Use AWS Auto Scaling to scale your Laravel EC2 instances based on traffic.
- Set the desired capacity, minimum, and maximum instances in your Auto Scaling Group.
Set Up Monitoring:
- Use AWS CloudWatch to monitor the health and performance of your EC2 instances.
- Set up alarms for key metrics (e.g., CPU usage, memory, disk space).
7. Backup and Security
Backup MySQL:
- Regularly back up your MySQL database using AWS RDS snapshots or EC2 instance backups.
Ensure Security Best Practices:
- Use IAM roles for EC2 instances for secure access to AWS services (e.g., S3, Secrets Manager).
- Enable Multi-AZ for MySQL if using RDS or set up replication for fault tolerance.
Backup MySQL:
- Regularly back up your MySQL database using AWS RDS snapshots or EC2 instance backups.
Ensure Security Best Practices:
- Use IAM roles for EC2 instances for secure access to AWS services (e.g., S3, Secrets Manager).
- Enable Multi-AZ for MySQL if using RDS or set up replication for fault tolerance.
Conclusion
This guide takes you through the full process of setting up a Laravel application system on AWS
Code Python
Full Python Code to Build the System
Key Components in the Script
- VPC Setup: Creates a VPC, subnets, and an internet gateway.
- Security Groups: Creates a security group for EC2 instances.
- EC2 Instances: Creates EC2 instances for the Load Balancer and Laravel application.
- Load Balancer: Creates an Application Load Balancer (ALB).
- MySQL: Creates an RDS MySQL instance for the database.
- Redis: Creates an ElastiCache Redis instance.
Full Python Code to Delete Resources
Key Functions in the Code
- Delete EC2 Instances: Terminates all the EC2 instances (both Load Balancer and Laravel app instances).
- Delete Load Balancer: Deletes the Application Load Balancer (ALB) using its ARN.
- Delete RDS MySQL Instance: Deletes the MySQL RDS instance.
- Delete Redis Instance: Deletes the Redis ElastiCache cluster.
- Delete Security Group: Deletes the security group associated with the EC2 instances.
- Delete VPC: Deletes the VPC and all associated resources like subnets and Internet Gateway.
Thank you.
No comments:
Post a Comment