5

Django 部署上线——踩坑 3 天

 2 years ago
source link: https://www.v2ex.com/t/847401
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

Django 部署上线——踩坑 3 天

  maloneleo88 · 14 小时 52 分钟前 · 1643 次点击
第一步,服务器上安装和配置数据库
第二步,上传项目到服务器
第三步,用 uWSGI 替代 runserver 启动服务器
第四步,配置 Nginx 反向代理服务器

安装 MySQL8
sudo apt-get update
sudo apt-get install mysql-server
开机启动
systemctl status mysql
查看随机密码
sudo cat /etc/mysql/debian.cnf
登录
mysql -u root -p
设置密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
启动
sudo service mysql start
创建数据库
create database '你的数据库名' DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

mysqlclient 装不上的话安装依赖
sudo apt-get install libmysqlclient-dev
输出开发环境需要的依赖库
pip3 freeze > requirement.txt
上传到服务器,使用如下命令安装依赖包
pip3 install -r requirement.txt

( win 的话用 WinSCP 上传,PuTTY 连接 Shell )


安装 uwsgi
sudo pip3 install uwsgi
配置 uWSGI.ini 启动文件
[uwsgi]
http=0.0.0.0:8000 #他妈的用 127 就是启不起来,也没有报错,怎么也找不到原因,最后改成四个零可以了
socket=127.0.0.1:8001 #后面配 NGINX 时候再改用 socket
chdir=/home/ubuntu/zrhd_bj #直接 pwd 看绝对路径,指向项目根目录
wsgi-file=zrhd_bj/wsgi.py #指向同名目录下 django 生成的 wsgi 文件
process=2
threads=2
pidfile=uwsgi.pid
daemonize=uwsgi.log
master=ture
static-map = /static=/home/ubuntu/zrhd_bj/static #管理后台静态文件丢了的最终解决办法,网上全是 coppy 的垃圾
buffer-size = 32768 #提交表单就报 502 ,给个参数才好
settings 要从开发环境改生产环境
DEBUG='Fales'

安装 nginx
sudo apt-get nginx
nginx 配置要去 /etc/nginx/sites-enabled 安装目录 vim 修改,因为权限问题
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
uwsgi_pass 127.0.0.1:8001;
include /etc/nginx/uwsgi_params;
}
上传文件继续 403 ,找个地方加上附件大小限制,默认好像就 1MB or 2MB ,静态文件夹 nginx 没设置没发现问题
client_max_body_size 20m;


#------------------------------------------------------------------------------------------

创建虚拟环境(在项目目录下运行)
python -m venv tutorial-env
激活虚拟环境
activate
source activate
关闭虚拟环境
deactivate

pip 更新 linux 用 python3 命令
python -m pip install --upgrade pip
pip 安装软件
pip install django==2.2.7 -i https://pypi.tuna.tsinghua.edu.cn/simple

新建 django 项目
django-admin startproject


超级用户
manage.py createsuperuser

数据迁移
manage.py makemigrations
manage.py migrate

启动、停止、看状态
uwsgi --ini uwsgi.ini
uwsgi --stop uwsgi.pid
ps aux|grep 'uWSGI'

杀端口
sudo kill -9 '+pid'
看端口
sudo lsof -i:8001

nginx 开始 停止 重启
sudo /etc/init.d/nginx start|stop|restart|status

ubuntu 环境变量有些没指定,不是这个运行不了就是那个运行不了,看着加。 修改 etc/profile 追加
set PATH so it includes user's private ~/.local/bin if it exists 中文搜不到方法???
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

ubuntu 装 python3 和依赖
sudo apt-get install python3-dev
sudo apt-get install python3-pip

破 b 玩意,浪费时间学,专业的事交给专业的做,php 是最好的语言!(一键部署)
学 python 之前有人告诉我这个,我打死也不学! 太费时间了,部署 php 的话 20 个都早早完事了!成熟稳定才是硬道理,总有人说这个好,那个好,简直是扯屁!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK