The Complete Guide to Laravel Deployment: Server Setup and Initial Configuration

John Finch

John Finch

prological

5 m read
The Complete Guide to Laravel Deployment: Server Setup and Initial Configuration

Deploying a Laravel application to production can be challenging, especially when moving from local development to a live server environment. This comprehensive guide walks you through setting up a Digital Ocean droplet with cPanel/WHM for Laravel deployment, covering everything from SSH key generation to initial server configuration.

Prerequisites

Before diving into the deployment process, ensure you have:

  1. A Digital Ocean account with a droplet (16GB RAM, 50GB disk recommended)
  2. Basic knowledge of SSH and command line operations
  3. Your Laravel application ready for deployment
  4. Domain name configured (we'll use yourdomain.com throughout this guide)

Step 1: Setting Up SSH Access

Generating SSH Keys

First, generate an SSH key pair on your local machine:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

When prompted, save the key with a descriptive name like laravel_app_ssh_key. This creates both private and public key files.

Connecting to Your Server

Once your Digital Ocean droplet is ready (IP: 171.50.123.100 - use your_ip_address), set proper permissions and connect:

chmod 600 /path/to/your/laravel_app_ssh_key
ssh -i /path/to/your/laravel_app_ssh_key root@171.50.123.100

You'll be greeted with the Ubuntu welcome screen and cPanel & WHM interface information.

Step 2: WHM Initial Setup

Accessing WHM

After SSH connection, you'll receive a WHM login URL similar to:

https://171-50-123-100.cprapid.com:2087/cpsess[session_id]/login/

Creating Your First cPanel Account

Navigate to Account Functions → Create a New Account and configure:

Account Details:

  1. Domain: yourdomain.com
  2. Username: your_app_name (avoid special characters)
  3. Password: Use a strong, unique password
  4. Email: Your administrative email

Resource Allocation:

  1. Disk Space: 50GB+ (important for file uploads and logs)
  2. Bandwidth: Unlimited
  3. Email Accounts: 50+
  4. Databases: 10+

Step 3: PHP and Server Configuration

PHP Version Setup

Laravel requires PHP 8.1 or higher. Configure this through WHM:

  1. Navigate to Software → MultiPHP Manager
  2. Select PHP 8.2 for optimal performance
  3. Go to Software → EasyApache4
  4. Update all packages in "Currently Installed Packages"

If you encounter errors during updates, perform a graceful reboot:

  1. Search for "reboot" in WHM sidebar
  2. Execute graceful reboot
  3. Return to EasyApache4 and continue

Enabling Required PHP Extensions

In Software → EasyApache4 → Customize Currently Installed Packages → Enable Extensions, ensure these are active:

  1. OpenSSL
  2. PDO
  3. Mbstring
  4. Tokenizer
  5. XML
  6. Ctype
  7. JSON
  8. BCMath
  9. Fileinfo

Step 4: Database Configuration

Creating MySQL Database

Through cPanel, navigate to Databases → MySQL Databases:

  1. Database Name: your_app_digital_db
  2. Username: your_app_digital_db_user
  3. Password: Generate a strong password
  4. Privileges: Grant all privileges to the user

Step 5: File Upload and Initial Setup

Uploading Your Laravel Application

  1. Access cPanel File Manager
  2. Navigate to public_html
  3. Upload your Laravel application as a ZIP file
  4. Extract the contents

Creating Essential Files

Create .env file:

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

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

Create .htaccess file in root directory:

<IfModule mime_module>
AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Step 6: Setting Proper Permissions

SSH into your server and set Laravel-specific permissions:

cd /home/your_username/public_html

# Set directory permissions
find /home/your_username/public_html -type d -exec chmod 755 {} \;

# Set file permissions
find /home/your_username/public_html -type f -exec chmod 644 {} \;

# Set storage and cache permissions
chmod -R 775 /home/your_username/public_html/storage
chmod -R 775 /home/your_username/public_html/bootstrap/cache

Step 7: Installing Dependencies

Composer Installation

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

Node.js and NPM Setup

If Node.js isn't installed:

sudo apt update
sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

Install and build assets:

npm install
npm run build

Laravel Artisan Commands

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

# Create storage link
php artisan storage:link

# Run migrations (if needed)
php artisan migrate --force

Troubleshooting Common Issues

NPM Installation Failures

If npm install fails, ensure Node.js is properly installed and try:

rm -rf node_modules/ package-lock.json
npm install
npm run build

Permission Denied Errors

Ensure your SSH user has proper permissions:

chown -R your_username:your_username /home/your_username/public_html

Cache Issues

Clear everything and rebuild:

php artisan optimize:clear
rm -rf public/build/ node_modules/ package-lock.json
npm install
npm run build

Testing Your Deployment

Access your application using the server IP:

  1. http://171.50.123.100 (your_ip_address)
  2. http://171-50-123-100.cprapid.com

Update your .env file accordingly:

APP_URL=http://171.50.123.100

What's Next?

This initial setup provides a solid foundation for your Laravel application. In our next guide, we'll cover domain configuration, SSL certificates, and performance optimization to take your deployment to production-ready status.

Building and deploying web applications is a core skill every developer needs. At ScriptVil, we're creating a marketplace where developers can share deployment scripts, configuration templates, and automation tools. Whether you're looking for ready-made deployment solutions or want to monetize your DevOps expertise, ScriptVil will be your go-to platform when we launch.

Key Takeaways

  1. Always use SSH keys for secure server access
  2. Proper file permissions are crucial for Laravel applications
  3. cPanel/WHM provides an intuitive interface for server management
  4. Testing with IP addresses before domain configuration saves time
  5. Environment-specific configurations prevent common deployment issues

Ready to take your Laravel deployment further? Stay tuned for our next guide on domain configuration and SSL implementation.

Featured Items

Scalable E-commerce Web Application Inspired by Industry Leaders
by Prological in PHP Scripts

$59.00

(1)

Comments (0)

Sign in to post a comment

No comments yet. Be the first to comment!