Flask Vue.js全栈开发|第20章:Linux云主机部署

  • 原创
  • Madman
  • /
  • /
  • 2
  • 3561 次阅读

flask vuejs 全栈开发-min.png

Synopsis: 前端打包编译后只有静态文件,所以由 Nginx 提供静态资源服务,速度非常快。由于 Flask 自带的开发 Web 服务器性能不足以应对生产环境的并发访问,所以我们使用 Gunicorn 来替代它。正式环境中数据库不再使用 SQLite,由于我们使用了 SQLAlchemy ORM,所以可以很方便的将数据库切换到 MySQL 或 PostgreSQL。同时,我们还可以使用 Supervisor 来监控 Gunicorn、RQ worker 等进程的存活(自动重启进程)

注意: 请确保你的代码是最新的 http://www.madmalls.com/blog/post/latest-code/

1. 部署后端 Flask API

你可以直接选择购买云主机,服务商会默认安装你选择的 Linux 版本。或者你用自己的虚拟机,要安装 Linux 环境,可以参考: http://www.madmalls.com/blog/post/customize-centos-7-3-autoinstall-iso/

1.1 MySQL

开发过程中使用的 SQLite 数据库不足以支持正式环境中大量用户的读/写请求,由于我们使用了 SQLAlchemy ORM,所以可以很方便的将数据库切换到 MySQLPostgreSQL

(1)安装

这里我将选择安装 MySQL 社区版:

[root@CentOS ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
[root@CentOS ~]# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@CentOS ~]# yum install -y mysql-server
[root@CentOS ~]# systemctl start mysqld
[root@CentOS ~]# systemctl enable mysqld

(2)安全加固

启动 mysqld 服务后,我们还需要对服务器的安全性做一次强化,运行 mysql_secure_installation 命令时,它会提示你设置 root 用户的密码(默认初始时是空密码)、删除 anonymous 用户、删除 test 数据库、不允许 root 用户远程访问等

[root@CentOS ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   # 默认是空密码,直接回车即可
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

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.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

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? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Cleaning up...

(3)创建 Flask 应用需要的数据库

使用刚更改密码后的 root 用户登录 MySQL

[root@CentOS ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

假设我们将创建名为 madblog 的数据库,和用户名为 testuser 的用户,密码是 password

mysql> create database madblog character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on madblog.* to 'testuser' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

1.2 Redis

我们后端应用会使用 RQ 任务队列,它需要 Redis

[root@CentOS ~]# yum install -y epel-release
[root@CentOS ~]# yum install -y redis
[root@CentOS ~]# systemctl start redis
[root@CentOS ~]# systemctl enable redis

1.3 Elasticsearch

(1)安装

我们后端应用会提供全文检索功能,需要 Elasticsearch 7.0

# 0. Need Java 8++
[root@CentOS ~]# yum -y install java-1.8.0-openjdk

# 1. Download and install the public signing key
[root@CentOS ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

# 2. Create yum repository
[root@CentOS ~]# vim /etc/yum.repos.d/elasticsearch.repo
内容如下:

[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

# 3. Install Elasticsearch
[root@CentOS ~]# yum -y install elasticsearch
[root@CentOS ~]# systemctl start elasticsearch.service
[root@CentOS ~]
                                
                            
未经允许不得转载: LIFE & SHARE - 王颜公子 » Flask Vue.js全栈开发|第20章:Linux云主机部署

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

2 条评论

cjd9023
cjd9023

成功按此方法在公有云上部署,谢谢大神

Madman
Madman cjd9023 Author

不用谢,你自己努力而已,加油!

专题系列