Laravel is the world’s most popular PHP web application framework, renowned for its elegant syntax, Model-View-Controller (MVC) architecture, Eloquent ORM, robust routing, and powerful ecosystem. Deploying a Laravel application to DirectAdmin web hosting is secure, performant, and straightforward when following proper directory structuring and environment configuration.

Because Laravel uses a security-first architecture where only the public/ folder should be accessible to the internet, DirectAdmin allows you to securely place your core framework files (such as app/, config/, vendor/, and .env) outside the public web root while routing web traffic seamlessly to public/index.php.

In this comprehensive step-by-step tutorial, we will guide you through preparing, uploading, and configuring your Laravel project in DirectAdmin, including MySQL database creation, .env configuration, .htaccess web root routing, file permissions, and task scheduling (Cron Jobs).


Prerequisites


Step-by-Step Guide: How to Deploy Laravel in DirectAdmin

Step 1: Prepare the Laravel Project Locally

Before uploading to DirectAdmin, optimize your dependencies and compile frontend assets locally:

  1. Open your terminal in your local Laravel project directory.
  2. Install production PHP dependencies and generate an optimized class map:
    composer install --optimize-autoloader --no-dev
  3. Compile your frontend assets (CSS / JavaScript) using Vite:
    npm run build
  4. Compress project files into a ZIP archive: Select all project files and folders (including app/, bootstrap/, config/, database/, public/, resources/, routes/, storage/, vendor/, artisan, and composer.json), and compress them into laravel-app.zip.
    Note: Do not include the node_modules/ folder or .git/ directory in the zip archive to keep the file lightweight.

Step 2: Create a MySQL Database in DirectAdmin

  1. Log in to your DirectAdmin control panel.
  2. Under Account Manager, click on Databases.
  3. Click the Create Database section.
  4. Enter a Database Name (e.g. laravel_db) and Database Username.
  5. Generate or enter a strong password, then click CREATE.
  6. Take note of your Full Database Name, Full Username, and Password for the next steps.

DirectAdmin Databases - Create MySQL Database

Step 3: Access File Manager & Upload the Laravel Archive

  1. In DirectAdmin, navigate to System Info & Files > File Manager.
  2. Navigate to your domain directory:
    domains > domain.com
  3. Click New Folder and create a directory named laravel alongside public_html.
  4. Open the newly created laravel directory, click Upload in the top navigation bar, and upload your laravel-app.zip file.
  5. Right-click laravel-app.zip and select Extract.

DirectAdmin File Manager - Domain Directory and Structure

Step 4: Configure Document Root Routing in public_html

CRITICAL STEP FOR LARAVEL SECURITY: In Laravel, all incoming HTTP requests must hit laravel/public/index.php while keeping configuration and environment files private. To route web traffic from public_html to Laravel's public directory seamlessly:

  1. In File Manager, navigate to domains > domain.com > public_html.
  2. Delete any default placeholder files (such as a default index.html).
  3. Click + New File, enter .htaccess as the filename, and click Create.
  4. Select .htaccess, click Edit, and paste the following proxy rewrite rules:
    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteRule ^(.*)$ ../laravel/public/$1 [L]
    </IfModule>
  5. Click Save.

DirectAdmin File Manager - Create .htaccess File for Laravel Routing

Step 5: Configure the Production .env File

  1. In File Manager, navigate to domains > domain.com > laravel.
  2. If .env does not exist, rename .env.example to .env (or create a new .env file).
  3. Edit the .env file and update your production environment values and database credentials:
    APP_NAME="MyLaravelApp"
    APP_ENV=production
    APP_KEY=base64:YOUR_APP_KEY_FROM_LOCAL_ENV
    APP_DEBUG=false
    APP_URL=https://domain.com
    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=avesdns_laravel_db
    DB_USERNAME=avesdns_laravel_user
    DB_PASSWORD=YOUR_STRONG_PASSWORD
  4. Click Save.

Step 6: Set Directory Permissions for Storage and Cache

Laravel requires write permissions for session storage, compiled views, file uploads, and system logs:

  1. In File Manager under domains > domain.com > laravel, locate the following folders:
    • storage/ (including storage/app, storage/framework, storage/logs)
    • bootstrap/cache/
  2. Right-click each folder, select Set Permissions, and set permissions to 775 (or 755). Check the box to apply recursively if available.

Step 7: Import Database Schema & Optimize Caches

  1. Import Database: In DirectAdmin under Account Manager > Databases, click PHPMYADMIN. Select your created database, click the Import tab, and upload your local .sql database dump.
  2. Optimize Caches: If you have SSH / Terminal access in DirectAdmin, run the optimization commands:
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache

Step 8: Test and Verify Your Live Laravel Website

  1. Open your web browser and visit https://domain.com.
  2. Your Laravel web application should load with all styles, database connections, and routes operational!

Automating Laravel Task Scheduling (Cron Jobs)

Laravel includes a built-in Task Scheduler (php artisan schedule:run). To enable automatic background jobs, scheduled tasks, and queue monitoring:

  1. In DirectAdmin, navigate to Advanced Features > Cron Jobs.
  2. Click Create Cron Job.
  3. Set the interval to Every Minute (* * * * *).
  4. In the Command field, enter:
    * * * * * cd /home/username/domains/domain.com/laravel && php artisan schedule:run >> /dev/null 2>&1
    (Replace username and domain.com with your actual account username and domain).
  5. Click SAVE.

Frequently Asked Questions & Troubleshooting

Q: Why does my site return "500 Internal Server Error"?
A: A 500 error is typically caused by three common reasons:

  1. Missing or invalid APP_KEY in your .env file.
  2. Incorrect file permissions on storage/ or bootstrap/cache/ (ensure they are writable).
  3. Incorrect database credentials in the .env file. (You can check detailed logs inside laravel/storage/logs/laravel.log).

Q: How do I create the storage symlink for uploaded files (storage:link)?
A: If your application uses public disk uploads (Storage::url()), you can create the symlink via SSH with php artisan storage:link or create a simple temporary route in routes/web.php:

Route::get('/create-storage-link', function () {
    Artisan::call('storage:link');
    return 'Storage link created successfully!';
});
Visit https://domain.com/create-storage-link once in your browser, then remove the route.

Q: How do I change the PHP version for Laravel?
A: In DirectAdmin, go to Account Manager > PHP Settings. Under PHP Version Selector, choose PHP 8.2 or PHP 8.3 and click Save.


Need Further Assistance?

If you encounter any issues deploying or configuring your Laravel web application in DirectAdmin, our technical support team is available 24/7. Feel free to submit a support ticket through your client area for prompt assistance.

Esta resposta foi útil? 0 Os usuários acharam isso útil (0 Votos)