Laravel Deployment and Production Setup

Lesson 49 of 50

98%
1. What Is Laravel Deployment?

Laravel deployment means publishing your Laravel application to a production server so users can access it through a domain name.

Development environment:

  • Application runs on your local computer.
  • Debugging and development tools are enabled.
  • Local database credentials may be used.

Production environment:

  • Application runs on a live hosting server.
  • HTTPS protects communication.
  • Debugging is disabled.
  • Production database credentials are configured.
  • Files, logs, caches, and permissions are managed carefully.

Common deployment options include shared hosting, VPS hosting, cloud servers, and managed Laravel hosting. The exact steps depend on your hosting provider.

2. Prepare Your Laravel Project

Before uploading your project, make sure it works correctly on your development computer.

Check your installed PHP version:

php -v

Check the Laravel project dependencies:

composer check-platform-reqs

Run this command in your project directory. It checks whether the current PHP platform meets the installed packages' requirements.

Run your automated tests:

php artisan test

Install production dependencies:

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

Run the Composer command in your deployment build environment or on the server after uploading the required project files.

Before deployment:

  • Resolve important errors and failing tests.
  • Check PHP extensions required by the application.
  • Confirm that the production server supports your Laravel version.
  • Make a backup of important database and uploaded files.
  • Never include real production secrets in public repositories.
3. Configure the Production .env File

Laravel uses the .env file to read environment-specific settings such as the application URL, database credentials, and debugging mode.

Example production configuration:

APP_NAME="Student Management"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://example.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=production_database
DB_USERNAME=production_user
DB_PASSWORD=your_secure_password

Replace the example domain and database credentials with values provided by your hosting company.

Important security rules:

  • Set APP_ENV=production.
  • Set APP_DEBUG=false.
  • Use a strong database password.
  • Keep .env outside public web access.
  • Never commit the production .env file to Git.
  • Keep the same application encryption key when deploying an existing application.

Do not generate a new application key on every deployment. Changing the key can invalidate encrypted data and existing sessions.

4. Upload the Project to Your Hosting Server

You can deploy Laravel using SSH and Git, an automated deployment pipeline, or a hosting control panel.

Typical project structure:

/var/www/student-management/
    app/
    bootstrap/
    config/
    database/
    public/
    resources/
    routes/
    storage/
    vendor/
    artisan
    composer.json
    composer.lock
    .env

The example path is illustrative. Use the directory assigned by your hosting provider.

Critical rule:

Configure your website's document root to point to Laravel's public directory, not the project root.

/var/www/student-management/public

This prevents internal application files, such as configuration and source files, from being directly served by the web server.

On shared hosting, if the provider does not allow changing the document root, follow its official Laravel deployment instructions. Do not expose the project root or make the entire project publicly accessible.

5. Configure the Database and Run Migrations

Create a production database and a database user with only the permissions the application needs. Import existing data if required.

Test the database configuration:

php artisan config:clear

Confirm that your production environment variables are correctly configured before running migrations.

Run production migrations:

php artisan migrate --force

The --force option allows migrations to run in production without an interactive confirmation. Review pending migrations and back up the database before running this command.

Check the migration status:

php artisan migrate:status

Best practices:

  • Back up the database before schema changes.
  • Test migrations in a staging environment.
  • Avoid destructive changes without a recovery plan.
  • Never run migrate:fresh on a production database to perform a routine deployment.
6. Optimize Laravel for Production

Laravel provides Artisan commands that help cache configuration, routes, and views for production.

Cache configuration:

php artisan config:cache

Cache routes:

php artisan route:cache

Cache Blade views:

php artisan view:cache

These commands are commonly used during deployment. Ensure your routes and configuration are compatible with caching.

Clear cached configuration when needed:

php artisan optimize:clear

Use the clearing command when troubleshooting stale cached configuration or other cached application artifacts. Rebuild the required caches afterward.

Example deployment sequence:

composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache

Run these commands from the correct project directory and only after configuring the production environment. Follow your deployment platform's recommended order and release procedure.

7. Configure Storage, Permissions, and HTTPS

Laravel needs write access to certain directories for logs, cached files, and generated files.

Important directories include:

storage/
bootstrap/cache/

Configure ownership and permissions according to your hosting provider's security model. The web server user needs appropriate write access.

Avoid setting broad permissions such as chmod -R 777. They can expose your application to unnecessary security risks.

For publicly accessible uploaded files:

php artisan storage:link

This creates the public storage link when supported by the hosting environment. Confirm that uploaded files intended to be private are not exposed through a public link.

Enable HTTPS:

  • Configure a valid TLS certificate for your domain.
  • Enable HTTPS redirects at the web server or hosting platform.
  • Set APP_URL to your HTTPS domain.
  • Configure trusted proxy settings correctly if the application runs behind a proxy.
  • Test login forms, cookies, redirects, and uploaded files after enabling HTTPS.
8. Configure Logs, Queues, and Scheduled Tasks

A production application needs a way to identify errors and run background tasks.

Check application logs:

storage/logs/

Laravel's logging configuration is controlled through the logging configuration files and environment variables. Protect logs because they may contain sensitive operational information.

Run a queue worker:

php artisan queue:work

For production, run the worker under a process manager such as Supervisor or use your hosting platform's managed worker service. Configure failed-job handling and restart workers during deployments when required.

Run scheduled tasks:

On a server with cron access, Laravel's scheduler is commonly invoked once per minute:

* * * * * cd /var/www/student-management && php artisan schedule:run >> /dev/null 2>&1

Replace the directory with your actual project path and configure the cron entry using the appropriate server account.

Production checklist:

  • Confirm that logs are being written correctly.
  • Monitor application errors and server resources.
  • Keep queue workers running when queues are used.
  • Configure scheduled tasks only when required.
  • Use a process manager for long-running workers.
9. Final Production Deployment Checklist

Before announcing your application, verify the following:

  • PHP version and required extensions are compatible.
  • Composer production dependencies are installed.
  • Production environment variables are correct.
  • APP_DEBUG=false is set.
  • The website document root points to the public directory.
  • The database connection works.
  • Migrations have been reviewed and applied.
  • Storage and cache permissions are correct.
  • HTTPS is enabled and working.
  • Authentication and authorization are tested.
  • Backups and a rollback plan are available.
  • Logs, queues, and scheduled tasks are configured where needed.
  • Important application workflows have been tested on the live environment.

Practice Assignment:

  1. Prepare a Laravel project for production.
  2. Configure a separate production database.
  3. Set up the production environment variables.
  4. Deploy the project to a test hosting server.
  5. Run migrations and configure storage.
  6. Enable HTTPS and disable debugging.
  7. Test the application's main features.
  8. Write a rollback plan for a failed deployment.

Remember: A successful deployment is not just uploading files. It also requires secure configuration, reliable database changes, correct permissions, monitoring, and backups.

Quick Quiz

1. Which value should APP_DEBUG have in production?




2. Which directory should normally be the web server's document root?




3. Which command runs Laravel migrations in production without an interactive confirmation?