部署
线上服务器配置gitee的ssh免密登陆
拿到一个新的云服务器
◎ 线上服务器配置gitee的ssh免密登陆
cd ~/.ssh
ssh-keygen -t rsa -f id_rsa.gitee
cat ~/.ssh/id_rsa.gitee.pub # - ★ 将该公钥配置到gitee上
ssh-add ~/.ssh/id_rsa.gitee # !! 若通过不了,则执行该命令之前先执行命令 ssh-agent bash
ssh -T git@gitee.com
◎ ~家目录下克隆项目下来
git clone git@gitee.com:One_PieceDC/pear_admin_django.git
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
安装mysql并启动
yum install mariadb-server -y
yum install mariadb -y
systemctl enable mariadb
systemctl start mariadb
mysql -u root -p # - 首次登陆无需密码,直接enter回车即可
UPDATE mysql.user SET password=password('root123') WHERE user='root';
flush privileges;
exit;
mysql -u root -p # - 输入密码root123
select user,host,password from mysql.user;
delete from mysql.user where host='localhost' and user='';
delete from mysql.user where host='iz2vcassvhmuty1upbxbeez' and user=''; # 这个host值自己观察下表!
exit;
systemctl stop mariadb
systemctl start mariadb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
编译安装python3.9
# - 安装gcc,用于后续安装Python时编译源码
yum install gcc -y
# - 安装Python3相关依赖
yum install zlib zlib-devel -y
yum install bzip2 bzip2-devel -y
yum install ncurses ncurses-devel -y
yum install readline readline-devel -y
yum install openssl openssl-devel -y
yum install xz lzma xz-devel -y
yum install sqlite sqlite-devel -y
yum install gdbm gdbm-devel -y
yum install tk tk-devel -y
yum install mysql-devel -y
yum install python-devel -y
yum install libffi-devel -y
# - 下载python3.9源码 若没有wget,则先安装 `yum install wget`
mkdir /data/
cd /data/
# - 下载缓慢的话,可以在本地下载好后,通过FTP传上去
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
# - 编译安装"有一小丢丢久" <命令默认在/usr/local/bin/下!!>
tar -xvf Python-3.9.5.tgz
cd Python-3.9.5
./configure
make all
make install
python3.9 --version # 验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
安装虚拟环境. 并在虚拟环境中安装项目所需的第三方模块 + 配置local_settings + 收集静态资源
pip3.9 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # -- 配置使用清华源!
pip3.9 install virtualenv
mkdir /envs # -- 我们约定以后创建的所有虚拟环境都放在/envs目录下
virtualenv /envs/pear_admin_django --python=python3.9 # -- 在envs的pear_admin_django目录下创建了该虚拟环境
[root@iZ2vcassvhmuty1upbxbeeZ ~]# ls
pear_admin_django
[root@iZ2vcassvhmuty1upbxbeeZ ~]# cd pear_admin_django/
[root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# source /envs/pear_admin_django/bin/activate
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# pip install -r requirements.txt
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# ls
apps manage.py package.json poetry.lock README.md service templates
LICENSE media pear_admin_django pyproject.toml requirements.txt static
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# cd pear_admin_django/
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# ls
asgi.py __init__.py local_settings.py __pycache__ settings.py urls.py wsgi.py
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# vim local_settings.py
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# cat local_settings.py
import os
from pathlib import Path
# 关于静态资源,后续希望将Django项目中每个app中的所有静态资源都收集到指定目录下!!
# 因为项目部署后,Django找静态文件的效果很差,Nginx不会说按照注册app的顺序去查找静态资源.
# 所以我们需要将静态资源一起放到某一处!!让nginx的静态资源查找指向这一处地址!!
# ★ 此处设置的静态资源STATIC_ROOT的路径与项目根目录同级!!
BASE_BASE_DIR = Path(__file__).resolve().parent.parent.parent
STATIC_ROOT = os.path.join(BASE_BASE_DIR,"allstatic")
# 报错不显示信息
DEBUG = False
# 按理说应该写当前服务器的IP,但部署到多台服务器上的话,比较麻烦. 写*代指允许项目部署在任意地址上/任>意服务器IP
ALLOWED_HOSTS = ['*']
# 线上mysql数据库的配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'pear_admin_django', # !
'USER': 'root', # !
'PASSWORD': 'root123', # !
'HOST': '127.0.0.1',
'PORT': '3306',
'CHARSET': 'utf8'
},
}
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# cd ..
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# ls
apps manage.py package.json poetry.lock README.md service templates
LICENSE media pear_admin_django pyproject.toml requirements.txt static
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# python manage.py collectstatic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
数据迁移和批量添加数据
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# python manage.py makemigrations base
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# python manage.py migrate base
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# python manage.py init_script
开始批量添加数据...
Good,爱卿退下吧! Command executed successfully!
1
2
3
4
5
2
3
4
5
到这一步执行 python manage.py runserver 0.0.0.0:8000
, 访问是可以看到页面的, 但静态文件的访问有问题..
uwsgi的配置
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# pip install uwsgi
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ls
allstatic pear_admin_django
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# touch uwsgi_pear.ini
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# vim uwsgi_pear.ini
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ls
allstatic pear_admin_django uwsgi_pear.ini
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# cat uwsgi_pear.ini
[uwsgi]
# uwsgi接收nginx 转发/反向代理 过来的请求!!nginx的反向代理的是sorket请求!!
# nginx监听80端口,uwsgi监听其他端口(此处我设置的是8001)!!
# uwsgi允许本机的nginx传过来的sorket请求即可,所以是127.0.0.1 即只有本服务器能找到uwsgi!!
# Ps: 若nginx在其他服务器上,那么这里得写成 0.0.0.0,且uwsgi所在的服务器的安全组得开放8001端口.
# 这样的话,部署在其他服务器上的nginx才能找到这台服务器上的uwsgi!!
socket = 127.0.0.1:8001
# 执行uwsgi命令时先进入该目录
chdir = /root/pear_admin_django/
# 找到wsgi.py
wsgi-file = pear_admin_django/wsgi.py
# 通常会开4个进程,测试阶段可以只开1个,这样杀进程的时候也好杀
processes = 1
# 虚拟环境路径
virtualenv = /envs/pear_admin_django
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
nginx的配置
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# yum install nginx -y
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# cd /etc/nginx
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ nginx]# ls
conf.d fastcgi_params mime.types scgi_params win-utf
default.d fastcgi_params.default mime.types.default scgi_params.default
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ nginx]# mv nginx.conf nginx.conf.bak
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ nginx]# vim nginx.conf
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ nginx]# cat nginx.conf
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream django {
server 127.0.0.1:8001;
}
server {
listen 80;
listen [::]:80;
server_name _;
# root /usr/share/nginx/html;
# include /etc/nginx/default.d/*.conf;
location /static/ {
alias /root/allstatic/;
}
location /media/ {
alias /root/pear_admin_django/media/;
}
location / {
uwsgi_pass django;
include uwsgi_params;
}
# error_page 404 /404.html;
# location = /404.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
在虚拟环境里. 先启动nginx, 再启动uwsgi
systemctl start nginx
uwsgi --ini uwsgi_pear.ini
1
2
2
★ 远端项目更新了, 服务器拉取下来.. uwsgi要重启; 重新启动nginx服务..
有需要的话, 要重新收集静态资源、要重新进行数据库迁移..
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ls
allstatic pear_admin_django uwsgi_pear.ini
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# rm -rf uwsgi_pear.ini
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ls
allstatic pear_admin_django
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ssh-agent bash
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ssh-add ~/.ssh/id_rsa.gitee
Identity added: /root/.ssh/id_rsa.gitee (/root/.ssh/id_rsa.gitee)
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# ssh -T [email protected]
Hi 菜市场(@One_PieceDC)! You've successfully authenticated, but GITEE.COM does not provide shell access.
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ ~]# cd pear_admin_django/
(pear_admin_django) [root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# git pull origin main
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From gitee.com:One_PieceDC/pear_admin_django
* branch main -> FETCH_HEAD
Updating e4f553a..8296317
Fast-forward
shell/reboot.sh | 24 ++++++++++++++++++++++++
shell/stop.sh | 13 +++++++++++++
shell/uwsgi_pear.ini | 15 +++++++++++++++
3 files changed, 52 insertions(+)
create mode 100644 shell/reboot.sh
create mode 100644 shell/stop.sh
create mode 100644 shell/uwsgi_pear.ini
[root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# ls
apps manage.py package.json poetry.lock README.md service static
LICENSE media pear_admin_django pyproject.toml requirements.txt shell templates
[root@iZ2vcassvhmuty1upbxbeeZ pear_admin_django]# cd shell
[root@iZ2vcassvhmuty1upbxbeeZ shell]# ls
reboot.sh stop.sh uwsgi_pear.ini
[root@iZ2vcassvhmuty1upbxbeeZ shell]# sh reboot.sh
[root@iZ2vcassvhmuty1upbxbeeZ shell]# systemctl restart nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39