django-uwsgi-部署

今天在把Django部署到服务器的时候,本地跑得好好的东西,遇到了各种奇葩问题的轮番轰炸。

把我逼到抓狂,网上各种说法都有,各种奇葩解决方案,乱七八糟。终于搞定,特此记录一下。

开发环境

Ubuntu 16.01(没用最新是因为腾讯云最新就这个,一升级就炸,好吧是我自己太垃圾了)

Python3.7(从源码编译安装)

遇到问题

问题1: ubuntu默认有python2.7和python3.5,没python3.7

apt install不星,最高3.5,习惯了homebrew一键搞定,还真挺懵逼,发现自己这个所谓的运维居然没从源码编译安装过东西,太傻逼了,一脸懵。

解决1:

没啥说的,编译安装就完了。

1
2
3
4
5
6
7
8
$ wget https://www.python.org/downloads/release/python-370/
$ tar -zxvf Python-3.7.0.tgz
# 把依赖库装一下子(并不知道为啥是这些,待思考)
$ sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev
$ cd /path/to/downloaded/file/
$ sudo ./configure
$ sudo ./make
$ sudo ./make install

P.S. 如果还缺东西 那就缺啥补啥 少啥装啥

问题2: ubuntu安装mysql

1
2

$ sudo apt-get install mysql-server mysql-client # 让输密码就数一下 这是root的密码

这步倒是没出啥问题,但是Django跑起来的时候会出现Error loading MySQLdb module: No module named 'MySQLdb'

因为Python3.5+并不支持MySQLdb,使用MySql可以使用pymysql代替,据说两者使用方式相同

解决2:

1
2
$ pip install pymysql
...

在__init.py__中添加

1
2
3
import pymysql

pymysql.install_as_MySQLdb()

问题3: 配置uwsgi

1
2
$ pip3 install uwsgi
...

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
# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;

# max upload size
client_max_body_size 75M; # adjust to taste

# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}

location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}

uwsg配置文件:

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
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = mysite.wsgi:application

# process-related settings
# master
master = True

pidfile=/tmp/project-master.pid

# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664

max-requests=5000
# clear environment on exit
vacuum = True
daemonize=/var/log/uwsgi/yourproject.log

应用配置文件

1
$ uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

问题4: Django admin页面css等静态文件丢失

这个其实文档里面写清楚了,没注意看。

解决:

修改seetings.py 添加 STATIC_ROOT = "/var/www/example.com/static/"

运行python manage.py collectstatic收集静态文件。

在linux配置文件中设置好地址,就OK。

参考资料:

https://blog.csdn.net/hzlnice/article/details/81140865

https://www.jianshu.com/p/3111290b87f4

https://www.jianshu.com/p/968403165b92

https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html

https://docs.djangoproject.com/zh-hans/2.1/ref/databases/#mysql-notes

https://docs.djangoproject.com/zh-hans/2.1/howto/deployment/wsgi/uwsgi/


django-uwsgi-部署
https://blog.yrpang.com/posts/37675/
作者
yrPang
发布于
2019年1月22日
许可协议