@cdmonkey
2019-08-06T01:42:22.000000Z
字数 7097
阅读 983
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
首先安装必要依赖包:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite*
更换 pip 源:
[root@svn-test ~]# mkdir ~/.pip[root@svn-test ~]# vim ~/.pip/pip.conf[global]index-url=https://pypi.tuna.tsinghua.edu.cn/simpletrusted-host=pypi.tuna.tsinghua.edu.cn
还有就是把工程开发依赖包导出:
# Executed in the current project Pycharm terminal window:pip3 freeze > /Users/cdmonkey/Desktop/requirements.txt
创建虚拟场景。
https://blog.qikqiak.com/post/python-virtualenv-all-know
[root@testnginx01 tools]# tar zxvf Python-3.6.5.tgz[root@testnginx01 tools]# cd Python-3.6.5[root@testnginx01 Python-3.6.5]# ./configure --prefix=/usr/local/python3[root@testnginx01 Python-3.6.5]# make && make install[root@testnginx01 Python-3.6.5]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
[root@testnginx01 ~]# python3 -m pip install virtualenv# 创建虚拟环境,注意要选择好创建位置:[root@testnginx01 ~]# cd /usr/local/[root@testnginx01 local]# /usr/local/python3/bin/virtualenv envdjangoUsing base prefix '/usr/local/python3'New python executable in /usr/local/envdjango/bin/python3Also creating executable in /usr/local/envdjango/bin/pythonInstalling setuptools, pip, wheel...done.
创建完成后,用下面命令即可激活指定虚拟环境:
[root@testnginx01 local]# source envdjango/bin/activate(envdjango) [root@testnginx01 local]# # 这就表示已经进入了虚拟环境。(envdjango) [root@testnginx01 local]# python -VPython 3.6.5# 退出虚拟环境:(envdjango) [root@testnginx01 local]# deactivate
这是一个基于 virtualenv 之上的工具,它将所有虚拟场景进行统一管理。
# 首先进行安装:[root@svn-test ~]# python3 -m pip install virtualenvwrapper
可使用这个命令启动 virtualenvwrapper:
[root@svn-test ~]# source /usr/local/python3/bin/virtualenvwrapper.sh
注意:我们这里是使用的编译安装的 python3.6,因而需要对这个文件进行相应修改:
# 首先进行备份:[root@PBSFINNGINX02 ~]# cd /usr/local/python3/bin/[root@PBSFINNGINX02 bin]# cp virtualenvwrapper.sh virtualenvwrapper.sh.ori[root@PBSFINNGINX02 bin]# vim virtualenvwrapper.sh# Locate the global Python where virtualenvwrapper is installed.if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]thenVIRTUALENVWRAPPER_PYTHON="/usr/bin/python3"fi# Set the name of the virtualenv app to use.if [ "${VIRTUALENVWRAPPER_VIRTUALENV:-}" = "" ]thenVIRTUALENVWRAPPER_VIRTUALENV="/usr/local/python3/bin/virtualenv"fi# Set the name of the virtualenv-clone app to use.if [ "${VIRTUALENVWRAPPER_VIRTUALENV_CLONE:-}" = "" ]thenVIRTUALENVWRAPPER_VIRTUALENV_CLONE="/usr/local/python3/bin/virtualenv-clone"fi
缺省会将所有虚拟场景放置于 ~/.virtualenvs 目录下管理,可修改环境变量 WORKON_HOME 来指定保存目录。
可将启动指令及场景变量于 ~/.bashrc 或是 ~/.profie 等 shell 启动配置文件中进行设定,便于登陆后可直接使用相关指令。
# 将相关配置内容配置给根用户:[root@PBSFINNGINX02 ~]# vim .bashrc#export VIRTUALENV_USE_DISTRIBUTE=1 # <-- Always use pip/distributeexport WORKON_HOME=$HOME/.virtualenvs # <-- Where all virtualenvs will be storedsource /usr/local/python3/bin/virtualenvwrapper.shexport PIP_VIRTUALENV_BASE=$WORKON_HOMEexport PIP_RESPECT_VIRTUALENV=true
创建虚拟场景:
[root@svn-test ~]# mkvirtualenv env-testUsing base prefix '/usr/local/python3'New python executable in /root/.virtualenvs/env-test/bin/python3Also creating executable in /root/.virtualenvs/env-test/bin/pythonInstalling setuptools, pip, wheel...done.virtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/predeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/postdeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/preactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/postactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env-test/bin/get_env_details(env-test) [root@svn-test ~]# # 安装完成后自动进入虚拟场景中。
退出虚拟场景:
(env-test) [root@svn-test ~]# deactivate
再次启动虚拟场景:
[root@svn-test ~]# workon env-test
列出已有虚拟场景:
[root@svn-test ~]# workoncdmonkeyenv-test
移除指定虚拟场景:
[root@svn-test ~]# rmvirtualenv env-testRemoving env-test...
指定 Python 版本:
# 需要使用 -p 参数:[root@PBSFINNGINX02 ~]# mkvirtualenv -p python3 JR-Nginx
我们要安装 Django 及其他依赖包。
# Import requirements.txt:(JR-Nginx) [root@PBSFINNGINX02 ~]# pip3 install -r requirements.txt# Where is Django/usr/local/envdjango/lib/python3.6/site-packages/django
创建项目:
(envdjango) [root@testnginx01 ~]# cd /usr/local/envdjango/(envdjango) [root@testnginx01 envdjango]# mkdir Projects(envdjango) [root@testnginx01 envdjango]# cd Projects/# Create Project(envdjango) [root@testnginx01 Projects]# django-admin startproject cdmonkey
创建完成后可运行服务,进行测试:
(envdjango) [root@testnginx01 Projects]# cd cdmonkey/(envdjango) [root@testnginx01 cdmonkey]# python manage.py runserver 172.16.136.171:9000
仍旧于虚拟环境中进行安装:
(envdjango) [root@testnginx01 local]# pip3 install uwsgi# Test whether the installation was successful(JR-Nginx) [root@PBSFINNGINX02 ~]# uwsgi --http :8000 --module muxue.wsgi# 若是安装成功,会显示许多相关信息。# 下面这个指令作用似乎同上,目前没发现有啥不同。(JR-Nginx) [root@PBSFINNGINX02 ~]# uwsgi --http 9000 --module librepath.wsgi
muxue.wsgi是指 Django 应用根目录下muxue文件夹下wsgi.py文件,这样就能直接把项目启动起来。
若是编译安装过高版本 OpenSSL,则需要使用这个指令安装:
CFLAGS="-I/usr/local/openssl/include" LDFLAGS="-L/usr/local/openssl/lib" UWSGI_PROFILE_OVERRIDE=ssl=true pip3 install uwsgi
还可使用下列办法进行检测。需要新创建一个 test.py 文件:
# test.pydef application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3
而后执行下面这个指令:
uwsgi --http :9000 --plugin python --wsgi-file test.py
如果能够正常显示“Hello World”则说明 uwsgi 正常运行。
| Image | |
|---|---|
![]() |
接下来就是连接 Django & uwsgi,实施简单之 web 服务器。
# 注意:需要于Django项目目录下执行下面指令uwsgi --http :9000 --plugin python --module cdmonkey.wsgi
注意这时项目之静态文件是不会被加载的,需要用Nginx对静态文件进行代理。
配置完成后可进行检测。
(envdjango) [root@testnginx01 envdjango]# uwsgi --socket :9000 --wsgi-file test.py
然后就可正常访问了,这时浏览器是通过 Nginx 服务器进行访问。
使用 TCP 端口套接字虽然简单,但是最好使用 Unix sockets 而不是端口套接字。
# django_vbill.confserver unix:///tmp/cdmonkey.sock;
重新运行 uWSGI:
uwsgi --socket /tmp/cdmonkey.sock --wsgi-file test.py
如果不能正常访问,请查看 Nginx 错误日志,通常不是
socket文件路径有问题就是该文件权限有问题。
如果是权限问题:
uwsgi --socket /tmp/cdmonkey.sock --wsgi-file test.py --chmod-socket=666
首先需要于Django项目目录下新建 uwsgi.ini 文件。
[uwsgi]uid = nginxgid = nginxhome = /usr/local/envdjangosocket = /tmp/cdmonkey.sockchmod-socket = 666chdir = /usr/local/envdjango/Projects/cdmonkeymodule = cdmonkey.wsgimaster = true# maximum number of worker processesprocesses = 2# clear environment on exitvacuum = true
使用这个文件运行 uWSGI:
# 需要于项目目录中运行指令:[root@testnginx01 cdmonkey]# uwsgi --ini uwsgi.ini# 后台运行:nohup uwsgi --ini uwsgi.ini --logto cdmonkey.log &
使用 pip 进行安装操作时,报 SSL 模块不可用。
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available
首先要保证 openssl-devel 这个包必须要安装。其次,因为当前系统升级过 openssl,因而相关 include 目录已改变,需要重新配置。配置过程如下:
# 首先解压源码,进入目录:[root@PBSFINNGINX02 tools]# cd Python-3.6.5[root@PBSFINNGINX02 Python-3.6.5]# vim setup.py# 找到这一部分,于这个列表中追加一行:search_for_ssl_incs_in = ['/usr/local/ssl/include','/usr/contrib/ssl/include/','/usr/local/openssl/include/' # <--]
修改完后,先进行配置操作:
./configure --prefix=/usr/local/python3
操作完成后再次进行修改操作:
[root@PBSFINNGINX02 Python-3.6.5]# vim Modules/Setup# 定位至这里,并按照下面进行修改,就是去掉注释并修改路径:# Socket module helper for socket(2)_socket socketmodule.c# Socket module helper for SSL support; you must comment out the other# socket line above, and possibly edit the SSL variable:SSL=/usr/local/openssl_ssl _ssl.c \-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \-L$(SSL)/lib -lssl -lcrypto
最后进行安装操作:
make make install
若不出意外,就可正常使用了,目前还没有啥问题。