鬼骨孖 | xD dOt cOM | 三人行 | web3nomad.eth

Share this post

在 GoDaddy Deluxe Linux 环境上部署 Django

blog.web3nomad.com
BUIDL

在 GoDaddy Deluxe Linux 环境上部署 Django

xD dOt cOM
Feb 5, 2013
Share this post

在 GoDaddy Deluxe Linux 环境上部署 Django

blog.web3nomad.com

GoDaddy的Linux环境是支持python的,但是默认版本是2.4.3

[bash] -bash-3.2$ python -V -bash-3.2$ Python 2.4.3 [/bash]

不过,Deluxe版本的服务器是支持2.7的,GoDaddy给出了使用方法两个方案: http://support.godaddy.com/help/article/7254/can-i-use-python-272-with-my-hosting-account 1). 直接引用python2.7的安装路径:#!/usr/local/bin/python2.7 2). 用virtualenv虚拟python2.7的环境 后者使用起来更加自如些。

所以就先介绍一下虚拟环境的安装。因为GoDaddy直接可以使用virtualenv,这方便了不少。只是不能调用编译器,比如gcc,所以如果安装MySQLdb这些需要编译C++源码的库,就要现在本地编译好,这里不扯开去了,感兴趣的可以搜索Google。

文中虚拟环境安装在 ~/py27venv/ Django 项目放在 ~/html/

1. 安装虚拟环境

创建虚拟环境

[bash] -bash-3.2$ cd ~ -bash-3.2$ virtualenv -p /usr/local/bin/python2.7 py27venv [/bash]

激活后, -bash-3.2$前出现 (py27venv)

[bash] -bash-3.2$ source ~/py27venv/bin/activate (py27venv)-bash-3.2$ [/bash]

这时检查python版本,变成了2.7.3

[bash] (py27venv)-bash-3.2$ python -V python 2.7.3 [/bash]

2. 安装Django

通常 python package 的目录是 ~/py27venv/lib/python2.7/site-packages

[bash] (py27venv)-bash-3.2$ cd ~/py27venv/lib/python2.7/site-packages (py27venv)-bash-3.2$ pip install Django [/bash]

3. 配置 Django

Django 安装完以后要先在设置下本地化信息, 不然在 syncdb 的时候无法新建 superuser 会报错

最简单的做法就是把以下两行写入 ~/.bash_profile 需要的时候加载

[bash] export LANG=en_US.UTF-8 export LC_ALL=en_US.UTF-8 [/bash]

然后关闭 python 虚拟环境,执行 syncdb (这个也可以不急着做,总之做之前一定要载入本地化信息)

[bash] (py27venv)-bash-3.2$ deactivate -bash-3.2$ source ~/.bash_profile -bash-3.2$ cd ~/html -bash-3.2$ python2.7 manage.py syncdb [/bash]

4. 使用 fastcgi 运行 Django

接下来使用 flup 来将 Django 部署在 fastcgi 上,这样就不需要通过 Django 来运行服务器了。 详细手册参阅 https://docs.djangoproject.com/en/1.4/howto/deployment/fastcgi

[bash] -bash-3.2$ source ~/py27venv/bin/activate (py27venv)-bash-3.2$ (py27venv)-bash-3.2$ wget 'http://pypi.python.org/packages/source/f/flup/flup-1.0.2.tar.gz#md5=24dad7edc5ada31dddd49456ee8d5254' (py27venv)-bash-3.2$ tar -xvzf flup-1.0.2.tar.gz (py27venv)-bash-3.2$ mv flup-1.0.2/flup/ . (py27venv)-bash-3.2$ rm flup-1.0.2.tar.gz (py27venv)-bash-3.2$ rm flup-1.0.2/ -r (py27venv)-bash-3.2$ deactivate [/bash]

5. 配置 Django 运行环境

在 ~/html 目录下, 建立 fastcgi 运行脚本 dispatch.py 并写入如下代码:

[python] #!/usr/local/bin/python2.7 ''' This is a comment, If you see this, CGI is not working ''' import sys, os sys.path += ['{absloute home path}/py27venv/lib/python2.7/site-packages'] sys.path += ['{absloute home path}/html/questhunter/'] os.environ['DJANGO_SETTINGS_MODULE'] = 'questhunter.settings' from flup.server.fcgi import WSGIServer from django.core.handlers.wsgi import WSGIHandler WSGIServer(WSGIHandler()).run() [/python]

记住用unix模式存储,并且赋予执行权限,比如 755 {absloute home path} 是你用户根目录在服务器上的绝对路径,比如我的是 /home/content/98/10206198/ 代码中都要使用绝对路径,因为 python 运行时环境和 bash 是不一样的

修改 Django 的 manage.py 文件,指定 Django 的位置

[python] sys.path += ['{absloute home path}/py27venv/lib/python2.7/site-packages'] [/python]

6. 最后,配置 Apache

新建 ~/html/.htaccess 并写入:

[text] AddHandler fcgi-script .fcgi AddHandler cgi-script cgi Options ExecCGI Options +FollowSymLinks RewriteEngine On RewriteBase /questhunter # folders with php & static content RewriteRule ^(stats/.*)$ - [L] RewriteRule ^(blog/.*)$ - [L] RewriteRule ^(favicon.ico)$ - [L] RewriteRule ^(favicon.gif)$ - [L] # everything else sent to django RewriteRule ^(dispatch\.py/.*)$ - [L] RewriteRule ^(.*)$ dispatch.py/$1 [L] [/text]

完毕。

Share this post

在 GoDaddy Deluxe Linux 环境上部署 Django

blog.web3nomad.com
Comments
TopNew

No posts

Ready for more?

© 2023 web3nomad.eth
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing