How to deploying Laravel projects on a live server

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

How to deploying Laravel projects on a live server, you’ll need to follow a structured process. Here’s a step-by-step guide to help you:

How to deploying Laravel projects on a live server

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
  • 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';
  • To:
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
  • 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

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

8. Install Composer Dependencies

If you have SSH access:

  1. SSH into your server using a terminal or a tool like PuTTY.
  2. Navigate to your project directory
cd /path/to/your/project
  • Run Composer to install the dependencies:
composer install --optimize-autoloader --no-dev
  • 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
  • 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

11. Configure .env for Production

  • Set the environment to production:
APP_ENV=production
APP_DEBUG=false

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]

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 How to integrate google Spreadsheet in laravel 11

This Post Has One Comment

Leave a Reply