- 로컬에서 개발 후, 서버 배포의 번거로움 줄이기
- pc 바뀌어도 환경에 상관없이 언제나 python 개발 가능하게 설정
#1. iwinv.kr 에서 ubuntu server 생성하고 python 3.10 설치
우선 테스트 단계에서는 저렴한 국내 서버를 사용하기로 결정
(iwinv.kr 의 CPU 1 Core/Ram 1 GB) => 5,600원/월
- Server 생성 후, Ubuntu 계정으로 접속
# ubuntu 업데이트
$ sudo apt update
$ sudo apt upgrade
# python 버전관리 위해 pyenv 설치
$ curl https://pyenv.run | bash
$ vi ~/.bashrc
# .bashrc 파일 하단에 아래 다음 단락의 내용을 추가 후,
$ source ~/.bashrc
$ pyenv install --list
# build 관련 install
$ sudo apt install build-essential liblzma-dev zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libbz2-dev libsqlite3-dev pkg-config wget
# python 3.10.16 버전 설치
$ pyenv install 3.10.16
- ~/.bashrc 하단 추가 내용
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
#2. jupyter lab 설치
가상환경도 설정한 후 시작하면 좋겠지만 다른 용도로 쓸 계획이 없으니, 우선 global 로 설치
$ pyenv global 3.10.16
$ pip install jupyterlab
#3. jupyter lab 외부접속 설정
$ jupyter lab --generate-config
Writing default config to: /home/ubuntu/.jupyter/jupyter_lab_config.py
$ vi /home/ubuntu/.jupyter/jupyter_lab_config.py
# 모든 ip 접속허용 -> 방화벽으로 막을 예정
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 8888
c.ServerApp.password = '****'
위에 비밀번호 Hash(c.ServerApp.password) 생성방법
from jupyter_server.auth import passwd
passwd()
jupyter lab 실행
$ jupyter lab
브라우저에서 http://서버_IP:8888 로 접속
#4. 재부팅후 자동 실행되도록 service 등록
- /etc/systemd/system/jupyterlab.service 파일 생성
[Unit]
Description=JupyterLab
[Service]
Type=simple
PIDFile=/run/jupyterlab.pid
ExecStart=/home/ubuntu/.pyenv/shims/jupyter-lab --allow-root --no-browser --ip=0.0.0.0 --port=8888
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/apps
Restart=always
[Install]
WantedBy=multi-user.target
- service 등록 및 시작
$ sudo systemctl daemon-reload
$ sudo systemctl enable jupyterlab
$ sudo systemctl start jupyterlab
$ sudo reboot
- reboot 후, 브라우저에서 접속 가능한지 확인