Remember to follow me and keep sharing your notes on programming development, data analysis, machine learning and so on.

Learn about cloud server development with my boss (if you are reading this series for the first time, it is highly recommended to learn the following articles) :

Pilot: Have a server, I unexpectedly so cool?

Alternative project: write a resume page in 10 lines of code!

How to apply for a free SSL certificate for a domain name

A pagoda in Linux, a real pagoda! Detailed tutorial

Finally, a website that everyone can visit

How to send alarm notifications to spikes using Python?

# How to send tweets automatically in Python?

Previously on

Last week, we started a new column (video tutorial) : Learn Python with my old table, mainly for the zero basic white, in the first episode contains 6 parts, from Python learning ideas to the environment, tool installation and use, the last section explains how to open the remote connection function of Jupyter, to achieve programming anytime and anywhere. Learn Python programming environment + Tools

The video tutorial is demonstrated using Windows OS, this question will share how to make their Own Linux server into a anytime, anywhere accessible and programming platform.

The process is essentially the same, but for Windows users you can go directly to the video tutorial: Click watch

Ii. Sharing summary

  • System: Aliyun ECS shared N4 server 1 core 2g storage 50G (purchased cheaply on Nov 11, 180 yuan /3 years)

  • Environment: python3.6.8 comes with it for easy demonstration, so use it directly

Three, start using your head

First we need to connect to the server, I directly use the pagoda panel login, if you do not know how to install the pagoda readers can see the Linux pagoda, the real pagoda! Detailed tutorial, directly through the pagoda connected to the server, click the terminal, you can enter the server command mode, operation.

3.1 installation jupyter

pip3 install jupyter
Copy the code

3.2 Generating the Jupyter Configuration File

jupyter notebook --generate-config
Copy the code

As shown in the figure above, my configuration file directory is /root/.jupyter/jupyter_notebook_config.py

3.3 Go to the Configuration file and modify the configuration

Here is a Linux file editing tool more suitable for beginners than Vim: Nano.

Enter nano + the file path to be modified to enter the editing mode of the corresponding file.

nano /root/.jupyter/jupyter_notebook_config.py
Copy the code

  • Change host to * to allow access from all IP addresses

  • Change remote access Settings to True to allow remote access

At this point, we can first save the file, press CTRL + O to save the file, and then press CTRL + X to exit the editing mode.

  • Set connection login password (remote connection requires token, easier to set password)

Terminal input the following command to set the password:

jupyter notebook password
Copy the code

After the password is set, Jupyter hashes the password and stores the password in JSON format. You need to open the password file, copy the hash value of the password, and then configure the password in the Jupyter configuration file.

nano /root/.jupyter/jupyter_notebook_config.json
Copy the code

After copying the password hash value, we opened the Jupyter configuration file again,

nano /root/.jupyter/jupyter_notebook_config.py
Copy the code

Press CTRL + W to enter the search mode, enter Notebookapp. password to search, then paste the copied password hash value to the corresponding position, then press CTRL + O to save the file, then press CTRL + X to exit the editing mode.

Three configurations are modified as follows:

## The IP address the notebook server will listen on.
c.NotebookApp.ip = The '*'

# Local IP addresses (such as 127.0.0.1 and ::1) are allowed as Local, along
# with hostnames configured in local_hostnames.
c.NotebookApp.allow_remote_access = True

## Hashed password to use for web authentication.
#
# To generate, type in a python/IPython shell:
#
# from notebook.auth import passwd; passwd()
# 
# The string should be of the form type:salt:hashed-password.
c.NotebookApp.password = 'Your password hash value'
Copy the code

3.4 Accessing the Server Background To allow external users to access the server through port 8888

Ali cloud server is a security policy group, Tencent cloud server is a firewall, to the corresponding location to add ports.

Enter the server background (take Ali Cloud as an example), click Instance -> Security Group -> Configuration Rules, you can enter.

Input port range: 8888/8888, authorization object: 0.0.0.0, and save.

3.5 Starting JUPyter and Attempting to Connect to Jupyter Remotely

Don’t panic if any of the following errors occur.

[root@xxx ~]# jupyter notebook[W 15:24:56. 017 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended. [C 15:24:56.021 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.Copy the code

The first warning about IP should not be ignored. The second warning cannot be run with root permission.

jupyter notebook --allow-root
Copy the code

Or change the allow_root in the jupyter configuration file to True.

## Whether to allow the user to run the notebook as root.
c.NotebookApp.allow_root = True
Copy the code

Enter Jupyter Notebool again to successfully start the Jupyter service, which is enabled on port 8888 by default.

You can access the IP address on the local PC using the following public IP address :8888. Enter the password page.

After entering the password, you can enter the home page of Jupyter. The default directory is the root directory (can be changed).

I’m gonna create a new ipynb file, just type in some code, nice.

  • The computer display

  • IPad landscape display

  • The phone is displayed in landscape

3.6 Setting the daemon process in the Background Prevents the process from being killed after the terminal is shut down

In my case, after logging in to the pagoda panel, go to /etc/systemd/system and create a new jupyter_me. Service file and write the following:

[Unit]
Description=Jupyter Service

[Service]
Type=forking
ExecStart=/usr/bin/python3 -m IPython notebook
KillMode=process
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target
Copy the code

Type=forking means that when the program is started, it will run in the background. ExecStart service execution instructions (execute scheduler.py file); KillMode=process Indicates that the main process of the program is killed when the service is stopped. Restart=on-failure Indicates that the system automatically restarts when the program exits due to an accident.

After saving the file, we can directly execute the following command in the terminal to start the daemon process. After running, it will enter the daemon state. We can press CTRL + C to exit without affecting the daemon process:

systemctl start jupyter_me
Copy the code

If you are not sure whether the process is started successfully, run the following command to check the process status.

systemctl status jupyter_me
Copy the code

If you do not want to set up the daemon, you can stop the service (and the program will also stop) by executing the stop command:

systemctl stop jupyter_me
Copy the code

3.7 Modifying other Configurations of Jupyter

  • The directory corresponding to the first (Home) page of Jupyter (user root by default)
## The directory to use for notebooks and kernels.
# Default: ''
c.NotebookApp.notebook_dir = 'Directory you want to use yourself'
Copy the code
  • Whether the browser is automatically opened at the same time of starting JUPyter (on by default), which we use under Linux, can be set to False.
## Whether to open in a browser after starting.
# The specific browser used is platform dependent and
# determined by the python standard library `webbrowser`
# module, unless it is overridden using the --browser
# (NotebookApp.browser) configuration option.
# Default: True
c.NotebookApp.open_browser = False
Copy the code
  • Jupyter default start in which port (default 8888), can be modified to their own want, the default value we all know, easy to be attacked.
## The port the notebook server will listen on (env: JUPYTER_PORT).
# Default: 8888
c.NotebookApp.port = 7777
Copy the code
  • SSL is used for encrypted communication, which is more secure

In addition, in order to make remote connection access and use JUPyter more secure, we can use SSL for encrypted communication to ensure more secure data transmission.

We need a free SSL certificate for a domain name (the same as configuring a certificate for the server)

You can also use OpenSSL to create a self-signed SSL certificate.

Website introduces related address: jupyter – notebook. Readthedocs. IO/en/stable/p…

# yum install OpenSSL # yum install OpenSSL

Create a directory to store the certificate, and then enter the command to create a self-signed SSL certificate. Enter the country, province, city, company, department, common name, and email address as required.

mkdir ssl && cd ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
Copy the code

After generating a good certificate, we need to modify the jupyter configuration file, first enter:

nano /root/.jupyter/jupyter_notebook_config.py
Copy the code

Find certFile and keyFile and write the path of the corresponding file.

## The full path to an SSL/TLS certificate file.
c.NotebookApp.certfile = '/root/ssl/mycert.pem'

## The full path to a private key file for usage with SSL/TLS.
c.NotebookApp.keyfile = '/root/ssl/mykey.key'
Copy the code

In theory, we could just restart the Jupyter daemon.

systemctl restart jupyter_me
Copy the code

But my local access to https:// public IP address :8888 shows that the connection is not private…

SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN It will not work to change the certificate applied before. I will study it again and talk to you next time.

We welcome you to understand the reader to study and exchange, progress together.

Next up

Before the nail enterprise robot automatic reply configuration, there are also readers in the draft, the last two days on the arrangement oh ~

Ok, see you next time, I love the cat love technology, more love si si’s old cousin Da Mian ଘ(˙꒳˙)ଓ Di Di