Ubuntu16下部署Django+Nginx+uwsgi
Posted vhyz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu16下部署Django+Nginx+uwsgi相关的知识,希望对你有一定的参考价值。
1.更新apt-get
apt-get update
apt-get upgrade
2.安装nginx
apt-get install nginx
然后在浏览器输入IP地址若有nginx欢迎界面则成功
3.安装python3-pip
apt-get instll python3-pip
注意安装python3的pip而不是python,安装成功后可以更新pip
4.安装Django以及uwsgi
pip3 install Django
pip3 install uwsgi
也可以采用虚拟环境下的安装
安装成功后将项目上传至服务器
5.配置uwsgi
在Django的manage.py同目录下创建uwsgi.ini
添加下面内容,注意将地址改成自己的
[uwsgi]
chdir = /home/feixue/python/www/for_test //项目根目录
module = for_test.wsgi:application //指定wsgi模块
socket = 127.0.0.1:8000 //对本机8000端口提供服务
master = true //主进程
#vhost = true //多站模式
#no-site = true //多站模式时不设置入口模块和文件
#workers = 2 //子进程数
#reload-mercy = 10
#vacuum = true //退出、重启时清理文件
#max-requests = 1000
#limit-as = 512
#buffer-size = 30000
#pidfile = /var/run/uwsgi9090.pid //pid文件,用于下脚本启动、停止该进程
daemonize = /home/feixue/python/www/for_test/run.log
disable-logging = true //不记录正常信息,只记录错误信息
6.配置Nginx
打开/etc/nginx/sites-available/default,更改配置
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
server_name your_ip;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
location /media {
alias /usr/local/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /usr/local/mysite/statics;
}
# pass the php scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
将your_ip更改为你的ip,且项目地址自己改
7.启动
sudo uwsgi uwsgi.ini
sudo service nginx restart
若出现You may need to add ‘XXXXXXXX‘ to ALLOWED_HOSTS.
则在seting.py文件中添加即可
8.静态文件设置
在setting.py中修改下列信息
DEBUG = False
STATIC_ROOT = os.path.join(BASE_DIR, 'statics')
然后执行
python manage.py collectstatic
这样就产生了static文件,静态请求由Nginx处理
以上是关于Ubuntu16下部署Django+Nginx+uwsgi的主要内容,如果未能解决你的问题,请参考以下文章
ubuntu 16.04部署python项目(Nginx+uwsgi+django)
ubuntu 16.04 部署 pypy+nginx+uwsgi+django(详细)
django+nginx+uwsgi的生产环境部署(Ubuntu16.04)
Django+nginx+uwsgi部署教程(centos7+ubuntu16.4)