Deploying a Laravel application on Amazon Web Services (AWS) can be a powerful way to host your web application in a scalable and reliable cloud environment. AWS offers a range of services that complement Laravel applications, providing high availability and seamless scalability. In this step-by-step guide, we'll walk you through the process of deploying a Laravel application on AWS.
Step 1: Set Up AWS Account
If you don't have an AWS account, sign up for one at https://aws.amazon.com. Once you have an account, log in to the AWS Management Console.
Step 2: Launch an EC2 Instance
Step 3: Connect to the Instance
ssh -i /path/to/your/private-key.pem ec2-user@your-instance-public-ip
Step 4: Install Dependencies
Once connected to the instance, install the necessary dependencies for your Laravel application:
sudo yum update -y
sudo yum install git -y
sudo amazon-linux-extras install epel -y
sudo yum install httpd php php-mysqlnd php-mbstring php-xml -y
sudo systemctl start httpd
sudo systemctl enable httpd
Step 5: Install Composer
Install Composer, a PHP package manager required for Laravel:
sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo php -r "unlink('composer-setup.php');"
Step 6: Deploy Laravel Application
Clone your Laravel application from your version control system (e.g., GitHub) to the server:
git clone https://github.com/your-github-username/your-laravel-repo.git /var/www/html
Install Laravel dependencies and generate the application key:
cd /var/www/html
composer install --no-dev
cp .env.example .env
php artisan key:generate
Step 7: Configure Apache
sudo nano /etc/httpd/conf.d/laravel.conf
Add the following configuration (replace your-domain.com
with your domain or server's public IP):
<VirtualHost *:80>
ServerName your-domain.com
DocumentRoot /var/www/html/public
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
Restart Apache to apply the changes:
sudo systemctl restart httpd
Step 8: Configure Security Groups
Ensure that your AWS security groups allow incoming traffic on ports 80 (HTTP) and 443 (HTTPS) to access your Laravel application.
Step 9: Set Up Database
If your Laravel application requires a database, set up the database on AWS RDS or any compatible database service.
Step 10: Point Domain
If you have a domain, point it to your server's public IP or create a DNS record pointing to your AWS Elastic IP.
Step 11: SSL Certificate (Optional)
For HTTPS support, obtain an SSL certificate using AWS Certificate Manager (ACM) or any other SSL provider. Configure Apache to use the SSL certificate.
Step 12: Final Checks
Test your Laravel application by accessing your domain or server's public IP in a web browser. Ensure everything is functioning as expected.