第二篇: 搭建環(huán)境 -- Centos7(64位)云主機部署Python Flask項目實戰(zhàn)系列

項目將可能會到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)該知識這個庫.具體介紹大家可自行百度. 這里我就貼一張圖了.

image.png

安裝依賴

命令: 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)用:

image.png

安裝flask flask-SQLAlchemy

命令: pip install flask flask-SQLAlchemy
安裝好之后確認(rèn)下:

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的方式來進(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

寫得比較倉促, 如有錯誤, 請各位看官幫忙批評指正!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市囊嘉,隨后出現(xiàn)的幾起案子温技,更是在濱河造成了極大的恐慌,老刑警劉巖扭粱,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舵鳞,死亡現(xiàn)場離奇詭異,居然都是意外死亡琢蛤,警方通過查閱死者的電腦和手機蜓堕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來虐块,“玉大人俩滥,你說我怎么就攤上這事嘉蕾『氐欤” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵错忱,是天一觀的道長儡率。 經(jīng)常有香客問我挂据,道長,這世上最難降的妖魔是什么儿普? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任崎逃,我火速辦了婚禮,結(jié)果婚禮上眉孩,老公的妹妹穿的比我還像新娘个绍。我一直安慰自己,他們只是感情好浪汪,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布巴柿。 她就那樣靜靜地躺著,像睡著了一般死遭。 火紅的嫁衣襯著肌膚如雪广恢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天呀潭,我揣著相機與錄音钉迷,去河邊找鬼。 笑死钠署,一個胖子當(dāng)著我的面吹牛糠聪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谐鼎,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼枷颊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了该面?” 一聲冷哼從身側(cè)響起夭苗,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎隔缀,沒想到半個月后题造,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡猾瘸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年界赔,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片牵触。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡淮悼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出揽思,到底是詐尸還是另有隱情袜腥,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布钉汗,位于F島的核電站羹令,受9級特大地震影響鲤屡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜福侈,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一酒来、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧肪凛,春花似錦堰汉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至远荠,卻和暖如春矮固,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背譬淳。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工档址, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人邻梆。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓守伸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親浦妄。 傳聞我的和親對象是個殘疾皇子尼摹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容