Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程

人码图技术分享866

具体环境:
Ubuntu 14.04 Python 2.7.6 Django 1.7.1 Virtualenv name:test Nginx uwsgi

假设 项目文件夹位于 /data/www/ts 设置保存在 ./conf


复制代码代码如下:

virtualenv name = test
domain name = example.com


django+uwsgi的部署实在是太蛋疼了..网上已有的教程似乎有新版本的兼容问题。最后跑到uwsgi官网上找的教程终于跑通了..
不过官网的教程似乎有引导教学性质,部署的时候就显得很绕弯路,在这里记录下来精简内容

http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

首先,需要一个uwsgi_params文件,放在项目的conf文件夹里面。之后需要指向它。文件内容如下:


复制代码代码如下:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;


创建一个叫做ts_nginx.conf 的文件,内容如下


复制代码代码如下:

# ts_nginx.conf


# 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:8001; # 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 .example.com; # 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 /data/www/ts/media; # your Django project's media files - amend as required
    }

    location /static {
        alias /data/www/ts/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass django;
        include /data/www/ts/conf/uwsgi_params; # the uwsgi_params file you installed
    }
}


把这个conf文件连接到nginx的搜索目录里面。


复制代码代码如下:

sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-enabled/


先决条件:这里要设置好django项目的settings里面static files


复制代码代码如下:

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


python manage.py collectstatic


之后:


复制代码代码如下:

service nginx restart


应该就可以看到
http://example.cn:8000/media/1.gif
了(事先放进去一个静态文件)

之后的blabla步骤都是废话,跳到这里:


复制代码代码如下:

Configuring uWSGI to run with a .ini file


ts_uwsgi.ini 在项目根目录


复制代码代码如下:

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /data/www/ts
# Django's wsgi file
module = ts.wsgi
# the virtualenv (full path)
home = /root/.envs/test
# 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:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# set an environment variable
env = DJANGO_SETTINGS_MODULE=conf.settings
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file


这里环境变量设置env需要conf文件夹有init.py,否则conf不会被认为是module

(目前除了80端口,其他端口都可以通过地址:端口访问。已经测试8000,81.80测试不知道为什么不成。明天待续)


相关文章

网络安全扫描工具

网络安全扫描工具

网络安全扫描工具是用于检测计算机系统、网络和应用程序中存在的安全漏洞和弱点的软件工具。在一般环境中,我们主要针对sql和html进行安全扫描,帮助开发人员优化提升系统环境的安全。以下是15个常用的网络...

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

1.安装ubuntu有uwsgi的ppa:123add-apt-repository ppa:stevecrozz/ppaapt-get update apt-get install uwsgi 2....

开源--open-falcon

开源--open-falcon

一、open-falcon简介  open-falcon是由小米运维团队,从互联网公司角度为出发点,开发出来的一套面向互联网行业的企业级的开源监控系统,截至2019年7月,open-falcon最新稳...

Python 虚拟环境venv详解

Python 虚拟环境venv详解

Python 虚拟环境主要是为不同 Python 项目创建一个隔离的环境,每个项目都可以拥有独立的依赖包环境,而项目间的依赖包互不影响,对Python 虚拟环境venv相关知识感兴趣的朋友一起看看吧+...

开源--Ventoy

开源--Ventoy

简单来说,Ventoy是一个制作可启动U盘的开源工具。有了Ventoy你就无需反复地格式化U盘,你只需要把 ISO/WIM/IMG/VHD(x)/EFI 等类型的文件直接拷贝到U盘里面就可以启动了,无...

linux中用conda创建虚拟环境

linux中用conda创建虚拟环境

首先安装anaconda,使用wegt命令,下载sh文件官网链接:https://www.anaconda.com/products/individual下载命令:wget https://repo....

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。