本地雏形搭建使用的是 mac电脑开发,用到了 python3.6 chrome django2.0 tastypie 云打码 sqlite
服务器使用的是 centos7 python3.6.1 firefox django2.0 tasypie 云打码 mysql
部署步骤及遇到的难题如下:
整体安装参考了这篇文章:从0开始搭建nginx-uWSGI-Django-python服务器, 以下简称主手册
1.安装python ,问题不大
主要参考的这篇文章:centos 7.3 安装配置python3.6.1
2.安装firefox
这里本来花了点时间安装chrome,但后面几天碰到的问题,最终用firefox解决了,故这里也就不介绍安装chrome的步骤了,以后服务端就用firefox。
centos7 自带的firefox版本是52.X, 去这里下载最新的驱动版本为0.19.1,发现不能使用,尝试更新firefox但未起到作用,猜想是因为原来的firefox还存在,代码还是调用了原来的firefox,后下载了0.17.0版本,成功!
3.安装python虚拟环境:安装配置VirtualEnv和VirtualEnvWrapper
安装问题不大,配置项因人而异,我这边是这样的:
vi ~/.bashrc
export WORKON_HOME=/usr/share/nginx/cy
source /usr/python3.6/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
4.服务器部署极知git代码
进入虚拟环境,将gitlab中ji_pyhon的项目克隆下来,用pip 安装必要的包:
其中pip install python-mimeparse : 这个要注意python-mimeparse instead of mimeparse
至于代码层面,在本地开发的时候基本解决,简单提一点tastypie里有个小bug,还没更新到pip里面,所以用的源码下载。
5.安装uwsgi,问题不大,参考主手册,即可,如果用的阿里云服务器,记得开阿里云的端口。
6.配通nginx和uwsgi,问题不大,最终的配置文档如下:vi jz-python.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///usr/share/nginx/cy/jz_env/jz_python/jz_python.sock; # for a file socketserver
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;# the domain name it will serve for
server_name jz.baiyoudata.com;# substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M;# adjust to taste
# Django media
location /media {
alias /usr/share/nginx/cy/jz_env/jz_python/media;# your Django project's media files - amend as required
}
location /static {
alias /usr/share/nginx/cy/jz_env/jz_python/static;# your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /usr/share/nginx/cy/jz_env/jz_python/uwsgi_params;# the uwsgi_params file you installed
}
}
7.通过配置.ini文件运行uWSGI.最终配置文件如下:home那个地方要注意,是你建立的虚拟环境目录
8.服务器配置mysql,问题不大,主要参考Django的MySQL Driver配置,和CentOS7使用pip安装mysqlclient提示mysql_config not found,这里只用这行就行了sudo yum install mariadb-devel
#uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /usr/share/nginx/cy/jz_env/jz_python
# Django's wsgi file
module = jz_python.wsgi
# the virtualenv (full path)
home = /usr/share/nginx/cy/jz_env
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /usr/share/nginx/cy/jz_env/jz_python/jz_python.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
以上是基本的服务器配置,问题不算很大,接下来是本项目遇到的问题,花不少时间,由于本地环境和服务器环境的差别导致了很多代码和配置不能用,现罗列下:
Django框架默认启用了缓存,这个很重要,不然你改的代码,半天不起作用。解决方案如下:修改jz_python_uwsgi.ini配置,增加py-autoreload=N配置
invalid element state: Element isnot currently interactable and may not be manipulated:原因是服务器使用了Xvfb,webdriver打开的浏览器不够大,定位不到这个元素。尝试用代码来设置打开窗口的大小,可以解决。不过后面又碰到,截图是黑色的问题。于是统一用这个方式解决了:Taking Browser Screenshots With No Display (Selenium/Xvfb) , 这个地方弄来弄去大概花了2天时间,最终用firefox 替代了chrome来解决这个问题。