Construction of Jupiter notebook server and solutions to common errors


Original link https://blog.jaspirit.cc/posts/69f0ff47/ Author himself

1 Introduction to Jupiter notebook

Jupyter notebook It is a Web-based python interactive development environment, and the file browser function is not its main function. However, because the author does not know other Web file management systems at that time and has the needs of python development, he chose to install Jupiter notebook service on the server.

Best in Anaconda Install and run jupyter notebook in the environment. Accessible Open source mirror station of Tsinghua University Download the latest version of Anaconda installation package.

2 startup of Jupiter notebook service

To run the Jupiter notebook, you need to switch to the conda environment in the terminal, and then enter the Jupiter notebook command. However, at this time, the Jupiter notebook is not open to the client, and we cannot access it through the browser. Therefore, you need to change the configuration file of the Jupiter notebook (~ /. Jupiter / jupiter_notebook_config.py, ~ is the user's home directory)

Uncomment and change the following fields:

c.NotebookApp.ip = '*' # Set the ip source for accessing the notebook, * means all, that is, the service is open to all sources  
c.NotebookApp.password = u'sha1:xxxx' # Fill in the password (see below)  
c.NotebookApp.open_browser = False # Prohibit the browser from opening automatically when notebook starts (it is unnecessary to open and most linux servers have no graphical interface)  
c.NotebookApp.port = 8888 # Specify the service port. The default is 8888. e

The password is obtained by running the following two lines of python code in an interactive python environment:

from notebook.auth import passwd  
passwd()

Enter the password and confirm to get the sha1 value of the password, and fill it in c.NotebookApp.password.

>>> 'sha1:xxxx'  

3. Encrypt the jupyter service with HTTPS

Since you want to use the domain name to access the service, you must configure c.notebookapp.allow in jupyter notebook_ Change the value of origin to * to allow access by the domain name (if the requirements are strict, you can fill in notebook.yourdomain.com). Otherwise, a Blocking Cross Origin API request for /api/sessions. Error will be reported.

In addition, the Jupiter notebook requires the web socket support of nginx (otherwise, an error Replacing stale connection will be reported). Therefore, modify the nginx configuration file as follows.

server {
        listen 443 ssl;
        server_name  notebook.yourdomain.com;
        charset utf-8;
        # Large file transfer support
        client_max_body_size 8192M;
        
        ssl_certificate yourdomain.com/fullchain.pem;
        ssl_certificate_key yourdomain.com/privkey.pem;
        ssl_trusted_certificate  yourdomain.com/chain.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        server_tokens off;

        fastcgi_param   HTTPS               on;
        fastcgi_param   HTTP_SCHEME         https;
        
        # Reverse proxy and websocket configuration
        location / {
            proxy_pass http://127.0.0.1:8888;
            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;
        }
}

For the Jupiter notebook running by a non root user, you need to have access to / usr/share. Therefore, execute the command sudo chmod 777 share /. Otherwise, an error of 500 will be reported:

PermissionError: [Errno 13] Permission denied: '/usr/share/jupyter/nbconvert/templates/conf.json' -R

After the above steps are completed, use a non root account and execute it in the conda environment

nohup jupyter notebook &

You can make jupyter notebook run in the background and https://notebook.yourdomain.com/ Use it.

{%asset_img jupyter-login.jpg 250 %}

  • If you are prompted that opening '/ home / user name / nohup.out' fails: the permission is insufficient, modify the command to

    Nohup Jupiter notebook > / dev/null 2 > & 1 & (redirect the log file and error file of nohup to / dev/null, see This article)

  • If you want to change the root directory path of the Jupiter notebook (for example, / var/www/notebook /), you can further modify the command to

    nohup jupyter notebook /var/www/notebook/ >/dev/null 2>&1 &

You can also modify c.NotebookApp.base_url = '/var/www/notebook /' in jupyter_notebook_config.py, so that you do not need to specify the root directory when running the command.

Tags: Python Big Data jupyter Nginx Tornado

Posted on Thu, 30 Sep 2021 20:59:59 -0400 by Spekta