1. django+uwsgi配置

  (1) 把django程序上传到服务器。通过python manage.py runserver 运行起来

  访问通过服务器的ip地址+端口号访问。例:187.134.123.233:8080

(2)安装uwsgi

  pip install uwsgi

  测试uwsgi是否安装正确:

     新建hello.py :   
      def application(env,start_response):
              start_response('200 OK',[('Content-Type','text/html')])#发送请求作用,必须写这句话
              return 'hello'

   用uwsgi启动这个py文件。uwsgi --http :8999 --wsgi-file hello.py
      访问网址服务器的ip地址:8999,页面会出现hello,则证明uwsgi安装成功。

(3)使用uwsgi打开django项目。

  django项目中在和项目名同名的文件夹下有一个wsgi.py的文件。例如:django项目名为photo_manage

  打开项目在photo_manage的文件夹下有wsgi.py 的文件。

  使用命令直接启动  uwsgi  --http :8000 --module photo_manage.wsgi

  运行: http://服务器的ip:8000

  (4) 把uwsgi的参数写到配置文件中 photo_manage.ini,配置文件的位置随意放,但是一般建在和项目同目录的位置。

  [uwsgi]

  http = :9000 #端口号 如果用nginx配置的话使用socket不能用http socket = :9000

  chdir = /opt/photo_manager/photo_manage  #项目的绝对路径

  module = photo_manage.wsgi  #项目的uwsgi的接口,项目名.wsgi

  master = true

  processes = 4

  vacuum = true

    用配置文件启动 在配置文件的上个目录下输入:

  例如:photo_manager是一个文件夹,放着photo_manage的django项目和photo_manage.ini

  (venv2) root@iZ25ai8ufs5Z:/opt/photo_manager#uwsgi --ini photo_manage.ini 

  启动了django项目。

(5) 测试。如果在配置文件中使用的是http,则现在已经部署好了,不用配置nginx了。如果是scoket,则访问会出现  invalid request block size: 21573 (max 4096)...skip。这是正常现象,现在就可以安装配置nginx了。

 (6) 安装nginx。

  sudo apt-get install nginx

   /etc/init.d/nginx start #启动

       /etc/init.d/nginx stop #关闭

       /etc/init.d/nginx restart #重启

   修改nginx的默认端口号

  whereis nginx 找到nginx所在的位置
  编辑nginx.conf  vim  nginx.conf

(7)配置nginx。

server{
        listen 8080; #监听的端口配置好后要访问的端口
        server_name 182.92.224.246; #服务器的ip
        location / {
                include uwsgi_params;  #主要作用是和uwsgi关联起来,必须要写。
                uwsgi_pass 182.92.224.246:8001; #这个ip和端口号必须要和uwsgi的配置文件一致。
                access_log off;
        }
        location ~ /static/ {
                 root /opt/photo_manager/photo_manage/;  #加载静态文件。
        }
}

(8)访问:IP地址:8080  #就可以启动django项目了。

(9)静态变量加载:

  在setting.py写 STATIC_ROOT = os.path.join(BASE_DIR,'static')

  在项目中运行 python manage.py collectstatic

  记得浏览器清除缓存!!!

  

 

  

  

 


                 

posted on 2018-02-24 11:14  hello_xiaoyu  阅读(250)  评论(0编辑  收藏  举报