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
- Log in to your AWS Management Console.
- Navigate to EC2 → Instances → Launch Instance.
- Select Ubuntu as the AMI (Amazon Machine Image).
- Choose the instance type (e.g.,
t2.micro
). - Configure security groups to allow HTTP (80), HTTPS (443), and SSH (22).
- Launch the instance and download the
.pem
key.
Step 2: Assign an Elastic IP
- Go to EC2 Dashboard → Elastic IPs → Allocate Elastic IP.
- Associate it with your EC2 instance.
- 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
- Upload your Laravel project using SCP or Git.
scp -i your-key.pem your-project.zip [email protected]:/var/www/html
- 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
- 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
- Install dependencies:
composer install
- 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
- 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>
- Enable the site and necessary modules:
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
Step 7: Install SSL with Certbot
- Install Certbot:
sudo apt install certbot python3-certbot-apache -y
- Obtain and install SSL certificate:
sudo certbot --apache -d example.com
- Verify SSL renewal setup:
sudo certbot renew --dry-run
Step 8: Configure Cloudflare
- Log in to your Cloudflare account.
- Add your domain and let Cloudflare scan the DNS records.
- Update the A record with your Elastic IP and enable the proxy (orange cloud).
- Enable Full (Strict) SSL under SSL/TLS → Overview.
- Enable basic protections using Cloudflare’s Firewall.
Step 9: Update DNS on Hostinger
- Log in to your Hostinger account.
- Navigate to Domains → DNS Management.
- Update your nameservers to the ones provided by Cloudflare.
Step 10: Final Checks
- Access your site at
https://example.com
. - Ensure SSL is applied and the site is secure.
- Test Laravel functionality and confirm no permission errors.
- 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!