1 #!/bin/bash
2
3 # 检查操作系统版本,该脚本只能运行在 Centos 7.x 系统上
4
5 cat /etc/redhat-release |grep -i centos |grep '7.[[:digit:]]' >/dev/null
6
7 if [[ $? != 0 ]]
8 then
9 echo -e "不支持的操作系统,该脚本只适用于CentOS 7.x x86_64 操作系统"
10 exit 1
11 fi
12
13 # 设置你的网站域名
14 domainname=your.domain.name
15 # 设置网站使用的端口号,免备案
16 httpd_port=580
17 # 设置ssh使用的端口号,安全问题
18 sshd_port=522
19
20
21 # 配置阿里云YUM源
22 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
23
24 # 安装EPEL
25 yum install -y epel-release
26
27 # 配置阿里云EPEL源
28 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
29
30 # 安装python3.6和nginx服务
31 yum install -y nginx python36
32
33 # 给当前用户配置阿里云pypi源
34 mkdir -p ~/.pip
35 echo '
36 [global]
37 index-url = https://mirrors.aliyun.com/pypi/simple/
38
39 [install]
40 trusted-host=mirrors.aliyun.com
41 ' > ~/.pip/pip.conf
42
43 # 安装jupyter
44 pip3 install jupyter
45
46 # 配置jupyter登录密码,这是一个交互程序,我们提前算好密码,直接写入配置文件
47 #jupyter notebook password 密码 Python-Jupyter-Notebook
48
49 # 自动生成jupyter配置文件到当前用户的家目录下:~/.jupyter/jupyter_notebook_config.py
50 jupyter notebook --generate-config
51
52 # 配置jupyter配置,包含上面提到的jupyter登录密码 Python-Jupyter-Notebook
53 # 后期可以使用jupyter notebook password 重置密码,密码重置后需要重启jupyter生效
54 echo '
55 c.NotebookApp.allow_remote_access = True
56 c.NotebookApp.allow_root = True
57 c.NotebookApp.open_browser = False
58 c.NotebookApp.password = u"sha1:738ad3e95869:2bbb56f1f1d94cde2cdddc97ff0566c771d27715"
59 ' >> ~/.jupyter/jupyter_notebook_config.py
60
61 # 设置jupyter开启自启动
62 echo 'cd /home/ && nohup jupyter notebook &' >> /etc/rc.d/rc.local
63
64 # 启动jupyter
65 cd /home/ && nohup jupyter notebook &
66
67 # 配置nginx,给jupyter做反向代理,注意修改成自己的域名
68 echo '
69 server_names_hash_bucket_size 64;
70
71 server {
72 listen '$httpd_port';
73 server_name '$domainname';
74
75 location / {
76 proxy_pass http://localhost:8888;
77 proxy_set_header Host $host:'$httpd_port';
78 }
79
80 location ~ /api/kernels/ {
81 proxy_pass http://localhost:8888;
82 proxy_set_header Host $host:'$httpd_port';
83 # websocket support
84 proxy_http_version 1.1;
85 proxy_set_header Upgrade "websocket";
86 proxy_set_header Connection "Upgrade";
87 proxy_read_timeout 86400;
88 }
89 } ' > /etc/nginx/conf.d/jupyter.conf
90
91 # 防火墙放行http(tcp '$httpd_port')
92 firewall-cmd --permanent --zone=public --add-port=$httpd_port/tcp
93 firewall-cmd --reload
94
95 # 设置nginx服务开机自启动,并重启nginx服务
96 systemctl enable nginx.service
97 systemctl restart nginx.service
98
99 # 修改sshd服务的端口号,同时修改防火墙策略
100 sed -i '/Port 22/a Port '$sshd_port'' /etc/ssh/sshd_config
101 systemctl restart sshd.service
102
103 firewall-cmd --permanent --zone=public --add-port=$sshd_port/tcp
104 firewall-cmd --reload