Deploying a Laravel project to an AWS EC2 instance involves several steps, including setting up the server, installing the necessary software, configuring your Laravel project, and setting up a database. Below is a step-by-step guide to help you through the process.
Step 1: Launch an EC2 Instance
1. Log in to AWS Management Console:
Go to the AWS Management Console.
2. Launch a New EC2 Instance:
- Navigate to the EC2 dashboard and click "Launch Instance".
- Choose an Amazon Machine Image (AMI). For a Laravel project, an Ubuntu Server is a good choice (e.g., Ubuntu Server 20.04 LTS).
- Select an instance type (e.g., `t2.micro` for free tier eligibility).
- Configure instance details (default settings are usually fine for a small project).
- Add storage (default settings are usually sufficient).
- Add tags if needed.
- Configure security group:
- Allow SSH (port 22).
- Allow HTTP (port 80).
- Allow HTTPS (port 443).
3. Launch and Connect to Your Instance:
- Launch the instance and download the key pair (.pem file).
- Use SSH to connect to your instance:
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-dns
Step 2: Set Up the Server
1. Update the Package List:
sudo apt update
2. Install Required Software:
- Nginx:
sudo apt install nginx
- PHP and Extensions:
sudo apt install php-fpm php-mysql php-cli php-curl php-zip php-gd php-mbstring php-xml
- MySQL:
sudo apt install mysql-server
- Composer:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Step 3: Configure MySQL
1. Secure MySQL Installation:
sudo mysql_secure_installation
2. Create a Database and User:
sudo mysql -u root -p
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 4: Deploy Laravel Application
1. Clone Your Laravel Project:
- Make sure `git` is installed:
sudo apt install git
- Clone your project:
cd /var/www
sudo git clone https://github.com/your-repo/your-laravel-project.git
cd your-laravel-project
2. Set Up Your Laravel Project:
- Install dependencies:
composer install
- Set permissions:
sudo chown -R www-data:www-data /var/www/your-laravel-project
sudo chmod -R 775 /var/www/your-laravel-project/storage
sudo chmod -R 775 /var/www/your-laravel-project/bootstrap/cache
- Create a `.env` file:
cp .env.example .env
- Generate an application key:
php artisan key:generate
- Update the `.env` file with your database credentials and other necessary configurations.
Step 5: Configure Nginx
1. Create a New Nginx Configuration File:
sudo nano /etc/nginx/sites-available/your-laravel-project
Add the following configuration:
server {
listen 80;
server_name your-domain.com; # or your EC2 public IP
root /var/www/your-laravel-project/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
2. Enable the Configuration and Restart Nginx:
sudo ln -s /etc/nginx/sites-available/your-laravel-project /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Step 6: Final Steps
1. Migrate the Database:
php artisan migrate
2. (Optional) Set Up a Queue Worker:
If your Laravel application uses queues, you might want to set up a queue worker. You can use `Supervisor` to manage your Laravel queues.
3. (Optional) Set Up HTTPS:
For a production environment, you should secure your application with HTTPS using a tool like Certbot to obtain a free SSL certificate from Let's Encrypt.
Step 7: Access Your Application
Open your browser and navigate to your EC2 public DNS or your domain name (if you have set it up) to see your Laravel application running.
This guide provides a comprehensive overview of deploying a Laravel project to an AWS EC2 instance. Adjustments may be needed based on your specific project requirements and server configurations.
0 Comments