一、安装nginx
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel prce pcre-devel
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zvxf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make && make install
二、安装并测试uwsgi
pip install uwsgi
在django项目的根目录下创建uwsgitest.py文件,添加源码如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
#return ["Hello World"] # python2
return [b"Hello World"] # python3
然后,Run uWSGI:
uwsgi --http :8000 --wsgi-file uwsgitest.py
参数含义:
http :8000: 使用http协议,8000端口
wsgi-file uwsgitest.py: 加载指定文件 uwsgitest.py
打开对应机器端口url,浏览器上应该显示hello world,uwsgi便安装成功了。
说明以下环节是通的
the web client <-> uWSGI <-> Python
如果缺少报这个错uwsgi: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory可以看https://blog.csdn.net/david_sheep/article/details/85327954这个文章的解决方法。
三、测试UWSGI运行Django项目
首先确保project本身是正常的:
python manage.py runserver 0.0.0.0:8000
如果没问题,使用uWSGI把project拉起来,–module选项加载wsgi module,后面要跟 自己的项目名.wsgi,
uwsgi --http :8000 --module Servicerestart.wsgi
这里UWSGI会找到 项目下的wsgi的配置文件,即 项目名/wsgi.py
如果project能够正常被拉起,说明以下环节是通的:
the web client <-> uWSGI <-> Django
四、通过socket连接Nginx和uWSGI
socket中文翻译为套接字,它的作用就像一个连接器,比方说把两段水管连接起来,每段管子都有自己不同的编号,下面就来试验下套接字的作用。
带socket的Nginx配置多了这个部分:
upstream django {
#server unix:///root/PycharmProjects/Servicerestart/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
使用socket连接,启动测试文件
uwsgi --socket :8001 --wsgi-file uwsgitest.py
访问成功
说明以下环节是通的
the web client <-> Nginx <-> Socket <-> uWSGI <-> Python
五、用UNIX socket取代TCP port
修改nginx.conf
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)
}
根据自己的项目进行设置sock文件的位置,位置最好在项目目录下,下面才可以直接在项目下运行,名称可以自定义,我设为mysite.sock。
重启nginx,并在项目目录下运行uWSGI测试文件
uwsgi --socket mysite.sock --wsgi-file uwsgitest.py --chmod-socket=666
如果还是没有成功:
检查 nginx error log(/var/log/nginx/error.log)。如果错误如下:
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
就修改下nginx.conf里的启动用户吧。。。改成root应该权限就够了。。可能是nginx默认用户的权限不够修改项目里的文件或其他地方的。。烦
测试访问成功
五、使用UWSGI Socket+Nginx运行django项目
如果使用的是python manage.py runserver 127.0.0.1:8080 这个命令来启动django并且DEBUG=True的话,django默认是会为setting.py里的STATIC_URL变量开头的静态文件请求做路由的。但是我们这里用的是uwsgi,并且在生产环境一般设置DEBUG=False代码报错才不会泄露,这两种情况django都是不会为STATIC_URL的静态文件做路由的,所以需要在nginx配置里对静态文件提前进行一个转发或者在项目最外层urls.py中做匹配路由,这两种方法都需要将文件收集到STATIC_ROOT中。
这里在nginx中进行设置静态转发,匹配/static是因为STATIC_URL这个变量默认为/static/
使用uwsgi或部署模式DEBUT=False时,需要设置STATIC_ROOT,并且将静态文件都收集到一起
设置setting.py
# 存放静态文件的目录,其中也可以包含url,这里使用的是直接在根目录下创建的static文件夹
STATICFILES_DIRS= [
os.path.join(BASE_DIR,'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static_new") #使用 collectstatic后收集的静态文件的存放绝对路径
然后执行下面命令,会将STATICFILES_DIRS下的文件都收集到STATIC_ROOT中
python manage.py collectstatic
具体可以看后面写的详细介绍的这篇博客Django中DEBUG模式及static静态文件
配置nginx.conf
# mysite_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 8000;
# 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 /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 django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
根据自己的项目将mysite修改。告诉nginx从文件系统中拉起media和static文件作为服务,同时响应django的request。static用来响应静态文件请求,media没找到具体的解释。
uwsgi_params默认在nginx的conf文件夹下,我这里修改的是nginx.conf文件,所以就直接用当前位置下的
测试可以访问设置的路径下的静态文件,不管django项目有没有启动,只要nginx有启动就行了,因为这里已经设置了静态文件的访问路径了。
如果上面一切都显示正常,则在项目下执行下面命令可以拉起django application
uwsgi --socket mysite.sock --module Servicerestart.wsgi --chmod-socket=666
也可以直接使用绝对路径,可以加到rc.local里设置开机自启
uwsgi --socket /root/PycharmProjects/Servicerestart/mysite.sock --module /root/PycharmPjects/Servicerestart/Servicerestart.wsgi --chmod-socket=666
检查
每次都需要进入到项目文件夹下并运行上面命令拉起django application实在麻烦,使用.ini文件能简化工作,方法如下:
创建文件mysite_uwsgi.ini,填入并修改下面内容,指定项目文件夹、module名称以及socket位置
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/PycharmProjects/Servicerestart
# Django's wsgi file
module = Servicerestart.wsgi
# the virtualenv (full path)
#home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /root/PycharmProjects/Servicerestart/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 666
# clear environment on exit
vacuum = true
现在,只要执行以下命令,就能够拉起django application:
uwsgi --ini mysite_uwsgi.ini
如果出现JS文件404的话,把JS文件权限改成777试试
参考:https://www.jianshu.com/p/e6ff4a28ab5a
http://www.pianshen.com/article/4838129727/