Skip to main content
Laravel Basics Tutorial
CHAPTER 18 Beginner

Deploying Laravel Applications

Updated: May 14, 2026
20 min read

# CHAPTER 18

Deploying Laravel Applications

1. Introduction

Building an application on localhost:8000 is only half the battle. To allow users worldwide to access your software, you must Deploy it to a live web server. Deploying Laravel is significantly more complex than deploying a simple HTML site because of its dependencies, environment variables, and directory structure. In this chapter, we will learn how to safely push a Laravel application to a live production server.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the difference between the public folder and the core application files.
  • Configure environment variables (.env) for production.
  • Migrate a local MySQL database to a live server.
  • Optimize Laravel for production speeds.

3. Beginner-Friendly Explanation

Imagine building a highly secure bank vault. If you build the vault in the middle of a public park, anyone can walk up and inspect the locks. This is dangerous. Instead, you build the vault deep underground (the Server). The only thing you put in the public park is an ATM machine (the public/ folder). The ATM connects to the vault through a secure underground wire. When deploying Laravel, the core framework files (Models, Controllers, .env) MUST be hidden from the internet. Only the public/index.php file and your CSS/JS should be exposed to web traffic.

4. Hosting Options

  • Shared Hosting (cPanel/Hostinger): Cheap and popular, but deploying Laravel here requires complex file manipulation to hide the core files.
  • VPS (DigitalOcean/Linode): You rent a blank Linux server and install everything yourself. Highly customizable, but requires Linux command-line skills.
  • PaaS (Laravel Forge / Heroku): The industry standard. Laravel Forge connects to your GitHub repository, provisions a server, and deploys your code automatically whenever you push to the main branch.

5. Deployment Step 1: The .env File Setup

Your local .env file contains your local database passwords. You do not upload this to the live server! On the live server, you must create a *new* .env file with the live server's credentials.

CRITICAL Production Changes:

env
123456789
APP_ENV=production
APP_DEBUG=false
APP_URL=https://www.mycoolapp.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=live_database_name
DB_USERNAME=live_db_user
DB_PASSWORD=SuperSecretLivePassword123!

*(If APPDEBUG is left true, your app is vulnerable to severe hacking!)*

6. Deployment Step 2: Transferring Files

If using a modern workflow, you push your code to GitHub, log into your server via SSH, and run git pull. If using Shared Hosting, you use FTP (FileZilla):
  1. 1. Zip your entire project (excluding the vendor folder and .env file).
  1. 2. Upload the zip to the server.
  1. 3. Unzip the files into a private directory (NOT publichtml).
  1. 4. Copy the contents of Laravel's public/ folder into the server's publichtml folder.
  1. 5. Edit publichtml/index.php to point to the new location of the hidden core files.

7. Deployment Step 3: Installing Dependencies

Your live server does not have the vendor folder (the Laravel engine). You must install it. SSH into your server, navigate to your project folder, and run:
bash
1
composer install --optimize-autoloader --no-dev

*(The --no-dev flag tells Composer not to install heavy testing tools that are only needed on your laptop).*

8. Deployment Step 4: The Database

  1. 1. Open your live hosting dashboard (e.g., phpMyAdmin) and create a blank database.
  1. 2. Ensure the credentials match your live .env file.
  1. 3. Run the migrations on the live server via SSH:
bash
1
php artisan migrate --force

*(The --force flag is required because Laravel knows it is in production and wants to make sure you really intend to alter the live database).*

9. Deployment Step 5: Production Optimization

Laravel reads dozens of configuration files on every click. In production, this is slow. You must tell Laravel to cache everything into one massive, fast file.

Run these Artisan commands on the live server:

bash
123
php artisan config:cache
php artisan route:cache
php artisan view:cache

10. Common Mistakes

  • The 500 Server Error: If you deploy your app and see a blank 500 error, 99% of the time it is a file permission issue. The web server (Apache/Nginx) MUST have write permissions to the storage/ and bootstrap/cache/ directories. (e.g., chmod -R 775 storage).

11. Exercises

  1. 1. Why must the .env file remain hidden, and what are the two critical variables (APPENV and APPDEBUG) that must be changed before an app goes live?

12. Coding Challenges

  • Challenge: Research how to set up a free GitHub repository. Explain the workflow of how pushing code to GitHub can trigger an automated deployment using a service like Laravel Forge.

13. MCQs with Answers

Question 1

When deploying a Laravel application to a live server, which folder is the ONLY folder that should be accessible to the public internet?

Question 2

Which Artisan command must be run on a production server to compile all configuration files into a single file for massive speed improvements?

14. Interview Questions

  • Q: Walk me through the security architecture of a deployed Laravel application. Why is it disastrous to put the entire Laravel framework inside a shared host's public_html directory?
  • Q: Explain the purpose of running composer install --no-dev and php artisan route:cache during a production deployment pipeline.

15. FAQs

Q: I changed a variable in my live .env file, but the site didn't update! A: Because you ran php artisan config:cache! The app is ignoring the .env file and reading the cache. Whenever you edit the .env on a live server, you must run php artisan config:clear to erase the old cache.

16. Summary

In Chapter 18, our application went public. Deploying Laravel requires strict adherence to security and performance protocols. By isolating the core framework files from the public internet, configuring our production environment variables securely, executing live migrations, and aggressively caching our routes and configurations, we guarantee our application is fast, impenetrable, and ready for real users.

17. Next Chapter Recommendation

Your app is live, and 10,000 users just logged on at the exact same time. The server is crashing. Proceed to Chapter 19: Optimizing and Scaling Laravel Applications.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·