Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 309
miki725 Avatar asked Aug 17 '11 15:08

miki725


People also ask

Can we deploy Django on shared hosting?

Django is an open-source python web framework. At the moment, Django will not work with our Shared or Cloud web hosting plans, as Python is also not supported.

How static files are served in Django?

django.contrib.staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily. This will copy all files from your static folders into the STATIC_ROOT directory. Use a web server of your choice to serve the files.

Can Django serve static files in production?

During development, as long as you have DEBUG set to TRUE and you're using the staticfiles app, you can serve up static files using Django's development server. You don't even need to run the collecstatic command.


1 Answers

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.

like image 177
koniiiik Avatar answered Sep 20 '22 16:09

koniiiik