当前位置:   article > 正文

Ubuntu18.04上Flask的部署流程_ubuntu上flask + nginx

ubuntu上flask + nginx

Ubuntu18.04+Flask+Nginx+Uwsgi+MySQL


1.系统环境说明

  • Ubuntu 18.04
  • Python 3.9
  • MySQL 5.6

2.创建Python环境

2.1 升级系统自带Python版本

  1. 可以使用以下命令查看系统自带Python版本:
python -V
python3 -V
  • 1
  • 2
  1. 安装Python 3.9bash中输入python并按两下Tab键可以看到所有Python版本。
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa
  • 1
  • 2
  • 3
sudo apt update
sudo apt install python3.9 -y
  • 1
  • 2
  1. Python 3.9设为默认版本
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3
  • 1
  • 2
  • 3

  运行上面的指令结果如下:

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.9   2         auto mode
  1            /usr/bin/python3.6   1         manual mode
  2            /usr/bin/python3.9   2         manual mode
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 安装pip:
sudo apt update
sudo apt install python3-pip -y
  • 1
  • 2
sudo apt install python3.9-venv -y
  • 1

  使用pip3 -V查看是否安装成功。

2.2 创建vitualenv虚拟环境

  1. 安装vitualenv
sudo apt-get install python-virtualenv -y
  • 1
  1. 创建虚拟环境:
mkdir env
cd env
virtualenv -p /usr/bin/python3.9 py39env
  • 1
  • 2
  • 3
  1. 将虚拟环境设为自启动
sudo vim ~/.bashrc
  • 1
# 在~/.bashrc末尾添加
source /root/env/py39env/bin/activate
  • 1
  • 2
source ~/.bashrc
  • 1

  虚拟环境激活后bash前面有(py39env)标示,后面内容都在该环境下进行。


3.安装与配置uwsgi

3.1 离线安装uwsgi

  1. 安装:
sudo apt-get install build-essential python-dev -y
sudo apt-get install python3.9-dev -y
  • 1
  • 2
cd ~
mkdir uwsgi
cd uwsgi
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar -zxvf uwsgi-latest.tar.gz
cd uwsgi-2.0.20/
sudo python3 uwsgiconfig.py --build
make
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 将uwsgi设为全局命令
sudo ln -s /root/uwsgi/uwsgi-2.0.20/uwsgi /usr/bin/uwsgi
  • 1

  测试:

cd ~
uwsgi
  • 1
  • 2

  如果安装成功则结果如下:

*** Starting uWSGI 2.0.20 (64bit) on [Thu Jun 16 10:28:37 2022] ***
compiled with version: 7.5.0 on 16 June 2022 07:26:14
os: Linux-4.4.0 #1 SMP Tue Aug 25 11:59:26 MSK 2020
nodename: 158t.c.hostens.cloud
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /root
detected binary path: /root/uwsgi/uwsgi-2.0.20/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62987
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
The -s/--socket option is missing and stdin is not a socket.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.2 部署项目(此处使用git)

  1. 安装Git
sudo apt-get install git -y
  • 1
  1. 下载项目:(并使用pip3安装项目所需第三方依赖)
git clone <你的项目地址>
  • 1

3.3 配置uwsgi

  进入项目根目录,创建uwsgi.ini文件:

vim uwsgi.ini
  • 1

  文件中输入以下内容后保存(注意删除其中的中文注释):

[uwsgi]
#ip和端口要与Nginx配置文件一致
socket=127.0.0.1:5000 

#项目的根目录
chdir=<你的项目根目录>

#启动文件,注意这里是相对项目根目录的地址。
wsgi-file=<你的启动文件>

#启动文件实例的名称
callable=app 

processes=4 

threads=10

master=true

virtualenv=/root/env/py39env

#启用多线程
enable-threads=true

preload=true

lazy-apps=true

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

4.安装和配置Nginx

4.1 安装Nginx

sudo apt-get install nginx -y
  • 1

4.2 配置Nginx

# 进入到nginx配置文件的目录
cd /etc/nginx
# 修改配置文件,注意先对nginx.conf进行备份
cp nginx.conf nginx.conf.bak
vim nginx.conf
  • 1
  • 2
  • 3
  • 4
  • 5

  需要找到或在http{}中添加server{}部分并修改,为了能修改项目根目录,还需要把开始的userwww-data改为root。配置文件可以通过nginx -t找到。

server {
    listen       80;
    server_name  xxx.xxx.xxx.xxx; #服务器外网的ip地址, 也可以是你的域名,或者两者都有,用空格隔开
    root         <与uwsgi中的项目根目录一致>;

    location / {
        include     uwsgi_params;
        uwsgi_pass      127.0.0.1:5000; #这里与uwsgi配置文件里的socket保持一致
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

  使用nginx -t检查是否有问题,尝试启动nginx。(启动报错请先执行关闭再启动。)

nginx #启动nginx
pkill -9 nginx #关闭nginx服务
  • 1
  • 2

4.3 打开80端口

  1. 安装ufw
sudo apt-get install ufw
  • 1
  1. 打开端口
ufw enable
  • 1
ufw allow 80
ufw allow 22 #记得开放SSH端口
ufw reload
ufw status
  • 1
  • 2
  • 3
  • 4

5.安装与配置MySQL

5.1 安装MySQL

sudo apt-get update
sudo apt-get install mysql-server -y
sudo apt-get install mysql-client -y
  • 1
  • 2
  • 3

5.2 配置MySQL

  1. 初始化配置
sudo mysql_secure_installation
  • 1

  配置时推荐选项(参考https://blog.csdn.net/weixx3/article/details/80782479):

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

  检查MySQL服务运行状态用systemctl status mysql.service指令。

  1. 配置远程访问
vim /etc/mysql/mysql.conf.d/mysqld.cnf
  • 1
# 将bind-address改为0.0.0.0
bind-address = 0.0.0.0
  • 1
  • 2
  1. 允许root用户被访问

  输入mysql -u root -p后进入mysql,再依次输入:

GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "<你的密码>";
  • 1
use mysql;
update user set host = '%' where user = 'root';
  • 1
  • 2

  如果还存在问题可以参考https://blog.csdn.net/weixx3/article/details/80782479https://blog.csdn.net/weixin_46765649/article/details/123754890

  1. 修改MySQL的编码格式为utf-8:
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
  • 1

  在[mysqld]下增加:

character-set-server=utf8
  • 1

  然后:

sudo vi /etc/mysql/conf.d/mysql.cnf
  • 1

  在[mysql]下增加:

default-character-set=utf8
  • 1

  最后重启mysql

/etc/init.d/mysql restart
  • 1

6.Flask配置

  Flask的依赖可通过pip3进行安装,需要使用export设置环境变量FLASK_APP,即export FLASK_APP=xxx.py。在启动之前需要创建数据库并初始化各表。

  为了能在后台运行,需要安装screen

sudo apt-get install screen -y
  • 1

  使用指令创建后台bash如下:

screen -S <后台名称>
  • 1

  启动nginx后启动uwsgi:

pkill -9 nginx
pkill -9 uwsgi
nginx
uwsgi /root/<你的项目地址>/uwsgi.ini
  • 1
  • 2
  • 3
  • 4

参考链接:
https://blog.csdn.net/xujianjun229/article/details/118500154
https://blog.csdn.net/G_D_L_Dupai/article/details/122498187
http://t.zoukankan.com/GavinSimons-p-10775293.html
https://blog.csdn.net/trium_KW/article/details/124849163
https://zhuanlan.zhihu.com/p/435731668
https://www.runoob.com/python3/python-uwsgi.html
https://blog.csdn.net/weixx3/article/details/80782479
https://blog.csdn.net/tanhuanzheng/article/details/108960426
https://blog.csdn.net/weixin_46765649/article/details/123754890
https://blog.csdn.net/qq_29761395/article/details/104076771
https://blog.csdn.net/weixin_39808877/article/details/111010702
https://blog.csdn.net/qq_38613380/article/details/111315277
https://www.likecs.com/show-203364581.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/711713
推荐阅读
相关标签
  

闽ICP备14008679号