项目将可能会到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
即可调用:
安装flask flask-SQLAlchemy
命令: pip install flask flask-SQLAlchemy
安装好之后确认下:
安装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
写得比较仓促, 如有错误, 请各位看官帮忙批评指正!
网友评论