Introduction
In this article, we will deploy a Laravel application using Dokku (https://dokku.com), a lightweight Platform-as-a-Service (PaaS) that enables you to deploy applications on your own server easily.
1. What is Dokku?
Dokku is an open-source and extensible PaaS that runs on a single server of your choice. It allows you to deploy applications using Git and manage them similarly to Heroku.
Minimum Requirements for Dokku:
- A fresh installation of Ubuntu 20.04/22.04/24.04 or Debian 11+ x86_64 or arm64
- At least 1GB of system memory
- Optionally, a domain name pointing to your server’s IP address
2. Setting Up Dokku
A. Installing Dokku
To install the latest stable version of Dokku, run the following shell commands:
wget -NP . https://dokku.com/install/v0.35.14/bootstrap.sh
sudo DOKKU_TAG=v0.35.14 bash bootstrap.sh
This installation process takes around 5-10 minutes, depending on your internet speed.
B. Configuring SSH Key and Virtual Host Settings
To allow deployment from another machine, add its SSH key:
dokku ssh-keys:add <NAME> <PATH_TO_PUBLIC_KEY>
or
echo "ssh-rsa ..." | dokku ssh-keys:add <NAME>
Example:
dokku ssh-keys:add developer ~/.ssh/developer.pub
3. Setting Up Databases and Caching
Dokku provides official plugins for various databases, including PostgreSQL and Redis. We will install and configure them.
A. Installing PostgreSQL Plugin
sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
B. Creating a PostgreSQL Database
dokku postgres:create myapp_db
C. Install Redis for Caching and Queues (Optional)
sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis
D. Creating a Redis Instance
dokku redis:create myapp_redis
4. Configuring the Laravel Application
A. Creating the Dokku Application
dokku apps:create myapp
B. Linking the Database and Redis
dokku postgres:link myapp_db myapp
dokku redis:link myapp_redis myapp
If the database is on another server:
dokku config:set myapp DATABASE_URL=postgres://user:password@host:port/db_name
C. Setting Environment Variables
Replace the values in the following command and execute it:
dokku config:set myapp \
APP_DEBUG=false \
APP_ENV=prod \
APP_NAME="My App" \
APP_URL=https://myapp.com \
ASSET_URL=https://myapp.com \
DB_CONNECTION=pgsql \
MAIL_ENCRYPTION=ssl \
MAIL_FROM_ADDRESS=noreply@my-app.com \
MAIL_FROM_NAME=MyApp \
MAIL_HOST=smtp.mailer.com \
MAIL_MAILER=smtp \
MAIL_PASSWORD=yourpassword \
MAIL_PORT=2525 \
MAIL_USERNAME=yourusername \
NPM_CONFIG_PRODUCTION=false \
YARN_PRODUCTION=false
D. Assigning a Domain Name
dokku domains:add myapp myapp.com
E. Persistent Storage
dokku storage:ensure-directory myapp_storage
dokku storage:mount myapp /var/lib/dokku/data/storage/myapp_storage:/app/storage
5. Deployment Configuration
To ensure smooth deployment, we need to define configuration files for Dokku in your application.
Create the following files on the root of your Laravel project
A. .buildpacks
Its used to tell what kind of application you need to build.
https://github.com/heroku/heroku-buildpack-nodejs
https://github.com/heroku/heroku-buildpack-php
If your application is an API then use only the heroku-buildpack-php
.
B. php.ini
upload_max_filesize = 200m
post_max_size = 200m
memory_limit = 512M
C. nginx.conf
client_max_body_size 200m;
location / {
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
D. app.json
It's used to declare the deployment scripts.
{
"scripts": {
"dokku": {
"predeploy": "./.scripts/predeploy.sh",
"postdeploy": "./.scripts/postdeploy.sh"
}
}
}
E. Procfile
Create a Procfile to define the processes to be executed:
web: vendor/bin/heroku-php-nginx -C nginx.conf -i php.ini public/
worker: php artisan queue:work --timeout=60 --sleep=3 --tries=3 --queue=high,default
Scale the processes on the server:
dokku ps:scale myapp web=1 worker=1
F. Deployment scripts
Create a .scripts
folder on the root folder, then it this folder
Create predeploy.sh
to optimize the application before deployment:
#!/bin/bash
mkdir -p storage/app/public
mkdir -p storage/framework/{cache/data,sessions,testing,views}
mkdir -p storage/logs
chmod -R 755 storage
chmod -R 755 public
php artisan storage:link
php artisan optimize
Create postdeploy.sh
to handle database migrations after deployment:
#!/bin/bash
php artisan migrate --force
Make the scripts executable:
chmod +x -R .scripts
6. Configuring the Local Development Machine
A. Add Remote Repository
git remote add dokku dokku@<SERVER_IP>:myapp
B. Push the Application to Dokku
git push dokku main:main
C. Finalize Laravel Setup
Generate the Laravel application key:
dokku run myapp php artisan key:generate --show
dokku config:set myapp APP_KEY=<PASTE_YOUR_KEY_HERE>
7. Enabling SSL with Let’s Encrypt (optional)
Secure your application with a free SSL certificate
dokku letsencrypt:set myapp email you@example.com
dokku letsencrypt:enable myapp
Conclusion
With this setup, your Laravel application is fully deployed using Dokku with PostgreSQL and Redis. Dokku simplifies self-hosted deployments by providing a Heroku-like experience on your own server. You can now focus on building your application while leveraging a powerful and flexible deployment pipeline.
If you found this guide helpful, feel free to share it or leave a comment!
Top comments (2)
Thank you for this article, I have never done it yet but it will be very useful to me when the time comes. It is very detailed and qualitative
Thank you for your kind words! I'm glad you found the article useful. When you get the chance to try it out, feel free to reach out if you have any questions or run into any issues. Happy coding!