DEV Community

Ravi Sengar Rajasthan
Ravi Sengar Rajasthan

Posted on

Deploying a Laravel Portfolio to AWS EC2: Complete Production Setup

Why AWS EC2 Over Shared Hosting?

Full server control for custom configurations

Scalability as your projects grow

Professional credibility with clients

Learning experience with cloud infrastructure

Tech Stack
Backend: Laravel 12

Frontend: Tailwind CSS

Server: Ubuntu 22.04 on AWS EC2 t2.micro

Web Server: Nginx

Database: SQLite (perfect for portfolios)

SSL: Let's Encrypt (free)

Step-by-Step Deployment
1. EC2 Setup

Launch t2.micro instance (free tier)

Configure security groups: HTTP (80), HTTPS (443), SSH (22)

Copy
bash
2. Server Configuration

Update system

sudo apt update && sudo apt upgrade -y

Install PHP 8.3 and extensions

sudo apt install php8.3-fpm php8.3-mysql php8.3-xml php8.3-curl php8.3-zip

Install Nginx

sudo apt install nginx

Install Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Copy
bash
3. Laravel Deployment

Clone your repository

git clone https://github.com/yourusername/portfolio.git /var/www/portfolio

Install dependencies

cd /var/www/portfolio
composer install --optimize-autoloader --no-dev

Set permissions

sudo chown -R www-data:www-data /var/www/portfolio
sudo chmod -R 755 /var/www/portfolio
sudo chmod -R 775 /var/www/portfolio/storage

Copy
bash
4. Nginx Configuration
server {
listen 80;
server_name yourdomain.com;
root /var/www/portfolio/public;
index index.php;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}
Enter fullscreen mode Exit fullscreen mode

}

Copy
nginx
5. SSL with Let's Encrypt

Install Certbot

sudo apt install certbot python3-certbot-nginx

Get SSL certificate

sudo certbot --nginx -d yourdomain.com

Auto-renewal (already configured)

sudo systemctl status certbot.timer

Copy
bash
Auto-Deployment with GitHub Webhooks
Created a webhook endpoint that automatically pulls changes:

<?php
// webhook.php
$payload = json_decode(file_get_contents('php://input'), true);

if (isset($payload['ref']) && $payload['ref'] === 'refs/heads/main') {
shell_exec('cd /var/www/portfolio && git pull origin main');
shell_exec('cd /var/www/portfolio && php artisan config:clear');
shell_exec('cd /var/www/portfolio && php artisan cache:clear');
echo "Deployed successfully!";
}
?>

Copy
php
Workflow: Edit locally → Push to GitHub → Auto-deploy to production

Security Hardening
Security Headers Middleware
class SecurityHeaders
{
public function handle($request, Closure $next)
{
$response = $next($request);

    $response->headers->set('X-Content-Type-Options', 'nosniff');
    $response->headers->set('X-Frame-Options', 'DENY');
    $response->headers->set('X-XSS-Protection', '1; mode=block');

    return $response;
}
Enter fullscreen mode Exit fullscreen mode

}

Copy
php
Environment Security
Never commit .env files

Use strong database passwords

Configure proper file permissions

Regular security updates

Performance Optimizations

Enable OPcache

sudo nano /etc/php/8.3/fpm/php.ini

opcache.enable=1

Optimize Composer autoloader

composer install --optimize-autoloader --no-dev

Laravel optimizations

php artisan config:cache
php artisan route:cache
php artisan view:cache

Copy
bash
Monitoring and Maintenance
Log Monitoring

Check Laravel logs

tail -f /var/www/portfolio/storage/logs/laravel.log

Check Nginx logs

tail -f /var/log/nginx/error.log

Copy
bash
Backup Strategy
Database backups via cron jobs

Code versioning with Git

Server snapshots (AWS AMI)

Cost Breakdown
EC2 t2.micro: Free for 12 months, then ~$9/month

Domain: ~$12/year

SSL: Free (Let's Encrypt)

Total: Essentially free for the first year

Results
The deployed portfolio (https://sengaram.com) features:

⚡ Sub-2 second load times

🔒 A+ SSL rating

📱 Mobile-responsive design

🚀 Automatic deployments

📊 Google Analytics integration

Key Takeaways
Start simple - t2.micro is perfect for portfolios

Automate everything - deployment, SSL renewal, backups

Security first - proper headers, permissions, updates

Monitor actively - logs, performance, uptime

Common Pitfalls to Avoid
Forgetting to set proper file permissions

Not configuring security groups correctly

Skipping SSL certificate setup

Ignoring log monitoring

Next Steps
Consider adding:

CDN integration (CloudFront)

Database migration to RDS for scaling

Load balancing for high traffic

CI/CD pipelines with AWS CodePipeline

Have you deployed Laravel applications to AWS? What challenges did you face?

Check out the live result at (https://sengaram.com) - built following these exact steps.

laravel #aws #deployment #portfolio #webdev #php #nginx #ssl

Top comments (0)