资讯专栏INFORMATION COLUMN

Django-Nginx-uwsgi

罗志环 / 2026人阅读

摘要:安装创建文件创建配置文件启动也可编写启动脚本配置文件,添加下面一行内容在新建文件重新启动访问页面测试调试过程中,报错多多查看日志排错。

在开发环境下调试好python项目之后,把项目迁移到nginx上面 首先把django admin的静态文件目录复制一份到你设置的static文件目录下:

</>复制代码

  1. python manage.py collectstatic
关闭settings.py中的debug,关闭debug之后staticfiles就不生效了,需要在nginx配置静态文件的访问。 安装uwsgi

</>复制代码

  1. pip install uwsgi
创建wsgi文件:

</>复制代码

  1. root@server1 testweb]# pwd
  2. /Django/testweb
  3. [root@server1 testweb]# cat wsgi.py
  4. import os
  5. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")
  6. from django.core.wsgi import get_wsgi_application
  7. application = get_wsgi_application()
创建ini配置文件:

</>复制代码

  1. [root@server1 testweb]# pwd
  2. /Django/testweb
  3. [root@server1 testweb]# cat app.ini
  4. [uwsgi]
  5. chdir = /Django/testweb/
  6. ;wsgi-file = /Django/testweb/wsgi.py
  7. module = testweb.wsgi
  8. socket = 127.0.0.1:3400
  9. ;socket = /var/log/%(project).sock
  10. chmod-socket = 664
  11. ;http = 0.0.0.0:8001
  12. ;stats = 0.0.0.0:8001
  13. master = true
  14. processes = 4
  15. threads = 2
  16. max-requests = 6000
  17. vacuum = true
  18. pidfile = /var/log/uwsgi.pid;
  19. daemonize = /var/log/uwsgi.log
启动uwsgi:

</>复制代码

  1. [root@server1 testweb]# uwsgi --ini app.ini
也可编写uwsgi启动脚本:

</>复制代码

  1. [root@server1 ~]# cat /etc/init.d/uwsgi
  2. uwsgi_path=/usr/bin/uwsgi
  3. uwsgi_ini=/Django/testweb/app.ini
  4. uwsgi_pid=/var/log/uwsgi.pid
  5. if [ ! -n $1 ]
  6. then
  7. echo "Usages: [start|stop|restart]"
  8. exit 0
  9. fi
  10. if [ $1 = start ]
  11. then
  12. psid=`ps aux | grep "zh" | grep -v "grep" | wc -l`
  13. if [ $psid -gt 4 ]
  14. then
  15. echo "uwsgi is running!"
  16. exit 0
  17. else
  18. uwsgi --ini $uwsgi_ini
  19. echo "Start uwsgi service [OK]"
  20. fi
  21. elif [ $1 = stop ];then
  22. killall -s INT uwsgi
  23. echo "Stop uwsgi service [OK]"
  24. elif [ $1 = restart ];then
  25. #killall -s INT uwsgi
  26. #uwsgi --ini uwsgi_ini
  27. kill -HUP $uwsgi_pid
  28. echo "Restart uwsgi service [OK]"
  29. else
  30. echo "Usages: [start|stop|restart]"
  31. fi
  32. [root@server1 ~]# cat /etc/init.d/uwsgi
  33. uwsgi_path=/usr/bin/uwsgi
  34. uwsgi_ini=/Django/testweb/app.ini
  35. uwsgi_pid=/var/log/uwsgi.pid
  36. if [ ! -n $1 ]
  37. then
  38. echo "Usages: [start|stop|restart]"
  39. exit 0
  40. fi
  41. if [ $1 = start ]
  42. then
  43. psid=`ps aux | grep "zh" | grep -v "grep" | wc -l`
  44. if [ $psid -gt 4 ]
  45. then
  46. echo "uwsgi is running!"
  47. exit 0
  48. else
  49. uwsgi --ini $uwsgi_ini
  50. echo "Start uwsgi service [OK]"
  51. fi
  52. elif [ $1 = stop ];then
  53. killall -s INT $uwsgi_path
  54. echo "Stop uwsgi service [OK]"
  55. elif [ $1 = restart ];then
  56. #killall -s INT uwsgi
  57. #uwsgi --ini uwsgi_ini
  58. kill -HUP $uwsgi_pid
  59. echo "Restart uwsgi service [OK]"
  60. else
  61. echo "Usages: [start|stop|restart]"
  62. fi
配置nginx.conf文件,添加下面一行内容:

</>复制代码

  1. include /usr/local/nginx/conf/conf.d/*;
在conf.d新建app.conf文件:

</>复制代码

  1. [root@server1 conf.d]# cat app01.conf
  2. server {
  3. listen 80;
  4. server_name localhost;
  5. charset utf-8;
  6. access_log logs/testweb/app01/access_log;
  7. error_log logs/testweb/app01/error_log;
  8. client_max_body_size 75M; # adjust to taste
  9. #location /admin {
  10. #alias /Django/testweb/static/admin
  11. # }
  12. location /static {
  13. alias /Django/testweb/static; # your Django project"s static files - amend as required
  14. }
  15. # Finally, send all non-media requests to the Django server.
  16. location / {
  17. uwsgi_pass 127.0.0.1:3400;
  18. include uwsgi_params; # the uwsgi_params file you installed
  19. }
  20. }
重新启动Nginx:

</>复制代码

  1. [root@server1 conf.d]# /etc/init.d/nginx stop
  2. [root@server1 conf.d]# /etc/init.d/nginx start
访问页面测试:


调试过程中,报错多多查看日志排错。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41596.html

相关文章

发表评论

0条评论

罗志环

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<