第二篇: 搭建环境 -- Centos7(64位)云主机部署Python Flask项目实战系列

项目将可能会到mysql, redis, mongoDB, 因此打算使用docker来运行这些数据库.毕竟购买的云服务器配置太差, 希望能运行起来(实验性质).
关于云服务器的购买部份,大家可以参阅我的另一篇文章: 第一篇: 购买云服务器 -- Centos7(64位)云主机部署Python Flask项目实战系列

Python环境

因为当时选择的是Centos7 64位镜像, 云服务器创建成功后, 系统默认已经安装了python2.7.5.

安装ipython 5.2

因为ipython 5.4或更高版本只支持python3.x, 所以这里就安装ipython 5.2的版本.

经常使用Python的同学应该知识这个库.具体介绍大家可自行百度. 这里我就贴一张图了.

image.png

安装依赖

命令: yum install gcc libffi-devel python-devel openssl-devel -y
如果不安装这些依赖的话, 可能话导致以下出错:

  gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python2.7 -c _scandir.c -o build/temp.linux-x86_64-2.7/_scandir.o
    _scandir.c:14:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-bGli0A/scandir/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-tfAHCZ-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-bGli0A/scandir/

安装ipython 5.2

命令: pip install ipython==5.2
安装完成后, 在命令行输入,ipython即可调用:

image.png

安装flask flask-SQLAlchemy

命令: pip install flask flask-SQLAlchemy
安装好之后确认下:

image.png

安装redis pymongo

  • redis: pip install redis
  • pymongo: pip install pymongo

安装MySQLdb

安装依赖

yum install -y mysql-devel

安装MySQLdb

首先需要下载MySQLdb zip文件, 下载页面地址: https://pypi.python.org/pypi/MySQL-python/1.2.5
$ wget https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c

解压文件:
$ unzip MySQL-python-1.2.5
安装:
$ cd MySQL-python-1.2.5
$ python setup.py install

安装uwsgi

命令: pip install uwsgi

flask部署的方式有多种, 大家也可以参考flask官方文档中介绍的几种方案, 这里我选择用uwsgi的方式来进行部署.

关于uwsgi部署的实现, 请参阅第三篇: Flask demo项目部署 -- Centos7(64位)云主机部署Python Flask项目实战系列

安装supervisor

命令: pip install supervisor

生成配置文件

生成配置文件:
在shell终端输入echo_supervisord_conf > /etc/supervisord.conf

自定义配置

待更新

添加开机自启服务

添加自启脚本
[root@localhost etc]#vim /lib/systemd/system/supervisord.service
添加以下脚本:

[Unit]

Description=Process Monitoring and Control Daemon

After=rc-local.service

[Service]
Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

SysVStartPriority=99

[Install]

WantedBy=multi-user.target

将启动脚本都能够添加到systemctl自启动服务

[root@localhost etc]# systemctl enable supervisord.service
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

安装Docker

关于docker的详细介绍大家可以去看看菜鸟教程, 以快速进行了解.
docker的安装也很简单:
命令: yum -y install docker

启动docker服务并加入开机启动项中

[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service

安装Docker版 mysql, redis, mongoDB

关于这部分安装的详细介绍, 大家可以参阅我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位). 虽然使用的系统不一样, 但安装原理都差不多.这里的话我主要贴出一些关键的指令.

安装docker版mysql

安装docker版的mysql5.6: docker pull mysql:5.6

Trying to pull repository docker.io/library/mysql ...
5.6: Pulling from docker.io/library/mysql
ad74af05f5a2: Pull complete
0639788facc8: Pull complete
de70fa77eb2b: Pull complete
724179e94999: Pull complete
7a61946a7226: Pull complete
fa1f0822fe12: Pull complete
2b2e255eb8e7: Pull complete
38a8b3ee3554: Pull complete
46652a6944cf: Pull complete
0dec4ac74eab: Pull complete
0190940ca68e: Pull complete
Digest: sha256:2897982d4c086b03586a1423d0cbf33688960ef7534b7bb51b9bcfdb6c3597e7

新建/usr/local/mysql文件夹, 并在该目录下创建三个目录: conf data logs和conf/my.cnf配置文件

  • data目录将映射为mysql容器配置的数据文件存放路径
  • logs目录将映射为mysql容器的日志目录
  • conf目录里的配置文件将映射为mysql容器的配置文件
  • conf/my.cnf 配置文件

添加my.cnf配置

以下为安装mysql后的默认的配置, 大家可根据需要自行优化。

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket        = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket        = /var/run/mysqld/mysqld.sock
port        = 3306
basedir        = /usr
datadir        = /var/lib/mysql
tmpdir        = /tmp
lc-messages-dir    = /usr/share/mysql
skip-external-locking
skip-name-resolve
character-set-server=utf8
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size        = 16M
max_allowed_packet    = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit    = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries    = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id        = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size   = 100M
#binlog_do_db        = include_database_name
#binlog_ignore_db    = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

然后, 就可以运行docker版的mysql5.6

root@localhost:/usr/local/mysql$ docker run -p 3306:3306 --name my-mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.6 
fe39c0d434bbec20d42de236fb252009e7d8cd93e595df6a3f2d65bd004abb23
root@localhost:/usr/local/mysql$ 

*** 命令说明 ***

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口
  • -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf
  • -v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs
  • -v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data
  • -e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
  • -d 后台运行my-mysql容器。

查看mysql 5.6的运行状态

root@localhost:/usr/local/mysql$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d06b9b6ed3ab        mysql:5.6           "docker-entrypoint..."   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   my-mysql
root@localhost:/usr/local/mysql$ 

安装docker版redis

安装方法与上面相同, 详细可参阅我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位)

安装docker版mongoDB

安装方法与上面相同, 详细可参阅我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位)

docker开机自启

命令: systemctl enable docker

为了也能开机自启docker 容器, 在运行容器时使用--restart参数来设置。

例如, 为了使用容器my-mysql能够实现开机自启, 执行以下指令:

[root@localhost mysql] docker run --restart=always -p 3306:3306 --name my-mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.6

安装nginx

图方便, 就直接用yum来安装nginx了

centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库
命令: rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

  • 安装: yum install nginx
  • 启动: systemctl start nginx
  • 查看状态: systemctl status nginx
  • 开机自启: systemctl enable nginx.service

写得比较仓促, 如有错误, 请各位看官帮忙批评指正!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容