You are currently viewing Deploying a Laravel Site on AWS EC2 with Elastic IP, Cloudflare, and Hostinger

Deploying a Laravel Site on AWS EC2 with Elastic IP, Cloudflare, and Hostinger

In this guide, we’ll walk through deploying a Laravel website on an AWS EC2 instance using an Elastic IP, integrating Cloudflare, and managing your domain on Hostinger. We’ll use example.com as the example domain.

Prerequisites

  • An AWS account with an EC2 instance running Ubuntu.
  • A registered domain on Hostinger.
  • Cloudflare account.
  • SSH access to your EC2 instance.
  • Laravel project ready for deployment.

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.
  2. Navigate to EC2InstancesLaunch Instance.
  3. Select Ubuntu as the AMI (Amazon Machine Image).
  4. Choose the instance type (e.g., t2.micro).
  5. Configure security groups to allow HTTP (80), HTTPS (443), and SSH (22).
  6. Launch the instance and download the .pem key.

Step 2: Assign an Elastic IP

  1. Go to EC2 DashboardElastic IPsAllocate Elastic IP.
  2. Associate it with your EC2 instance.
  3. Note down the IP address (e.g., 54.123.45.67).

Step 3: Connect to Your Instance

Use the following command to SSH into your server:

ssh -i your-key.pem [email protected]

Step 4: Update and Install Required Packages

sudo apt update && sudo apt upgrade -y
sudo apt install apache2 php php-cli php-mbstring php-xml php-bcmath php-curl php-zip unzip composer -y

Step 5: Deploy Laravel Application

  1. Upload your Laravel project using SCP or Git.
scp -i your-key.pem your-project.zip [email protected]:/var/www/html
  1. SSH into your instance and unzip the project:
cd /var/www/html
sudo unzip your-project.zip
sudo mv your-project example.com
cd example.com
  1. Set permissions:
sudo chown -R www-data:www-data /var/www/html/example.com
sudo chmod -R 775 /var/www/html/example.com/storage
sudo chmod -R 775 /var/www/html/example.com/bootstrap/cache
  1. Install dependencies:
composer install
  1. Configure environment file:
cp .env.example .env
sudo nano .env

Update the .env file with your database details and application URL.


Step 6: Configure Apache

  1. Create a Virtual Host file:
sudo nano /etc/apache2/sites-available/example.com.conf

Add the following content:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example.com/public

    <Directory /var/www/html/example.com/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. Enable the site and necessary modules:
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Step 7: Install SSL with Certbot

  1. Install Certbot:
sudo apt install certbot python3-certbot-apache -y
  1. Obtain and install SSL certificate:
sudo certbot --apache -d example.com
  1. Verify SSL renewal setup:
sudo certbot renew --dry-run

Step 8: Configure Cloudflare

  1. Log in to your Cloudflare account.
  2. Add your domain and let Cloudflare scan the DNS records.
  3. Update the A record with your Elastic IP and enable the proxy (orange cloud).
  4. Enable Full (Strict) SSL under SSL/TLSOverview.
  5. Enable basic protections using Cloudflare’s Firewall.

Step 9: Update DNS on Hostinger

  1. Log in to your Hostinger account.
  2. Navigate to DomainsDNS Management.
  3. Update your nameservers to the ones provided by Cloudflare.

Step 10: Final Checks

  1. Access your site at https://example.com.
  2. Ensure SSL is applied and the site is secure.
  3. Test Laravel functionality and confirm no permission errors.
  4. Monitor the SSL certificate with certbot renew --dry-run.

Conclusion

Congratulations! You have successfully deployed your Laravel website on AWS EC2 with an Elastic IP, secured with SSL using Certbot, and managed via Cloudflare and Hostinger. If you face any issues, check your Apache logs using:

sudo tail -f /var/log/apache2/error.log

Good luck with your project!