项目部署

WEB工作原理

  • 客户端(chrom) <=> WEB服务器(nginx) <=> WSGI(uWSGI) <=> Python(Flask) <=> 数据库(MySQL)
  • Flask框架自带一个测试服务器,开发时直接运行即可;但是在生成环境中,必须进行完整部署。

nginx服务器

  • 安装:sudo apt-get install nginx

  • 添加虚拟主机:

    • /etc/nginx/sites-available目录下创建虚拟主机配置文件www.blog.com
    server {
    listen 80;
    server_name www.blog.com blog.com; location / {
    root /var/www/blog;
    index idnex.html;
    }
    }
    • 创建软链接
    sudo ln -s /etc/nginx/sites-available/www.blog.com /etc/nginx/sites-enabled
    • 重启nginx服务(若重启失败,可以关闭然后再打开)
    • 创建目录:/var/www/blog,然后在目录下创建index.html文件
    • 添加本地域名解析,修改配置文件/etc/hosts,在末尾添加如下内容:
    127.0.0.1    www.blog.com
    127.0.0.1 blog.com

移植博客项目

  • 将书写好的项目拷贝到/var/www/blog

uWSGI

  • 说明:是一个实现了WSGI协议的应用程序。

  • 安装:pip3 install uwsgi

  • 配置:

    http:            # 以HTTP协议启动
    socket: # 以socket方式启动
    chdir: # uwsgi启动项目的根目录
    wsgi-file: # 数据交给哪个模块处理
    callable: # 具体可调用的对象
    daemonize: # 后台运行(指定一个日志文件即可)
    processes: # 指定进程数
    threads: # 指定线程数
  • 启动演示(以HTTP协议启动)

    uwsgi --http 127.0.0.1:5000 --wsgi-file blog.py --callable app
  • 以socket形式启动

    • nginx转发请求
    server {
    listen 80;
    server_name www.blog.com blog.com; location / {
    #root /var/www/blog;
    #index idnex.html;
    # 包含请求参数
    include uwsgi_params;
    # 以socket形式转发请求数据
    uwsgi_pass 127.0.0.1:5000;
    }
    }

    提醒:一定不要忘记重启nginx。

    • 启动uwsgi
    uwsgi --socket 127.0.0.1:5000 --wsgi-file blog.py --callable app
    • 将启动参数写入配置文件uwsgi.ini
    [uwsgi]
    socket = 127.0.0.1:5000
    wsgi-file = blog.py
    callable = appp
    # 后台运行
    daemonize = /var/log/uwsgi.log

    启动:uwsgi uwsgi.ini

静态资源处理

  • 说明:静态资源nginx可以自行处理,没有必要转发到python中。因此需要添加路由转发规则。

  • 静态资源路由规则:

    location /static {
    root /var/www/blog;
    # 等价于
    alias /var/www/blog/static;
    }
  • 测试准备:在blog目录下创建static目录,并拷贝一张图片

  • 在浏览器地址栏输入:www.blog.com/static/gyy.jpg

最新文章

  1. Dapper 数据操作框架
  2. C# 创建开机启动核心代码
  3. Android -- android.os.Parcelable[] cannot be cast to ...
  4. python-操作excel数据文件
  5. POJ1743 Musical Theme
  6. Mysql,SqlServer,Oracle主键自动增长的设置
  7. tf–idf算法解释及其python代码实现(下)
  8. Asp.Net Core
  9. 【转】关于C语言生成不重复的随机数
  10. Android超炫日期日历控件:TimesSquare
  11. win10环境下jdk1.8+Android Developer Tools Build: v22.3.0-887826的问题
  12. 面向面试编程——javascript对象的几种创建方式
  13. 使用Jquery.cookie.js操作cookie
  14. selenium之元素定位-css
  15. 2019-3-22KeyDown,KeyPress 和 KeyUp 事件
  16. Azure中block和Page的比较 Azure: Did You Know? Block vs Page Blobs
  17. Apache kylin 入门
  18. 前端 搜索样式 html
  19. DB安装
  20. VMware虚拟机安装红帽系统无法上网解决办法(转)

热门文章

  1. VC++ IPv6的支持
  2. JavaScript switch语句
  3. Microsoft Windows Scripting Self-Paced Learning Guide
  4. 使用electron构建跨平台Node.js桌面应用
  5. apache2 tomat https 变成http的解决方案
  6. QT控件大小的方法
  7. 大数据学习---大数据的学习【all】
  8. 对于char *s1 和 char s2[] 的认识
  9. Linux内核收包过程
  10. linux和aix内核参数检查