Canvas LMSをインストールする
※検証日は2025年6月です。
※ConoHaWingの2GBと4GBでCanvas LMSをUbuntu 24にインストールテストを実施しました。
はじめに
Canvas LMSをUbuntu 24にインストールする為のメモです。
構築事態はこれでできましたが追加設定や修正はお願いします。
サーバーは8GB以上を推奨としますが2GBと4GBでもメモリスワップ領域を追加すれば可能です。
途中で必要な場面がありますので先にその他を読んで追加しても大丈夫です。
ポート開放
production版の為,SSL(TLS)が必須です。
非SSL環境で実施する場合は追加で設定が必要です。
ufw allow 80
ufw allow 443
ufw reload
必要なものをインストール
production版(本番環境)で構築していきます。
Canvas LMSの日本語での情報があまりにもない為、セキュリティ強度が不明です。
自己判断で構築してください。
sudo apt update
sudo apt install -y git curl gnupg2 build-essential libxml2-dev libxslt1-dev libpq-dev libsqlite3-dev \
imagemagick libmagickwand-dev python3 python3-pip nodejs yarn redis-server postgresql postgresql-contrib
sudo apt install -y libyaml-dev libreadline-dev zlib1g-dev libssl-dev libffi-dev
sudo apt install -y libidn11-dev libxmlsec1-dev pkg-config
Nodeとyarnをインストールする
公式の推奨バージョンをインストールします。
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
よくyarnはエラーがでます。
npm install -g yarn
エラーがでたら以下をためしてください(何度かやる必要があるかもそれか--force)
sudo rm /usr/bin/yarn
npm install -g yarn
データベース作成中にエラーがでた為、gulp-cliのインストールを追加。
npm install -g gulp-cli
Rubyのインストール
rbenv のインストール
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
source ~/.bashrc
ruby-buildのインストール
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Rubyのインストール (例: 3.4.1)
※bundle install でバージョンエラーが出る場合があります。
エラーが出た場合は指定のバージョン以上に変更してください。
rbenv install 3.4.1
rbenv global 3.4.1
bundlerのインストール
gem install bundler
Canvas LMSのクローン
2GBくらいあります。
今回はoptにインストールします。
cd /opt
git clone https://github.com/instructure/canvas-lms.git
cd canvas-lms
設定ファイルの作成
必要に応じてconfig/database.yml等を編集してください。
cp config/database.yml.example config/database.yml
cp config/outgoing_mail.yml.example config/outgoing_mail.yml
cp config/domain.yml.example config/domain.yml
cp config/security.yml.example config/security.yml
cp config/delayed_jobs.yml.example config/delayed_jobs.yml
データベースの作成
DBユーザー名やDB名は自由に変更してください。
sudo -u postgres createuser canvas --no-createdb --no-superuser --no-createrole --pwprompt
sudo -u postgres createdb canvas_production --owner=canvas
データベースを設定する
vi config/database.yml
productionを編集する
ダブルクォーテーションしといたほうがいいです。
production:
adapter: postgresql
encoding: utf8
database: canvas_production
host: 127.0.0.1
username: canvas
password: "あなたが設定したパスワード"
編集後はこんな感じ
# do not create a queue: section for your test environment
test:
adapter: postgresql
encoding: utf8
database: canvas_test
host: localhost
username: canvas
password: your_password
timeout: 5000
shard1: canvas_test_shard_1
shard2: canvas_test_shard_2
development:
adapter: postgresql
encoding: utf8
database: canvas_development
password: your_password
timeout: 5000
secondary:
replica: true
username: canvas_readonly_user
production:
adapter: postgresql
encoding: utf8
database: canvas_production
host: 127.0.0.1
username: canvas
password: "あなたが設定したパスワード"
bundleやyarnのインストール
bundle config set --local path vendor/bundle
bundle install
yarn install --pure-lockfile && yarn install --pure-lockfile
gulp rev
encryption keyを編集
データベースの初期化でencryption keyが短すぎるというエラーが出た為追加設定
vi config/security.yml
encryption_key: 長いランダムな文字列に変更します。
以下は例です(そのまま使わないでください)。
encryption_key: "tK2fR1Z9tQ8wLd3vSb6xYp0mN7eHs5zB"
rubyで長い乱数を生成してみる
必要があれば使ってください。
ruby -rsecurerandom -e 'puts SecureRandom.hex(32)'
dbのマイグレーションを実施する
Springエラーが絶対にでるきもするので1つしたをやった方が確実かも
bin/rails db:migrate RAILS_ENV=production
Spring関連でエラーが出た場合はこっちを試してください
DISABLE_SPRING=1 bin/rails db:migrate RAILS_ENV=production
データベースの初期化
RAILS_ENV=production bundle exec rake db:initial_setup
管理者ユーザーの作成等もあります What email address will the site administrator account use? メールアドレスの設定
What password will the site administrator use?
What do you want users to see as the account name? This should probably be the name of your organization. > サイト名や組織名をいれてください。
To help our developers better serve you, Instructure would like to collect some usage data about your Canvas installation. You can change this setting at any time.:
- Opt in(利用状況データを(個人情報含めて)送信します)
- Only send anonymized data(匿名化された利用データのみ送信します)
- Opt out completely(データを一切送信しません) 利用状況データをどこまで送るかです。 3が無難です。
compile_assetsを実行する
※サーバーメモリが2、4GBだと失敗するのでメモリスワップ有効化部分を必ず読んで有効化してください。
(8GB等は試してませんが発生するようならスワップ領域を作成してください)
runtime-corejs3がないといわれたので追加してます。
yarn add -W @babel/runtime-corejs3
RAILS_ENV=production NODE_ENV=production bundle exec rake canvas:compile_assets
サーバーの起動
RAILS_ENV=production bundle exec rails server
warning以外にエラーがでてなければ以下のようにサーバーのURLがでるはずです。
URLがでているなら基本的には問題がないはずなので次へ
Puma starting in single mode...
* Puma version: 6.6.0 ("Return to Forever")
* Ruby version: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
* Min threads: 0
* Max threads: 1
* Environment: production
* PID: 41683
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop
ドメインを指定する
vi config/domain.yml
productionのドメインを自分のドメインにしてください。
production:
domain: "momijiina.com"
# whether this instance of canvas is served over ssl (https) or not
# defaults to true for production, false for test/development
ssl: true
# files_domain: "canvasfiles.example.com"
サーバーの自動起動設定と永続化(例)
vi /etc/systemd/system/canvas.service
/root/.rbenv/shims/bundleはruby等をインストールしたときのユーザー等なので変更してください。
[Unit]
Description=Canvas LMS Rails Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/canvas-lms
Environment=RAILS_ENV=production
ExecStart=/root/.rbenv/shims/bundle exec rails server -b 0.0.0.0 -p 3000
Restart=always
[Install]
WantedBy=multi-user.target
リロードと有効化する
sudo systemctl daemon-reload
sudo systemctl enable canvas
sudo systemctl start canvas
sudo systemctl status canvas
Certbotをインストールする
sudo apt -y install certbot
証明書を取得する
ポート開放で80が必須です。
nginx等で使われている場合は停止してください。
sudo certbot certonly --standalone -d momijiina.com
Nginxの最新版をインストールする
最新のNginx取得に必要なものをインストール
sudo apt -y install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
nginx の署名鍵をインポート
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
署名確認(面倒なら不要)
gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
最新の安定板を取得する
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
インストール
sudo apt update
sudo apt -y install nginx
nginx のバージョン確認
2025年6月
nginx version: nginx/1.28.0と表示されました。
nginx -v
Nginx の自動起動設定
systemctl enable nginx --now
nginxの権限をwww-dataへ変更する
別にnginxでもいいですがwww-data部分は権限設定をした場所を合わせてください。
user nginx;を以下に user www-data;
cd /etc/nginx
vi nginx.conf
Canvas所有者をwww-dataに変更
sudo chown -R www-data:www-data /opt/canvas-lms/public
sudo chown -R www-data:www-data /opt/canvas-lms/tmp
ディレクトリに実行権限、ファイルに読み取り権限を与える
sudo chmod -R 755 /opt/canvas-lms/public
今回はNginxのデフォルトファイルをいじります。
好きにファイルを作成してもらっても大丈夫です。(例です)
cd /etc/nginx/conf.d
vi default.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name momijiina.com;
ssl_certificate "/etc/letsencrypt/live/momijiina.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/momijiina.com/privkey.pem";
root /opt/canvas-lms/public;
client_max_body_size 100M;
sendfile on;
keepalive_timeout 10;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
# ファビコンやアセット等の静的ファイルはnginxが配信
location ~ ^/(assets|dist|fonts|images|javascripts|stylesheets|favicon.ico) {
expires max;
add_header Cache-Control public;
try_files $uri =404;
}
# Canvas LMSのproxy
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
}
}
nginxをリロードする
sudo systemctl reload nginx
サイトへアクセスする
https://あなたのドメイン.com へアクセスする。
ssl化されてない場合は初期設定ではログインができないので注意
その他
Rubyファイルサーバーを有効化する(不要です)
※今回、めんどくさいのでproxy_passのみで起動テストをしましたのでメモです
静的ファイル設定がデフォルトだとfalseになっています。
おすすめはしないのでその他においておきます。
vi config/environments/production.rb
以下に変更する
config.public_file_server.enabled = true
Nginx proxy_passのみを使う場合
Rubyファイルサーバーで直接は推奨されてないので注意
以下のように簡素です。
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name momijiina.com;
ssl_certificate "/etc/letsencrypt/live/momijiina.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/momijiina.com/privkey.pem";
location / {
proxy_headers_hash_bucket_size 128;
proxy_headers_hash_max_size 1024;
proxy_buffer_size 64k;
proxy_buffers 8 64k;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
proxy_next_upstream error timeout;
proxy_next_upstream_timeout 0;
proxy_next_upstream_tries 3;
proxy_redirect off;
proxy_read_timeout 120;
proxy_intercept_errors on;
proxy_pass http://127.0.0.1:3000;
}
}
ブラウザでエラーログを全て出力するようにする
本番環境では非推奨です
vi config/environments/production.rb
28行目
config.consider_all_requests_local = true
スワップ領域を確保する
アセットのコンパイル中にKilled error Command failed with exit code 137.というエラーがでました。
メモリ不足が原因でLinuxカーネルによってビルドプロセスが強制終了されたみたいです。
メモリスワップを有効化します。
2,4GBの場合はスワップ領域を作成してください。
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
スワップが有効になったか確認
swapon --show
free -h
再起動しても有効にする場合(不要)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab