【Python】部署上手App后端服务器 - Linux环境搭建安装Python、Tornado、SQLAlchemy

基于阿里云服务器端环境搭建

简单记录 - 掘金 基于 Python 轻松自建 App 服务器

目标是搭建一款简洁、高效,并能快速部署及上手的 App 后端服务器。

先环境搭建。

配置开发环境

基于阿里云的服务器端环境搭建

开发环境需要安装如下软件:Python 3、Tornado、MySQL 和 SQLAlchemy。

Shell连接阿里云CentOS7服务器, CentOS 是 Red Hat 的开源版本,由 Red Hat 公司维护测试。

【Python】部署上手App后端服务器 - Linux环境搭建安装Python、Tornado、SQLAlchemy

安装 Python 3.8.2

CentOS 7 安装 Python 3.8.2

安装依赖包

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

我的已经安装了

[root@liuawen Python-3.8.2]# yum -y groupinstall "Development tools"
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Maybe run: yum groups mark install (see man yum)
No packages in any requested group available to install or update
[root@liuawen Python-3.8.2]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Package bzip2-devel-1.0.6-13.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-19.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package sqlite-devel-3.7.17-8.el7_7.1.x86_64 already installed and latest version
Package readline-devel-6.2-11.el7.x86_64 already installed and latest version
Package 1:tk-devel-8.5.13-6.el7.x86_64 already installed and latest version
Package gdbm-devel-1.10-8.el7.x86_64 already installed and latest version
Package libdb4-devel-4.8.30-13.el7.x86_64 already installed and latest version
Package 14:libpcap-devel-1.5.3-11.el7.x86_64 already installed and latest version
Package xz-devel-5.2.2-1.el7.x86_64 already installed and latest version
Nothing to do
[root@liuawen Python-3.8.2]#

创建安装目录

mkdir /usr/local/python

我的执行

[root@liuawen ~]# cd /usr/local
[root@liuawen local]# pwd
/usr/local
[root@liuawen local]# mkdir python3
[root@liuawen local]# cd python3
[root@liuawen python3]# pwd
/usr/local/python3

下载 Python 3.8.2

2020-04-16 3.8.2 下载

https://www.python.org/downloads/

CentOS7 执行下载Python 3.8.2

wget https://www.python.org/ftp/python/3.6.2/Python-3.8.2.tar.xz

安装 gcc

由于 Python 3.8.2 的编译需要编译环境,故需安装 gcc。

yum -y install gcc

我的已经安装了

[root@liuawen python3]# yum -y install gcc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package gcc-4.8.5-39.el7.x86_64 already installed and latest version
Nothing to do
[root@liuawen python3]#

安装Python 3.8.2

解压 Python 3.8.2 并安装在 /usr/local/python3 目录下。

tar -xvJf  Python-3.8.2.tar.xz
cd Python-3.8.2
./configure --prefix=/usr/local/python3
make && make install

创建软连

ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

测试python3

【Python】部署上手App后端服务器 - Linux环境搭建安装Python、Tornado、SQLAlchemy

[root@liuawen Python-3.8.2]# python3 --version
Python 3.8.2
[root@liuawen Python-3.8.2]# python3
Python 3.8.2 (default, Apr 16 2020, 22:55:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
KeyboardInterrupt
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()

安装 Tornado

HTTP 服务器框架Tornado, 简洁高效,易扩展,高并发

使用 pip 安装 Tornado,先执行 pip3 install --upgrade pip 命令升级 pip,再执行 pip3 install tornado 命令安装 Tornado。

pip3 install --upgrade pip
pip3 install tornado

我的之前下载过了

[root@liuawen python3]# pip3 install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already up-to-date: pip in ./lib/python3.8/site-packages (20.0.2)
[root@liuawen python3]# pip3 install tornado
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: tornado in ./lib/python3.8/site-packages (6.0.4)
[root@liuawen python3]#

测试 Tornado 是否安装成功:

[root@liuawen Python-3.8.2]# python3
Python 3.8.2 (default, Apr 16 2020, 22:55:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!
>>> import tornado
>>>
>>> exit()
[root@liuawen Python-3.8.2]#

执行 import tornado 没有报错,表示 Tornado 已安装成功。

安装 MySQL

MySQL 是流行的关系型数据库管理系统(RDBMS)。

yum install mysql-develwget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpmrpm -ivh mysql-community-release-el7-5.noarch.rpmyum -y install mysql-community-serverpip3 install mysqlclientservice mysqld restart

我的很久之前安装了。

[root@liuawen Python-3.8.2]# service mysqld restart
Shutting down MySQL.... [ OK ]
Starting MySQL. [ OK ]
[root@liuawen Python-3.8.2]#

安装 mysqlclient

使用mysqlclient来操作mysql。

pip3 install mysqlclient
[root@liuawen Python-3.8.2]# pip3 install mysqlclient
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85 kB)
|████████████████████████████████| 85 kB 7.3 MB/s
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-1.4.6

安装 SQLAlchemy

使用第三方SQLAlchemy来操作数据库,使用第三方的好处是很多底层 MySQL 的命令被封装成简单的API暴露给用户,并提供强大的整合功能。

使用 pip3 安装 SQLAlchemy:

ip3 install SQLAlchemy
[root@liuawen Python-3.8.2]# pip3 install SQLAlchemy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting SQLAlchemy
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/1a/65673e3d242dd557576c538b72e8ae85224d698cd08b7b60e822d203fab4/SQLAlchemy-1.3.16-cp38-cp38-manylinux2010_x86_64.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 10.4 MB/s
Installing collected packages: SQLAlchemy
Successfully installed SQLAlchemy-1.3.16
[root@liuawen Python-3.8.2]#

测试 SQLAlchemy 是否安装成功,服务器端依次输入如下命令。

[root@liuawen Python-3.8.2]# python3
Python 3.8.2 (default, Apr 16 2020, 22:55:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>>

没有报错,证明 SQLAlchemy 已安装成功。

基本环境已搭建

上一篇:range 小数据池介绍


下一篇:Educational Codeforces Round 4