之前通过uwsgi 部署的,昨天查到还可以这样部署,遂用之。总的来说确实很简单。
当前部署环境是阿里云,centos
步骤:
1.安装依赖
这个没啥好说的
sudo yum install python3
sudo yum install nginx
2.安装pip
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
3.安装gunicorn工具
sudo pip install gunicorn
sudo pip install gevent # 这个是gunicorn运行的一种模式
说明:安装完以后,系统会提示"The scripts gunicorn and gunicorn_paster are installed in '/usr/local/python3/bin' which is not on PATH "
[root@iZm5e8ip0m748x62a7n6i4Z build]# pip uninstall gunicorn
Uninstalling gunicorn-19.9.0:
Would remove:
/usr/local/python3/bin/gunicorn
/usr/local/python3/bin/gunicorn_paster
/usr/local/python3/lib/python3.6/site-packages/gunicorn-19.9.0.dist-info/*
/usr/local/python3/lib/python3.6/site-packages/gunicorn/*
Proceed (y/n)? y
Successfully uninstalled gunicorn-19.9.0
[root@iZm5e8ip0m748x62a7n6i4Z build]# pip install gunicorn
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting gunicorn
Downloading http://mirrors.aliyun.com/pypi/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl (112kB)
100% |████████████████████████████████| 122kB 2.2MB/s
Installing collected packages: gunicorn
The scripts gunicorn and gunicorn_paster are installed in '/usr/local/python3/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed gunicorn-19.9.0
此时需要将gunicorn 添加到环境变量中,2种永久方式添加,一个是
vim /etc/profile #对所有用户生效
# 新增一行
export PATH=$PATH:/usr/local/python3/bin
另一个是,
vim ~/.bash_profile # 对当前用户生效
# 新增一行
export PATH=$PATH:/usr/local/python3/bin
修改完文件后,重新登录系统即可生效。
但是我的账号有问题,增加了不生效,索性在启动脚本中每次增加。
4.配置nginx
- 配置nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types; # 这个规定了静态文件的类型,很重要,不然样式没法生效
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
include /etc/nginx/sites-available/*.conf;
}
- 配置你自己定义的服务server
路径:/etc/nginx/sites-available/exmple.conf;
server {
listen 80;
server_name build.2222.com;
charset utf-8;
client_max_body_size 200m;
access_log /var/log/nginx/build-access.log;
error_log /var/log/nginx/build-err.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
#静态文件如js,css的存放目录
alias /home/build/build/static/;
}
location / {
proxy_pass http://0.0.0.0:8000; # 这里要配合启动文件使用
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# 重启nginx
/etc/init.d/nginx restart 或者 nginx -s reload
5.开发环境迁移项目
如果从开发环境迁移项目,则会涉及到很多依赖,所以这时候需要一个将依赖库同步部署到新服务器上。
方法:
- 在开发环境中,项目根目录执行:详见:Python_项目迁移便捷方法
sudo pip freeze >requirements.txt
# 此时项目根目录会生成一个requirements.txt文件
- 然后在服务器执行:
sudo pip install -r requirements.txt
则完成了项目迁移
至此基础依赖都完事了。然后就是启动服务即可。
6.启动
6.1 项目根目录新建gunicorn配置文件
#gunicorn.py
# coding:utf-8
__author__ = 'xcma'
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = '0.0.0.0:8000' #绑定ip和端口号
backlog = 512 #监听队列
chdir = '/home/build/build' #gunicorn要切换到的目的工作目录
timeout = 30 #超时
worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
workers = multiprocessing.cpu_count() * 2 + 1 #进程数
threads = 2 #指定每个进程开启的线程数
loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' #设置gunicorn访问日志格式,错误日志无法设置
"""
其每个选项的含义如下:
h remote address
l '-'
u currently '-', may be user name in future releases
t date of the request
r status line (e.g. ``GET / HTTP/1.1``)
s status
b response length or '-'
f referer
a user agent
T request time in seconds
D request time in microseconds
L request time in decimal seconds
p process ID
"""
accesslog = "/home/log/gunicorn_access.log" #访问日志文件
errorlog = "/home/log/gunicorn_error.log" #错误日志文件
6.2 配置django的settting文件
# 在setting中新增app,这个app在ide中会报错,不用管。
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appack',
'api',
'gunicorn'
]
6.3 启动脚本
- restart.sh
#!/bin/sh
## service name
#项目的目录
SERVICE_DIR=/home/build/build
#gunicorn的名字
SERVICE_NAME=gunicorn
#gunicorn的配置文件名
SERVICE_CONF=gunicon.py
#pid存放的位置
PID=gunicorn\.pid
export PATH=$PATH:/usr/local/python3/bin
cd $SERVICE_DIR
git checkout . && git clean -xdf
git pull
start(){
nohup gunicorn build.wsgi:application -c $SERVICE_DIR/$SERVICE_CONF >/dev/null 2>&1 &
echo $! > $SERVICE_DIR/$PID
echo "*** start $SERVICE_NAME ***"
}
stop(){
kill `cat $SERVICE_DIR/$PID`
rm -rf $SERVICE_DIR/$PID
echo "*** stop $SERVICE_NAME ***"
sleep 2
P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "*** $SERVICE_NAME process not exists or stop success ***"
else
echo "*** $SERVICE_NAME process pid is:$P_ID ***"
echo "*** begin kill $SERVICE_NAME process,kill is:$P_ID ***"
kill -9 $P_ID
fi
}
f_usage() {
echo "USAGE: restart [options]"
echo "OPTIONS:"
echo " start"
echo " stop "
echo " restart"
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
stop
sleep 2
start
echo "*** restart $SERVICE_NAME ***"
;;
*)
f_usage
;;
esac
exit 0
6.3 用脚本启动:
sh restart.sh start