操作系统ubuntu 14.04.2
1、安装uwsgi,我的机器上有两个python环境,开始是在真实环境下装的,后来发现有问题,去虚拟环境又装了一次,但是还是没能连接上uwsgi和django,应该是需要重启uwsgi,第二天就直接能启动了,但是无法加载django的静态资源,静态资源要交给nginx来处理。
pip install uwsgi
尝试连接uwsgi前,先分别尝试uwsgi和django,django用自带的开发服务器,uwsgi可以写一个简单的函数,如下
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
启动uwsgi
<pre name="code" class="plain">uwsgi --http :8000 --wsgi-file django_uwsgi.py
连接uwsgi和django项目,在项目根目录下创建django_uwsgi.py
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
os.environ.setdefault("DJANGO_SETTINGS_MODULE","project_name.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
启动uwsgi,看uwsgi与django连接是否正常
uwsgi --http :8000 --wsgi-file django_uwsgi.py
2、安装nginx
apt-get install nginx
测试nginx是否能正常工作
service nginx start
service nginx stop
service nginx restart
开启服务后,若能正常访问主页,即为成功的
3、配置nginx
nginx服务启动的时候会去加载/etc/nginx/nginx.conf中的配置,可以把配置信息直接添加到其中,也可以单独创建一个自己的配置文件,比如mysite.conf放在conf.d文件夹中,因为/etc/nginx/nginx.conf会加载conf.d下的所有配置文件,配置内容如下:
<pre class="hljs php" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="php" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;"><span class="hljs-comment" style="color: rgb(147, 161, 161);"># the port your site will be served on</span>
listen <span class="hljs-number" style="color: rgb(42, 161, 152);">8000</span>;
<span class="hljs-comment" style="color: rgb(147, 161, 161);"># the domain name it will serve for</span>
server_name .example.com; <span class="hljs-comment" style="color: rgb(147, 161, 161);"># substitute your machine's IP address or FQDN</span>
charset utf-<span class="hljs-number" style="color: rgb(42, 161, 152);">8</span>;</code>
#格式化日志输出
log_format access '$msec $status $request $request_time $http_referer $remote_addr [ $time_local ] $upstream_response_time $host $bytes_sent $request_length $upstream_addr';
access_log /var/log/nginx/access.log access; #存放日志;
error_log /var/log/nginx/error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media 我没用media文件就不需要写这一项
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:8077; #把不能处理请求(动态请求?)的发到uwsgi进行处理
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed 在/etc/nginx/目录下
}
}
4、配置uwsgi
我们设置uwsgi和nginx的通信端口是8077。可以在项目主目录下配置xml文件,(第一步已经设置好django_uwsgi):
<uwsgi>
<socket>:8077</socket>
<chdir>project_dir</chdir>
<module>django_wsgi</module>
<processes>4</processes> <!-- 进程数 -->
<daemonize>uwsgi.log</daemonize>
</uwsgi>
5、补充,在步骤3之前要设置django的静态文件目录,在项目的setting.py中加入
STATIC_ROOT = '/static/'
注意这里的STATIC_ROOT,这是项目的相对路径,运行
pyhton manage.py collectstatic
把所有静态资源移到设定的目录中
6、运行
service nginx start
uwsgi -x django_uwsgi.xml
我的问题:代码修改后,要怎样重启uwsgi?杀掉每一个进程再开?
参考:
http://www.jianshu.com/p/e6ff4a28ab5a