22. Jupyter Notebook構築 [さくらのVPS/CentOS7]

・Jupyter Notebook のインストール

#-- jupyter notebook のログインするパスワードを設定
PASSWORD=********
JPUSER=jupyter
JPHOME="/home/${JPUSER}"
JPPORT=8081

yum install -y openssl-devel readline-devel sqlite-devel

adduser ${JPUSER}
git clone https://github.com/yyuu/pyenv ${JPHOME}/.pyenv
echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ${JPHOME}/.bash_profile
echo 'export PATH="${PYENV_ROOT}/bin:${PATH}"' >> ${JPHOME}/.bash_profile
echo 'eval "$(pyenv init -)"' >> ${JPHOME}/.bash_profile
chown -R ${JPUSER}. ${JPHOME}/.pyenv

#-- python3
su -l ${JPUSER} -c "yes | pyenv install anaconda3-5.3.1"
su -l ${JPUSER} -c "pyenv global anaconda3-5.3.1"
su -l ${JPUSER} -c "pyenv rehash"
su -l ${JPUSER} -c "yes | conda create --name py3.7 python=3.7 anaconda"

cat <<_EOF_> /tmp/ana3.sh
source /home/${JPUSER}/.pyenv/versions/anaconda3-5.3.1/bin/activate py3.7
conda install jupyter ipykernel
jupyter kernelspec install-self --user
pip install chainer
_EOF_

su -l ${JPUSER} -c "/bin/bash /tmp/ana3.sh"

#-- python2
su -l ${JPUSER} -c "yes | pyenv install anaconda2-5.3.1"
su -l ${JPUSER} -c "pyenv global anaconda2-5.3.1"
su -l ${JPUSER} -c "pyenv rehash"
su -l ${JPUSER} -c "yes | conda create --name py2.7 python=2.7 anaconda"

cat <<_EOF_> /tmp/ana2.sh
source /home/${JPUSER}/.pyenv/versions/anaconda2-5.3.1/bin/activate py2.7
conda install jupyter ipykernel
jupyter kernelspec install-self --user
pip install chainer
_EOF_
su -l ${JPUSER} -c "/bin/bash /tmp/ana2.sh"

#-- 設定作成
su -l ${JPUSER} -c "jupyter notebook --generate-config"
hashedp=$(su -l ${JPUSER} -c "python -c 'from notebook.auth import passwd; print(passwd(\"${PASSWORD}\",\"sha256\"))'")

cat <<_EOF_>>  ${JPHOME}/.jupyter/jupyter_notebook_config.py
c.NotebookApp.password = '${hashedp}'
c.NotebookApp.port = ${JPPORT}
c.NotebookApp.open_browser = False
c.NotebookApp.ip = '127.0.0.1'
c.NotebookApp.notebook_dir = '${JPHOME}'
c.NotebookApp.base_url = 'https://masdon.life/notebook/'
c.NotebookApp.base_url = 'notebook/'
_EOF_

#-- 起動スクリプト作成
cat <<_EOF_> /etc/systemd/system/jupyter.service
[Unit]
Description = jupyter daemon

[Service]
ExecStart = /home/${JPUSER}/.pyenv/shims/jupyter notebook
Restart = always
Type = simple
User = ${JPUSER}

[Install]
WantedBy = multi-user.target
_EOF_

#-- 起動
systemctl enable jupyter
systemctl start jupyter

#-- nginx の設定作成
cat <<'_EOF_' > /etc/nginx/conf.d/https.d/notebook.conf
  location ^~ /notebook {
    location /notebook/ {
      proxy_pass       http://localhost:8081;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_http_version 1.1;
      proxy_redirect off;
      proxy_buffering off;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_read_timeout 86400;
    }
  }
_EOF_

systemctl reload nginx

URLは https://masdon.life/notebook/

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)