Introduced Jupyter Notebook to CentOS 7

1. Purpose

Install Jupyter Notebook in CentOS on the virtual machine. In addition, it will be possible to access and execute with a browser from a remote machine such as a host machine.

2. Environment

Host machine Constitution
OS OS X 10.11.6
VirtualBox version 5.1.18
Virtual machine Constitution
OS CentOS 7.3 (minimal)
Memory capacity 512MB
Number of CPU cores 1 core
Storage capacity 8GB
network settings Bridge adapter

The installation method of VirtualBox and CentOS is omitted.

3. Introduction

3.1. Update existing packages with Yum

Yum Update


$ sudo yum update -y

3.2. Introduce what you need with Anaconda

Introduced Anaconda



#Get Anaconda with curl
$ curl https://repo.continuum.io/archive/Anaconda3-4.3.1-Linux-x86_64.sh -O
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  474M  100  474M    0     0  3431k      0  0:02:21  0:02:21 --:--:-- 3837k

#Introduced bzip2
#Required for Anaconda installation
#If it is minimal, it is not installed
$ sudo yum install bzip2 -y

#run sh file
$ bash ./Anaconda3-4.3.1-Linux-x86_64.sh 

#Check Python version
$ python --version
Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

#Check Jupyter version
$ jupyter --version
4.2.1

3.3. Running JupyterNotebook

Run Jupyter Notebook


$ jupyter notebook

Make sure that Jupyter Notebook can be started. However, in this state, the Jupyter Notebook cannot be accessed from the remote machine. Therefore, edit the config file so that it can be connected from the outside.

4. Allow remote connection

4.1. Creating a config file

#Make sure your home directory is in the path of the Config file
$ jupyter --path

#Create a config file in your home directory
$ mkdir ~/.jupyter
$ touch ~/.jupyter/jupyter_notebook_config.py

4.2. Editing the config file

py3:~/.jupyter/jupyter_notebook_config.py



c = get_config()

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888

--Accept connections from all IP addresses. --Jupyter starts on port 8888. --The browser does not start when Jupyter Notebook starts.

4.3. Editing firewalld

Add a rule to firewalld to allow access to port 8888 from the outside.

Open port 8888


$ sudo firewall-cmd --add-port=8888/tcp --zone=public --permanent
success
$ sudo firewall-cmd --reload
success
$ sudo firewall-cmd --list-all --zone=public
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3 enp0s8
  sources: 
  services: dhcpv6-client http ssh
  ports: 8888/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

4.4. Setting login password

4.4.1. Generate iPython encrypted strings

#Launch iPython
$ ipython

#Load library to generate encrypted strings
In [1]: from notebook.auth import passwd

#Enter password to generate encrypted string
In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:(Encrypted string)'

#Get out of iPython
In [3]:                                                                                                                                               
Do you really want to exit ([y]/n)? y

4.4.2. Write the encrypted string to the Jupyter config file

py3:~/.jupyter/jupyter_notebook_config.py



c.NotebookApp.password = u'sha1:(4.4.1.(Character string generated by)'

4.5. Run Jupyter Notebook

Run Jupyter Notebook


$ jupyter notebook

After starting Jupyter Notebook, access the IP address 8888 assigned to the running machine.

JupyterNotebook ログイン画面

You will be prompted to enter the password, so enter the password you set earlier.

JupyterNotebook ホーム画面

If you log in successfully, a list of files in your home directory is displayed.

5. Make JupyterNotebook a service with systemd

5.1. Create Unit definition file

#Become root
$ su root

#Check Jupyter path
\# which jupyter

#Create Unit definition file
\# touch /etc/systemd/system/notebook.service

5.2. Editing the Unit definition file

[Unit]
Description = Jupyter Notebook

[Service]
Type=simple
PIDFile=/var/run/jupyter-notebook.pid
ExecStart='Absolute path to Jupyter' notebook
WorkingDirectory='The path of the home directory of the user who installed Anaconda'
User='Username that introduced Anaconda'
Group='Username that introduced Anaconda'
Restart=always

[Install]
WantedBy = multi-user.target

--In ExecStart, enter the command with notebook as an argument in the Jupyter path confirmed in 5.1. --Specify the path of the home directory of the user who installed Anaconda in WorkingDirectory. --In User and Group, enter the name of the user who installed Anaconda.

5.3. Get a list of registered Units

\# systemctl list-unit-files --type=service

notebook.service                            disabled

Confirm that the Unit file defined earlier exists in the output list.

5.4. Set to run at startup

#Set Jupyter to start automatically at startup
\# systemctl enable notebook

#Start service
\# systemctl start notebook

#Check if Jupyter is working properly
\# systemctl status notebook
● notebook.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/notebook.service; enabled; vendor preset: disabled)
   Active: active (running)since month 2017-03-20 21:27:49 JST; 2s ago

#Reboot and make sure Jupyter is running
\# reboot

6. Apply SSL

6.1. Create SSL server certificate and private key using OpenSSL

In Using SSL for encrypted communication, you can create a self-signed certificate and private key. The following command is given as an example.

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mykey.key -out mycert.pem
Generating a 1024 bit RSA private key

Regarding the meaning of the arguments of the OpenSSL command, [Create a private key and CSR using CentOS7.2 64bit OpenSSL and create a self-signed SSL server certificate](http://www.kakiro-web.com/ You can find out by reading "Create a self-signed SSL server certificate" in linux / ssl.html).

For the above command

--Create private key and CSR at once --X509 certificate standard --Certificate is valid for 365 days

You can see that.

6.2. Editing the config file

Place the certificate and private key created in 6.1. In the _ ~ / .jupyter_ folder.

Moving self-signed certificate and private key file


$ mv mycert.pem ~/.jupyter/
$ mv mykey.key ~/.jupyter/

Edit the certificate and private key location in the config file.

py3:~/.jupyter/jupyter_notebook_config.py


c.NotebookApp.certfile = u'${Absolute path to home directory}/.jupyter/mycert.pem'
c.NotebookApp.keyfile = u'${Absolute path to home directory}/.jupyter/mykey.key'

6.3. Restart the service and access via HTTPS

$ sudo systemctl restart notebook

$ sudo systemctl status notebook
● notebook.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/notebook.service; enabled; vendor preset: disabled)
   Active: active (running)since month 2017-03-20 22:15:08 JST; 6s ago

7. Change the default working directory

Change the working directory of Jupyter Notebook, which currently has a home directory specified by default, to another location.

7.1. Editing the config file

Create a directory for Jupyter directly under your home directory


$ mkdir ~/jupyter_files

Edit the config file.

py3:~/.jupyter/jupyter_notebook_config.py


c.NotebookApp.notebook_dir = u'${Absolute path to the directory created earlier}'

Restart Jupyter Notebook


$ sudo systemctl restart notebook

7.2. Confirm changes

Access Jupyter Notebook. You can see that the "anaconda3" directory that was displayed earlier is no longer displayed.

Notebook Home画面

If you create a file in the working directory specified earlier in the terminal, you can check the changes in jupyter notebook.

$ cd ~/jupyter_files/
$ touch TEST_FILE.py

notebook home画面

8. Reference website

Recommended Posts

Introduced Jupyter Notebook to CentOS 7
How to use Jupyter Notebook
Easy to use Jupyter notebook (Python3.5)
Jupyter Notebook memo
Introducing Jupyter Notebook
Markdown to get Jupyter notebook results to Qiita
I want to blog with Jupyter Notebook
Make Jupyter Notebook a service on CentOS
Powerful Jupyter Notebook
How to execute commands in jupyter notebook
How to use jupyter notebook with ABCI
Build jupyter notebook on remote server (CentOS)
Jupyter notebook password
Jupyter Notebook Basics of how to use
Jupyter Notebook memo
How to use Jupyter notebook [Super Basic]
How to debug with Jupyter or iPython Notebook
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Jupyter Notebook Settings-How to use (EC2 Amazon Linux 2)
Get started Jupyter Notebook
Convert jupyter to py
3 Jupyter notebook (Python) tricks
Introducing Python 2.7 to CentOS 6.6
[Cloud103] # 3 Jupyter Notebook again
Convert jupyter notebook .ipynb files to python executable .py files
Customize Jupyter notebook shortcuts to look like sublime text
Mac application for double-clicking to open Jupyter Notebook (* .ipynb)
How to instantly launch Jupyter Notebook from the terminal
Next to Excel, for the time being, jupyter notebook
How to view progress bar on Jupyter Notebook to see progress
Introduced python3-OpenCV3 to Raspberry Pi
Shortcut key for Jupyter notebook
[IPython] How to Share IPython Notebook
Using Graphviz with Jupyter Notebook
Display HTML in Jupyter notebook
How to import NoteBook as a module in Jupyter (IPython)
Use pip with Jupyter Notebook
How to change Jupyter layout
Multiprocessing error in Jupyter Notebook
Try using Jupyter Notebook dynamically
[Super Basics] About jupyter Notebook
High charts on Jupyter notebook
View PDF on Jupyter Notebook
How to use IPython Notebook
Use Cython with Jupyter Notebook
homebrew, pyenv, anaconda, Jupyter Notebook
Play with Jupyter Notebook (IPython Notebook)
I want to use a virtual environment with jupyter notebook!
Switch from python2.7 to python3.6 (centos7)
How to see the contents of the Jupyter notebook ipynb file
The usual way to add a Kernel with Jupyter Notebook
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
[Complete version] Jupyter Notebook shortcut
Unable to display tensorboard in jupyter notebook on docker (solved)
How to import NoteBook as a module in Jupyter (IPython)
A very convenient way to give a presentation on Jupyter Notebook
I tried to touch jupyter
Run Jupyter Notebook on windows
python3.8 venv environment jupyter notebook
A simple way to launch Jupyter Notebook / Lab and set a password
How to batch start a python program created with Jupyter notebook