Laravel Deployment: Setup environment

Our Laravel app will be cloned from source control:

# cd /path-to-app && git clone <app_git_repo>

Then create the .env file: nano .env

APP_NAME=""
APP_ENV=production
APP_KEY=base64:......
APP_DEBUG=true
APP_URL=https://laravel.app.domain

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=
REDIS_PASSWORD=
REDIS_PORT=
MAIL_DRIVER=
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=""
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Install composer dependencies:


# from official documentation
# Autoloader Optimization

When deploying to production, make sure that you are optimizing Composer's class autoloader map so Composer can quickly find the proper file to load for a given class:

composer install --optimize-autoloader --no-dev

If your run into the following error: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes, update AppServiceProvider file:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    ...
    Schema::defaultStringLength(191);
}

Setup Queue connections

# php artisan queue:table
# php artisan migrate

Setup supervisor:

# apt install supervisor 

Configure supervisor for laravel queue:

nano /etc/supervisor/conf.d/queue.conf
[program:queue]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-app/artisan queue:work --tries=3 --daemon --queue=notifications -vvv
user=www-data
autostart=true
autorestart=true
numprocs=3
redirect_stderr=true
stdout_logfile=/path-to-app/storage/logs/queue.log

Reload supervisor:

# supervisorctl reread
# supervisorctl update
# supervisorctl restart all

Setup directory permissions: