1.1 Installing a virtual environment Create a folder
mkdir data 目录文件夹cd data 进入data文件夹mkdir nginx 创建安装nginx的文件夹mkdir server 存放代码的文件夹 mkdir logs 存放日志的文件夹mkdir backup 备份代码的文件夹mkdir softs 软件存放的位置mkdir virtual 虚拟环境的位置mkdir scripts 脚本的运行位置mkdir scp_codes 上传的代码
Install the virtual environment
apt-get install python-virtualenv (ubuntu已经安装过,可省略)virtualenv -p /usr/bin/python3 api_server (采用他,安装在本文件夹,)mkvirtualenv api_server(不要用它,它会自己安装到其他地方)source api_server/bin/activate (在虚拟环境的active进入虚拟环境)安装的软件会在虚拟环境下的bin目录下
Installation of Nginx Environment
解压cd ~/data/softs/ tar xf pcre-8.39.tar.gz配置cd ~/data/softs/pcre-8.39./configure编译make安装sudo make install
Installing 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 Simple Basic operation
检查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, first install Gunicorn
pip install gunicorn复制代码
2, in the entrance file App.run () plus the following
from werkzeug.contrib.fixers import ProxyFixapp.wsgi_app = ProxyFix(app.wsgi_app)复制代码
Cases:
from flask import Flaskapp = 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. Start Gunicorn
The simplest way to
gunicorn 入口文件名:app复制代码
The default is to listen 127.0.0.1:8000
If you are dealing with high concurrency, you need to open multiple processes and modify the listening port of the picture
gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app复制代码保存在supervisord 里面的etc/supervisord.d目录下添加一个logo.ini配置文件
Add a configuration file
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 = 1autostart = 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 = devredirect_stderr = truestdout_logfile_maxbytes = 20MBstdout_logfile_backups = 20stdout_logfile = /data/api-service/logs/supervisor.log
This enables 4 processes to process HTTP requests at the same time, improving the efficiency and performance of the system. You can also change the port 8000 to another
In real-world applications, you should start the service in a way that is performed in the background
nohup 启动服务的命令 &复制代码
That
nohup gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:app &复制代码
At this point you can access the 127.0.0.1:8000 on the browser of this machine, and the Hello world! will appear on the browser.
Note: If you want to access through the Internet, the IP will be changed to the intranet IP
4. Configure Nginx
The configuration changes to
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 ports can be forwarded to port 8000 after this is started.
5. Check the configuration
nginx -t复制代码
If the content appears, the configuration is successful
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful复制代码
6. Update Nginx Configuration
nginx -s reload复制代码
On the premise that the Gunicorn is already running, the 127.0.0.1 will appear in the browser. Hello world!
7. Restart Gunicorn
Input command
pstree -ap|grep gunicorn
1.2: Install wget (skip if already installed)
1.3: Install python3.6 (skip if already installed)
2.1. Download python3.6
wget http://cdn.npm.taobao.org/dist/python/3.6.5/Python-3.6.5.tgz
2.2. Unzip the installation package
tar -zxvf Python-3.6.5.tgz
---------------------
2.3 Installing the python3.6 dependency package
---------------------
2.4. Configure the installation path to/usr/local/python3
./configure --prefix=/usr/local/python3 --with-sslmake && make install
2.5. Add the path of the python3.6 to path
echo "export PATH=$PATH:/usr/local/python3/bin/" >>/etc/profilesource /etc/profile
2.6. Establishing a soft connection
ln -s /usr/local/python3/bin/python3 /usr/bin/python3ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
---------------------
2.7. Installing Supervisor
yum install supervisor -y
If you report the following error, install the Yum extension source
Installing the Yum extension source
yum install epel-*
2.8. Add the Supervisor Configuration
Add a Logo.ini configuration file under the/ETC/SUPERVISORD.D directory
Vim/etc/supervisord.d/logo.ini
The contents are as follows:
[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 = 1autostart = 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 = devredirect_stderr = truestdout_logfile_maxbytes = 20MBstdout_logfile_backups = 20stdout_logfile = /data/api-service/logs/supervisor.log
2.9 Creating the Data Directory
mkdir /data
2.10 Download the code from Git, git in the net, if it is online, please download git and upload it to the online bad environment via FTP.
cd /datagit clone http://zhangxiaoyang:[email protected]:3000/graphics/logo_api api-servicels -
2.11 Setup Dependency Packages
cd /data/api-service/yum install cairo-develpip3 install --upgrade pippip3 install -r requirements.txt
Nginx+gunicorn Project Deployment