Contents
环境
- CentOS 6.9+
- python 2.7+
- nginx 1.16.1
安装Nginx
安装教程: https://nginx.org/en/linux_packages.html#RHEL-CentOS
rpm -ivh http://nginx.org/packages/centos/6/x86_64/RPMS/nginx-1.12.2-1.el6.ngx.x86_64.rpm
启动:service nginx start
开机自启:chkconfig nginx on
将项目的nginx配置文件添加到 /etc/nginx/conf/ 目录中
安装git
安装基础环境
yum groupinstall "Development Tools"
yum install zlib-devel perl-ExtUtils-MakeMaker asciidoc xmlto openssl-devel curl-devel
下载
wget https://github.com/git/git/archive/v2.14.0.tar.gz
tar -zvxf v2.14.0.tar.gz
安装
cd git-2.14.0
autoconf
./configure
make && make install
yum remove git
ln -s /usr/local/bin/git /usr/bin/git
git config --global credential.helper store
将项目代码git clone 下来cd /data/idb; git clone http://gitlab.xxxx.com/xxx.git
安装 python2.7 环境
# 安装 python3.6教程
yum install https://centos6.iuscommunity.org/ius-release.rpm
yum install python36u python36u-pip
yum install python36u-devel
ln -s /usr/bin/python3.6 /usr/bin/python3
ln -s /usr/bin/pip3.6 /usr/bin/pip3
# 安装python2.7教程
wget https://centos6.iuscommunity.org/ius-release.rpm
rpm -Uvh ius-release.rpm
yum install python27 python27-devel python27-pip
cd ~
mkdir .pip
cd .pip
vi pip.conf
[global]
index-url=http://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
安装环境
pip install -r requirements.txt
# 遇到错误解决
1、EnvironmentError: mysql_config not found
yum install mysql-devel
2、error: command 'gcc' failed with exit status 1
yum install openldap; yum install openldap24-libs ; yum install openldap-clients; yum install openldap-devel; yum install openssl-devel
部署Uwsgi
uwsgi --ini uwsgi.ini
或者并发模式
uwsgi --gevent 40 --gevent-early-monkey-patch uwsgi.ini
日志:/var/log/uwsgi9001.log
部署celery (项目中使用了异步任务Celery)
安装 supervisor
pip install supervisor
修改 /etc/supervisord.conf
(如没有此文件,请拷贝默认配置文件)文件,末尾
[include]
;files = relative/directory/*.ini
files = /etc/supervisor/*.ini
创建目录/etc/supervisor/
,将文件supervisord_idb_celery.ini
(注意确认修改相关路径)上传至该目录
文件配置示例:
[program:celerywoker]
user=root
command=/usr/local/python/python2.7.9/bin/python -u manage.py celery worker --loglevel=info
directory=/data/idb/idb-api
stdout_logfile=/var/log/celery/worker_info.log
autostart=true
stderr_logfile=/var/log/celery/worker_err.log
autorestart=true
redirect_stderr=true
stopsignal=QUIT
[supervisord]
[supervisorctl]
相关命令:
重启
supervisord -c /etc/supervisord.conf
查看守护进程
supervisorctl status
重启守护进程
supervisorctl restart xxx
部署daphne(项目中使用了异步通信websocket:channels)
supervisor 配置文件示例(根据实际情况自行修改):
[fcgi-program:oddstorm-ws]
# TCP socket used by Nginx backend upstream
socket=tcp://127.0.0.1:9010
# Directory where your site's project files are located
directory=/home/OddStormServer
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=daphne -u /run/daphne/daphne-pro%(process_num)d.sock --fd 0 --access-log /var/log/oddstorm-ws.log --proxy-headers OddStorm.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=2
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/var/log/oddstorm-ws.log
redirect_stderr=true
nginx 中的配置
upstream django-ws {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:9010; # for a web port socket (we'll use this first)
}
...
server {
...
location /ws/ {
uwsgi_pass django-ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $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-Host $server_name;
}
...
}