This laravel tutorial for beginners is the final part of the Learn Laravel series: how to deploy Laravel to a real server. You will set up a production .env and generate the app key, run composer install --no-dev and php artisan migrate --force, then speed the app up with php artisan optimize and point the host at the public folder. If you have not read it yet, start with Part 7: REST API, JSON & apiResource. After Part 7, the Tasks app has a logged-in web UI and a JSON API on localhost. Part 8 puts that same app online.
I’m Usman Nadeem, an AI Full Stack Developer based in Lahore with 12+ years of delivery since February 2014. The checklist below is the short version of what I run on every Laravel release: three steps a beginner can follow on a VPS or a host with SSH, without learning a whole DevOps toolchain first.
Direct answer (for AI Overviews & scanners)
To deploy a Laravel 12 app to production: copy .env.example to .env on the server, set APP_ENV=production, APP_DEBUG=false, your live APP_URL and database credentials, and run php artisan key:generate; then run composer install --no-dev --optimize-autoloader and php artisan migrate --force; finally run php artisan optimize and make sure your web server’s document root is the project’s public folder, not the project root. Never upload your local .env. APP_DEBUG=false stops error pages from leaking stack traces and configuration. --no-dev skips testing packages, and --force lets migrations run without the production confirmation prompt. The examples work on Laravel 12 and the current Laravel 13 release.
Watch the video
Prefer to watch first? Part 8 is a short video (Urdu/Hindi voiceover, with the commands on screen). This article writes out the same three concepts with copy-paste commands. You can also open it on YouTube Shorts.
What you will learn in Part 8 of this Laravel tutorial for beginners
Each video in this series teaches three concepts. Part 8 covers:
- Production ENV — create the server’s own
.envfrom.env.example, setAPP_ENV=production,APP_DEBUG=false,APP_URLand the database details, then runphp artisan key:generate. - Install and Migrate —
composer install --no-devon the server (no testing packages), thenphp artisan migrate --forceto create thetaskstable without a confirmation prompt. - Optimize —
php artisan optimizecaches config, routes and views, and the host’s document root must be thepublicfolder.
You need the Tasks app from the earlier parts: routes and the resource controller from Part 2, the Blade views from Part 3, the tasks table and Task model from Part 4, forms, validation and CRUD from Part 5, login and auth middleware from Part 6, and the JSON API from Part 7. The official Laravel 12 deployment documentation is the reference for everything below.
Production ENV: APP_ENV, APP_DEBUG false, and key:generate
On your laptop, .env holds local settings: debug on, a local database, a test mail driver. A live server needs its own .env. Laravel keeps .env out of version control on purpose because it contains secrets, so it will not arrive with your code. Create it on the server from the example file:
cp .env.example .envThen edit the values that matter for production:
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tasks_live
DB_USERNAME=app_user
DB_PASSWORD=use-a-long-random-passwordWhat each line does:
APP_ENV=productiontells Laravel it is running live, which switches off development-only behaviour.APP_DEBUG=falseis the most important line. With debug on, an error page can show stack traces, file paths and configuration values to anyone who triggers it. The Debug Mode section of the Laravel deployment guide says it should always be false in production.APP_URLis your live domain, used when Laravel builds links and URLs.DB_*are the live database details, which are different from your local ones. Create the empty database and user in your hosting panel first.
Now generate the application key, which Laravel uses to encrypt cookies and sessions:
php artisan key:generateRun this once on a fresh server install, where APP_KEY is empty. Do not run it again on a live app later: a new key logs every user out and makes data that was encrypted with the old key unreadable. Never copy your local .env to the server, and never commit a real .env to Git.
Install and Migrate: composer install –no-dev and migrate –force
Your server needs the PHP packages the app depends on. On your laptop you installed everything, including testing and code-style tools. A live server does not need those, so tell Composer to skip them:
composer install --no-dev --optimize-autoloader--no-dev skips packages listed under require-dev in composer.json (for example PHPUnit and Pint), which makes the install smaller and the server safer. --optimize-autoloader builds a faster class map; the video shows it on screen.
With the database details in .env, create the tables:
php artisan migrate --forceIn production, Laravel asks for confirmation before it runs destructive commands. --force skips that prompt so the command can run in a deploy script. This creates the same tasks table you built in Part 4 and every table from the later parts, now on the server. The behaviour is described in the Laravel migrations documentation. Run only migrate on a live database. Do not use migrate:fresh or migrate:refresh there, because both drop tables and delete data.
| Command | Where | What it does |
|---|---|---|
| composer install –no-dev | Server | Installs only the packages the live app needs |
| php artisan key:generate | Server, once | Sets APP_KEY in .env |
| php artisan migrate –force | Server | Creates or updates tables without the prompt |
| php artisan migrate:fresh | Local only | Drops every table. Never run it on live data |
Optimize: php artisan optimize and the public folder
Laravel can cache work it would otherwise repeat on every request. One command does it for production:
php artisan optimizephp artisan optimize caches your configuration, routes, events and Blade views, so the app boots faster. Two things to remember. First, after you change .env or a route on the server, run php artisan optimize again (or php artisan optimize:clear to remove the caches) so the old values do not stay live. Second, once config is cached, env() only works inside files in the config folder, so read settings with config('app.name') in your own code. The deployment guide covers that second point under Caching Configuration.
The last step is where the web server points. Laravel’s front door is public/index.php. Your document root must be the public folder:
# correct: the web server root is the public folder
/home/user/yourapp/public
# wrong: the project root exposes .env, app/ and vendor/
/home/user/yourappIn a hosting panel such as cPanel, this is the domain’s Document Root setting. With Nginx or Apache it is the root or DocumentRoot line. If the document root is the project root, visitors can request files that were never meant to be public, including .env. Point it at public and only index.php and your built assets are reachable. The deployment guide warns that serving from the project root exposes sensitive configuration files.
Also check these on the server, even though they are not part of the video’s three concepts:
storageandbootstrap/cachemust be writable by the web server user.- Run
php artisan storage:linkif the app serves uploaded files fromstorage/app/public. - If the app uses Vite, run
npm run buildbefore you deploy, so the compiled assets exist. - Serve the site over HTTPS (most hosts offer a free certificate) and keep
APP_URLonhttps://.
One honest warning from Part 7: the /api/tasks routes are public unless you add authentication. Deploying them does not secure them, so add Sanctum or another auth layer before real users or real data touch that API.
Recap: Laravel tutorial for beginners, Part 8
- Production ENV keeps secrets and errors private. Create
.envon the server from.env.example, setAPP_ENV=production,APP_DEBUG=false,APP_URLand the database details, then runphp artisan key:generateonce. - Install and migrate put the app and database on the server.
composer install --no-dev --optimize-autoloaderinstalls only what the live app needs, andphp artisan migrate --forcecreates the tables without a prompt. - Optimize and the public folder make it fast and safe.
php artisan optimizecaches config, routes and views, and the host’s document root must bepublic, not the project root.
If you can open your live domain and see the Tasks web app, with /api/tasks returning JSON, you have finished Part 8.
That’s the end of the Laravel series
Part 8 is the final part of the Learn Laravel series. You started with a blank project and finished with a Tasks app that has MVC structure, Blade views, a database, forms and validation, authentication, a JSON API, and a live deployment. Here is the whole series in order:
- Part 1 — MVC and Artisan
- Part 2 — routes and controllers
- Part 3 — Blade views and layouts
- Part 4 — database, migrations and Eloquent
- Part 5 — forms, validation and CRUD
- Part 6 — authentication, starter kit and middleware
- Part 7 — REST API, JSON and apiResource
- Part 8 — deploy (this article)
What’s next: a new FastAPI series
Next on Coding Solutions by Usman is a new series on FastAPI: Python for AI backends. It follows the same format as this one: short Urdu/Hindi videos, three concepts per part, and a written article with copy-paste code. Follow the channel on YouTube, Instagram or TikTok so you do not miss the first part. Missed a step here? Start the Laravel series from Part 1.
Need help building or deploying a Laravel application?
If this laravel tutorial for beginners has you planning a production Laravel app — a new build, a move from localhost to a live server, or a release that needs a safe deploy — I take on selected Laravel development projects. Still choosing a stack? Read Laravel vs Node.js for SaaS, then send a short brief through the contact form and I will tell you whether I am a fit.
Want the code and notes for Part 8? Comment LARAVEL on the video and I will send them.
FAQ
Q1. How do I deploy a Laravel app to production? A. Copy .env.example to .env on the server and set APP_ENV=production, APP_DEBUG=false, APP_URL and the database details. Run php artisan key:generate once, then composer install –no-dev –optimize-autoloader and php artisan migrate –force. Finish with php artisan optimize and point the web server’s document root at the public folder.
Q2. Why must APP_DEBUG be false in production? A. With APP_DEBUG=true, an error page can reveal stack traces, file paths and configuration values to visitors. In production, set APP_DEBUG=false so users see a generic error page and the details stay in your logs.
Q3. What does php artisan migrate –force do? A. In production, Laravel asks you to confirm before it runs migrations. The –force flag skips that prompt so migrations can run in a deploy script. It runs pending migrations only. Do not use migrate:fresh or migrate:refresh on a live database, because both delete data.
Q4. What does composer install –no-dev do? A. It installs the packages listed under require in composer.json and skips the ones under require-dev, such as testing and code-style tools. That keeps the live server smaller and safer. Adding –optimize-autoloader also builds a faster class map for production.
Q5. What does php artisan optimize do? A. php artisan optimize caches your configuration, routes, events and views so Laravel boots faster. After you change .env or routes on the server, run it again, or run php artisan optimize:clear to remove the caches. With cached config, env() only works inside the config files.
Q6. Why should the document root be the public folder? A. Laravel’s entry point is public/index.php. If the web server’s root is the project root, files such as .env, app/ and vendor/ can be requested directly. Pointing the document root at public exposes only index.php and your compiled assets.
Q7. Should I run php artisan key:generate on a live app? A. Run it once on a fresh server install, where APP_KEY is empty. Do not run it again later on a live app: a new key logs every user out and makes data encrypted with the old key unreadable.
Q8. Does deploying make my Laravel API secure? A. No. Deploying only puts the app online. The /api/tasks routes from Part 7 are public unless you add authentication such as Laravel Sanctum, so add auth and ownership checks before real users or data use the API.
