項目將可能會到mysql, redis, mongoDB, 因此打算使用docker來運行這些數(shù)據(jù)庫.畢竟購買的云服務(wù)器配置太差, 希望能運行起來(實驗性質(zhì)).
關(guān)于云服務(wù)器的購買部份,大家可以參閱我的另一篇文章: 第一篇: 購買云服務(wù)器 -- Centos7(64位)云主機部署Python Flask項目實戰(zhàn)系列
Python環(huán)境
因為當(dāng)時選擇的是Centos7 64位鏡像, 云服務(wù)器創(chuàng)建成功后, 系統(tǒng)默認(rèn)已經(jīng)安裝了
python2.7.5
.
安裝ipython 5.2
因為ipython 5.4或更高版本只支持python3.x, 所以這里就安裝ipython 5.2的版本.
經(jīng)常使用Python的同學(xué)應(yīng)該知識這個庫.具體介紹大家可自行百度. 這里我就貼一張圖了.
安裝依賴
命令: yum install gcc libffi-devel python-devel openssl-devel -y
如果不安裝這些依賴的話, 可能話導(dǎo)致以下出錯:
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
即可調(diào)用:
安裝flask flask-SQLAlchemy
命令: pip install flask flask-SQLAlchemy
安裝好之后確認(rèn)下:
安裝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的方式來進(jìn)行部署.
關(guān)于uwsgi部署的實現(xiàn), 請參閱第三篇: Flask demo項目部署 -- Centos7(64位)云主機部署Python Flask項目實戰(zhàn)系列
安裝supervisor
命令: pip install supervisor
生成配置文件
生成配置文件:
在shell終端輸入echo_supervisord_conf > /etc/supervisord.conf
自定義配置
待更新
添加開機自啟服務(wù)
添加自啟腳本
[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自啟動服務(wù)
[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
關(guān)于docker的詳細(xì)介紹大家可以去看看菜鳥教程, 以快速進(jìn)行了解.
docker的安裝也很簡單:
命令:yum -y install docker
啟動docker服務(wù)并加入開機啟動項中
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
安裝Docker版 mysql, redis, mongoDB
關(guān)于這部分安裝的詳細(xì)介紹, 大家可以參閱我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位). 雖然使用的系統(tǒng)不一樣, 但安裝原理都差不多.這里的話我主要貼出一些關(guān)鍵的指令.
安裝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文件夾, 并在該目錄下創(chuàng)建三個目錄: conf data logs和conf/my.cnf配置文件
- data目錄將映射為mysql容器配置的數(shù)據(jù)文件存放路徑
- logs目錄將映射為mysql容器的日志目錄
- conf目錄里的配置文件將映射為mysql容器的配置文件
- conf/my.cnf 配置文件
添加my.cnf配置
以下為安裝mysql后的默認(rèn)的配置嗤无, 大家可根據(jù)需要自行優(yōu)化摹恨。
#
# 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:將主機當(dāng)前目錄下的conf/my.cnf掛載到容器的/etc/mysql/my.cnf
- -v $PWD/logs:/logs:將主機當(dāng)前目錄下的logs目錄掛載到容器的/logs
- -v $PWD/data:/mysql_data:將主機當(dāng)前目錄下的data目錄掛載到容器的/mysql_data
- -e MYSQL_ROOT_PASSWORD=123456:初始化root用戶的密碼
- -d 后臺運行my-mysql容器卓缰。
查看mysql 5.6的運行狀態(tài)
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
安裝方法與上面相同, 詳細(xì)可參閱我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位)
安裝docker版mongoDB
安裝方法與上面相同, 詳細(xì)可參閱我的另一篇文章: Ubuntu Docker (Ubuntu 16.04 64位)
docker開機自啟
命令: systemctl enable docker
為了也能開機自啟docker 容器, 在運行容器時使用--restart
參數(shù)來設(shè)置蛛株。
例如, 為了使用容器my-mysql能夠?qū)崿F(xiàn)開機自啟, 執(zhí)行以下指令:
[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系統(tǒng)庫中默認(rèn)是沒有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
- 查看狀態(tài):
systemctl status nginx
- 開機自啟:
systemctl enable nginx.service
寫得比較倉促, 如有錯誤, 請各位看官幫忙批評指正!