Warning: You are reading an outdated version of this guide. Unless you're here for a specific reason, you probably want the updated version instead.
This article covers the necessary environment configuration for running a Laravel app in production, along with a few pre-deployment optimizations.
We’ll use your app’s .env
file for the following production-specific settings:
You’ll probably need to create a fresh .env
file on the server—I suggest using the .env.example
file (which should be included by default with a Laravel app) as a starting point.
# Create a `.env` file in your Laravel project's root directory, using the
# example .env.example file file as a starting point.
laravel@server$ cd /srv/www/laravel-project
laravel@server:laravel-project$ cp .env.example .env
Then restrict the .env
file’s permissions (this file is used to store sensitive information, so it shouldn’t be readable by all users); I’d suggest 640
permissions:
# Restrict the env file's permissions (rw for owning user, r for owning group)
laravel@server:laravel-project$ chmod 640 .env
Then open the .env
file, and inside make the following changes.
# Update to a name of your choice
APP_NAME="My Cool App"
# Leave this as "production"
APP_ENV=production
# Leave this as "false"---debug mode should be disabled to avoid exposing
# sensitive config information.
# See e.g. https://laravel.com/docs/10.x/configuration#debug-mode
APP_DEBUG=false
# Set this to your server's IP address. We'll change this to your app's
# domain name when we set that up a few articles later.
APP_URL=http://1.2.3.4
Then establish your app’s database connection—I’ve included settings for SQLite, MySQL, and PostgreSQL:
# The database connection should be "sqlite"
DB_CONNECTION=sqlite
# Specify the full path to your app's SQLite database file
DB_DATABASE=/srv/www/laravel-project/path/to/database.sqlite
# You'll probably want to enable the use of foreign keys with SQLite
DB_FOREIGN_KEYS=true
# The database connection should be "mysql"
DB_CONNECTION=mysql
# Leave this as 127.0.0.1---the IP address of localhost.
# This tells Laravel that your database is hosted locally on your server.
# Update only if your DB is hosted on a different machine, in which case you
# probably know what you're doing.
DB_HOST=127.0.0.1
# Leave this as 3306---the standard port number for MySQL
DB_PORT=3306
# Change to the name of your app's database, created earlier in this guide
DB_DATABASE=foo
# Change to the name of the database user created earlier in this guide
DB_USERNAME=bar
# Change to the database user's password
DB_PASSWORD=baz
# The database connection should be "pgsql""
DB_CONNECTION=pgsql
# Leave this as 127.0.0.1---the IP address of localhost.
# This tells Laravel that your database is hosted locally on your server.
# Update only if your DB is hosted on a different machine, in which case you
# probably know what you're doing.
DB_HOST=127.0.0.1
# Leave this as 5432---the standard port number for PostgreSQL
DB_PORT=5432
# Change to the name of your app's database, created earlier in this guide
DB_DATABASE=foo
# Change to the name of the database user created earlier in this guide
DB_USERNAME=bar
# Change to the database user's password
DB_PASSWORD=baz
The settings in this article should work well for most users and are all you need to get a basic Laravel app up and running…
…but there are lots of other settings (Logging, Redis, email, AWS integration, etc.) you could reasonably tweak from the .env
file or config
folder.
These are beyond the scope of this guide (and if you’re using these more advanced features you probably know what you’re doing anyway); consider poking around your app’s config
folder and reading through Laravel docs as a starting point for learning more.
You need to generate an encryption key for your production app:
# Generate an encryption key
laravel@server$ cd /srv/www/laravel-project
laravel@server:laravel-project$ php artisan key:generate
(The Laravel project on your development machine probably had an encryption key pregenerated when you created the project, but this key won’t have been copied to the server, so you have to generate a new key server-side. See e.g. this StackOverflow answer for details.)
You’ll need to run your database migrations and seed your database for your app to run.
Exactly how you do this will depend on how you’ve set up your app, but it should usually suffice to use Artisan’s migrate
and db:seed
commands:
# Change into your Laravel project's root directory
laravel@server$ cd /srv/www/laravel-project
# Run database migrations
laravel@server:laravel-project$ php artisan migrate
# Seed your database
laravel@server:laravel-project$ php artisan db:seed
Finally, run a few standard optimizations recommended by Laravel to speed up your app in production:
laravel@server$ cd /srv/www/laravel-project
# Cache your app's routes
laravel@server:laravel-project$ php artisan route:cache
# Cache your Laravel configuration settings
laravel@server:laravel-project$ php artisan config:cache
# Precompile your Blade views for faster loading
laravel@server:laravel-project$ php artisan view:cache
These optimizations should be rerun after each (re)deployment—we’ll take care of that in a future article.
Next: The next article covers a few ownership and permission settings needed to run a Laravel app.
Finding this tutorial series useful? Consider saying thank you!
The original writing and media in this series is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.