Code | Solutions

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:

  1. Production ENV — create the server’s own .env from .env.example, set APP_ENV=production, APP_DEBUG=false, APP_URL and the database details, then run php artisan key:generate.
  2. Install and Migrate — composer install --no-dev on the server (no testing packages), then php artisan migrate --force to create the tasks table without a confirmation prompt.
  3. Optimize — php artisan optimize caches config, routes and views, and the host’s document root must be the public folder.

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 .env

Then 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-password

What each line does:

  • APP_ENV=production tells Laravel it is running live, which switches off development-only behaviour.
  • APP_DEBUG=false is 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_URL is 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:generate

Run 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 --force

In 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.

CommandWhereWhat it does
composer install –no-devServerInstalls only the packages the live app needs
php artisan key:generateServer, onceSets APP_KEY in .env
php artisan migrate –forceServerCreates or updates tables without the prompt
php artisan migrate:freshLocal onlyDrops 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 optimize

php 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/yourapp

In 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:

  • storage and bootstrap/cache must be writable by the web server user.
  • Run php artisan storage:link if the app serves uploaded files from storage/app/public.
  • If the app uses Vite, run npm run build before you deploy, so the compiled assets exist.
  • Serve the site over HTTPS (most hosts offer a free certificate) and keep APP_URL on https://.

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

  1. Production ENV keeps secrets and errors private. Create .env on the server from .env.example, set APP_ENV=production, APP_DEBUG=false, APP_URL and the database details, then run php artisan key:generate once.
  2. Install and migrate put the app and database on the server. composer install --no-dev --optimize-autoloader installs only what the live app needs, and php artisan migrate --force creates the tables without a prompt.
  3. Optimize and the public folder make it fast and safe. php artisan optimize caches config, routes and views, and the host’s document root must be public, 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:

  1. Part 1 — MVC and Artisan
  2. Part 2 — routes and controllers
  3. Part 3 — Blade views and layouts
  4. Part 4 — database, migrations and Eloquent
  5. Part 5 — forms, validation and CRUD
  6. Part 6 — authentication, starter kit and middleware
  7. Part 7 — REST API, JSON and apiResource
  8. 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.


Tags

2026 ai tools aiagent Angular 21 Angular Developer for Hire Angular Performance Angular Signals Angular TypeScript Best Practices API Authentication API Development API Integration API Security API Versioning B2B SaaS Backend Development Backend Framework Comparison Best PHP Framework 2026 Best Practices Box Model CakePHP CakePHP 2026 CakePHP Laravel PHP Developer CakePHP Tutorial 2026 CakePHP vs Laravel Case Study Client Management cost to hire freelance developer crewai CRM Integration css css animation CSS Tips CSS Tutorial Database Optimization Developer Tools Enterprise Angular Development Express.js FastAPI Tutorial 2026 framework comparison Freelance Angular Developer Freelance Developer freelance developer guide freelance developer tips Freelance Development freelance full-stack developer Freelance Laravel Developer Freelance PHP Developer Freelance Web Developer freelance web developer process Freelance Web Development Freelancer vs Agency Freelancing Tips Front-End Development frontend Frontend Development Full Stack Developer Full Stack Developer Pakistan Full Stack Development Full-Stack Case Study Full-Stack Development Grid Hire a Developer Hire Angular Developer hire freelance developer hire freelance full-stack developer Hire Laravel Developer how to work with a freelance dev how to write a project brief individual software engineer freelance individual software engineer hire Inertia.js javascript JavaScript Frameworks JWT Authentication laravel Laravel 13 Laravel 2026 Laravel API 2026 Laravel Application Laravel Development Laravel Optimization Scale Laravel Performance Laravel REST API Laravel REST API Tutorial Laravel Sanctum Laravel Tutorial Laravel vs CakePHP Laravel Vue Tutorial multi-agent Next.js Node.js Node.js API Node.js developer Node.js Developer Portfolio Node.js Express Node.js Tutorial Nuxt PERN Stack PHP PHP Developer PHP Developer for Hire PHP Development PHP Framework PHP Framework Comparison PHP Performance PHP Web Development PostgreSQL Prisma Production Deployment project brief template Project Management Python Backend Developer Python FastAPI react React developer for hire React Node.js Developer Portfolio React SPA React Tutorial React vs Vue Real-Time Chat App Redis Caching Responsive Design REST API SaaS Development Sanctum SMB Web Development Socket.io React Real-Time App Socket.io Tutorial Software Development software development process Software Integration software project brief SSG SSR Standalone Components System Integration TypeScript Strict Mode Vite vue Vue 4 Composition API Vue Laravel Developer Vue.js Development web app development process Web Design Web Design Fundamentals web development Web Development 2026 Web Development Cost Web Layout Web Projects WebSocket

Recent News