django+uwsgi+python3+nginx 部署项目
写在前面
之前项目要部署到线上,采用py3 + django + uwsgi + nginx 的组合,折腾了半天才搞定,写下此文留作笔记
安装nginx
yum install epel-release
yum -y install nginx
systemctl disable firewalld
- 修改selinux的配置文件
/etc/sysconfig/selinux
为SELINUX=disabled
并关闭防火墙 - nginx的启动:
nginx
- nginx重新加载文件启动:
nginx -s reload
- nginx的配置文件位置:
/etc/nginx
- nginx开机自启动:
systemctl enable nginx
安装django + uwsgi
pip3 install django
pip3 install uwsgi
注意: 如果服务器上安装了py2,并pip install uwsgi
,此时uwsgi uwsgi.ini默认使用的是py2, 而项目是py3编写的,会报什么no app load
类似错误,所以我的做法是创建一个软连接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
安装py3
首先安装依赖包
yum -y install wget
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
mkdir /usr/local/python3
tar -xvJf Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make && make install
mv /usr/bin/python /usr/bin/python2.7.5
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
vi /usr/bin/yum
把#! /usr/bin/python修改为#! /usr/bin/python2.7.5
uwsgi 启动django项目
在django项目和manage.py的同目录下创建uwsgi.ini,内容如下
[uwsgi]
http = :8001
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:9002
# the base directory (full path)
chdir = /root/wang/smsc
# Django's wsgi file
wsgi-file = SMSC/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
daemonize = /data/logs/uwsgi/uwsgi9002.log
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true
其中http设置的8001,是wsgi启动django的http端口,通过该端口可以访问django项目,但是静态页面是找不到的,可以通过配置找到静态页面,再次不做太多描述
socket设置的9002是本地socket端口,用于和nginx进行通信
cd /root/wang/smsc
uwsgi3 uwsgi.ini
nginx和uwsgi配合使用
第一步先把/etc/nginx目录下的 uwsgi_params 移动到和项目manage.py 同级目录下
第二步是编写自己的nginx配置文件,文件名自取
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:9002; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8002;
# the domain name it will serve for
server_name smsc.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 {
autoindex on;
alias /root/wang/smsc/static/; # your Django project's static files - amend as req
uired
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /root/wang/smsc/uwsgi_params; # the uwsgi_params file you installed
}
}
对于配置文件,有如下几个需要注意的点:
server 127.0.0.1:9002
中的 9002 端口对应 wsgi 的 socket 端口- server设置的 listen 8002 就是web的http绑定的端口,其中的8002 和uwsgi设置的http端口不要一致
location /static
设置的 alias /root/wang/smsc/static/, 最后需要有/
,没有nginx静态文件就会找不到- 在django的 settings.py 里面加上
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
,这里我的静态文件目录是statics,`STATICFILES_DIRS = [os.path.join(BASE_DIR, 'statics')], static 和 statics 必须是不同的,否则会报错 python manage.py collectstatic
- 把静态文件拷贝到
/etc/nginx/conf.d
- 运行uwsgi3 uwsgi.ini
- 运行
nginx -s reload