Linux部署Django遇到的问题
安装Python3
Notes:操作系统:CentOS7
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz tar -xvf Python-3.6.3.tar.xz cd Python-3.6.3 ./configure make && make install
然而,报错了!!
各种报错
- zipimport. ZipImportError: can't decompress data; zlib not available
Solution: 缺少包,下载安装 yum install zli*
- Error: Multilib version problems found.
Solution: 发现了多个版本,忽略 yum install -y zlib zlib-devel --setopt=protected_multilib=false
cd Python-3.6.3 ./configure make && make install
成功: Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-9.0.1 setuptools-28.8.0
将原来 python 的软链接重命名: mv /usr/bin/python /usr/bin/python.bak
将 python 链接至 python3: ln -s /usr/local/bin/python3 /usr/bin/python
- pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
该错误在执行 pip install django
时出现。
Solution: 不支持 SSL,安装 openssl-devel
yum install openssl-devel
装完后,重新编译安装Python cd Python-3.6.3 ./configure --with-ssl make && make install
- File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
该错误在执行 yum install openssl-devel
时出现。
cd /usr/bin/ ls -l python*
Solution: yum安装软件时,需要Python的支持,默认为Python2
vim /usr/bin/yum vim /usr/libexec/urlgrabber-ext-down 将#! /usr/bin/python 改为 #! /usr/bin/python2
- Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:zlib-1.2.7-18.el7.x86_64 is a duplicate with zlib-1.2.7-13.el7.i686
该错误在执行 yum install mysql-devel gcc gcc-devel python-devel --skip-broken
时出现。
Solution: 版本冲突,列出所有的版本,移除没用的版本
rpm -qa | grep zlib zlib-1.2.7-18.el7.x86_64 zlib-devel-1.2.7-18.el7.x86_64 zlib-1.2.7-13.el7.i686 yum remove zlib-1.2.7-13.el7.i686
- OSError: mysql_config not found
该错误在执行 pip install mysqlclient
时出现。
Solution: 主要是仓库里没有mysql的源。。先移除,再安装
yum -y remove mysql57-community-release-el7-7.noarch [root@rabbitmq yum.repos.d]# cd /etc/yum.repos.d/ [root@rabbitmq yum.repos.d]# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Base.repo.rpmnew CentOS-Epel.repo CentOS-Sources.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Vault.repo [root@rabbitmq yum.repos.d]# rpm -ivh http://repo.mysql.com/mysql57-community-release-el7-7.noarch.rpm Retrieving http://repo.mysql.com/mysql57-community-release-el7-7.noarch.rpm Preparing...################################# [100%] Updating / installing... 1:mysql57-community-release-el7-7 ################################# [100%] [root@rabbitmq yum.repos.d]# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo mysql-community.repo CentOS-Base.repo.rpmnew CentOS-Epel.repo CentOS-Sources.repo mysql-community-source.repo CentOS-CR.repo CentOS-fasttrack.repo CentOS-Vault.repo
终于没有错误了,启动。。
pip install mysqlclient python manage.py runserver
开发环境方式启动Django,一段时间后应用没响应了。。
上次在线上部署了 Django
项目,原本以为通过 python manage.py runserver 0.0.0.0:8000
启动项目就完成了Django的部署,那可真是太天真了。。
这个项目作为独立的服务,单独部署在另外一台服务器上,过了一段时间,测试反映这个项目访问不到内容,到线上查看服务正常启动,但是就是没有响应,然后就重启了。。经过两三次这样的过程,才意识到部署可能有问题??
Solution
其实将开发环境的启动方式应用到了生产环境,先解决问题。
- 安装配置
uwsgi
Django的文档这样介绍 uwsgi
:
uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C.
pip install uwsgi
uwsgi --version
- 在项目根目录创建
uwsgi.ini
vi uwsgi.ini
[uwsgi] chdir=/opt/hua #项目目录 http=0.0.0.0:8000 wsgi-file=/opt/hua/hua/wsgi.py #uwsgi路径 processes=4 threads=2 master=true vacum=true pidfile=/opt/hua/uwsgi.pid #进程id文件路径,自动生成,可用于控制项目启停 daemonize=/opt/hua/uwsgi.log #日志文件路径 module=hua.wsgi
Notes:
- 上述方式配置以http启动,如果以https方式启动项目,需要配置:
https=0.0.0.0:8000, /opt/cert/certfile.crt, /opt/cert/keyfile.key
; - 当然,https访问项目,还需要
django-sslserver
;
- 控制项目启停
进入uwsgi.ini所在目录
uwsgi --ini uwsgi.ini # 启动 uwsgi --stop uwsgi.pid # 停止 uwsgi --reload uwsgi.pid # 重启 killall -8 uwsgi # 强制停止
Analysis
那么问题来了,在生产环境可以使用 python manage.py runserver 0.0.0.0:8000
来启动项目吗?
答案:可以用来测试,但不建议,即使以nohup
的方式启动。
References
- uwsgi-docs.readthedocs.io/en/latest/
- wsgi.readthedocs.io/en/latest/w…
- docs.djangoproject.com/en/2.2/howt…
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!