Laravel Production Deployment: Domain Configuration and SSL Implementation

John Finch

John Finch

prological

6 m read
Laravel Production Deployment: Domain Configuration and SSL Implementation

After setting up your Laravel application on a Digital Ocean droplet with cPanel/WHM, the next critical step is configuring your domain and implementing SSL certificates. This guide walks you through the complete process of connecting your domain to your server and securing it with HTTPS.

Understanding the Domain-Server Connection

When you deploy a Laravel application, you need to establish the connection between your domain name and your server IP address. This involves DNS configuration, server-side setup, and proper redirects to ensure a seamless user experience.

Step 1: DNS Configuration in Your Domain Registrar

Setting Up DNS Records

For this example, we'll use a domain purchased through GoDaddy, but the process is similar for other registrars.

Access your domain's DNS management:

  1. Login to your domain registrar (GoDaddy, Namecheap, etc.)
  2. Navigate to DNS Management for your domain
  3. Add the following DNS records:
Type: A Record
Name: @
Value: 171.50.123.100 (your_ip_address)
TTL: 1 Hour

Type: A Record
Name: www
Value: 171.50.123.100 (your_ip_address)
TTL: 1 Hour

Type: A Record
Name: server
Value: 171.50.123.100 (your_ip_address)
TTL: 1 Hour

Understanding DNS Record Types

  1. @ Record: Points your root domain (yourdomain.com) to your server
  2. www Record: Handles www.yourdomain.com requests
  3. server Record: Creates server.yourdomain.com for WHM access

Step 2: WHM Hostname Configuration

Setting Up Server Hostname

In WHM, navigate to Server Configuration → Basic WHM Setup:

Configure these essential settings:

Hostname: server.yourdomain.com
Primary Nameserver: ns1.yourdomain.com
Secondary Nameserver: ns2.yourdomain.com
Contact Email: admin@yourdomain.com

This configuration ensures your server is properly identified and can handle nameserver requests.

Step 3: Digital Ocean DNS Zone Setup

Adding Domain to Digital Ocean

Even though you're using external DNS, adding your domain to Digital Ocean provides additional DNS redundancy:

  1. Access Digital Ocean Control Panel
  2. Navigate to Networking → Domains
  3. Add your domain: yourdomain.com
  4. Create these A records:
Type: A Record
Hostname: server
Value: 171.50.123.100 (your_ip_address)
TTL: 3600

Type: A Record
Hostname: @
Value: 171.50.123.100 (your_ip_address)
TTL: 3600

Type: A Record
Hostname: www
Value: 171.50.123.100 (your_ip_address)
TTL: 3600

Step 4: Laravel Application Configuration

Updating Environment Variables

Once DNS propagation is complete (usually 24-48 hours), update your Laravel .env file:

APP_NAME=YourLaravelApp
APP_ENV=production
APP_KEY=your_app_key_here
APP_DEBUG=false
APP_URL=https://yourdomain.com

# Database remains the same
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_app_digital_db
DB_USERNAME=your_app_digital_db_user
DB_PASSWORD=your_database_password

# Update OAuth redirect URIs
GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/google/callback
GITHUB_REDIRECT_URI=https://yourdomain.com/auth/github/callback

# Email configuration
MAIL_FROM_ADDRESS=noreply@yourdomain.com
MAIL_FROM_NAME="${APP_NAME}"

Clearing Laravel Caches

After updating your environment file:

cd /home/your_username/public_html

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

# Rebuild caches with new configuration
php artisan config:cache
php artisan route:cache
php artisan view:cache

Step 5: Implementing URL Redirects

Root .htaccess Configuration

Create/update /home/your_username/public_html/.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
# Force HTTPS (once SSL is configured)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect www to non-www (or vice versa based on preference)
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]
# Direct all requests to public folder
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

# PHP handler
<IfModule mime_module>
AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
</IfModule>

Step 6: SSL Certificate Implementation

Option 1: Let's Encrypt (Free SSL)

Most cPanel installations include Let's Encrypt integration:

  1. Navigate to cPanel → SSL/TLS → Let's Encrypt
  2. Select your domain
  3. Check "Include www" option
  4. Click "Issue" to generate certificate

Option 2: Cloudflare SSL (Recommended)

For better performance and additional security features:

  1. Create Cloudflare account
  2. Add your domain to Cloudflare
Update nameservers to Cloudflare's:NS1: sarah.ns.cloudflare.comNS2: walt.ns.cloudflare.com
  1. Configure SSL/TLS settings:SSL Mode: Full (Strict)
  2. Edge Certificates: Enabled
  3. Always Use HTTPS: Enabled

Option 3: Custom SSL Certificate

For purchased SSL certificates:

  1. Navigate to cPanel → SSL/TLS → Manage SSL
  2. Upload certificate files:Certificate (CRT)
  3. Private Key
  4. Certificate Authority Bundle (if applicable)
  5. Install certificate for your domain

Step 7: Testing Your Configuration

DNS Propagation Check

Use online tools to verify DNS propagation:

# Command line check
dig yourdomain.com
nslookup yourdomain.com

SSL Certificate Validation

Test SSL implementation:

# Check SSL certificate
openssl s_client -connect yourdomain.com:443

# Test HTTPS redirect
curl -I http://yourdomain.com

Application Testing

Verify these URLs work correctly:

  1. https://yourdomain.com (main site)
  2. https://www.yourdomain.com (should redirect to non-www)
  3. http://yourdomain.com (should redirect to HTTPS)

Step 8: OAuth Application Updates

Updating External OAuth Providers

Google OAuth Console:

Authorized JavaScript origins:
- https://yourdomain.com

Authorized redirect URIs:
- https://yourdomain.com/auth/google/callback

GitHub OAuth App:

Homepage URL: https://yourdomain.com
Authorization callback URL: https://yourdomain.com/auth/github/callback

Facebook OAuth (if applicable):

Valid OAuth Redirect URIs:
- https://yourdomain.com/auth/facebook/callback

Common Issues and Solutions

SSL Mixed Content Errors

If you're getting mixed content warnings:

// In AppServiceProvider boot method
if (config('app.env') === 'production') {
\URL::forceScheme('https');
}

DNS Not Propagating

  1. Check TTL settings (lower values propagate faster)
  2. Use different DNS checkers to verify global propagation
  3. Clear local DNS cache: ipconfig /flushdns (Windows) or sudo dscacheutil -flushcache (Mac)

WHM Access After Domain Change

After domain configuration, your WHM URL will change to:

https://server.yourdomain.com:2087/

Vite Development Assets Loading

Remove the Vite hot file to prevent development asset loading:

rm public/hot

Performance Considerations

CDN Integration

Consider implementing a CDN like Cloudflare or AWS CloudFront for:

  1. Faster global content delivery
  2. DDoS protection
  3. Additional SSL/security features
  4. Bandwidth savings

Database Optimization

Update your database connection for production:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_app_digital_db
DB_USERNAME=your_app_digital_db_user
DB_PASSWORD=your_secure_password
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci

What's Next?

With your domain configured and SSL implemented, your Laravel application is now accessible via HTTPS with proper redirects. The next step is performance optimization, which we'll cover in our upcoming guide on server-level optimizations and caching strategies.

Domain and SSL configuration can be complex, but having the right tools and templates makes it manageable. At ScriptVil, we're building a platform where developers can find and share automation scripts for deployment tasks like these. From SSL certificate automation to DNS configuration templates, our marketplace will help developers streamline their deployment workflows.

Security Best Practices

  1. Always use HTTPS in production
  2. Implement proper redirect chains (HTTP → HTTPS, www → non-www)
  3. Keep SSL certificates updated and monitored
  4. Use strong passwords for all accounts
  5. Regularly update server software and dependencies

Ready to optimize your Laravel application's performance? Our next guide covers server-level optimizations, caching strategies, and performance monitoring.

Comments (0)

Sign in to post a comment

No comments yet. Be the first to comment!