Lesson 49 of 50
Laravel deployment means publishing your Laravel application to a production server so users can access it through a domain name.
Development environment:
Production environment:
Common deployment options include shared hosting, VPS hosting, cloud servers, and managed Laravel hosting. The exact steps depend on your hosting provider.
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:
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:
APP_ENV=production.APP_DEBUG=false..env outside public web access..env file to Git.Do not generate a new application key on every deployment. Changing the key can invalidate encrypted data and existing sessions.
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.
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:
migrate:fresh on a production database to perform a routine deployment.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.
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:
APP_URL to your HTTPS domain.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:
Before announcing your application, verify the following:
APP_DEBUG=false is set.public directory.Practice Assignment:
Remember: A successful deployment is not just uploading files. It also requires secure configuration, reliable database changes, correct permissions, monitoring, and backups.
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?