Skip to Content
Data EngineeringJupyter Notebook 외부 접속 서버 구축 및 운영 가이드
📊 Data Engineering2018년 10월 23일

Jupyter Notebook 외부 접속 서버 구축 및 운영 가이드

#jupyter#python#server#data-science#notebook

데이터 분석가들에게 하둡 클러스터 자원 위에서 Python 머신러닝 모델을 자유롭게 돌릴 수 있는 독립적인 분석 환경을 제공하기 위해 Jupyter Notebook 서버를 구축했습니다.

pip install jupyter로 설치한 기본 Jupyter는 보안상 localhost에서만 접속을 허용합니다. 원격 PC 브라우저에서 서버 IP로 접속하려면 추가적인 설정이 필요합니다.


1. 설치 (가상환경 권장)

시스템 전역을 오염시키지 않도록 가상환경 안에 설치합니다.

shell
# Python 3 가상환경 생성 $ python3 -m venv jupyter_env $ source jupyter_env/bin/activate # Jupyter 설치 (jupyter_env) $ pip install --upgrade pip (jupyter_env) $ pip install jupyter jupyterlab # 버전 확인 (jupyter_env) $ jupyter --version

2. 서버 설정 파일 생성

구동 시 읽어들일 마스터 설정 파일을 자동 생성합니다.

shell
$ jupyter-notebook --generate-config # → ~/.jupyter/jupyter_notebook_config.py 생성됨

3. 외부 접속 및 보안 설정

~/.jupyter/jupyter_notebook_config.py 를 열어 아래 항목을 수정합니다.

python
# 모든 IP에서 접속 허용 c.NotebookApp.ip = '0.0.0.0' # 기본 포트 (방화벽에서 open 필요) c.NotebookApp.port = 8888 # 브라우저 자동 실행 비활성화 (서버 환경) c.NotebookApp.open_browser = False # 작업 디렉터리 설정 c.NotebookApp.notebook_dir = '/data/jupyter/workspace' # CORS 허용 (리버스 프록시 사용 시) c.NotebookApp.allow_origin = '*' # 토큰 인증 비활성화 (비밀번호로 대체할 경우) c.NotebookApp.token = ''

비밀번호 설정

외부 접속을 열면 반드시 비밀번호를 설정해야 합니다.

shell
$ jupyter-notebook password Enter password: Verify password: # → ~/.jupyter/jupyter_notebook_config.json 에 해시값 저장

4. 백그라운드 데몬 실행

터미널을 닫아도 Jupyter가 계속 살아있도록 nohup으로 백그라운드 처리합니다.

shell
# startup_jupyter.sh 작성 $ cat > ~/startup_jupyter.sh << 'EOF' #!/bin/bash source ~/jupyter_env/bin/activate nohup jupyter-notebook --allow-root \ 1>/var/log/jupyter/stdout.log \ 2>/var/log/jupyter/stderr.log & echo $! > /var/run/jupyter.pid EOF $ chmod +x ~/startup_jupyter.sh $ ~/startup_jupyter.sh
shell
# 실행 확인 $ cat /var/run/jupyter.pid $ ps aux | grep jupyter

5. systemd 서비스 등록 (CentOS 7+ 권장)

재부팅 후 자동 기동을 위해 systemd 서비스로 등록합니다.

ini
# /etc/systemd/system/jupyter.service [Unit] Description=Jupyter Notebook Server After=network.target [Service] Type=simple User=datauser WorkingDirectory=/data/jupyter/workspace ExecStart=/home/datauser/jupyter_env/bin/jupyter-notebook \ --config=/home/datauser/.jupyter/jupyter_notebook_config.py Restart=on-failure RestartSec=10 StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target
shell
$ sudo systemctl daemon-reload $ sudo systemctl enable jupyter $ sudo systemctl start jupyter $ sudo systemctl status jupyter

6. 커널 추가 (다중 Python 환경 지원)

여러 가상환경의 Python을 각기 다른 커널로 등록하면, 분석가마다 독립된 패키지 환경을 제공할 수 있습니다.

shell
# TensorFlow GPU 환경을 Jupyter 커널로 등록 $ source ~/tensorflow_env/bin/activate (tensorflow_env) $ pip install ipykernel (tensorflow_env) $ python -m ipykernel install \ --user \ --name=tf_gpu \ --display-name="Python (TensorFlow GPU)"
shell
# 등록된 커널 목록 확인 $ jupyter kernelspec list Available kernels: python3 /usr/share/jupyter/kernels/python3 tf_gpu /home/datauser/.local/share/jupyter/kernels/tf_gpu

7. Nginx 리버스 프록시 설정 (선택)

기본 8888 포트 대신 표준 80/443 포트로 서비스하려면 Nginx를 앞단에 세웁니다.

nginx
server { listen 80; server_name jupyter.example.com; location / { proxy_pass http://localhost:8888; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; # WebSocket 지원 proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }

WebSocket 설정(Upgrade, Connection)이 없으면 노트북 셀 실행 결과가 실시간으로 업데이트되지 않습니다.


8. JupyterLab (다음 세대 UI)

Jupyter Notebook의 후속 UI입니다. 파일 탐색기, 탭 레이아웃, Git 통합 등 IDE에 가까운 경험을 제공합니다.

shell
$ pip install jupyterlab $ jupyter lab # 기본 실행

접속 URL은 동일하게 http://서버IP:8888/lab 입니다.

Last updated on