平台url: http://easytest.xyz
花巨資搞了個阿里雲服務器,哈哈,有想體驗指導的大佬私聊我~~~
部署環境
雲服務器:Ubuntu Server 16.04.1 LTS 64位
python 主要包版本:
python 3.5.2
Django 2.0.3
BeautifulReport 0.0.9
celery 3.1.25
celery-with-redis 3.0
colorlog 4.0.2
crypto 1.4.1
ddt 1.2.1
django-celery 3.2.2
django-redis 4.0.0
gevent 1.4.0
greenlet 0.4.15
gunicorn 19.9.0
ipython 7.5.0
matplotlib 3.0.0
Pillow 6.0.0
pip 19.1.1
pycrypto 2.6.1
PyMySQL 0.9.3
qrcode 6.1
redis 2.10.6
requests 2.22.0
問題
1.為什么使用python3.5?
ubuntu使用python3.6在安裝uwsgi的時候,老是報錯,然后還么有找到原因...
2.定時任務運行報錯:
1>.TypeError: can only concatenate tuple (not "NoneType") to tuple;
版本問題 需要 django-celery 3.1.17 celery 3.1.25 celery-with-redis 3.0; https://github.com/stanleylst/ansibleUI/issues/2
2>.TypeError: __init__() missing 1 required positional argument: 'on_delete';
django2.0 on_delete 是必須的 ; https://blog.csdn.net/qq_38038143/article/details/80286187
3>.AttributeError: type object 'BaseCommand' has no attribute 'option_list';
升級django-celery 到3.2.2 解決 ; https://chowyi.com/Django-1-10-celery-worker-%E5%90%AF%E5%8A%A8%E6%8A%A5%E9%94%99/
3.django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3;
django版本問題,降級到diango 2.2 往下就可以了;
4. You need tcl 8.5 or newer in order to run the Redis test.
安裝redis時報的錯,升級tcl即可; https://www.cnblogs.com/Security-Darren/p/4381932.html
5.nginx負載均衡設置時,命名不能用下划線;
upstream myApp {
server 127.0.0.1:9000;
}
6.BeautifulReport模塊;
result.report(filename=now + 'report.html', description=readConfig.title, log_path=report_path);
log_path 參數需要替換成 report_dir 參數
7.redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error;
redis-cli 輸入 config set stop-writes-on-bgsave-error no https://www.jianshu.com/p/3aaf21dd34d6
8.locust 守護進程; 要帶小括號 (locust -f base/performance.py --master &) https://www.cnblogs.com/maoxianfei/p/7987217.html
啟動slave 運行性能測試 locust -f base/performance.py --slave --master-host=129.28.187.64
9.定時任務守護進程 django-celery supervisor http://yshblog.com/blog/165
supervisord.conf
[program:celery.worker]
;指定運行目錄
directory=/home/ubuntu/EasyTest/
;運行目錄下執行命令
command=python manage.py celery worker --loglevel=info --logfile /home/ubuntu/EasyTest/celery_logs/celery_worker.log
;啟動設置
numprocs=1 ;進程數
autostart=true ;當supervisor啟動時,程序將會自動啟動
autorestart=true ;自動重啟
;停止信號,默認TERM
;中斷:INT (類似於Ctrl+C)(kill -INT pid),退出后會將寫文件或日志(推薦)
;終止:TERM (kill -TERM pid)
;掛起:HUP (kill -HUP pid),注意與Ctrl+Z/kill -stop pid不同
;從容停止:QUIT (kill -QUIT pid)
stopsignal=INT
;輸出日志
stdout_logfile=/home/ubuntu/EasyTest/celery_logs/celery_worker.log
stdout_logfile_maxbytes=10MB ;默認最大50M
stdout_logfile_backups=10 ;日志文件備份數,默認為10
;錯誤日志
redirect_stderr=false ;為true表示禁止監聽錯誤
stderr_logfile=/home/ubuntu/EasyTest/celery_logs/celery_worker_err.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=10
[program:celery.beat]
;指定運行目錄
directory=/home/ubuntu/EasyTest/
;運行目錄下執行命令
command=celery -A EasyTest beat -l info --loglevel info --logfile /home/ubuntu/EasyTest/celery_logs/celery_beat.log
;啟動設置
numprocs=1 ;進程數
autostart=true ;當supervisor啟動時,程序將會自動啟動
autorestart=true ;自動重啟
;停止信號
stopsignal=INT
啟動和關閉supervisor
啟動supervisor輸入如下命令,使用具體的配置文件執行:
supervisord -c supervisord.conf
關閉supervisord需要通過supervisor的控制器:
supervisorctl -c supervisord.conf shutdown
重啟supervisord也是通過supervisor的控制器:
supervisorctl -c supervisord.conf reload
10.django 匿名用戶限制;
裝飾器 @login_required
11.異常頁面配置 400 403 404 500 https://zhuanlan.zhihu.com/p/38006919
12.gunicorn配置;
啟動命令:gunicorn -c gunicorn-config.py EasyTest.wsgi:application
gunicorn-config.py
# !/usr/bin/env python
# coding=utf-8
from multiprocessing import cpu_count
bind = '127.0.0.1:9000'
daemon = True # 守護進程
workers = cpu_count() * 2
worker_class = 'gevent'
forwarded_allow_ips = '*'
# 維持TCP鏈接
keepalive = 6
timeout = 65
graceful_timeout = 10
worker_connections = 65535
# log
capture_output = True
loglevel = 'info'
accesslog = "/tmp/EasyTest_access.log" #訪問日志文件的路徑
errorlog = "/tmp/EasyTest_error.log"
13.nginx配置;
nginx啟動命令:sudo /usr/local/nginx/sbin/nginx
nginx.conf
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
multi_accept on;
worker_connections 10240;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 128k;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# keepalive_timeout 0;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
upstream myApp {
server 127.0.0.1:9000;
}
server {
listen 80 ;
server_name 129.28.187.64;
#charset koi8-r;
charset utf-8;
access_log /home/ubuntu/EasyTest/logs/access.log main;
error_log /home/ubuntu/EasyTest/logs/error.log;
location = /favicon.ico {
empty_gif;
access_log off;
}
location /media/ {
root /home/ubuntu/EasyTest/;
expires 30d;
access_log off;
}
location /static/ {
alias /var/static/static/;
}
location / {
root html;
index index.html index.htm;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_redirect off;
proxy_pass http://myApp;
}
}
}
14.supervisord啟動報錯; Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
ps -ef | grep supervisord 殺掉進程,重新啟動;
15.ubuntu--matplotlib顯示中文; https://blog.csdn.net/jeff_liu_sky_/article/details/54023745
下載simhei.ttf中文字體,放入 .virtualenvs/env/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf 中;
代碼中設置
import matplotlib as mpl
from matplotlib import pyplot as plt
mpl.rcParams[u'font.sans-serif'] = ['simhei']
mpl.rcParams['axes.unicode_minus'] = False
plt.title(u'測試用例運行結果')
16.mysql數據庫保存時報錯:django.db.utils.InternalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")
插入數據庫數據過大導致; https://stackoverflow.com/questions/93128/mysql-error-1153-got-a-packet-bigger-than-max-allowed-packet-bytes
在mysql中執行:set global net_buffer_length=1000000;
set global max_allowed_packet=1000000000;
17.安裝jenkins;
sudo apt-get install openjdk-8-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
執行 sudo apt-get install jenkins 時,可能會遇到各種小問題,可能是python2和python3不兼容,按照提示,修改py源碼內容即可;
jenkins 密碼:/var/lib/jenkins/secrets/initialAdminPassword
service jenkins start 開始 service jenkins restart 重啟 service jenkins stop 停止 service jenkins status 查看狀態
18.jenkins 重啟 django + gunicorn 項目:
shell重啟項目腳本
# !/bin/sh
sudo cp -Rf /var/lib/jenkins/workspace/django-EasyTest/* /home/ubuntu/project/
source /home/ubuntu/.virtualenvs/env/bin/activate
PROCESS=`ps -e | grep gunicorn | awk '{printf "%d\n", $1}'`
echo $PROCESS
for i in $PROCESS
do
echo "Kill the gunicorn process [ $i ]"
sudo kill -9 $i
done
cd /home/ubuntu/
echo 'stop server finish!'
sleep 2s
cd /home/ubuntu/project/
gunicorn -c /home/ubuntu/project/gunicorn-config.py EasyTest.wsgi:application
echo 'start server success!!!'
exit 0
issues:
shell 中啟動python虛擬環境:
source /home/ubuntu/.virtualenvs/env/bin/activate
source: not found
原因: ls -l `which sh` 提示/bin/sh -> dash
這說明是用dash來進行解析的。
改回方法:
命令行執行:sudo dpkg-reconfigure dash
在界面中選擇no
再ls -l `which sh` 提示/bin/sh -> bash
19.防火牆
https://www.cnblogs.com/sweet521/p/5733466.html
sudo apt-get install ufw
sudo ufw enable
sudo ufw default deny sudo ufw enable|disable
sudo ufw allow 端口 運行訪問端口
指定用戶組操作文件
sudo chown ubuntu:ubuntu -R EasyTest/logs/
20. OSError: [Errno 28] No space left on device
df -h
inode耗盡導致No space left on device;
解決:刪除 var的一個子目錄下一些沒用的臨時文件和日志文件;通過刪除大量的小文件得以解決
刪除jenkins日志文件,然后正常了呢!
21.修改admin和flower返回平台路徑
admin:/home/lixiaofeng/.virtualenvs/env/lib/python3.5/site-packages/simpleui/templates/admin/index.html
flower:/home/lixiaofeng/.virtualenvs/env/lib/python3.5/site-packages/flower/templates/navbar.html
22.數據庫遷移,提示 django.db.utils.InternalError: (1054, "Unknown column 'base_report.report_path' in 'field list'")
django_migrations 表中會有每次執行的記錄,可以通過修改這些記錄解決;
23. 一台服務器實現負載均衡
復制一份代碼到別目錄,修改 gunicorn-config.py,修改bind 參數 端口為9001;
修改nginx.conf;
然后重啟nginx和gunicorn;