美文网首页
宝塔面板整合 Flask/Django 搭建web应用

宝塔面板整合 Flask/Django 搭建web应用

作者: 漠_b5bf | 来源:发表于2019-02-11 16:06 被阅读0次

宝塔面板整合 Flask/Django 搭建web应用

系统环境:Ubuntu 16.04

需要安装的软件:Nginx,MySQL,Python,PHP,Redis,宝塔面板

Web框架:Flask,Django,WordPress

宝塔面板安装:
Python环境管理:
  • sudo apt-get update

  • sudo apt-get upgrade

  • sudo apt-get install python-pip python-dev build-essential

  • sudo pip install –upgrade pip #安装pip

  • update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

  • update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2 #默认使用Python3.5

  • mkdir ~/.pip

  • vi ~/.pip/pip.conf #更改pip默认源为国内豆瓣源

    
    [global]
    
    trusted-host =  mirrors.aliyun.com
    
    index-url = https://mirrors.aliyun.com/pypi/simple
    
    
其它应用安装:
  • sudo apt-get install git #安装Git

Django环境部署:

部署代码

  • 部署前的项目配置 : https://www.zmrenwu.com/post/20/ (参考追梦人物Django教程博客)

  • 涉及到 demo.zmrenwu.com 的地方通常都要替换你自己的域名

  • 接下是配置 Nginx 来处理用户请求。

    定位到/www/server/panel/vhost/nginx目录,添加自己网站conf文件,写上下面的配置内容:

    
    /etc/nginx/sites-available/demo.zmrenwu.com
    
    
    
    server {
    
        charset utf-8;
    
        listen 80;
    
        server_name demo.zmrenwu.com; ①
    
    
    
        location /static { ②
    
            alias /home/yangxg/sites/demo.zmrenwu.com/django-blog-tutorial/static;
    
        }
    
    
    
        location / { ③
    
            proxy_set_header Host $host;
    
            proxy_pass http://unix:/tmp/demo.zmrenwu.com.socket;
    
        }
    
    }
    
    

    ① 服务的域名为 demo.zmrenwu.com。

    ② 所有URL 带有 /static 的请求均由 Nginx 处理,alias 指明了静态文件的存放目录。

    ③ 其它请求转发给 Django 处理。proxy_pass 后面使用了 unix 套接字,其作用是防止端口冲突,这里就不再详述。

    service nginx reload #重启nginx

使用 Gunicorn

Gunicorn 一般用来管理多个进程,有进程挂了Gunicorn 可以把它拉起来,防止服务器长时间停止服务,还可以动态调整 worker 的数量,请求多的时候增加 worker 的数量,请求少的时候减少。

在虚拟环境下,安装 Gunicorn:


(env) yangxg@localhost:~/sites/demo.zmrenwu.com/django-blog-tutorial$ pip install gunicorn

用 Gunicorn 启动服务器进程:


(env) yangxg@localhost:~/sites/demo.zmrenwu.com/django-blog-tutorial$ gunicorn --bind unix:/tmp/demo.zmrenwu.com.socket blogproject.wsgi:application

浏览器输入域名,可以看到访问成功了!

自启动/后台启动

​ ~~~~安装 supervisor 通过它进行django 进程守护:pip install supervisor~~ (supervisor 需要python2的版本)

nohup

​ 如果让程序始终在后台执行,即使关闭当前的终端也执行(之前的&做不到),这时候需要nohup。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。关闭中断后,在另一个终端jobs已经无法看到后台跑得程序了,此时利用ps(进程查看命令)


ps -aux | grep "test.sh"  #a:显示所有程序 u:以用户为主的格式来显示 x:显示所有程序,不以终端机来区分

相关文章

网友评论

      本文标题:宝塔面板整合 Flask/Django 搭建web应用

      本文链接:https://www.haomeiwen.com/subject/okvqeqtx.html