第二篇: 搭建环境 — 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的同学应该知识这个库.具体介绍大家可自行百度. 这里我就贴一张图了.

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

安装依赖

命令: 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即可调用:

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

安装flask flask-SQLAlchemy

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

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

安装redis pymongo mysql-python

  • redis: pip install redis

  • pymongo: pip install pymongo

  • mysql-python: 待更新…

安装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
#
# 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位)

安装nginx

未完, 待续


发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>