Running iPython with Jupyter Notebook on Debian Server

When working on machine learning projects as a team, it's a good idea to have a place on the cloud where everyone can write and test all the code collaboratively. iPython is a powerful tool that enables pythoners to write, test, command and even draw inline graphics through a modern browser.

This post will show the steps of building a machine learning environment (using Anaconda distribution) with Jupyter Notebook interface on Debian Linux server.

jupyter_logo.png

You won't want the notebook runs as root because the notebook enables everyone who has access to the interface to run python codes on the server.
To eliminate such a risk, we create an isolated user to run the service.

sudo adduser ipyuser
#you may replace 'ipyuser' with username you like

Disable the user's login

sudo passwd -l ipyuser

use sudo su ipyuser to switch to ipyuser

Install Anaconda. Anaconda includes python, ipython, Jupyter Notebook and many useful machine learning libraries and tools. You may find the download link of the newest version on the offical website

wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh     
bash Anaconda3-5.1.0-Linux-x86_64.sh

The default installation path is the home directory of the current user. You may change it as you like. Here we'll keep the default directory /home/ipyuser/anaconda3.

Genrate the config file and prepare to run notebook after the installation finished

cd /home/ipyuser/anaconda3/bin    #your path may vary if you change the default installation directory
./jupyter notebook --generate-config

The output shows the config file's directory

Writing default config to: /home/ipyuser/.jupyter/jupyter_notebook_config.py

To set a passowrd for the notebook interface, Launch python command line using python3 and run from notebook.auth import passwd; passwd() in python console. The console will return a harsh from your password like 'sha1:******ea45'. Copy the value and paste it into the config file 'jupyter_notebook_config.py'

c.NotebookApp.password = u''sha1:******ea45'

Before close the config file, add these new lines:

c.NotebookApp.ip = '0.0.0.0'      #enable login from other ip address
c.NotebookApp.port = 8888 #select a port for the serice
c.NotebookApp.open_browser = False
c.NotebookApp.notebook_dir = '/home/ipyuser/notebooks' #default directory of the notebooks

If the system can't find python3, use source ~/.bashrc to update the path variables and try again.

It's all set. Exit python console and run

jupyter notebook

to launch the notebook. Now you can access the notebook by open http://server's_ip_address:port in broswer from any computer.

To launch jupyter notebook on system startup auomatedly, sudo vi /etc/init.d/jupyter-notebook.sh and add the following script

#!/bin/sh
### BEGIN INIT INFO
# Provides:          jupyter-notebook
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the jupyter-notebook
# Description:       starts jupyter-notebook using start-stop-daemon
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
USER=ipyuser
GROUP=ipyuser
DESC=jupyter-notebook                 # Introduce a short description here
NAME=jupyter-notebook                 # Introduce the short server's name here
DAEMON=/home/ipyuser/anaconda3/bin/jupyter-notebook    # Introduce the server's location here
DAEMONARGS=""
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log

. /lib/lsb/init-functions

test -f $DAEMON || exit 0

case "$1" in
        start)
                log_daemon_msg "Strating $DESC: $NAME"
                start-stop-daemon -S -q -b -m -p $PIDFILE -c $USER:$GROUP -a $DAEMON -- $DAEMONARGS
                log_end_msg $?
                ;;
        stop)
                log_daemon_msg "Stopping $DESC: $NAME"
                start-stop-daemon -K -q -p $PIDFILE
                log_end_msg $?
                if [ -e $PIDFILE ]; then
                    rm $PIDFILE
                fi
                ;;
        restart)
                $0 stop
                $0 start
                ;;
        status)
                start-stop-daemon -T -p $PIDFILE
                log_end_msg $?
                ;;
        *)
                echo "usage: $NAME {start|stop|restart|status}"
                exit 1
                ;;
esac

exit 0

save the script. run update-rc.d /etc/init.d/jupyter-notebook.sh defaults now you can use /etc/init.d/jupyter-notebook.sh start|stop|restart| to start,stop or restart the jupyter notebook service. In case need to remove the service update-rc.d /etc/init.d/jupyter-notebook.sh remove

append /etc/init.d/jupyter-notebook.sh start & to /etc/rc.local. Now the notebook will be started on boot.

reference: http://jupyter-notebook.readthedocs.io/en/latest/public_server.html#notebook-server-security



知识共享许可协议
除非注明,本博客文章均为原创
并采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。转载请以URL链接形式标注源地址。

标签: none

添加新评论