DEV Community

Cover image for How to deploying Laravel projects on a live server – Complete Step-by-Step Guide
Msh Sayket
Msh Sayket

Posted on

How to deploying Laravel projects on a live server – Complete Step-by-Step Guide

Learn How to deploying Laravel projects on a live server with this comprehensive guide. Step-by-step instructions on setting up hosting, configuring files, and deploying your Laravel app smoothly.Read Laravel Docs

1. Purchase Domain and Hosting

Make sure you have a domain and a hosting plan. Most shared hosting plans (like cPanel-based ones) or a VPS will work for Laravel, but ensure your server supports PHP and MySQL and meets Laravel’s requirements (PHP version, required extensions, etc.).

2. Prepare Your Laravel Project

Make sure your Laravel project is working locally.
Run the following command to clear any cached configuration and to optimize the project:

php artisan cache:clear 

php artisan config:clear 

php artisan route:clear 

php artisan view:clear
Enter fullscreen mode Exit fullscreen mode

Set up your environment variables (.env file). Make sure they are correctly configured for the live server (e.g., database, mail, and app URL settings).

3. Zip and Upload Your Laravel Project

Compress your entire Laravel project folder (without the node_modules and vendor directories) into a .zip file.
Use FTP (with FileZilla or any other FTP client) or File Manager in cPanel to upload the .zip file to your server. Typically, upload the file to the public_html or a subdirectory within it if you want to run your Laravel app in a subdirectory.

4. Extract the Files

Once uploaded, use File Manager in your hosting control panel to extract the .zip file.

5. Set Up the Public Directory

By default, Laravel’s entry point is the public folder, which contains the index.php file. On a shared hosting server:

Move everything in the public folder (including the .htaccess and index.php files) to the root directory (usually public_html).
Edit the index.php file to update the paths:
Change:

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
Enter fullscreen mode Exit fullscreen mode

To:

require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Enter fullscreen mode Exit fullscreen mode

This ensures that Laravel can find the necessary files in the correct directory.

6. Set File Permissions

Ensure that the following directories are writable by the server:

  • /storage
  • /bootstrap/cache Use the following command via SSH (if available) or through the hosting file manager:
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

7. Set Up a Database

Create a MySQL database and a user with privileges in cPanel (or via SSH if using VPS).
Update the .env file with your database credentials:

DB_HOST=localhost
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Enter fullscreen mode Exit fullscreen mode

8. Install Composer Dependencies

If you have SSH access:

SSH into your server using a terminal or a tool like PuTTY.
Navigate to your project directory

cd /path/to/your/project
Enter fullscreen mode Exit fullscreen mode

Run Composer to install the dependencies:

composer install --optimize-autoloader --no-dev
Enter fullscreen mode Exit fullscreen mode

If you don’t have SSH access, you can run composer install locally, zip the vendor folder, and upload it to the server.

9. Run Migrations

If you have SSH access, run the following command to migrate the database:

php artisan migrate --force
Enter fullscreen mode Exit fullscreen mode

If you don’t have SSH access, you can run the migrations locally and then export/import the database to the server via phpMyAdmin.

10. Set App Key

Generate a new application key if you haven’t already:

php artisan key:generate
Ensure the key is set in the .env file:

APP_KEY=base64:your_key_here
Enter fullscreen mode Exit fullscreen mode

11. Configure .env for Production

Set the environment to production:

APP_ENV=production
APP_DEBUG=false
Enter fullscreen mode Exit fullscreen mode

12. Point Your Domain to the Laravel Project

If your Laravel project is inside the public_html folder, it will automatically load when someone visits your domain. If it’s inside a subdirectory, point the domain or subdomain to the correct folder.

13. Enable Pretty URLs (Remove /public from the URL)

You might need to edit the .htaccess file in the root directory:

Add the following to your .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
Enter fullscreen mode Exit fullscreen mode

14. Test Your Application

Visit your domain or subdomain to ensure the Laravel app is working properly. Check for any errors and fix any configuration issues.

15. Optional: Use SSH or Git for Deployment

If your hosting supports Git, you can set up deployment using a repository, which makes it easier to push updates to the live server.

You Can Learn Laravel 11: How to Generate PDF and Send Emails – Step-by-Step Tutorial

Top comments (0)