django部署上云

Ikko Lv3

因为上一次部署遇到了很多问题,这一次决定记录一下,以便下次部署的时候可以快速上手。

配置gitee

1
2
git config --global user.name ""
git config --global user.email "654@qq.com"

生成密钥

1
ssh-keygen -t rsa -C "862049654@qq.com"

查看密钥并拷贝,将它添加到码云个人账号的SSH公钥里:

1
cat ~/.ssh/id_rsa.pub

测试链接

1
ssh -T git@git.oschina.net
1
2
3
4
The authenticity of host 'git.oschina.net (212.64.63.190)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)?

输入yes,而不是按enter

1
Hi ikko-debug! You've successfully authenticated, but GITEE.COM does not provide shell access.

然后就可以使用git clone了

1
git clone git@gitee.com:.git#从仓库主页复制

以后需要更新仓库只需要cd到项目目录下,然后git pull即可

配置服务器

配置pip包管理器

首先在自己虚拟环境下
pip freeze > requirements.txt
然后在服务器上
pip3.9 install -r requirements.txt
安装mysqlclient遇到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql_config
raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found
mysql_config --version
mariadb_config --version
mysql_config --libs
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

安装 MySQL C Connector 及其开发包,以便能够正确编译和链接 mysqlclient 包。

1
2
3
4
5
unbuntu:
sudo apt-get update
sudo apt-get install libmysqlclient-dev
centos:
sudo yum install mysql-devel

然后运行

1
mysql_config --version

重新pip,成功

安装配置nginx

我已经装过了

1
2
3
4
5
启动nginx: service nginx start
停止nginx:service nginx stop
重启nginx:service nginx restart;./nginx -s reload
重载配置文件:service nginx reload
查看nginx状态:service nginx status

配置nginx:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 90;
server_name ; # 设置您的域名或 IP 地址,服务器IP地址

location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001; # 需要和ini文件中的端口保持一致,这样才能实现两者的通信。
uwsgi_read_timeout 30;
}
location /static/ {
root /root/searchsys/collected_static; # CSS, JavaScript 和图片等静态文件的路径
}
}

安装mysql

也装过了

安装配置uwsgi

pip3.9 install uwsgi
在/home/根目录创建文件夹mylog_uwsg
在mylog_uwsgi文件夹下新建名为mylog.ini文件
填入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[uwsgi]

chdir = /usr/searchsys
#module = searchsys.wsgi:searchsys #对应你的项目名
wsgi-file = /usr/searchsys/searchsys/wsgi.py
python-path = /root/Python-3.9.16
master = True
processes = 4
harakiri = 60
max-requests = 5000

socket = 127.0.0.1:8001 #对应配置安全组时开放的端口,uWSGI根据socket协议与Nginx相互在这个端口通信,在进行Nginx配置时会设置相同端口,这步极其重要,否则会配置失败
uid = 1000
gid = 2000
buffer-size = 65536
pidfile = /usr/searchsys/mylog_uwsgi/master.pid
daemonize = /usr/searchsys/mylog_uwsgi/mylog.log

vacuum = True

注意文件中路径后面不能有空格,不然会报错,我找了一天错没找到,服了,这一般人谁能看出来是有空格,建议copy的时候删除所有注释

1
2
chdir() to /usr/searchsys  #你的项目文件夹路径
chdir(): No such file or directory [core/uwsgi.c line 2625]

uwsgi –ini root/mylog_uwsg/mylog.ini
启动:uwsgi -ini /root/mylog_uwsgi/mylog.ini
然后报错:
uwsgi: invalid option – ‘n’
getopt_long() error
网上查了下使用很多人提供的解决方案,不行,于是放弃,只用nginx
后来把项目从root下移动到了/usr下,然后
折磨了一天使用这条命令成功了

1
uwsgi --ini /usr/searchsys/mylog_uwsgi/mylog.ini

[uWSGI] getting INI configuration from /root/mylog_uwsgi/mylog.ini
关闭uwwsgi

1
fuser -k 8000/tcp

静态文件收集

settings.py中配置

1
2
3
4
5
6
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
# 如果有多个应用,可以继续添加
]

然后执行

1
python manage.py collectstatic

启动

1
2
3
4
重启uwsgi:
uwsgi --reload /usr/searchsys/mylog_uwsgi/master.pid

./nginx -s reload //重启nginx

注意,每次修改代码都需要执行上述操作

总结

强烈推荐使用vscode的ssh连接服务器,可以很方便的查看文件结构
如果遇到报错,可以查看
uwsgi的日志在mylog_uwsgi/mylog.log中
nginx的日志在/nginx/logs/error.log中

  • Title: django部署上云
  • Author: Ikko
  • Created at : 2023-04-14 16:53:40
  • Updated at : 2023-04-18 11:51:03
  • Link: https://redefine.ohevan.com/2023/04/14/django部署上云/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments