Deploying Laravel Applications
# CHAPTER 18
Deploying Laravel Applications
1. Introduction
Building an application onlocalhost: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
publicfolder 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 (thepublic/ 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
mainbranch.
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:
*(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 rungit pull.
If using Shared Hosting, you use FTP (FileZilla):
-
1.
Zip your entire project (excluding the
vendorfolder and.envfile).
- 2. Upload the zip to the server.
-
3.
Unzip the files into a private directory (NOT
publichtml).
-
4.
Copy the contents of Laravel's
public/folder into the server'spublichtmlfolder.
-
5.
Edit
publichtml/index.phpto point to the new location of the hidden core files.
7. Deployment Step 3: Installing Dependencies
Your live server does not have thevendor folder (the Laravel engine). You must install it.
SSH into your server, navigate to your project folder, and run:
*(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. Open your live hosting dashboard (e.g., phpMyAdmin) and create a blank database.
-
2.
Ensure the credentials match your live
.envfile.
- 3. Run the migrations on the live server via SSH:
*(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:
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/andbootstrap/cache/directories. (e.g.,chmod -R 775 storage).
11. Exercises
-
1.
Why must the
.envfile remain hidden, and what are the two critical variables (APPENVandAPPDEBUG) 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
When deploying a Laravel application to a live server, which folder is the ONLY folder that should be accessible to the public internet?
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_htmldirectory?
-
Q: Explain the purpose of running
composer install --no-devandphp artisan route:cacheduring 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.