部署方式

Nginx+uwsgi

  • 部署Python Web的方式有好多中, 这里只是举例其中一种方式, 使用Nginx + uwsgi部署. 查阅资料,认为uwsgi性能还不错

uwsgi安装与配置

安装

  • pip 安装uwsgi(如果有建有python虚拟环境的请先进入虚拟环境再安装)
  1. pip install uwsgi

配置

  • 新建一个文件uwsgi.ini (建议在osroom根目录下),写入如下配置:
  1. [uwsgi]
  2. # 使用uwsgi示范
  3. # uwsgi 启动时所使用的地址与端口
  4. socket = 127.0.0.1:6001
  5. # 指向网站目录
  6. chdir=/home/work/project/osroom
  7. # python 启动程序文件
  8. wsgi-file = start.py
  9. # python 程序内用以启动的 application 变量名
  10. callable = app
  11. master = true
  12. enable-threads = true
  13. # 启动的线程
  14. processes = 4
  15. vacuum = true
  16. die-on-term = true
  17. harakiri = 30
  18. # 每一个工作进程都生成以后才加载应用程序
  19. lazy = true
  20. disable-logging = true
  21. # 状态检测地址
  22. stats = 127.0.0.1:9191
  23. # pid
  24. pidfile = /tmp/osroom_uwsgi.pid

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

启动网站

  • 注意:启动前确认osroom的数据库是否已配置正确
  • 启动命令
  1. # 如果uwsgi装在python虚拟环境,要先进入虚拟环境,再用如下命令启动
  2. uwsgi /home/work/project/osroom/uwsgi.ini

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

  1. # venv_osroom是我的虚拟环境目录
  2. /home/work/project/venv_osroom/bin/uwsgi /home/work/project/osroom/uwsgi.ini
  • 启动成功后:由于uwsgi.ini配置的端口是6001,所以不能通过其他端口访问网站,需要下面配置Nginx转发到6001端口
  1. ...
  2. uwsgi socket 0 bound to TCP address 127.0.0.1:6001 fd 3
  3. Python version: 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
  4. Python main interpreter initialized at 0x1b9bcd0
  5. python threads support enabled
  6. ...
  7. *** Operational MODE: preforking ***
  8. *** uWSGI is running in multiple interpreter mode ***
  9. spawned uWSGI master process (pid: 22449)
  10. spawned uWSGI worker 1 (pid: 22450, cores: 1)
  11. spawned uWSGI worker 2 (pid: 22451, cores: 1)
  12. spawned uWSGI worker 3 (pid: 22452, cores: 1)
  13. spawned uWSGI worker 4 (pid: 22453, cores: 1)
  14. ...
  15. worker 1 buried after 1 seconds
  16. worker 2 buried after 1 seconds
  17. worker 3 buried after 1 seconds
  18. worker 4 buried after 1 seconds
  19. ...

Nginx 安装与配置

安装

  • Ubuntu 14.04或Ubuntu 16.04使用apt-get直接安装如下:
  1. sudo apt-get install nginx
  • 也可以使用wget下载,再安装,请自行Google查找教程哈

配置文件

  • 创建一个nginx配置文件osroom-naginx.conf,文件配置如下(这里只是示范,Nginx有许多配置项可以自己查找教程):
  1. upstream osroom-web {
  2. # 转发到6001端口
  3. server 127.0.0.1:6001;
  4. }
  5. server {
  6. # 监听80端端口
  7. listen 80;
  8. server_name <你的域名>;
  9. gzip on;
  10. gzip_comp_level 5;
  11. gzip_types application/json text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
  12. open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
  13. access_log /var/log/nginx/manage.vhost.access.log;
  14. error_log /var/log/nginx/manage.vhost.error.log;
  15. location / {
  16. include uwsgi_params;
  17. # upstream 的那个名称
  18. uwsgi_pass osroom-web;
  19. # python虚拟环境目录路径
  20. uwsgi_param UWSGI:_PYHOME /home/work/project/venv_osroom;
  21. # 项目目录路径
  22. uwsgi_param UWSGI_CHDIR /home/work/project/osroom-demo;;
  23. uwsgi_param UWSGI_SCRIPT start:app;
  24. }
  25. }
  • 如果需要配置ssl请加上监听443端口,配置好证书,然后80端口301重定向到443 https请求, 如下:(不需配置SSL证书的请直接跳过此步骤)
  1. upstream osroom-web {
  2. # 转发到6001端口
  3. server 127.0.0.1:6001;
  4. }
  5. server {
  6. # 监听443端端口
  7. listen 443;
  8. server_name <你的域名>;
  9. ssl on;
  10. root html;
  11. index index.html index.htm;
  12. # ssl证书文件
  13. ssl_certificate /home/work/project/15212232323.pem;
  14. ssl_certificate_key /home/work/project/15212232323.key;
  15. ssl_session_timeout 5m;
  16. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  17. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  18. ssl_prefer_server_ciphers on;
  19. gzip on;
  20. gzip_comp_level 5;
  21. gzip_types application/json text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
  22. open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
  23. access_log /var/log/nginx/manage.vhost.access.log;
  24. error_log /var/log/nginx/manage.vhost.error.log;
  25. location / {
  26. include uwsgi_params;
  27. # upstream 的那个名称
  28. uwsgi_pass osroom-web;
  29. # python虚拟环境目录路径
  30. uwsgi_param UWSGI:_PYHOME /home/work/project/venv_osroom;
  31. # 项目目录路径
  32. uwsgi_param UWSGI_CHDIR /home/work/project/osroom-demo;;
  33. uwsgi_param UWSGI_SCRIPT start:app;
  34. }
  35. }
  36. server {
  37. listen 80;
  38. server_name <你的域名>;
  39. return 301 https://$server_name$request_uri;
  40. }
  • 写好配置文件后编辑nginx主配置文件include 上一步写的配置文件
  1. # Ubuntu apt-get安装后的Nginx配置文件在/etc/nginx/nginx.conf
  2. # 编辑/etc/nginx/nginx.conf找到在http {}里面最后添加osroom.conf
  3. # 可以用*通配符,前提是该目录下没有其他非nginx得.conf配置文件
  4. http {
  5. ...
  6. include /home/work/project/*.conf;
  7. }

重启Nginx

  1. sudo /etc/init.d/nginx restart

访问

  • 这个时候就可以通过域名(域名有解析到服务器的情况下)或IP访问OSROOM了