主要是写 nginx 下部署django 项目
我这个项目是 django == 1.8.2 也就是1.7以上的django版本
1、系统对项目的影响
由于CentOs 的yum需要依赖 python2.6且自带 python2.6, 而项目需要 python2.7 所以需要另外安装 python2.7, 使2.6和2.7共存
2、更新系统安装包
yum update
3、安装python2.7
下载 对应版本的 python2.7 https://www.python.org/downloads/
tar jxvf Python-2.7.12.tgz
cd Python-2.7.12
./configure --prefix=/root/Python-2.7.12
make && make install
mv /usr/bin/python /usr/bin/python2.6
ln -s /root/Python-2.7.12/bin/python2.7 /usr/bin/python
mv之前 确认一下 python的软链接问题,
把系统默认的 python重命名为python2.6
把yum的依赖改为2.6
vim /usr/bin/yum
!/user/bin/Python 改为 !/user/bin/python2.6
4、安装pip
官网的安装略微坑爹 我提供一个 百度云的链接 http://pan.baidu.com/s/1sl0BIqx
python steup.py build
python setup.py install
5、安装 nginx
我那台已经有 项目在上面了,跳过了这步。 这里不会自行百度了
6、安装 uwsgi
http://uwsgi-docs.readthedocs.io/en/latest/Download.html
tar -zxvf uwsgi-2.0.14.tar.gz
cd uwsgi-2.0.14
make
cp uwsgi /usr/bin
chmod a+x /usr/bin/uwsgi
7、安装setuptool
http://pan.baidu.com/s/1slvRZ3n
tar -zxvf setuptools-28.3.0.tar.gz
cd setuptools-28.3.0
python setup.py build
python setup.py install
8、安装MySQL-python
http://pan.baidu.com/s/1c2E9yhE
tar -zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build
python setup.py install
9、用pip安装一些 其他的工具
pip install django==1.8.2
pip install uwsgi
pip install xlwt
10、添加 nginx 的配置
因为 有好几个项目都在用这个nginx ,就不是直接 修改 nginx,.conf 就行了
首先
创建在 vhost目录下 创建 对用的配置 xxx.example.com.conf
server {
listen 80;
server_name xxx.example.com;
#access_log /var/log/access_example<span style="font-family: Arial, Helvetica, sans-serif;">.log main;</span>
location / {
root /var/www/example/;
# index index.php index.html index.htm;
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /static {
alias /var/www/example/static;
index index.html index.htm;
}
#location ~ /.ht {
# deny all;
#}
}
配置 项目路径、 引入 nginx下的 uwsgi_params
配置一些 静态文件的路径
这个文件写好了,要在 nginx.conf 中确认是否被 引入到其中
一般会有 一行
http {
include vhost/*.conf;
。。。
。。。
}
11、配置 uwsgi 在项目和 manager.py同一目录下 创建 配置文件 uwsgi.ini
[uwsgi]
socket=127.0.0.1:9090
chdir=/var/www/example
wsgi-file=/var/www/example/wsgi.py
uid=root
gid=root
env=DJANGO_SETTINGS_MODULE=example.settings
# module=django.core.handlers.wsgi:WSGIHandler()
env = LANG=zh_CN.UTF-8
processes=4
threads=2
stats=127.0.0.1:1080
在django1.7以后,千万不要加,不然各种找不到 modules
12、最后就是 启动 uwsgi
uswgi uwsgi.ini
13、 nginx 重读配置文件
nginx -s reload
最后总结 各种报错
1、编码问题
-
UnicodeDecodeError:'ascii' codec can't decode byte 0xe8 in position 40: ordinal not in range(128)
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding("utf8")
在 __init__中添加 这个
2、加载问题
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don’t make non-lazy gettext calls at import time.
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
网上都这么说
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
改成:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
其实我本来就是 这样写的了
主要问题就是 在 uwsgi.ini 那里 不要写module = xxxxxxx
还有就是 可以尝试在
wsgipy 文件中 添加
import django
django.setup()