码迷,mamicode.com
首页 > 系统相关 > 详细

Ubuntu16下部署Django+Nginx+uwsgi

时间:2018-04-25 12:46:50      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:isa   tps   php7.0   config   uri   reload   cal   fas   generated   

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

标签:isa   tps   php7.0   config   uri   reload   cal   fas   generated   

原文地址:https://www.cnblogs.com/vhyz/p/8942980.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!