uWSGI是一个web服务器,实现了WSGI
协议、uwsgi
协议、http
协议等。
uWSGI
的主要特点是:
app
管理app
的性能和瓶颈) uWSGI
服务器自己实现了基于uwsgi
协议的server
部分,我们只需要在uwsgi
的配置文件中指定application
的地址,uWSGI
就 能直接和应用框架中的WSGI application
通信。
Nginx 是一个高性能的负载均衡HTTP和反向代理服务器,Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器。
特点是占有内存少,并发能力强。
nginx相当于是服务器,负责接收请求
uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
2个基本概念:
服务器(接收请求),应用程序(处理请求并返回)
通信过程:
客户端发送一个http请求,被nginx服务器接收,nginx服务器将请求转发给uwsgi,uwsgi将请求转发给实现uwsgi
协议的应用程序(flask,gunicorn等等)
4.1: [root@crazy-acong ~]# pip3 install uwsgi
4.2: 创建django项目并配置uWSGI配置文件:
cd 进入到 django 的主目录
vi test-uwsgi.ini # 添加以下配置文件,按需修改即可:
[uwsgi]4.3 通过 uwsgi 配置文件启动 django 程序
uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序
4.4. 查看uwsgi的启动进程状态
netstat -lnpt | grep uwsgi
4.4 . 安装nginx :
apt-get install nginx
5、配置 nginx 的配置文件
在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件目录中添加该文件也可以
5.1 创建 nginx 配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-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 /data/django_test/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/django_test/uwsgi_params; # the uwsgi_params file you installed } }
5.2 重启nginx 服务
[root@crazy-acong django_test]# nginx -t nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful [root@crazy-acong django_test]# nginx -s reload [root@crazy-acong django_test]# netstat -lnpt | grep 8000 tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx
这个时候就可以通过 http://ip:8000 访问 django 程序了,不过目前还存在一个问题,访问 http://ip:8000/admin 发现静态文件貌似没读取到,需要通过下面的方法解决静态文件的问题
6、解决 django 多 app 静态文件的问题
# 在 django 程序的 settings.py 文件中添加以下内容 STATIC_ROOT = os.path.join(BASE_DIR, "static_all") # 然后通过执行该命令,将静态文件整合到一个目录中 [root@crazy-acong django_test]# python3 manage.py collectstatic [root@crazy-acong django_test]# ll total 40 drwxr-xr-x 3 nginx games 4096 Mar 14 14:42 app01 -rw-r--r-- 1 root root 3072 Mar 14 14:51 db.sqlite3 -rw-r--r-- 1 root root 1026 Mar 14 15:18 django-nginx.conf drwxr-xr-x 3 nginx games 4096 Mar 14 15:45 django_test -rwxr-xr-x 1 nginx games 809 Mar 14 14:37 manage.py drwxr-xr-x 2 nginx games 4096 Mar 14 14:42 static drwxr-xr-x 3 root root 4096 Mar 14 15:47 static_all # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了 drwxr-xr-x 2 nginx games 4096 Mar 14 14:40 templates -rw-r--r-- 1 root root 565 Mar 14 15:40 test-uwsgi.ini -rw-r--r-- 1 root root 664 Mar 14 15:28 uwsgi_params
然后需要修改 nginx 配置文件中 指向 django 静态目录的配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-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 /data/django_test/static_all; # 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/django_test/uwsgi_params; # the uwsgi_params file you installed } }
最后重启 nginx 服务即可
[root@crazy-acong django_test]# nginx -t nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful [root@crazy-acong django_test]# nginx -s reload