1.1安装虚拟环境
创建文件夹
mkdir data 目录文件夹
cd data 进入data文件夹
mkdir nginx 创建安装nginx的文件夹
mkdir server 存放代码的文件夹
mkdir logs 存放日志的文件夹
mkdir backup 备份代码的文件夹
mkdir softs 软件存放的位置
mkdir virtual 虚拟环境的位置
mkdir scripts 脚本的运行位置
mkdir scp_codes 上传的代码
安装虚拟环境
apt-get install python-virtualenv (ubuntu已经安装过,可省略)
virtualenv -p /usr/bin/python3 api_server (采用他,安装在本文件夹,)
mkvirtualenv api_server(不要用它,它会自己安装到其他地方)
source api_server/bin/activate (在虚拟环境的active进入虚拟环境)
安装的软件会在虚拟环境下的bin目录下
安装nginx的环境
解压
cd ~/data/softs/
tar xf pcre-8.39.tar.gz
配置
cd ~/data/softs/pcre-8.39
./configure
编译
make
安装
sudo make install
安装nginx
解压
cd ~/data/softs/
tar xf nginx-1.10.2.tar.gz
配置
cd nginx-1.10.2/
./configure --prefix=/root/data/nginx 安装在root/data下面的nginx文件夹
编译
make
安装
make install
查看进程
ps aux | grep nginx
nginx简单基础操作
检查
sudo ~/data/nginx/sbin/nginx -t
开启
sudo ~/data/nginx/sbin/nginx
关闭
sudo ~/data/nginx/sbin/nginx -s stop
重载
sudo ~/data/nginx/sbin/nginx -s reload
1、首先安装Gunicorn
pip install gunicorn
复制代码
2、在入口文件的app.run()加上以下内容
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
复制代码
例:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
app.run()
复制代码
3、启动Gunicorn
最简单的方式为
gunicorn 入口文件名:app
复制代码
默认是监听127.0.0.1:8000
如果是要处理高并发则要开多个进程和修改监听端口的画
gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app
复制代码保存在supervisord 里面的etc/supervisord.d目录下添加一个logo.ini配置文件
添加配置文件
vim /etc/supervisord.d/logo.ini
[program:logo_api_server]
directory = /data/api-service #代码存放的地方
command = /usr/local/python3/bin/gunicorn -w 4 -b :5005 LogoSeverApi:app #-w的参数根据CPU的核数来定,不要超过CPU的核数
#process_name = %(process_num)s ; process_name expr (default %(program_name)s)
#process_name = %(process_num)s
#numprocs = 4 ; number of processes copies to start (def 1)
numprocs_start = 1
autostart = true ; start at supervisord start (default: true)
autorestart = unexpected ; whether/when to restart (default: unexpected)
startsecs = 10 ; number of secs prog must stay running (def. 1)
startretries = 3 ; max # of serial start failures (default 3)
user = dev
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/api-service/logs/supervisor.log
这样就可以启动4个进程同时处理HTTP请求,提高系统的使用效率及性能。 还可以把端口8000改为其他
而在实际应用中,应使用后台执行的方式启动服务
nohup 启动服务的命令 &
复制代码
即
nohup gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app &
复制代码
这时你可以在本机的浏览器*问127.0.0.1:8000,浏览器上就会出现Hello World!
注意:如果是想通过外网访问的话就要把ip改为内网的ip
4、配置nginx
配置改为
server {
listen 80;
server_name example.org; # 这是HOST机器的外部域名,用地址也行
location / {
proxy_pass http://127.0.0.1:8000; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
复制代码
这样启动之后80端口就可以转发到8000端口了
5、检查配置
nginx -t
复制代码
若出现一下内容,则表示配置成功
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
复制代码
6、更新nginx配置
nginx -s reload
复制代码
在已经运行了Gunicorn的前提下,在浏览器访问127.0.0.1就会出现Hello World!了
7、重启Gunicorn
输入命令
pstree -ap|grep gunicorn
1.2:安装wget(如果已经安装跳过)
yum install wget
1.3:安装python3.6(如果已经安装跳过)
2.1.下载python3.6
wget http://cdn.npm.taobao.org/dist/python/3.6.5/Python-3.6.5.tgz
2.2.解压安装包
tar -zxvf Python-3.6.5.tgz
---------------------
2.3安装python3.6的依赖包
yum install -y gcc zlib*
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
---------------------
2.4.配置安装路径为/usr/local/python3
./configure --prefix=/usr/local/python3 --with-ssl
make && make install
2.5.将python3.6的路径加入到PATH中
echo "export PATH=$PATH:/usr/local/python3/bin/" >>/etc/profile
source /etc/profile
2.6.建立软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
---------------------
2.7.安装supervisor
yum install supervisor -y
如果报如下错误,安装yum的扩展源
安装yum的扩展源
yum install epel-*
2.8.添加supervisor的配置
在/etc/supervisord.d目录下添加一个logo.ini配置文件
vim /etc/supervisord.d/logo.ini
内容如下:
[program:logo_api_server]
directory = /data/api-service #代码存放的地方
command = /usr/local/python3/bin/gunicorn -w 4 -b :5005 LogoSeverApi:app #-w的参数根据CPU的核数来定,不要超过CPU的核数
\#process_name = %(process_num)s ; process_name expr (default %(program_name)s)
\#process_name = %(process_num)s
\#numprocs = 4 ; number of processes copies to start (def 1)
numprocs_start = 1
autostart = true ; start at supervisord start (default: true)
autorestart = unexpected ; whether/when to restart (default: unexpected)
startsecs = 10 ; number of secs prog must stay running (def. 1)
startretries = 3 ; max # of serial start failures (default 3)
user = dev
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/api-service/logs/supervisor.log
2.9创建data目录
mkdir /data
2.10从git上面将代码下载下来,git在内网,如果是线上请将git下载下来然后通过ftp上传到线上坏境
cd /data
git clone http://zhangxiaoyang:messi1020@git.epweike.net:3000/graphics/logo_api api-servicels -
2.11安装程序依赖包
cd /data/api-service/
yum install cairo-devel
pip3 install --upgrade pip
pip3 install -r requirements.txt