ubuntu 20.04 サーバを (ConoHa や AWSなどで)セットアップしておいてください。
最初に、update, upgrade しておきます:
sudo apt -y update; sudo apt -y upgrade;
apache などがもし動いていれば停止しておきます:
sudo systemctl disable apache2.service
sudo systemctl stop apache2.service
サーバのホスト名が無い場合は、サーバ名を DNS 登録して、ホスト名でアクセスできるようにしておいてください。
(以下では、そのホスト名が your.host.name であるとしています。)
その後、letsencrypt で SSL証明書を取得:
(your.host.name は自分のホスト名に, your@mail.address は実際のメールアドレスに差し替えてください)
sudo snap install --classic certbot
sudo certbot certonly -n --standalone -d your.host.name -m your@mail.address --agree-tos
https://www.anaconda.com/products/individual#Downloads へ行き、Linux用の
64-Bit (x86) Installer の最新版のURLを確認して、サーバ上で wget を用いてダウンロードしてAnaconda をインストール:
wget https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh
sudo chmod +x Anaconda3-2021.11-Linux-x86_64.sh
sudo ./Anaconda3-2021.11-Linux-x86_64.sh -b -p /opt/anaconda3
LTI_CLIENT_KEY, LTI_SHARED_SECRET, JUPYTERHUB_CRYPT_KEY に使うために、
openssl rand -hex 32
を3回実行して, 長い文字列を3つ作っておく(それぞれ、xxxxxxxxxxxx, yyyyyyyyyyyy, zzzzzzzzzzzz とする)。
Anaconda のコマンドへの path を通すため、また、LTI_CLIENT_KEY, LTI_SHARED_SECRET, JUPYTERHUB_CRYPT_KEY の設定のために、
以下のように /etc/profile.d/anaconda.sh を作ります:
export PATH="$PATH:/opt/anaconda3/bin"
export LTI_CLIENT_KEY=xxxxxxxxxxxx
export LTI_SHARED_SECRET=yyyyyyyyyyyy
export JUPYTERHUB_CRYPT_KEY=zzzzzzzzzzzz
(xxxxxxxxxxxx, yyyyyyyyyyyy, zzzzzzzzzzzz は、上記の openssl rand -hex 32 の結果に置き換えること。以下も同じです。)
一旦、再起動。
sudo reboot
その後、jupyterhub をインストール:
sudo /opt/anaconda3/bin/conda install -c conda-forge jupyterhub
(Proceed ([y]/n)? と聞かれたら ENTERキーを押す)
日本語化パッケージをインストール:
sudo /opt/anaconda3/bin/pip install jupyterlab-language-pack-ja-JP
LTI認証をインストール:
sudo /opt/anaconda3/bin/pip install jupyterhub-ltiauthenticator
設定ファイル用のディレクトリを作っておく:
sudo mkdir -p /etc/jupyterhub/ /srv/jupyterhub/
設定ファイルを2つ作る:
(以下の your.host.name の部分は、自分のサーバのホスト名に置き換えてください。
JupyterHub 上のユーザ名としては、lis_person_sourcedid を使う設定になっています。これは Moodleの場合だと、username でなく、ユーザの idnumber のことです。)
import os
c = get_config()
# Allows multiple single-server per user
c.JupyterHub.allow_named_servers = True
c.JupyterHub.port = 443
c.JupyterHub.ssl_key = '/etc/letsencrypt/live/your.host.name/privkey.pem'
c.JupyterHub.ssl_cert = '/etc/letsencrypt/live/your.host.name/fullchain.pem'
c.JupyterHub.cookie_secret_file = '/srv/jupyterhub/jupyterhub_cookie_secret';
# c.JupyterHub.db_url = '/srv/jupyterhub/jupyterhub.sqlite';
# create system users that don't exist yet
c.LocalAuthenticator.create_system_users = True
# specify admin
c.Authenticator.admin_users = {'admin'}
# Set the LTI 1.1 authenticator.
c.JupyterHub.authenticator_class = "ltiauthenticator.LTIAuthenticator"
# Add the LTI 1.1 consumer key and shared secret. Note the use of
# `LTI11Authenticator` vs the legacy `LTIAuthenticator`.
c.LTI11Authenticator.consumers = {
os.environ["LTI_CLIENT_KEY"]: os.environ["LTI_SHARED_SECRET"]
}
# Set person_sourcedid (such as user's idnumber) as their user id
c.LTI11Authenticator.username_key = "lis_person_sourcedid"
# Set the user's email as their user id
# c.LTI11Authenticator.username_key = "lis_person_contact_email_primary"
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/anaconda3/bin
LTI_CLIENT_KEY=xxxxxxxxxxxx
LTI_SHARED_SECRET=yyyyyyyyyyyy
JUPYTERHUB_CRYPT_KEY=zzzzzzzzzzzz
(xxxxxxxxxxxx, yyyyyyyyyyyy, zzzzzzzzzzzz は、上記と同じ、 openssl rand -hex 32 の結果に置き換えること。)
OS起動時にJupyterHubを自動起動させるための設定ファイルを作る:
[Unit]
Description=Jupyterhub
After=syslog.target network.target
[Service]
User=root
EnvironmentFile=-/etc/jupyterhub/jup-env
ExecStart=/opt/anaconda3/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py
WorkingDirectory=/etc/jupyterhub
[Install]
WantedBy=multi-user.target
JupyterHubを自動起動するように設定し、起動する:
sudo systemctl enable jupyterhub.service
sudo systemctl start jupyterhub.service
MoodleユーザのIDナンバー(Moodleユーザプロファイルの一つ)と一致させたユーザアカウントを作成しておきます(各ユーザのファイルの保存先であるホームディレクトリも作成されます):
sudo useradd --create-home admin
sudo useradd --create-home --badname useridnumber1
sudo useradd --create-home --badname useridnumber2
...
(useridnumber1, useridnumber2 は、実際には Moodle上のユーザのIDナンバーに置き換えてください。)
これで、Moodleの「外部ツール」での呼び出しなどの、LTIでの利用ができます。
Moodleの「外部ツール」の設定画面では、
セキュアツールURL https://your.host.name/hub/lti/launch
コンシューマ鍵 xxxxxxxxxxxx
共有秘密鍵 yyyyyyyyyyyy
に設定してください。(your.host.name の部分は、自分のサーバのホスト名に置き換えてください。xxxxxxxxxxxx, yyyyyyyyyyyy も上で何度か書いた通り置き換えてください。)
Moodle の admin アカウントのIDナンバーは、admin としておくと、JupyterHub 上でも admin としてアクセスできます。
JupyterLab 上で Python だけでなく PHP も使いたい場合は、以下のように PHP と psysh と Jupyter-PHP をインストール:
sudo apt -y install php7.4 php7.4-cli composer php-zmq
wget https://psysh.org/psysh
sudo chmod +x psysh
sudo mv psysh /usr/local/bin/
wget https://litipk.github.io/Jupyter-PHP-Installer/dist/jupyter-php-installer.phar
sudo php jupyter-php-installer.phar install
なお、JupyterLab の画面の表示を日本語化するには、ログイン後のメニューの Settings - Language で Japanese を選びます。
参考にさせていただいたリソース: