如何在cpanel上安装django [英] how to install django on cpanel
问题描述
我需要在哪个目录中放置文件?
我尝试了public_html,但是当我将文件放置在其中并单击setup.py时,它没有启动脚本。
What directory do I need to put the files in?
I tried public_html but when I put the files there and clicked on setup.py, it didn't start the script.
推荐答案
由于以下原因,在cPanel(共享主机)上运行Django应用程序可能不是最好的选择:
It's probably not the best thing to run Django application on cPanel (shared hosting) because of the following:
- 大多数共享托管提供程序不允许您安装需要编译的自定义库。不过,您仍然可以创建 virtualenv 和
pip
只要不需要编译任何东西就可以安装软件包(例如Django) - Performance。以我的经验,可以在共享主机上部署一个简单的Django应用程序,但是它不是很可靠,并且性能也不太好。
- Most shared hosting providers do not allow you to install custom libraries which need to be compiled. You can still however create virtualenv and
pip
install packages into as long as they do not require anything to be compiled (e.g. Django) - Performance. In my experience it is possible to deploy a simple Django application on shared hosting however it is not very reliable and does not perform very well.
但是,并不是说不可能。这些粗糙的步骤应足够准确,以引导您走上正确的道路。我有一段时间没有做过,所以可能会有错误。
However it is not to say that it is impossible. These rough steps should be accurate enough to guide you to the right path. I haven't done this is a while so there might be mistakes.
- 首先,您必须具有SSH访问权限
-
登录到您的帐户并为Django项目创建virtualenv
- First you have to have SSH access
Login into your account and create virtualenv for your django project
$ cd ~
$ wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz
$ tar -zxvf virtualenv-1.9.1.tar.gz
$ python virtualenv-1.9.1/virtualenv.py djangovevn
将virtualenv bin文件夹添加到路径(在 .bash_profile
内部)
export PATH="/home/<username>/djangovenv/bin:$PATH" # inside .bash_profile
# activate .bash_profile
$ source .bash_profile
然后 pip
安装项目所需的一切。确保先激活virtualenv
Then pip
install everything your project requires. Make sure to activate virtualenv first
$ source ~/djangovevn/bin/activate
$ pip install django
像往常一样配置Django。确保 DEBUG
为 False
内部 public_html
创建 index.fcgi
。确保使用virtualenv Python路径。关于此此处。
Inside public_html
create index.fcgi
. Make sure to use the virtualenv Python path. Django docs about this here.
!/home/<username>/djangovenv/bin/python
import sys, os
# add projects directory to the path so that
# settings from the project can be imported
sys.path.insert(0, "/home/<username>/path/to/project")
# Switch to the directory of your project #
os.chdir("/home/<username>/path/to/project")
# Set the DJANGO_SETTINGS_MODULE environment variable #
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
# Run the fastcgi instance #
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
在 public_html / .htaccess中配置
index.fcgi
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
激活fastcgi
Activate the fastcgi
$ cd ~/public_html
$ touch index.fcgi
$ chmod 0755 .htaccess
$ chmod 0755 index.fcgi
全部完成!
All Done!
但是此方法会快把你逼疯了。 Apache并非用于此目的,并且由于某种原因该方法不受欢迎。刚开始时这应该足够好,但是随着您将更高级地部署Django应用,您应该考虑使用其他托管服务提供商,例如WebFaction或heroku。
However this method will drive you crazy very fast. Apache is not meant for this and this method is not popular for a reason. This should be good enough in the beginning however as you will get more advanced deploying Django app, you should consider using some other hosting provider which allows for more flexibility such as WebFaction or heroku.
这篇关于如何在cpanel上安装django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!