Nginx+Uwsgi部署Django项目(基于ubuntu系统

作者: 眼君 | 来源:发表于2017-07-21 14:51 被阅读43次

    安装Nginx:

    ubuntu系统可以直接通过sudo apt-get install nginx命令安装,安装前简易先执行sudo apt-get update命令更新软件源后再安装。

    安装成功后执行/etc/init.d/nginx start命令启动nginx服务,看到下图表示启动成功。

    启动Nginx

    此时可以在命令行运行curl 127.0.0.1命令,系统返回下面的信息表示运行成功,

    curl命令返回信息

    也可以直接打开浏览器127.0.0.1看到如下页面,

    浏览器页面返回信息

    表示nginx系统已成功安装并启动了。

    安装Uwsgi

    安装

    >>>pip3 install uwsgi

    测试

    假设django的wsgi文件路径在projectname/projectname/wsgi.py,

    则命令行进入根目录后,执行以下命令:

    >>>uwsgi --http :8000 --module XXX.wsgi(xxx为你的项目名称)

    出现以下页面则表示UWSGI

    测试正常

    然后打开浏览器,输入当前IP:8000能正常打开页面,静态资源的加载问题先忽略。

    Nginx系统配置:

    添加配置文件

    在项目根目录下新建一个conf目录,然后按照路径conf/nginx/uc_nginx.conf新建一个uc_nginx.conf文件,用vim打开此文件,输入以下配置内容:

    # the upstream component nginx needs to connect to

    upstream django {

    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket

    server 127.0.0.1:8000; # 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 你的ip地址 ; # substitute your machine's IP address or FQDN

    charset    utf-8;

    # max upload size

    client_max_body_size 75M;  # adjust to taste

    # Django media

    location /media  {

    alias 你的目录路径/media;  # 指向django的media目录

    }

    location /static {

    alias 你的目录路径/static; # 指向django的static目录

    }

    # Finally, send all non-media requests to the Django server.

    location / {

    uwsgi_pass  django;

    include    uwsgi_params; # the uwsgi_params file you installed

    }

    }

    填好后保存退出,然后把这个文件软连接到nginx的配置文件里去:

    >>>sudo ln -s 你的目录路径/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/

    重启系统

    >>>sudo service nginx restart

    如果没有报错,运行以下命令

    >>>ps aux|grep nginx

    看到以下画面,证明nginx启动正常了

    启动正常

    集中静态资源

    打开Django项目的配置文件settings.py,添加下面一行代码

    STATIC_ROOT = os.path.join(BASE_DIR, "static/")

    代码

    注意:如果settings.py文件中配置过STATICFILES_DIRS的话一定要注释掉,这两行代码共存会报错。

    然后在项目的根目录下运行以下命令就会将所有静态文件都集中到根目录下都static文件中。 

    >>>python3 manage.py collectstatic

    配置UWSGI

    写入uwsgi的配置文件

    在项目根目录下,运行下列命令,进入刚刚创建的conf文件,并新建一个uwsgi.ini文件

    >>>vim conf/uwsgi.ini

    然后将以下内容粘贴进文件

    # mysite_uwsgi.ini file

    [uwsgi]

    # Django-related settings

    # the base directory (full path)

    chdir          = /home/bobby/Projects/MxOnline

    # Django's wsgi file

    module          = MxOnline.wsgi

    # the virtualenv (full path)

    # process-related settings

    # master

    master          = true

    # maximum number of worker processes

    processes      = 10

    # the socket (use the full path to be safe

    socket          = 127.0.0.1:8000

    # ... with appropriate permissions - may be needed

    # chmod-socket    = 664

    # clear environment on exit

    vacuum          = true

    virtualenv = /home/bobby/.virtualenvs/mxonline

    保存退出后,在conf目录下运行以下命令,启动这个配置文件即可

    >>>uwsgi -i uwsgi.ini

    相关文章

      网友评论

        本文标题:Nginx+Uwsgi部署Django项目(基于ubuntu系统

        本文链接:https://www.haomeiwen.com/subject/kfknqxtx.html