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.
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.
Yum Update
$ sudo yum update -y
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
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.
#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
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.
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:
#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
py3:~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.password = u'sha1:(4.4.1.(Character string generated by)'
Run Jupyter Notebook
$ jupyter notebook
After starting Jupyter Notebook, access the IP address 8888 assigned to the running machine.
You will be prompted to enter the password, so enter the password you set earlier.
If you log in successfully, a list of files in your home directory is displayed.
#Become root
$ su root
#Check Jupyter path
\# which jupyter
#Create Unit definition file
\# touch /etc/systemd/system/notebook.service
[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.
\# systemctl list-unit-files --type=service
notebook.service disabled
Confirm that the Unit file defined earlier exists in the output list.
#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
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.
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'
$ 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
Change the working directory of Jupyter Notebook, which currently has a home directory specified by default, to another location.
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
Access Jupyter Notebook. You can see that the "anaconda3" directory that was displayed earlier is no longer displayed.
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
Recommended Posts