前言
很久没更博客了,最近新写了一个小项目,后边有时间把一些心得放上来,先把环境的部署方式整理出来。
部署过程
先将环境的python升级为2.7
保证有pip
-
安装了nginx并配置
vim /Data/apps/nginx/conf/include/sqlaudit.conf
server {
listen 80; #对外80端口
server_name sqladmin.xxxx.net;
location / {
root /Data/apps/djangoproject/sqlaudit/; #项目目录的位置
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
uwsgi_param UWSGI_CHDIR /Data/apps/djangoproject/sqlaudit/;
uwsgi_param UWSGI_SCRIPT sqlaudit_wsgi; #对应的sqlaudit_wsgi.py脚本
access_log /Data/apps/nginx/logs/sqlaudit_access.log;
error_log /Data/apps/nginx/logs/sqlaudit_error.log;
}
location /static/ { #静态文件单独处理一下
alias /Data/apps/djangoproject/sqlaudit/static/;
index index.html;
access_log /Data/apps/nginx/logs/sqlaudit_access.log;
error_log /Data/apps/nginx/logs/sqlaudit_error.log;
}
}
```
4. 安装uwsgi
/usr/local/bin/pip install uwsgi
5. 把相关依赖都装上
yum install redis mysql
yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel mysqlclient16-devel mysqlclient16
vim /etc/ld.so.conf.d/mysqlclient18-x86_64.conf
/usr/local/lib/
ldconfig
6. 代码部署
cd /Data/apps/djangoproject/
git clone https://git.coding.net/caseast/SQLaudit.git sqlaudit/
把代码拿下来之后
初始化数据库,然后执行
pip install -r requirement.txt -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com
python manage_prod.py loaddata sqlaudit_init.json
7. supervisord启动celery相关
pip install supervisor -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
```python
[program:celery]
command=/usr/bin/python manage_prod.py celery worker -c 4
directory=/Data/apps/djangoproject/sqlaudit
stdout_logfile=/Data/apps/nginx/logs/celery.log
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT
user=nginx
[program:flow]
command=/usr/bin/python manage_prod.py celery flower
directory=/Data/apps/djangoproject/sqlaudit
stdout_logfile=/Data/apps/nginx/logs/flow.log
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=QUIT
user=nginx
```
8. 配置uwsgi,uwsgi就类似tomcat,是真正解析python的容器
cd /Data/apps/djangoproject/sqlaudit && cp uwsgi /etc/init.d/ #先把启停脚本弄好
```bash
#!/bin/bash
# uwsgi script
# it is v.0.0.1 version.
# chkconfig: - 89 19
# description: uwsgi script
# processname: uwsgi
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
uwsgi_config=/Data/apps/djangoproject/sqlaudit/sqlaudit.ini
#uwsgi_pid=`ps -eo pid,comm|grep uwsgi|grep -v 'init.d'|grep -v grep|sed -n 1p|awk '{print $1}'`
uwsgi_pid=`ps -ef|grep uwsgi|grep -v 'init.d'|grep -v grep|sed -n 1p|awk '{print $2}'`
uwsgi_PID=/Data/apps/nginx/logs/uwsgi.pid
uwsgi=/usr/bin/uwsgi
RETVAL=0
prog="uwsgi"
# Source function library.
. /etc/rc.d/init.d/functions
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi
# Start nginx daemons functions.
start() {
uwsgi_pn=`ps aux|grep -v "grep"|grep -c "uwsgi"`
if [ $uwsgi_pn -gt 4 ];then
action "uwsgi is running!" /bin/true
exit 0
fi
daemon $uwsgi --ini ${uwsgi_config}
action "uwsgi start ..." /bin/true
}
# Stop nginx daemons functions.
stop() {
uwsgi_pn=`ps aux|grep -v "grep"|grep -c "uwsgi"`
if [ $uwsgi_pn -gt 4 ]
then
kill -9 $uwsgi_pid
RETVAL=$?
action "uwsgi stopping ..." /bin/true
else
action "uwsgi not running!" /bin/false
fi
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
sleep 2
start;;
*)
echo $"Usage: $prog {start|stop|restart}"
exit 1
esac
exit $RETVAL
```
下边附上uwsgi的配置文件sqlaudit.ini,需要和nginx的配置对应
/Data/apps/djangoproject/sqlaudit/sqlaudit.ini
```python
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /Data/apps/djangoproject/sqlaudit/
# Django's wsgi file
module = sqlaudit_wsgi #这个需要和nginx配置里的uwsgi_param UWSGI_SCRIPT对应
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 8
# the socket (use the full path to be safe
socket = 127.0.0.1:9000 #这个也和nginx配置对应
# clear environment on exit
vacuum = true
daemonize=/Data/apps/nginx/logs/sqlaudit_uwsgi.log
```
-
最后启动一切服务
/Data/apps/redis/bin/redis-server &
/Data/apps/mysql/bin/mysqld_safe &
/usr/local/bin/supervisord
/etc/init.d/uwsgi start
/Data/apps/nginx/sbin/nginx -
备注
这个非部署问题了,就是把django项目部署到nginx上之后,会找不到admin相关的static文件
所以在代码里执行这个,把admin相关的static文件搞到项目的static中
python manage.py collectstatic