部署方式

  Nginx+uwsgi

uwsgi安装与配置

  安装

pip install uwsgi



  配置

[uwsgi]
# 使用uwsgi示范
# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:6001
# 指向网站目录
chdir=/home/work/project/osroom
# python 启动程序文件
wsgi-file = start.py

# python 程序内用以启动的 application 变量名
callable = app
master = true
enable-threads = true

# 启动的线程
processes = 4
vacuum = true
die-on-term = true
harakiri = 30

# 每一个工作进程都生成以后才加载应用程序
lazy = true
disable-logging = true
# 状态检测地址
stats = 127.0.0.1:9191
# pid
pidfile = /tmp/osroom_uwsgi.pid

其他配置项可以参考uwsgi文档

  启动网站

# 如果uwsgi装在python虚拟环境,要先进入虚拟环境,再用如下命令启动
uwsgi /home/work/project/osroom/uwsgi.ini



(附加)方法2:如果uwsgi安装在python虚拟环境中,又不想进入虚拟环境,可以这样启动

# venv_osroom是我的虚拟环境目录
/home/work/project/venv_osroom/bin/uwsgi /home/work/project/osroom/uwsgi.ini



- 启动成功后: 由于uwsgi.ini配置的端口是6001,所以不能通过其他端口访问网站,需要下面配置Nginx转发到6001端口

...

uwsgi socket 0 bound to TCP address 127.0.0.1:6001 fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
Python main interpreter initialized at 0x1b9bcd0
python threads support enabled

...

*** Operational MODE: preforking ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 22449)
spawned uWSGI worker 1 (pid: 22450, cores: 1)
spawned uWSGI worker 2 (pid: 22451, cores: 1)
spawned uWSGI worker 3 (pid: 22452, cores: 1)
spawned uWSGI worker 4 (pid: 22453, cores: 1)

...

worker 1 buried after 1 seconds
worker 2 buried after 1 seconds
worker 3 buried after 1 seconds
worker 4 buried after 1 seconds

...




Nginx 安装与配置

  安装

sudo apt-get install nginx



  配置文件



upstream osroom-web {
      # 转发到6001端口
      server 127.0.0.1:6001;
}

server {
        # 监听80端端口
        listen 80;
        server_name <你的域名>;
        gzip on;
        gzip_comp_level 5;
        gzip_types application/json text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
        open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
        access_log /var/log/nginx/manage.vhost.access.log;
        error_log /var/log/nginx/manage.vhost.error.log;
        location / {
             include      uwsgi_params;
             # upstream 的那个名称
             uwsgi_pass osroom-web;
             # python虚拟环境目录路径 
             uwsgi_param UWSGI:_PYHOME /home/work/project/venv_osroom;
             # 项目目录路径 
             uwsgi_param UWSGI_CHDIR /home/work/project/osroom-demo;;
             uwsgi_param UWSGI_SCRIPT start:app;
          }
}



upstream osroom-web {
      # 转发到6001端口
      server 127.0.0.1:6001;
}

server {
        # 监听443端端口
        listen 443;
        server_name <你的域名>;
        ssl on;
        root html;
        index index.html index.htm;
        # ssl证书文件
        ssl_certificate   /home/work/project/15212232323.pem;
        ssl_certificate_key  /home/work/project/15212232323.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        gzip on;
        gzip_comp_level 5;
        gzip_types application/json text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
        open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
        access_log /var/log/nginx/manage.vhost.access.log;
        error_log /var/log/nginx/manage.vhost.error.log;
        location / {
             include      uwsgi_params;
             # upstream 的那个名称
             uwsgi_pass osroom-web;
             # python虚拟环境目录路径 
             uwsgi_param UWSGI:_PYHOME /home/work/project/venv_osroom;
             # 项目目录路径 
             uwsgi_param UWSGI_CHDIR /home/work/project/osroom-demo;;
             uwsgi_param UWSGI_SCRIPT start:app;
          }
}

server {
        listen 80;
        server_name <你的域名>;
        return    301 https://$server_name$request_uri;
}



# Ubuntu apt-get安装后的Nginx配置文件在/etc/nginx/nginx.conf
# 编辑/etc/nginx/nginx.conf找到在http {}里面最后添加osroom.conf 
# 可以用*通配符,前提是该目录下没有其他非nginx得.conf配置文件
http {

   ...

  include /home/work/project/*.conf;
}



  重启Nginx

sudo /etc/init.d/nginx restart



  访问