Flask 网站 Web服务器部署-Ubuntu18.0.4

cd /home

1. 安装anaconda

下载

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2019.03-Linux-x86_64.sh

或者

wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh

安装

bash Anaconda3-2019.03-Linux-x86_64.sh

修改了安装目录:/home/anaconda3

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below
  [/root/anaconda3] >>> /home/anaconda3

安装成功

Thank you for installing Anaconda3!

===========================================================================

Anaconda and JetBrains are working together to bring you Anaconda-powered
environments tightly integrated in the PyCharm IDE.

PyCharm for Anaconda is available at:
https://www.anaconda.com/pycharm

(base) root@brave-hop-2:~# conda -V
conda 4.6.11

2. 创建虚拟环境并定义web目录

/home/Web/app存放flask项目
本次加入项目目录为

/home/web/app
-static
-templates
-main.py
conda create -n flask python=3.7

conda activate flask
conda install flask
pip install flask_bootstrap

...
mkdir /home/Web
mkdir /home/Web/app
mkdir /home/Web/log

3. 安装uwsgi

3.1 gcc版本校正

https://blog.csdn.net/weixin_33127753/article/details/84874147

#查看当前系统安装所有版本的gcc
ls /usr/bin/gcc* -l 
#如果gcc有5以下的版本,则不用在安装
sudo apt-get  install gcc-4.8
#更改gcc系统默认版本
sudo rm /usr/bin/gcc #删除已有软连接
sudo ln -s /usr/bin/gcc-4.8 /usr/bin/gcc #创建指向gcc4.8的软连接

3.2 安装uwsgi

pip install uwsgi

3.3 添加uwsgi配置文件

apt-get install vim
vim /home/Web/config.ini

/home/Web/config.ini内容

[uwsgi]
# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:10087
# 指向网站目录
chdir = /home/Web/app/
# python 启动程序脚本文件
wsgi-file = main.py
# python 程序内用以启动的 application 变量名
callable = app
# 处理器数
processes = 1
# 线程数
threads = 2
#状态检测地址
stats = 127.0.0.1:9191

这里需要注意的是callable = app,这个app是我们Flask中创建一个application变量名。

3.4 测试uwsgi启动情况

先把flask项目放到/home/web/app中

uwsgi /home/Web/config.ini

结果

...
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 166688 bytes (162 KB) for 2 cores
*** Operational MODE: threaded ***
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from flask_bootstrap import Bootstrap
ModuleNotFoundError: No module named 'flask_bootstrap'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1859)
spawned uWSGI worker 1 (pid: 1860, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 10 ***

4. 安装Supervisor

apt-get install supervisor

4.1 配置supervisor

which uwsgi

(flask) root@brave-hop-2:~# which uwsgi
/home/anaconda3/envs/flask/bin/uwsgi
vim /etc/supervisor/conf.d/card.conf

/etc/supervisor/conf.d/card.conf内容

[program:card]
command = /home/anaconda3/envs/flask/bin/uwsgi /home/Web/config.ini
directory = /home/Web/app
user = root
startsecs = 3
autostart=true
autorestart=true
redirect_stderr = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups = 10
stdout_logfile = /home/Web/log/app.log

commanda内容左边是上面which的结果,右边是uwsgi配置文件路径

4.2 启动supervisor服务

若supervisorctl start card不行,则

supervisorctl reread
supervisorctl update

5. nginx安装

apt-get install nginx

5.1 配置nginx(添加Servers)

vim /etc/nginx/sites-available/default

5.1.1 域名访问

server {
    listen 80;
    listen [::]:80;
    root /home/Web/app;
    access_log /home/Web/log/access_log;
    error_log /home/Web/log/error_log;
    server_name xn--1iu29hk7n.top www.xn--1iu29hk7n.top;
     location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:10087; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_param UWSGI_CHDIR /home/Web/app; # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT main:app; # 指定启动程序
      }
}

5.1.2 端口访问

#端口访问
server {
    listen 8888;
    listen [::]:8888;
    root /home/Web/app;
    access_log /home/Web/log/access_log;
    error_log /home/Web/log/error_log;
    server_name 127.0.0.1:8888;
     location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:10087; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_param UWSGI_CHDIR /home/Web/app; # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT main:app; # 指定启动程序
      }
}

server_name 便是让nginx监听我面域名uwsgi_pass 为/home/Web/config.ini配置文件中的socket地址

5.2 创建软连接

ln -s /etc/nginx/sites-available/default .

5.3 启动nginx

service nginx start

6.

如果有问题可以重启一下,然后重新启动nginx,注意要先进入虚拟环境flask

reboot
...

source activate flask
service nginx start

参考链接

https://blog.csdn.net/qwe511455842/article/details/80373160

https://www.168seo.cn/linux/24490.html

https://blog.csdn.net/weixin_33127753/article/details/84874147**

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值