ubuntu django web服务器部署

前端之家收集整理的这篇文章主要介绍了ubuntu django web服务器部署前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.在ubuntu14.04上 安装pip3 https://bootstrap.pypa.io/get-pip.py

  1. python3 get-pip.py

2.安装django 最新版

  1. pip3 install django

3.修改django 支持中文 在settings.py 设置

  1. LANGUAGE_CODE = 'zh_CN'

将 /usr/local/lib/python3.4/dist-packages/django/contrib/admin/locale/zh_Hans 拷贝为zh_CN

4.修改系统支持中文,同时也可解决软件源中找不到个别软件的问题

  1. apt-get install language-pack-zh-hans*
  2.  
  3. apt-get update

5.安装虚拟环境

  1. ~/djangogirls$ sudo apt-get install python-virtualenv
  2. ~/djangogirls$ virtualenv --python=python3.4 venv

6.

使用虚拟环境

上面的命令将创建一个名为myvenv目录 (或任何你选择的名字),其中包含我们的虚拟环境 (基本上是一堆的目录和文件)。

  1. ~/djangogirls$ source myvenv/bin/activate

7. 安装web服务器 openresty 安装指导

8.安装uwsgi

pip install uwsgi 如果报错 fatal error: Python.h: No such file or directory,就要安装python开发包

  1. sudo apt-get install python-dev # for python2.x installs
  2. sudo apt-get install python3-dev # for python3.x installs

9.编写配置文件 mysite_uwsgi.ini 启动:uwsgi --inimysite_uwsgi.ini

重新加载: uwsgi --reload /tmp/uwsgi.pid 停止:uwsgi --stop /tmp/uwsgi.pid

  1. # mysite_uwsgi.ini file
  2. [uwsgi]
  3.  
  4. # Django-related settings
  5. # the base directory (full path)
  6. chdir = /root/web/mysite #django 的工程目录
  7. # Django's wsgi file
  8. module = mysite.wsgi
  9. # the virtualenv (full path)
  10. home = /root/web/venv
  11.  
  12. # process-related settings
  13. # master
  14. master = true
  15. pidfile = /tmp/uwsgi.pid #方便管理uwsgi的更新和停止
  16. # maximum number of worker processes
  17. processes = 10
  18. # the socket (use the full path to be safe
  19. socket = /root/web/mysite/mysite.sock
  20. # ... with appropriate permissions - may be needed
  21. # chmod-socket = 664
  22. # clear environment on exit
  23. vacuum = true


10.配置Nginx.conf 启动: Nginx (这里需要配置PATH=/your/Nginx/path/)

  1. user root;
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. #access_log logs/access.log main;
  10.  
  11. sendfile on;
  12. #tcp_nopush on;
  13. #keepalive_timeout 0;
  14. keepalive_timeout 65;
  15. #gzip on;
  16. # the upstream component Nginx needs to connect to
  17. upstream django {
  18. server unix:///root/web/mysite/mysite.sock; # for a file socket
  19. # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
  20. }
  21. server {
  22. listen 8000;
  23. server_name 192.168.1.225;
  24. #charset koi8-r;
  25. charset utf-8;
  26.  
  27. #access_log logs/host.access.log main;
  28. # max upload size
  29. client_max_body_size 75M; # adjust to taste
  30. # Django media
  31. location /media {
  32. alias /root/web/mysite/media; # your Django project's media files - amend as required
  33. }
  34.  
  35. location /static {
  36. include mime.types;#如果不加这句,你的css样式就不会显示
  37. alias /root/web/mysite/static; # your Django project's static files - amend as required
  38. }
  39.  
  40. # Finally,send all non-media requests to the Django server.
  41. location / {
  42. uwsgi_pass django;
  43. include uwsgi_params; # the uwsgi_params file you installed
  44. }
  45. }
  46. }

11.注意:uwsgi 和 Nginx的启动都要是同一个user,django 项目要调用python manage.py collectstatic命令,整理静态文件(js/css等)

12.加入开机启动项 :crontab -e 编辑添加如下内容

  1. @reboot su - root -c /usr/local/openresty/Nginx/sbin/Nginx &
  2. @reboot su - root -c "/usr/local/bin/uwsgi --ini /root/web/mysite/mysite_uwsgi.ini" &

猜你在找的Ubuntu相关文章