开发者

Django - How to setup separate media/static server on shared hosting?

I would like to get a virtual hosting for my django application, however storage space is rather expensive on virtual-hosting, so I would like to use a shared hosting provider to store the media/static content of the web site since it is a lot cheaper. How can I get this done?

Thank you.

note

For my application I need to run specialized libraries (e.g. matplotlib) which cannot be installed on shared-hosting, therefore I will not be able to run Django on shared-hosting using FastCGI.

edit

To clarify the question, I will need to be able to upload in django and store the content on the shared hosting. Here is a quote from a django book from the section Running a Separate Media Server (link):

This step can be slightly tricky, however. If your application involves file uploads, Django needs to be able开发者_开发技巧 to write uploaded media to the media server. If media lives on another server, you’ll need to arrange a way for that write to happen across the network.

But there is no details on how this can be achieved.


Nothing is preventing you from pointing your URLs at a different host in your (X)HTML output.

If your shared hosting account sits at, for example, http://awesome-hosting-provider.com/~myname/, you can set MEDIA_URL in your settings.py to this value, make sure to use RequestContext when rendering and to have django.core.context_processors.media included in TEMPLATE_CONTEXT_PROCESSORS (note it is in there by default) and then you can just use {{ MEDIA_URL }} to prefix your URLs in templates.

Even the admin will work with this, as long as you copy all its required static files over to the shared environment to make them accessible.

Since you also need to take care of file uploads, as the book says, you'll have to make it possible to write to the storage location (on the shared server) from your Django deployment. There are several ways of achieving this.

The easiest would probably be to mount the top-level directory of your storage environment on your virtual server using SSHFS with the right options (like reconnect, reasonable timeouts etc.). This requires SSH access to the shared environment, of course. To make things easier, setting up a SSH authentication key pair might come in handy.

Alternately, you can try NFS if your shared hosting provider allows you to do that, although I find it improbable to say the least.

If everything else fails, you'll have to set up a webservice of some kind on your shared environment that would listen to uploads from your Django deployment and make the Django app forward them. In this case, however, hou'll have to make sure it is safe enough and no malicious user will be able to compromise your storage server by directly talking to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜