nginx + uswgi +django
适合ubuntu 系统,不只是树莓派
安装必要软件
pt-get install build-essential psmisc apt-get install python-dev libxml2 libxml2-dev apt-get install python-setuptools cd /opt/ wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar -zxvf uwsgi-latest.tar.gz mv uwsgi-latest/ uwsgi/ cd uwsgi/ python setup.py install
安装nginx/安装mysql
apt-get install mysql-server apt-get install nginx
安装uwsgi,阿里源带了uwsgi这里直接安装
apt-get install uwsgi uwsgi-core uwsgi-plugin-python
安装django
apt-get install python-django python-mysqldb
配置
cat /etc/uwsgi/apps-enabled/www.ini
[uwsgi] socket = 192.168.0.14:9000 chmod-socket = 667 limit-as = 256 processes = 4 master = true daemonize = /var/log/uwsgi.log enable-threads = true vacuun = true chdir = /var/www/html/testp/mysite #django 项目目录 module = mysite.wsgi #创建完项目后会在mysite侠自动生成wsgi.py
####说明module 配置######
tree /var/www/html/testp/mysite/
/var/www/html/testp/mysite/
├── manage.py
└── mysite
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── wsgi.py
└── wsgi.pyc
重启uwsgi服务,此时会开放9000端口
voke-rc.d uwsgi restart
nginx配置
cat /etc/nginx/sites-enabled/www
server {
listen 8000; //转发8080端口到uwsgi端口9000
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 192.168.0.14:9000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}
socket配置,这里可以选择写ip或者改成socket
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 cat > /etc/uwsgi/apps-enabled/www.ini << EOF 2 [uwsgi] 3 socket = /var/run/uwsgi/app/www/socket 4 chmod-socket = 666 5 limit-as = 256 6 processes = 6 7 max-request = 2000 8 memory-report = true 9 enable-threads = true 10 pythonpath = /home/user/www 11 chdir = /home/user/www/mysite 12 wsgi-file = /home/user/www/mysite/wsgi.py 13 EOF 14 15 # 重启uwsgi 16 invoke-rc.d uwsgi restart 17 注意上面的socket配置,这个在我机子上是这个路径,大家可以cd /var/run里面找下,然后配置正确,接下来建立Nginx配置文件: 18 19 20 21 22 nginx配置 23 24 cat > /etc/nginx/sites-enabled/www << EOF 25 server { 26 listen 80; 27 server_name localhost; 28 location / { 29 include uwsgi_params; 30 uwsgi_pass unix:///var/run/uwsgi/app/www/socket; 31 } 32 } 33 EOF 34 35 36 37 这里的uwsgi_pass依旧是上面我们找到的socket,注意一下server_name,这个是我们绑定的域名,通过nginx -s reload重新加载nginx。
设置django admin 静态文件
如果不配置,访问admin就会没什么页面效果
1先设置setting
#增加ROOT这个行指定一个自己的目录,自行创建下面的目录 STATIC_ROOT ='/var/www/html/server/root_static/' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static'), )
2更改nginx
server { listen 8888; server_name localhost; location / { include uwsgi_params; uwsgi_pass unix:///var/run/uwsgi/app/www/socket; uwsgi_read_timeout 60; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /var/www/html/server/root_static; 配置成刚才的目录即可 } }
重启nginx即可
配置sqlite3数据库权限(这样就赋予给2个文件权限比较安全)
sudo chgrp www-data project_dir //nginx使用的是www-data用户,project_dir项目目录 sudo chmod g+w project_dir sudo chgrp www-data project_dir/db.sqlite3 # 更改为你的数据库名称,项目目录的下一层就是默认的数据库存储路径 sudo chmod g+w project_dir/db.sqlite3
nginx这个文章写的不错
http://code.ziqiangxuetang.com/django/django-nginx-deploy.html