安装相关软件
更新源
sudo apt-get update
sudo apt-get upgrade
安装虚拟环境
sudo apt-get install python-virtualenv
安装nginx
sudo apt-get install nginx
安装uwsgi
sudo pip3 install uwsgi
或者
sudo pip3 install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
安装uwsgi出错就安装
sudo apt-get install python-dev
sudo apt-get install python3.5-dev
本文用到的软件环境
ubuntu 16.04
Python 3.5.2
Django 1.11.4
nginx 1.10.3
uwsgi 2.0.15
创建一个普通用户
创建用户组webapps
sudo groupadd –system webapps
创建用户user_test并设置密码
sudo useradd –system –gid webapps –shell /bin/bash –home /webapps/test user_test
passwd user_test
创建用户目录并修改目录权限
mkdir -p /webapps/test
chown user_test /webapps/test/
添加用户user_test sudo权限
在/etc/sudoers添加
user_test ALL=(ALL:ALL) ALL
创建测试应用
切换用户为user_test
su - user_test
cd /webapps/test
创建python虚拟环境,这里指定为3.5版本,并激活
virtualenv -p /usr/bin/python3.5 .
source bin/activate
安装django
pip install django
创建django工程mysite
django-admin startproject mysite
创建应用app
cd mysite
django-admin startapp app
修改mysite/settings.py
将外网ip添加到ALLOWED_HOSTS
ALLOWED_HOSTS = ['xxx.xxx.xxx.xxx', '127.0.0.1']
在INSTALLED_APPS里追加 ‘app’
新建app/static目录
在mysite/settings.py里添加
STATIC_ROOT = os.path.join(BASE_DIR, 'app', 'static')
收集静态文件到app/static目录
python manage.py collectstatic
迁移数据库
python manage.py migrate
创建后台管理用户
python manage.py createsuper
添加视图函数
在app/views.py添加
from django.http import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
在mysite/urls.py添加
from app import views
urlpatterns = [
...
url(r'^$', views.index),
]
测试django应用运行是否正常
python manage.py runserver
使用curl命令访问
curl 127.0.0.1:8000
正常会输出<h1>Hello World!</h1>
Ctrl C 退出
当前所涉及的一些目录
用户目录:/webapps/test
虚拟环境目录: /webapps/test
工程目录:/webapps/test/mysite
配置ngnix
从https://github.com/nginx/nginx/blob/master/conf/uwsgi_params下载到/etc/nginx/conf/目录
新建mysite/test_ngnix.conf文件,内容如下
# the upstream component nginx needs to connect to
upstream testapp {
server unix:///webapps/test/mysite/mysite.sock; # for a file socket
#server 127.0.0.1:9090; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
access_log /webapps/test/mysite/nginx_access.log;
error_log /webapps/test/mysite/nginx_error.log;
# Django media
location /media {
alias /webapps/test/mysite/app/media; # your Django project's media files - amend as required
}
location /static {
alias /webapps/test/mysite/app/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass testapp;
include /etc/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
}
}
将test_ngnix.conf链接到/etc/nginx/sites-enabled/目录
sudo ln -s /webapps/test/mysite/test_ngnix.conf /etc/nginx/sites-enabled/
记得删除自带的default文件,否则输入外网ip看到的是ngnix欢迎页
sudo rm /etc/nginx/sites-enabled/default
这里用到的是unix file socket
所以把用户www-data加到webapps用户组,否则读取socket文件时会有权限问题
sudo usermod -a -G webapps www-data
重启ngnix
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
配置uwsgi
新建wsgi.ini, 内容如下
[uwsgi]
#socket = 127.0.0.1:9090
chdir=/webapps/test/mysite
module=mysite.wsgi
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
vacuum=true
daemonize = /webapps/test/mysite/uwsgi.log
virtualenv = /webapps/test
gid=webapps
uid=user_test
socket = /webapps/test/mysite/mysite.sock
运行uwsgi
uwsgi –ini wsgi.ini
用浏览器访问外网ip,正常会显示Hello World
使用uwsgi Emperor mode
# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /webapps/test/mysite/wsgi.ini /etc/uwsgi/vassals/
# run the emperor
sudo uwsgi --emperor /etc/uwsgi/vassals --uid user_test --gid webapps
参考文档
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
http://blog.csdn.net/woniu02141/article/details/49836443
http://blog.csdn.net/c465869935/article/details/53242126
http://www.jianshu.com/p/e6ff4a28ab5a