[关闭]
@cdmonkey 2019-08-06T01:42:22.000000Z 字数 7097 阅读 706

Python-Django发布

Python Django


http://blog.51cto.com/zhou123/1688434
http://www.cnblogs.com/jhao/p/6071790.html
https://gaussic.github.io/2016/08/03/django-uwsgi-deploy
https://delcoding.github.io/2018/01/django-nginx-uwsgi

生产服务器准备

首先安装必要依赖包:

  1. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite*

更换 pip 源:

  1. [root@svn-test ~]# mkdir ~/.pip
  2. [root@svn-test ~]# vim ~/.pip/pip.conf
  3. [global]
  4. index-url=https://pypi.tuna.tsinghua.edu.cn/simple
  5. trusted-host=pypi.tuna.tsinghua.edu.cn

还有就是把工程开发依赖包导出:

  1. # Executed in the current project Pycharm terminal window:
  2. pip3 freeze > /Users/cdmonkey/Desktop/requirements.txt

Python VENV

创建虚拟场景。
https://blog.qikqiak.com/post/python-virtualenv-all-know

Install virtualenv

  1. [root@testnginx01 tools]# tar zxvf Python-3.6.5.tgz
  2. [root@testnginx01 tools]# cd Python-3.6.5
  3. [root@testnginx01 Python-3.6.5]# ./configure --prefix=/usr/local/python3
  4. [root@testnginx01 Python-3.6.5]# make && make install
  5. [root@testnginx01 Python-3.6.5]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
  1. [root@testnginx01 ~]# python3 -m pip install virtualenv
  2. # 创建虚拟环境,注意要选择好创建位置:
  3. [root@testnginx01 ~]# cd /usr/local/
  4. [root@testnginx01 local]# /usr/local/python3/bin/virtualenv envdjango
  5. Using base prefix '/usr/local/python3'
  6. New python executable in /usr/local/envdjango/bin/python3
  7. Also creating executable in /usr/local/envdjango/bin/python
  8. Installing setuptools, pip, wheel...done.

创建完成后,用下面命令即可激活指定虚拟环境:

  1. [root@testnginx01 local]# source envdjango/bin/activate
  2. (envdjango) [root@testnginx01 local]# # 这就表示已经进入了虚拟环境。
  3. (envdjango) [root@testnginx01 local]# python -V
  4. Python 3.6.5
  5. # 退出虚拟环境:
  6. (envdjango) [root@testnginx01 local]# deactivate

Install virtualenvwrapper

https://www.cnblogs.com/codechangemyworld/p/5277032.html

这是一个基于 virtualenv 之上的工具,它将所有虚拟场景进行统一管理。

  1. # 首先进行安装:
  2. [root@svn-test ~]# python3 -m pip install virtualenvwrapper

可使用这个命令启动 virtualenvwrapper:

  1. [root@svn-test ~]# source /usr/local/python3/bin/virtualenvwrapper.sh

注意:我们这里是使用的编译安装的 python3.6,因而需要对这个文件进行相应修改:

  1. # 首先进行备份:
  2. [root@PBSFINNGINX02 ~]# cd /usr/local/python3/bin/
  3. [root@PBSFINNGINX02 bin]# cp virtualenvwrapper.sh virtualenvwrapper.sh.ori
  4. [root@PBSFINNGINX02 bin]# vim virtualenvwrapper.sh
  5. # Locate the global Python where virtualenvwrapper is installed.
  6. if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
  7. then
  8. VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3"
  9. fi
  10. # Set the name of the virtualenv app to use.
  11. if [ "${VIRTUALENVWRAPPER_VIRTUALENV:-}" = "" ]
  12. then
  13. VIRTUALENVWRAPPER_VIRTUALENV="/usr/local/python3/bin/virtualenv"
  14. fi
  15. # Set the name of the virtualenv-clone app to use.
  16. if [ "${VIRTUALENVWRAPPER_VIRTUALENV_CLONE:-}" = "" ]
  17. then
  18. VIRTUALENVWRAPPER_VIRTUALENV_CLONE="/usr/local/python3/bin/virtualenv-clone"
  19. fi

缺省会将所有虚拟场景放置于 ~/.virtualenvs 目录下管理,可修改环境变量 WORKON_HOME 来指定保存目录。

可将启动指令及场景变量于 ~/.bashrc 或是 ~/.profie 等 shell 启动配置文件中进行设定,便于登陆后可直接使用相关指令。

  1. # 将相关配置内容配置给根用户:
  2. [root@PBSFINNGINX02 ~]# vim .bashrc
  3. #
  4. export VIRTUALENV_USE_DISTRIBUTE=1 # <-- Always use pip/distribute
  5. export WORKON_HOME=$HOME/.virtualenvs # <-- Where all virtualenvs will be stored
  6. source /usr/local/python3/bin/virtualenvwrapper.sh
  7. export PIP_VIRTUALENV_BASE=$WORKON_HOME
  8. export PIP_RESPECT_VIRTUALENV=true

创建虚拟场景:

  1. [root@svn-test ~]# mkvirtualenv env-test
  2. Using base prefix '/usr/local/python3'
  3. New python executable in /root/.virtualenvs/env-test/bin/python3
  4. Also creating executable in /root/.virtualenvs/env-test/bin/python
  5. Installing setuptools, pip, wheel...done.
  6. virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/predeactivate
  7. virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/postdeactivate
  8. virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/preactivate
  9. virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/postactivate
  10. virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/get_env_details
  11. (env-test) [root@svn-test ~]# # 安装完成后自动进入虚拟场景中。

退出虚拟场景:

  1. (env-test) [root@svn-test ~]# deactivate

再次启动虚拟场景:

  1. [root@svn-test ~]# workon env-test

列出已有虚拟场景:

  1. [root@svn-test ~]# workon
  2. cdmonkey
  3. env-test

移除指定虚拟场景:

  1. [root@svn-test ~]# rmvirtualenv env-test
  2. Removing env-test...

指定 Python 版本:

  1. # 需要使用 -p 参数:
  2. [root@PBSFINNGINX02 ~]# mkvirtualenv -p python3 JR-Nginx

Install Django

我们要安装 Django 及其他依赖包。

  1. # Import requirements.txt:
  2. (JR-Nginx) [root@PBSFINNGINX02 ~]# pip3 install -r requirements.txt
  3. # Where is Django
  4. /usr/local/envdjango/lib/python3.6/site-packages/django

创建项目:

  1. (envdjango) [root@testnginx01 ~]# cd /usr/local/envdjango/
  2. (envdjango) [root@testnginx01 envdjango]# mkdir Projects
  3. (envdjango) [root@testnginx01 envdjango]# cd Projects/
  4. # Create Project
  5. (envdjango) [root@testnginx01 Projects]# django-admin startproject cdmonkey

创建完成后可运行服务,进行测试:

  1. (envdjango) [root@testnginx01 Projects]# cd cdmonkey/
  2. (envdjango) [root@testnginx01 cdmonkey]# python manage.py runserver 172.16.136.171:9000

Install uWSGI

仍旧于虚拟环境中进行安装:

  1. (envdjango) [root@testnginx01 local]# pip3 install uwsgi
  2. # Test whether the installation was successful
  3. (JR-Nginx) [root@PBSFINNGINX02 ~]# uwsgi --http :8000 --module muxue.wsgi
  4. # 若是安装成功,会显示许多相关信息。
  5. # 下面这个指令作用似乎同上,目前没发现有啥不同。
  6. (JR-Nginx) [root@PBSFINNGINX02 ~]# uwsgi --http 9000 --module librepath.wsgi

muxue.wsgi 是指 Django 应用根目录下 muxue 文件夹下 wsgi.py 文件,这样就能直接把项目启动起来。

若是编译安装过高版本 OpenSSL,则需要使用这个指令安装:

  1. CFLAGS="-I/usr/local/openssl/include" LDFLAGS="-L/usr/local/openssl/lib" UWSGI_PROFILE_OVERRIDE=ssl=true pip3 install uwsgi

还可使用下列办法进行检测。需要新创建一个 test.py 文件:

  1. # test.py
  2. def application(env, start_response):
  3. start_response('200 OK', [('Content-Type','text/html')])
  4. return [b"Hello World"] # python3

而后执行下面这个指令:

  1. uwsgi --http :9000 --plugin python --wsgi-file test.py

如果能够正常显示“Hello World”则说明 uwsgi 正常运行。

Image
hello2.png-231.4kB                              

接下来就是连接 Django & uwsgi,实施简单之 web 服务器。

  1. # 注意:需要于Django项目目录下执行下面指令
  2. uwsgi --http :9000 --plugin python --module cdmonkey.wsgi

注意这时项目之静态文件是不会被加载的,需要用Nginx对静态文件进行代理。

Config Nginx

配置完成后可进行检测。

  1. (envdjango) [root@testnginx01 envdjango]# uwsgi --socket :9000 --wsgi-file test.py

然后就可正常访问了,这时浏览器是通过 Nginx 服务器进行访问。

Use Unix sockets

使用 TCP 端口套接字虽然简单,但是最好使用 Unix sockets 而不是端口套接字。

  1. # django_vbill.conf
  2. server unix:///tmp/cdmonkey.sock;

重新运行 uWSGI:

  1. uwsgi --socket /tmp/cdmonkey.sock --wsgi-file test.py

如果不能正常访问,请查看 Nginx 错误日志,通常不是 socket 文件路径有问题就是该文件权限有问题。

如果是权限问题:

  1. uwsgi --socket /tmp/cdmonkey.sock --wsgi-file test.py --chmod-socket=666

Config uWSGI

首先需要于Django项目目录下新建 uwsgi.ini 文件。

  1. [uwsgi]
  2. uid = nginx
  3. gid = nginx
  4. home = /usr/local/envdjango
  5. socket = /tmp/cdmonkey.sock
  6. chmod-socket = 666
  7. chdir = /usr/local/envdjango/Projects/cdmonkey
  8. module = cdmonkey.wsgi
  9. master = true
  10. # maximum number of worker processes
  11. processes = 2
  12. # clear environment on exit
  13. vacuum = true

使用这个文件运行 uWSGI:

  1. # 需要于项目目录中运行指令:
  2. [root@testnginx01 cdmonkey]# uwsgi --ini uwsgi.ini
  3. # 后台运行:
  4. nohup uwsgi --ini uwsgi.ini --logto cdmonkey.log &

配置静态文件


错误排查

使用 pip 进行安装操作时,报 SSL 模块不可用。

  1. pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

首先要保证 openssl-devel 这个包必须要安装。其次,因为当前系统升级过 openssl,因而相关 include 目录已改变,需要重新配置。配置过程如下:

  1. # 首先解压源码,进入目录:
  2. [root@PBSFINNGINX02 tools]# cd Python-3.6.5
  3. [root@PBSFINNGINX02 Python-3.6.5]# vim setup.py
  4. # 找到这一部分,于这个列表中追加一行:
  5. search_for_ssl_incs_in = [
  6. '/usr/local/ssl/include',
  7. '/usr/contrib/ssl/include/',
  8. '/usr/local/openssl/include/' # <--
  9. ]

修改完后,先进行配置操作:

  1. ./configure --prefix=/usr/local/python3

操作完成后再次进行修改操作:

  1. [root@PBSFINNGINX02 Python-3.6.5]# vim Modules/Setup
  2. # 定位至这里,并按照下面进行修改,就是去掉注释并修改路径:
  3. # Socket module helper for socket(2)
  4. _socket socketmodule.c
  5. # Socket module helper for SSL support; you must comment out the other
  6. # socket line above, and possibly edit the SSL variable:
  7. SSL=/usr/local/openssl
  8. _ssl _ssl.c \
  9. -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  10. -L$(SSL)/lib -lssl -lcrypto

最后进行安装操作:

  1. make make install

若不出意外,就可正常使用了,目前还没有啥问题。

https://zhuanlan.zhihu.com/p/31536379

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注