Skip to content

Latest commit

 

History

History
188 lines (130 loc) · 6.7 KB

install_django_offline.md

File metadata and controls

188 lines (130 loc) · 6.7 KB

在线上服务器部署Django的时候,不可能将所有服务器都连接到internet,然后通过pip进行联网安装。

离线安装Django以及相关软件模块

pip freeze > requirements.txt
  • 使用以下命令为模块包构建一个wheelhouse目录
pip wheel -r requirements.txt -w wheelhouse

注意:如果没有-w wheelhouse参数,则默认在当前目录下构建wheels - 参考 pip wheel

  • 压缩wheelhouse目录
zip -r wheelhouse.zip wheelhouse
source venv2/bin/activate
unzip wheelhouse.zip
pip install wheelhouse/*

pip install wheelhouse的异常排查

pip版本过低导致安装失败

这里遇到一个报错:

Exception:
Traceback (most recent call last):
  File "/home/admin/venv2/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/basecommand.py", line 107, in main
    status = self.run(options, args)
  File "/home/admin/venv2/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/commands/install.py", line 225, in run
    InstallRequirement.from_line(name, None))
  File "/home/admin/venv2/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/req.py", line 118, in from_line
    return cls(req, comes_from, url=url)
  File "/home/admin/venv2/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg/pip/req.py", line 43, in __init__
    req = pkg_resources.Requirement.parse(req)
  File "/home/admin/venv2/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2510, in parse
    reqs = list(parse_requirements(s))
  File "/home/admin/venv2/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2436, in parse_requirements
    line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")
  File "/home/admin/venv2/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2404, in scan_list
    raise ValueError("Expected "+item_name+" in",line,"at",line[p:])
ValueError: ('Expected version spec in', 'wheelhouse/certifi-2017.11.5-py2.py3-none-any.whl', 'at', '/certifi-2017.11.5-py2.py3-none-any.whl')

Storing complete log in /home/admin/.pip/pip.log

参考 ValueError “Expected version spec” when installing local wheel via pip,原因是当前的pip版本太低导致的。

通过本地安装升级pip来解决上述问题,下载以下4个文件复制到offline主机上(setuptools有可能不需要),例如目录/tmp/update_pip

get-pip.py
pip-9.0.1-py2.py3-none-any.whl
setuptools-37.0.0-py2.py3-none-any.whl
wheel-0.30.0-py2.py3-none-any.whl

执行以下命令升级pip

python get-pip.py --no-index --find-links=/tmp/update_pip

可以看到升级了Python虚拟环境中的pipwheel

Collecting pip
Collecting wheel
Installing collected packages: pip, wheel
  Found existing installation: pip 1.2.1
    Uninstalling pip-1.2.1:
      Successfully uninstalled pip-1.2.1
Successfully installed pip-9.0.1 wheel-0.30.0

然后再次执行pip install wheelhouse/*即可以成功。

XXXX.whl is not a supported wheel on this platform

在执行pip install wheelhouse/*的时候,有些包安装会提示错误,例如:

MarkupSafe-1.0-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.

开始我以为是操作系统不同,实际对比了操作系统版本完全一致。差别只是Python版本,目标服务器的Python版本较低是 2.7.8,而源服务器的Python版本是2.7.14。但是,相同的大版本2.7,应该是通用才对啊。

参考 pip安装报错:is not a supported wheel on this platformCannot install numpy from wheel format,可以通过以下命令来验证兼容的关键字

import pip; print(pip.pep425tags.get_supported())

由于其中没有包含cp27mu所以导致拒绝安装。(cp表示CPython)

实际上可以通过重新命名.whl包名字来绕开这个问题:

mv MarkupSafe-1.0-cp27-cp27mu-linux_x86_64.whl MarkupSafe-1.0-cp27-none-linux_x86_64.whl

然后就可以正常安装和使用了。

Python mysqlclient 安装

上述pip install wheelhouse/*还有一个报错提示

mysqlclient-1.3.12-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.

参考 https://pypi.python.org/pypi/mysqlclient/1.3.12 显示 mysqlclient-1.3.12 要求的MySQL版本如下:

MySQL-5.5 through 5.7 and Python 2.7, 3.4+ are currently supported. PyPy is supported too.

检查服务器,发现只安装了mysql-libs-5.5.17-1.el5.remi,所以考虑升级MySQL

CentOS 5.7建议安装社区版本的MySQL 5.7

unzip mysql-5.7-community.zip
cd mysql-5.7-community

sudo rpm -e mysql-libs-5.5.17-1.el5.remi.x86_64 --nodeps
sudo rpm -ivh mysql*.rpm

然后下载源代码编译

tar xfz mysqlclient-1.3.12.tar.gz
cd mysqlclient-1.3.12
python setup.py install

通过源代码安装了mysqlclient之后,可以从wheelhouse目录下移除mysqlclient-1.3.12-cp27-cp27mu-linux_x86_64.whl

启动MySQL服务

/etc/init.d/mysqld start

如果数据库密码不正确,则执行MySQL Root用户密码重置

恢复Django应用

cd ~/django_project
  • 执行检查和同步迁移数据库
python manage.py check
python manage.py migrate
  • 运行django服务
python manage.py runserver 0.0.0.0:8000

参考