DJango - 部署指南

小灰灰 2022-05-11 11:40 359阅读 0赞

说明

本文描述 Centos 7 + DJango 1.8 安装方法
不建议使用 centos 7 自带的 DJango rpm 进行安装
rpm 安装的 DJango 无法使用本文中的启动脚本

需求

epel-release-7 RPM
当前使用 rhel7 默认 python 2.7 版本

安装方法

  1. rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

更新软件源方法

  1. yum makecache

DJango 软件安装

1 安装 python-pip

  1. yum install -y python-pip

2 可以选择更新一下pip

  1. pip install --upgrade pip
  2. Collecting pip
  3. Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
  4. 100% |████████████████████████████████| 1.3MB 330kB/s
  5. Installing collected packages: pip
  6. Found existing installation: pip 10.0.1
  7. Uninstalling pip-10.0.1:
  8. Successfully uninstalled pip-10.0.1
  9. Successfully installed pip-18.1

3 利用 python-pip 安装 DJango 软件

  1. python -m pip install "django<2"
  2. Downloading https://files.pythonhosted.org/packages/44/e7/872bbf76aa16b7a061698d75325dac023285db33db4bda8ba8fe5d3bb356/Django-1.11.16-py2.py3-none-any.whl (7.0MB)
  3. 100% |████████████████████████████████| 7.0MB 563kB/s
  4. Collecting pytz (from django<2)
  5. Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
  6. 100% |████████████████████████████████| 512kB 680kB/s
  7. Installing collected packages: pytz, django
  8. Successfully installed django-1.11.16 pytz-2018.5

4 利用 pip 安装一些常用的 python 库文件方法

  1. pip install --upgrade setuptools
  2. pip install six --upgrade --ignore-installed six
  3. pip install djangorestframework <- rest_framework 框架
  4. pip install django-filter
  5. pip install mysql-connector-python <- 数据库连接库
  6. pip install PyMySQL <- 数据库连接库
  7. pip install simplejson
  8. pip install uwsgi <- uwsgi 服务,用于支持 DJango 框架
  9. pip install enum34

温馨提示:
在安装 uwsgi 时, 假如如果了下面的错误

  1. InsecurePlatformWarning
  2. Using cached https://files.pythonhosted.org/packages/a2/c9/a2d5737f63cd9df4317a4acc15d1ddf4952e28398601d8d7d706c16381e0/uwsgi-2.0.17.1.tar.gz
  3. Installing collected packages: uwsgi
  4. Running setup.py install for uwsgi ... error
  5. Complete output from command /usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-3ZUXQt/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-sFae9c/install-record.txt --single-version-externally-managed --compile:
  6. /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'descriptions'
  7. warnings.warn(msg)
  8. running install
  9. using profile: buildconf/default.ini
  10. detected include path: ['/usr/include', '/usr/local/include']
  11. Traceback (most recent call last):
  12. File "<string>", line 1, in <module>
  13. File "/tmp/pip-install-3ZUXQt/uwsgi/setup.py", line 138, in <module>
  14. 'Programming Language :: Python :: 3.6',
  15. File "/usr/lib/python2.7/site-packages/setuptools/__init__.py", line 140, in setup
  16. return distutils.core.setup(**attrs)
  17. File "/usr/lib64/python2.7/distutils/core.py", line 152, in setup
  18. dist.run_commands()
  19. File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands
  20. self.run_command(cmd)
  21. File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
  22. cmd_obj.run()
  23. File "/tmp/pip-install-3ZUXQt/uwsgi/setup.py", line 77, in run
  24. conf = uc.uConf(get_profile())
  25. File "uwsgiconfig.py", line 750, in __init__
  26. raise Exception("you need a C compiler to build uWSGI")
  27. Exception: you need a C compiler to build uWSGI

需要安装 C rpm 包解决

  1. yum install -y gcc

假如遇到下面错误

  1. compilation terminated.
  2. In file included from plugins/python/web3_subhandler.c:1:0:
  3. plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
  4. #include <Python.h>
  5. ^
  6. compilation terminated.
  7. ----------------------------------------
  8. Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-uUo3ih/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-dyNXGe/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-uUo3ih/uwsgi/

需要安装 python-devel rpm 包解决

  1. yum install -y python-devel

验证

利用命令行输入命令看见返回即可

  1. django-admin --help
  2. Type 'django-admin help <subcommand>' for help on a specific subcommand.
  3. Available subcommands:
  4. [django]
  5. check
  6. compilemessages
  7. createcachetable
  8. dbshell
  9. diffsettings
  10. ....

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读