我需要将文件放在哪个目录中?
我尝试了 public_html,但是当我将文件放在那里并单击 setup.py 时,它没有启动脚本。
2 回答
It's probably not the best thing to run Django application on cPanel (shared hosting) because of the following:
- 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.
- 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
Add virtualenv bin folder to the path (inside
.bash_profile
)export PATH="/home/<username>/djangovenv/bin:$PATH" # inside .bash_profile # activate .bash_profile $ source .bash_profile
Then
pip
install everything your project requires. Make sure to activate virtualenv first$ source ~/djangovevn/bin/activate $ pip install django
Configure Django as usual. Make sure
DEBUG
isFalse
Inside
public_html
createindex.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")
Configure the
index.fcgi
inpublic_html/.htaccess
AddHandler fcgid-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
Activate the fastcgi
$ cd ~/public_html $ touch index.fcgi $ chmod 0755 .htaccess $ chmod 0755 index.fcgi
All Done!
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.
Django 是一个在 Python 实例中运行的框架,而不是作为一组从 Web 服务器提供的文件,如 HTML 或 PHP。您很可能需要另一个托管服务提供商来托管像Heroku这样的 Web应用程序。