原文地址css
最近在学习 python,使用 flask 实现了个我的博客程序,完了想部署到服务器上。由于是新手,一路磕磕绊绊最终把它基本搞定。网上资料对新手感受都不太友好,都是零零碎碎的,因此我整理了一下,一方面做为我本身的记录,方便之后查阅,另外一方面也但愿能帮助到跟我同样的新手。前端
有一个服务器(否则搞毛),购买能够参考优质国外vps推荐python
有我的域名(固然,你能够直接使用 IP访问,但有点奇怪不是?购买域名能够去GoDaddymysql
能够选择 github 或者Bitbucket,固然你也能够本身搭建 git服务器,但我以为没啥必要,我选择Bitbucket,主要是由于它私有库免费nginx
sudo yum install git
复制代码
后续就跟咱们本地开发没什么区别了,配置 ssh key,clone代码,就不展开了,项目目录建议放在 /home/www/
下c++
$wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm' $sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm $yum repolist all | grep mysql mysql-connectors-community/x86_64 MySQL Connectors Community 36 mysql-tools-community/x86_64 MySQL Tools Community 47 mysql57-community/x86_64 MySQL 5.7 Community Server 187 复制代码
$sudo yum install mysql-community-server
复制代码
$sudo service mysqld start
$sudo systemctl start mysqld #CentOS 7
$sudo systemctl status mysqld
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2017-05-27 12:56:26 CST; 15s ago
Process: 2482 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 2421 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 2481 (mysqld_safe)
CGroup: /system.slice/mysqld.service
├─2481 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─2647 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/...
复制代码
说明已经正在运行中了git
$ mysql -uroot -p
复制代码
这里有要求你输入密码,Mysql安装时会生成一个默认密码,使用 grep "temporary password" /var/log/mysqld.log
命令,返回结果最后引号后面的字符串就是root的默认密码github
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
复制代码
在 /etc/my.cnf 中设置默认的编码web
[client] default-character-set = utf8 [mysqld] default-storage-engine = INNODB character-set-server = utf8 collation-server = utf8_general_ci #不区分大小写 collation-server = utf8_bin #区分大小写 collation-server = utf8_unicode_ci #比 utf8_general_ci 更准确 复制代码
mysql> CREATE DATABASE <datebasename> CHARACTER SET utf8;
复制代码
CentOS 7 默认安装了 Python 2,当须要使用 Python 3 的时候,能够手动下载 Python 源码后编译安装。sql
sudo mkdir /usr/local/python3 # 建立安装目录 $ wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz # 下载 Python 源文件 # 注意:wget获取https的时候要加上:--no-check-certifica $ tar -xzvf Python-3.6.2.tgz # 解压缩包 $ cd Python-3.6.2 # 进入解压目录 sudo ./configure --prefix=/usr/local/python3 # 指定建立的目录 sudo make sudo make install # 编译安装 复制代码
执行./configure时可能会报错,configure: error: no acceptable C compiler found in $PATH,这是由于未安装合适的编译器,安装下就行了,
sudo yum install gcc-c++
(使用sudo yum install gcc-c++时会自动安装/升级gcc及其余依赖的包。)
建立 python3 的软连接:
$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
复制代码
这样就能够经过 python
命令使用 Python 2,python3
来使用 Python 3。
$ sudo yum -y install epel-release # 首先安装 epel 扩展源 $ sudo yum -y install python-pip # 安装 python-pip $ sudo yum clean all # 清除 cache 复制代码
经过这种方式貌似只能安装 pip2,想要安装 Python 3 的 pip,能够经过如下的源代码安装方式。
# 下载源代码 $ wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.1.tar.gz $ tar -zvxf 9.0.1.tar.gz # 解压文件 $ cd pip-9.0.1 $ python3 setup.py install # 使用 Python 3 安装 复制代码
建立连接:
$ sudo ln -s /usr/local/python3/bin/pip /usr/bin/pip3
复制代码
升级 pip
$ pip install --upgrade pip
复制代码
Gunicorn (独角兽)是一个高效的Python WSGI Server,一般用它来运行 wsgi application(由咱们本身编写遵循WSGI application的编写规范) 或者 wsgi framework(如Django,Paster),地位至关于Java中的Tomcat。 WSGI就是这样的一个协议:它是一个Python程序和用户请求之间的接口。WSGI服务器的做用就是接受并分析用户的请求,调用相应的python对象完成对请求的处理,而后返回相应的结果。 简单来讲gunicorn封装了HTTP的底层实现,咱们经过gunicorn启动服务,用户请求与服务相应都通过gunicorn传输
cd /home/www/blog
mkdir venv
python3 -m venv venv
复制代码
source venv/bin/activate
复制代码
而后根据requirements.txt
文件安装依赖包:
pip3 install -r requirements.txt
复制代码
pip3 install gunicorn
复制代码
在项目根目录建立一个wsgi.py文件
from app import create_app application = create_app('production') if __name__ == '__main__': application.run() 复制代码
再也不经过manage.py启动服务,那只在开发的时候使用
启动服务:
gunicorn -w 4 -b 127.0.0.1:8000 wsgi:application 复制代码
nginx 是一个高性能的web服务器。一般用来在前端作反向代理服务器。所谓正向与反向(reverse),只是英文说法翻译。代理服务,简而言之,一个请求通过代理服务器从局域网发出,而后到达互联网上服务器,这个过程的代理为正向代理。若是一个请求,从互联网过来,先进入代理服务器,再由代理服务器转发给局域网的目标服务器,这个时候,代理服务器为反向代理(相对正向而言)。
正向代理:{ 客户端 ---》 代理服务器 } ---》 服务器
反向代理:客户端 ---》 { 代理服务器 ---》 服务器 }
{} 表示局域网
nginx既能够作正向,也能够作反向。
$ yum -y install nginx
复制代码
$ service nginx start
复制代码
$ service nginx stop
复制代码
$ service nginx restart
复制代码
nginx配置改动了,能够从新加载而不用先关闭再打开
$ nginx -s reload
复制代码
启动后 ,在浏览器中 输入服务器的 ip 地址,就能够看到
到这里 yum
安装 nginx
就完成了
nginx的配置文件为:/etc/nginx/nginx.conf
server { listen 80; server_name adisonhyh.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 复制代码
gunicorn和nginx关系:
gunicorn 能够单独提供服务,但生产环境通常不这样作。首先静态资源(jscssimg)会占用很多的请求资源,而对于 gunicorn 来说它自己更应该关注实际业务的请求与处理而不该该把资源浪费在静态资源请求上;此外,单独运行 gunicorn 是没有办法起多个进程多个端口来负载均衡的。
nginx 的做用就是弥补以上问题,首先做为前端服务器它能够处理一切静态文件请求,此时 gunicorn 做为后端服务器,nginx 将会把动态请求转发给后端服务器,所以咱们能够起多个 gunicorn 进程,而后让 nginx 做均衡负载转发请求给多个 gunicorn 进程从而提高服务器处理效率与处理能力。最后,nginx 还能够配置不少安全相关、认证相关等不少处理,可让你的网站更专一业务的编写,把一些转发规则等其它业务无关的事情交给 nginx 作。
配置好后打开本地浏览器,输入域名,应该就能访问了。
若是你须要进程一直执行,若该进程因各类缘由中断,也会自动重启的话,supervisor是一个很好的选择。 supervisor管理进程,是经过fork/exec的方式将这些被管理的进程看成supervisor的子进程来启动,因此咱们只须要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就行了。此时被管理进程被视为supervisor的子进程,若该子进程异常终端,则父进程能够准确的获取子进程异常终端的信息,经过在配置文件中设置autostart=true,能够实现对异常中断的子进程的自动重启。
$ pip install supervisor $ echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件 $ vim supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 进程管理 复制代码
在blog supervisor.conf 配置文件底部添加 (注意个人工做路径是www/home/blog/
)
[program:blog]
command=/home/www/blog/venv/bin/gunicorn -w4 -b0.0.0.0:8000 wsgi:application ;supervisor启动命令
directory=/home/www/blog ; 项目的文件夹路径
startsecs=0 ; 启动时间
stopwaitsecs=0 ; 终止等待时间
autostart=false ; 是否自动启动
autorestart=false ; 是否自动重启
stdout_logfile=/home/www/blog/logs/gunicorn.log ; log 日志
stderr_logfile=/home/www/blog/logs/gunicorn.err ; 错误日志
复制代码
使用 supervsior 启动 gunicorn
$ sudo supervisord -c supervisor.conf $ sudo supervisorctl start blog 复制代码
在浏览器地址栏输入配置的地址便可访问网站。
最后一步,咱们使用fabric实现远程操做和部署。Fabric 是一个 Python 下相似于 Makefiles 的工具,可是可以在远程服务器上执行命令。
pip install fabric
复制代码
在 blog 目录下新建一个fabfile.py文件
import os from fabric.api import local, env, run, cd, sudo, prefix, settings, execute, task, put from fabric.contrib.files import exists from contextlib import contextmanager env.hosts = ['204.152.201.69'] env.user = 'root' env.password = '****'#密码 env.group = "root" DEPLOY_DIR = '/home/www/blog' VENV_DIR = os.path.join(DEPLOY_DIR, 'venv') VENV_PATH = os.path.join(VENV_DIR, 'bin/activate') @contextmanager def source_virtualenv(): with prefix("source {}".format(VENV_PATH)): yield def update(): with cd('/home/www/blog/'): sudo('git pull') def restart(): with cd(DEPLOY_DIR): if not exists(VENV_DIR): run("virtualenv {}".format(VENV_DIR)) with settings(warn_only=True): with source_virtualenv(): run("pip install -r {}/requirements.txt".format(DEPLOY_DIR)) with settings(warn_only=True): stop_result = sudo("supervisorctl -c {}/supervisor.conf stop all".format(DEPLOY_DIR)) if not stop_result.failed: kill_result = sudo("pkill supervisor") if not kill_result: sudo("supervisord -c {}/supervisor.conf".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf reload".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf status".format(DEPLOY_DIR)) sudo("supervisorctl -c {}/supervisor.conf start all".format(DEPLOY_DIR)) @task def deploy(): execute(update) execute(restart) 复制代码
如今代码若是更新了,能够直接在本地执行远程部署了
fab deploy
复制代码