LoginSignup
5
6

More than 5 years have passed since last update.

Laravel on Dokkuを構築する

Last updated at Posted at 2018-04-22

環境

VPSサービスはCloudGarageを使用。
OSはUbuntu 16.04。
本当はCentOSの方が慣れてるがDokkuはDebian系推奨のようなので。
作業はrootユーザで行う。
手元のLaravel開発環境は構築済とする。

Dokkuインストール

公式の通りにコマンドを叩く。

wget https://raw.githubusercontent.com/dokku/dokku/v0.12.4/bootstrap.sh;
DOKKU_TAG=v0.12.4 bash bootstrap.sh
Preparing to install v0.12.4 from https://github.com/dokku/dokku.git...
This installation script requires that you have a hostname set for the instance. Please set a hostname for 127.0.0.1 in your /etc/hosts

早速エラー。
解決するにはVPSにドメインを割り当てた上で、/etc/hostnameを変更し、さらに/etc/hostsに追記する必要がある。
ドメイン割り当ては「example.com」と「*.example.com」をAレコードで登録する。
example.comは各自のドメインに置き換えて読んで欲しい。

https://ygkb.jp/3974
http://www.broculos.net/2015/12/installing-and-running-dokku-on.html

/etc/hostname
- default_name
+ example.com
/etc/hosts
127.0.0.1 localhost
+ 127.0.1.1 example.com

設定を反映させるためにrebootする。

reboot

reboot後、気を取り直してbootstrap.shを叩く。

DOKKU_TAG=v0.12.4 bash bootstrap.sh

今度は動いたはず。

インストール終わったら、自分のvpsにブラウザでアクセス。
するとDokkuの設定画面を確認。
公開鍵の設定をしてUse virtualhost naming for appsにチェックを入れる(と書いたが、ここはお好みで)。
Finish Setupをクリック。

アプリ作成

dokkuコマンドはherokuと違ってVPS側で叩く。

dokku apps:create <アプリ名>

PosgreSQL設置。

dokku plugin:install https://github.com/dokku/dokku-postgres.git
dokku postgres:create <DB名>

PostgreSQLとアプリをつなげる。

dokku postgres:link <DB名> <アプリ名>

Procfile, .buiidpacks作成

プロジェクトルートに.buildpacksを作成。

.buildpacks
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-php.git

同じくルートにProcfile作成。

Procfile
web: vendor/bin/heroku-php-apache2 public

環境変数設定

シークレットキー

まずはシークレットキー。

php artisan key:generate --show
<シークレットキー>

シークレットキーをコピーしておく。

dokku config:set <アプリ名> APP_KEY=<シークレットキー>

DB接続設定

dokku config:get <アプリ名> DATABASE_URL
postgres://<ユーザ名>:<パスワード>@<ホスト>:5432/<DB名>
dokku config:set <アプリ名> DB_CONNECTION=pgsql DB_HOST=<ホスト> DB_DATABASE=<DB名> DB_USERNAME=<ユーザ名> DB_PASSWORD=<パスワード>

デプロイ

git remote add dokku dokku@example.com:<アプリ名>
git add -A
git commit -m "initial"
git push -u dokku master

DBマイグレートもしておく。

dokku run <アプリ名> php artisan migrate

参考

http://dokku.viewdocs.io/dokku/
https://ygkb.jp/3974
http://www.broculos.net/2015/12/installing-and-running-dokku-on.html

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6