环境
操作系统:CentOS 7.2 64bit
Nginx版本:1.6.3
Python版本:2.7
uwsgi版本:2.0.15
django版本:1.11
安装
- 安装基础开发包
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
安装pip
为了找到python-pip先安装epel扩展源
sudo yum -y install epel-release
安装pip
sudo yum -y install python-pip
清楚cache
sudo yum clean all
安装uwsgi
pip install uwsgi
安装Django
pip install django
安装Nginx
下载 wget [nginx address]
解压 tar xf [压缩包]
进入解压目录 cd [nginx 目录]
./configure --prefix=/usr/local/nginx-1.5.6 --with-http_stub_status_module --with-http_gzip_static_module make && make install
开始
新建Django项目
新建项目
django-admin.py startproject [projectname]
cd [projectname]
python manage.py runserver 0.0.0.0:8000
在浏览器中输入 ip:8000,检查django是否运行正常
配置Nginx
在nginx.conf文件中新增一个server
server {
listen 8081;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
- 配置uwsgi
- cd [projectName]
- vi [projectName].ini并填入内容
[uwsgi]
socket = 127.0.0.1:8000
chdir = [projectPath]
wsgi-file = [projectName]/wsgi.py
processes = 4
threads = 2
daemonize = [输出log的文件] # 使项目后台运行并且输出日志
- 启动服务
- 重启nginx
sudo nginx -s reload
- 启动uWsgi
uwsgi xxx.ini
- 测试:在浏览器中输入
ip:8081
遇到的问题:
- You may need to add u'' to ALLOWED_HOSTS.
在[项目路径]/[项目名]下面有一个setting.py,把其中的ALLOWED_HOSTS修改为
ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']
- Address already in use
- 清除tcp端口连接sudo fuser -k 9002/tcp
- 重启uwsgi
uwsgi xxx.ini
参考链接:
[Setting up Django and your web server with uWSGI and nginx](http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
https://docs.djangoproject.com/en/1.11/intro/tutorial01/)