How to: Deploy Laravel Sail application to an AWS EC2 instance Emman, November 11, 2023November 11, 2023 Deploying a Laravel Sail application to an AWS EC2 instance involves several steps, including setting up the EC2 instance, configuring Sail, and deploying your Laravel application. Here’s a step-by-step guide with detailed explanations and examples: 1. Create an AWS EC2 Instance: 1.1. Log in to your AWS Management Console. 1.2. Navigate to the EC2 Dashboard. 1.3. Click “Launch Instance” to create a new EC2 instance. 1.4. Choose an Amazon Machine Image (AMI), preferably a recent version of Amazon Linux or Ubuntu. 1.5. Select an instance type, configure instance details, add storage, and configure security groups to allow incoming traffic on ports 22 (SSH) and 80/443 (HTTP/HTTPS). 1.6. Review and launch the instance. Choose an existing key pair or create a new one to SSH into the instance. 2. Connect to EC2 Instance: SSH into your EC2 instance using the key pair you selected or created: ssh -i /path/to/your/key.pem ec2-user@your-ec2-ip 3. Install Docker and Docker Compose: Update the package manager and install Docker and Docker Compose: sudo yum update -y sudo amazon-linux-extras install docker sudo service docker start sudo usermod -a -G docker ec2-user sudo chkconfig docker on Log out and back in or run newgrp docker to apply group changes. 4. Install Laravel Sail: On your EC2 instance, install Laravel Sail globally: composer global require laravel/sail 5. Initialize Laravel Project with Sail: Navigate to your Laravel project on your local machine and initialize Sail: cd /path/to/your/laravel/project sail install 6. Configure Environment Variables: Edit the .env file in your Laravel project to configure environment variables, especially those related to your database and application: nano .env 7. Build and Run Laravel Sail: Build and start your Laravel Sail containers: sail up -d 8. Configure Security Group: In your AWS EC2 Console, go to the security group associated with your EC2 instance. Ensure that ports 80 and 443 are open for incoming traffic. 9. Access Laravel Application: Open a web browser and navigate to your EC2 instance’s public IP or domain name. You should see your Laravel application running. 10. Optional: Set Up a Domain Name: If you have a domain name, point it to your EC2 instance’s public IP address. Configure your Laravel application accordingly. 11. Secure Laravel Application: Consider securing your Laravel application by setting up SSL using a service like Let’s Encrypt. Ensure that your .env file has the correct APP_URL and SESSION_SECURE_COOKIE values. 12. Automate Deployment (Optional): Consider automating your deployment process using deployment tools like Laravel Envoyer, AWS CodeDeploy, or GitHub Actions. Congratulations! You have successfully deployed a Laravel Sail application to an AWS EC2 instance. Keep in mind that this is a basic setup, and in a production environment, you should take additional measures for security, performance, and scalability. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe AWS Dev Developer Docker EC2 Git Laravel PHP