🚀 Week 23, Day 3: Deployment (cPanel / Laravel Forge)

🎯 Learning Objectives

📋 Pre-Deployment Checklist

Production Readiness Checklist

Task Description Status
Environment Variables Set APP_ENV=production, APP_DEBUG=false ☐
Database Migrations All migrations tested and working ☐
Seeders Production seeders ready (if needed) ☐
Testing All tests passing ☐
Documentation README and user manual complete ☐
Git Repository All code committed and pushed ☐
Dependencies composer.lock and package-lock.json updated ☐
Assets CSS/JS compiled for production ☐
Security All security best practices implemented ☐

🔧 Step 1: Prepare Application

1. Update .env for Production

# Application
APP_NAME="Library Management System"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

# Database
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_secure_password

# Mail (Update with production SMTP)
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

# Session & Cache (Use database in production)
SESSION_DRIVER=database
CACHE_DRIVER=database

# Queue (Use database for simplicity)
QUEUE_CONNECTION=database

2. Optimize Application

# Clear all caches
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

# Optimize for production
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize

# Compile assets
npm run build

3. Create Deployment Archive

# Exclude unnecessary files
# Create .deployignore or use Git archive

# Using Git (recommended)
git archive --format=zip --output=library-system.zip main

# Or manually zip (excluding node_modules, .git, etc.)
# Exclude: node_modules, .git, .env, storage/logs/*

🌐 Step 2: Deploy to cPanel

cPanel Deployment Steps

1. Access cPanel

  1. Login to your hosting cPanel (usually: yourdomain.com/cpanel)
  2. Navigate to "File Manager"

2. Upload Files

  1. Navigate to public_html directory
  2. Create a subfolder (e.g., library-system) or use root
  3. Upload your ZIP file
  4. Extract the ZIP file

3. Configure File Permissions

# Set permissions via cPanel File Manager:
# Right-click → Change Permissions

storage/                    - 755
storage/framework/          - 755
storage/framework/cache/    - 755
storage/framework/sessions/ - 755
storage/framework/views/    - 755
storage/logs/               - 755
bootstrap/cache/            - 755

4. Create Database

  1. Go to cPanel → MySQL Databases
  2. Create new database (e.g., username_library)
  3. Create database user with strong password
  4. Add user to database with ALL PRIVILEGES
  5. Note down database name, username, and password

5. Configure .env File

  1. In File Manager, locate .env file
  2. Right-click → Edit
  3. Update database credentials
  4. Set APP_URL to your domain
  5. Set APP_DEBUG=false
  6. Save changes

6. Run Migrations via Terminal

# Access Terminal in cPanel or use SSH

# Navigate to project directory
cd public_html/library-system

# Install dependencies (if not included)
composer install --optimize-autoloader --no-dev

# Generate application key
php artisan key:generate

# Run migrations
php artisan migrate --force

# Create storage link
php artisan storage:link

# Cache configurations
php artisan config:cache
php artisan route:cache
php artisan view:cache

7. Configure Document Root

Point your domain to the public folder:

  1. cPanel → Domains
  2. Click on domain you want to configure
  3. Change Document Root to: public_html/library-system/public
  4. Save changes

🔐 Step 3: SSL Certificate Setup

Free SSL with Let's Encrypt (cPanel)

  1. cPanel → Security → SSL/TLS Status
  2. Select your domain
  3. Click "Run AutoSSL"
  4. Wait for certificate installation (1-2 minutes)
  5. Test: Visit https://yourdomain.com

Force HTTPS

Create/edit .htaccess in public folder:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Force HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    
    # Laravel rewrite rules
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

✅ Step 4: Post-Deployment Testing

Testing Checklist

Test Expected Result Status
Homepage loads No errors, CSS/JS working ☐
Registration works Can create new account ☐
Login works Can authenticate successfully ☐
Database connection Data displays correctly ☐
File uploads Images upload and display ☐
Email sending Notifications received ☐
CRUD operations Create/Read/Update/Delete work ☐
HTTPS enabled Secure connection (padlock icon) ☐
Mobile responsive Works on mobile devices ☐

🔧 Troubleshooting Common Issues

Common Errors & Solutions

Error: 500 Internal Server Error

# Solutions:
1. Check .env file exists and has correct values
2. Check file permissions (storage and bootstrap/cache)
3. Run: php artisan config:clear
4. Check error logs: storage/logs/laravel.log
5. Enable error display temporarily in .env:
   APP_DEBUG=true (remember to set to false after fixing!)

Error: Database Connection Failed

# Solutions:
1. Verify database credentials in .env
2. Check database exists in cPanel
3. Verify database user has privileges
4. Check DB_HOST (usually 'localhost' in shared hosting)
5. Test connection via cPanel → phpMyAdmin

Error: Assets (CSS/JS) Not Loading

# Solutions:
1. Check APP_URL in .env matches your domain
2. Run: php artisan storage:link
3. Verify public folder is document root
4. Check .htaccess file exists in public folder
5. Clear browser cache

Error: File Upload Not Working

# Solutions:
1. Run: php artisan storage:link
2. Check storage/app/public folder permissions (755)
3. Verify upload_max_filesize in PHP settings
4. Check post_max_size in PHP settings
5. Verify storage disk configuration in config/filesystems.php

Error: Email Not Sending

# Solutions:
1. Verify SMTP credentials in .env
2. Check MAIL_FROM_ADDRESS is valid
3. Enable "Less secure apps" for Gmail (or use App Password)
4. Test with: php artisan tinker
   >>> Mail::raw('Test', function($msg) {
           $msg->to('test@example.com')->subject('Test');
       });
5. Check spam folder

📊 Step 5: Monitoring & Maintenance

Post-Deployment Tasks

1. Set Up Monitoring

2. Backup Strategy

# Database Backup (via cPanel or command)
mysqldump -u username -p database_name > backup.sql

# File Backup
# Use cPanel Backup Wizard or FTP to download files regularly

# Automated Backups
# Configure cPanel automated backups if available

3. Regular Maintenance

✏️ Today's Tasks

Task 1: Prepare Application (45 minutes)

  1. Update .env with production settings
  2. Run optimization commands
  3. Test application locally one final time
  4. Create deployment package

Task 2: Deploy to cPanel (90 minutes)

  1. Upload files to cPanel
  2. Create and configure database
  3. Update .env file
  4. Run migrations
  5. Configure file permissions
  6. Set document root

Task 3: Configure SSL (15 minutes)

  1. Install SSL certificate
  2. Force HTTPS
  3. Test secure connection

Task 4: Test Deployment (45 minutes)

  1. Test all features thoroughly
  2. Test on different devices
  3. Create test accounts and data
  4. Fix any issues found

Task 5: Documentation (15 minutes)

  1. Document deployment process
  2. Note production URL and credentials
  3. Update README with live URL

📦 Deliverables

By end of day:

  1. Live Application:
    • Publicly accessible URL
    • HTTPS enabled
    • All features working
  2. Production Credentials:
    • Admin login details
    • Database credentials
    • cPanel login (if applicable)
  3. Deployment Documentation:
    • Step-by-step deployment guide
    • Troubleshooting notes
    • Server configuration details

🎉 Congratulations!

Your application is now live and accessible to users worldwide!

Share your project:

⚠️ Security Reminder: Never commit your production .env file to Git! Keep database credentials secure!
💡 Pro Tips: