Skip to content

Step 3 of 10

Set up .env and the database

Three things must be right here: the database user, a production .env, and the tables. Most "500 Server Error" pages on a new install come from one of them.

Create the database

  1. 1

    In cPanel → Manage My Databases, create a database and a user.

  2. 2

    Add the user to the database with All Privileges. This step is easy to miss, and it causes "Access denied" errors.

  3. 3

    Note the full names. cPanel adds your account name, so shop becomes cpaneluser_shop.

A production .env

APP_ENV=production
APP_DEBUG=false
APP_URL=https://example.com

LOG_CHANNEL=daily
LOG_LEVEL=warning

DB_HOST=127.0.0.1
DB_DATABASE=cpaneluser_shop
DB_USERNAME=cpaneluser_shop
DB_PASSWORD="your password"

SESSION_DRIVER=database
CACHE_STORE=database
QUEUE_CONNECTION=database
Keep APP_DEBUG=false on a live site. With it on, an error page can show your settings to visitors.

LOG_CHANNEL=daily keeps one file per day, so one log can't quietly fill your disk.

Set APP_KEY

With a Terminal, run php artisan key:generate. Without one, run it on your computer, then copy the APP_KEY line into the server's .env. Never change the key on a live site: encrypted data and sessions depend on it.

Create the tables

With a Terminal:

php artisan migrate --force

Without one, export the structure from your computer's database and import it in phpMyAdmin. To see the exact SQL for new migrations, run this locally:

php artisan migrate --pretend
When you import tables by hand, also add rows to the migrations table. Otherwise a later migrate tries to create them again.

Changed .env and nothing happened?

Laravel may be using a cached copy. Run php artisan config:clear, or with no Terminal delete bootstrap/cache/config.php.

Frequently asked questions

Should DB_HOST be localhost or 127.0.0.1?

Either usually works on cPanel. If one gives a connection error, try the other.

My password has special characters. Is that a problem?

Wrap it in double quotes in .env. Without quotes, characters like # and spaces can break the value.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.