Host Django site on windows on lan - django

I have a site that I want to expose to a bunch of colleagues that serves as an interface for some Machine learning tools.
I’ve made the site in Django 2.0 and would like to serve it from a small windows PC under my desk, and then from a more dedicated server once it’s operational.
I’ve had a look around and it looks like my options are using uWSGI or Django it self to serve the site. Obviously Django is mich slower, but on a PC with an i5 i recon it should be able to handle a couple of requests a minute, which is the peak traffic I’m expecting.
FastCGI appears to be depreciated, so what other options, prioritizing ease of confit on my part are there?

So anyone in a similar situation:
I ended up using waitress as the server and whitenoise to serve the static files.
This appears to work sufficiently fast and requires little configuration, once you have have worked out how to get the static files going with whitenoise.

Django itself can serve pages to other machines on your network. SImply start the local server with this command
python manage.py runserver 0.0.0.0:8000
Then, you can access the local website by requesting http://<dev_machine_ip>:8000. Obviously, this is a very simple and inefficient solution if you want to provide this website to many users connected all at the same time. But for tests/demo purpose, it is very handy

Related

Do I Really Need to Use A Server Application for Django?

To this point, I just created and played my Django server in my localhost, like setting up a basic server Linux distro on my another device and testing etc.
However, I also heard of server applications like apache2 or nginx. The thing I wonder about is: Do I really need to use one of them in production? I want to buy (or rent?) a VPS service, then deploy (or publish?) my project on that server. The questions on my head are:
Running server with manage.py runserver 0.0.0.0:80 means it does not make my application worldwide? An server application (or whatever it is) makes it accessible outside?
Or a server application is simply needed for better performance, optimization etc. ?
Simply, why do I need to use apache2 or nginx to deploy my project?
It's a long story. In few words:
Running your project on localhost surely wont make it worldwide accessible, since at least you need a public address for your server, but not a local one.
Speaking honestly it is not a problem to run a site in pro using django built-in server. But, as you can read in docs, it is strongly NOT recommended. Why? Because it was developed specially for testing. It is written in python (slow enough for web server) and not suitable for handling multiple queries to the server and it is only a matter of time when it will crash. Of course, there are plenty of other reasons like cache and access settings, redirects and others.

Django runserver & Firefox - caching conflict with multiple local sites running separately on same port?

I'm working on several Django projects on my local machine, following a single page application architecture. To initiate the server, I have a couple copies of a script in my /bin folder containing
#!/bin/bash
python /path/to/app/manage.py runserver 8080
and have each script with the app name. This makes the application accessible via localhost:8080. In addition, I usually have the majority of my site CSS inside main.css
My issue is that I seem to be coming across a caching issue with Firefox, regardless of which application server is running. Sometimes a page will load with almost no CSS styling, but the jQuery UI elements will be initialized and I can interact somewhat with the application, although the functionality and styling is seriously broken. Refreshing the page shows no improvement, and no errors are shown in the console.
Clearing the cache and changing the port in the scrip seem to solve the issue, but it requires me to have bookmarks for each project, whereas it is pretty convenient to have a single localhost:8080 URL for all projects.
Has anyone come across this issue, and is there a solution other than clearing cache and changing ports?
This thread discusses methods to prevent client side caching of content served by the development server in Django:
Fighting client-side caching in Django
I prefer to simply disable caching in my browser though, seeing that I spend so much time on developing that I don't want to bother with the hassle of trying to prevent it in my own code.
A simple web search for "how to disable caching in firefox" came up with this:
http://support.mozilla.org/en-US/questions/764993
I'm pretty sure that searching for the same thing for different browsers will also give you expected results.
EDIT:
These guys also seem to go pretty in depth about how to prevent the caching of static files when using the Django development server.
Turn off caching of static files in Django development server
Just add something like this to /etc/hosts:
127.0.0.1 site1.dev
127.0.0.1 site2.dev
Visit site1.dev:8080, now site1 has its own cache and cookies (session) in the browser.

Development server & production server

What is the right way to handle a production and development website on the same server? the development code shouldn't be available until it's used for production.
I'm using Apache and Django and VPS hosting.
What should I configure? Apache- so it will have a special prefix for development stuff, Django- and have some URL mangling in the urlconf, or just get another VPS for development?
Find an old computer and stick it in your basement. you really don't need tons of horsepower for a dev machine & should be able to do it for a couple hundred bucks.
The problem with developing on a production machine is that you could crash processes [apache?] with some 'not quite debugged yet' code and affect live services, even if you have configured separate subdomains or virtual hosts.
never never never develop live.
-sean
PS> another VPS is a workable solution if 'spare hardware' is not available. However you could have availability issues.

Deploying first Django project

I run a small VPS with 512M memory of memory that currently hosts 3 very low traffic PHP sites and a personal email account.
I have been teaching myself Django over the last few weeks and am starting to think about deploying a project.
There seem to be a very large number of methods for deploying a Django site. Given the limited resources I have available, what would be the most appropriate option?
Will the VPS be suitable to host both python and PHP sites or would it be worth getting a separate server?
Any advice appreciated.
Thanks.
There aren't really a great number of ways to do it. In fact, there's the recommended way - via Apache/mod_wsgi - and all the other ways. The recommended way is fully documented here.
For a low-traffic site, you should have no trouble fitting it in your 512MB VPS along with your PHP sites.
Django has documentation describing possible server arrangements. For light weight, yet very robust set up, I'd recommend Nginx setup. It's much lighter than Apache.
I run several low-traffic Django sites on a 256 VPS without problem. I have Nginx setup as a reverse proxy and to serve static files (javascript, CSS, images) and Apache using mod_wsgi for serving Django as described in the documentation.
Running PHP sites as well may add a little overhead, but, if you're talking about low-traffic "fun" sites then you should be fine.

Using Django's built in web server in a production environment

I'm going to setup a simple Django app running in a production environment on a Linux box. The app will have very little traffic - less that 100 page loads per day. Is it okay to use the builtin Django webserver for this or should I install Apache and mod_wsgi? If so, what are the reasons for this? Security perhaps?
UPDATE
OK it is clear I shouldn't be using the builtin server. Some of the alternatives to Apache look interesting. Is there one that is more popular/more frequently used with Django perhaps?
Is it okay to use the builtin Django webserver for this
No.
Should I install Apache and mod_wsgi?
Yes.
If so, what are the reasons for this? Security perhaps?
Partly.
More importantly, the little toy Django server is single-threaded and any hangup in your code hangs the server. This means that when two users click almost at the same time, user one's query must go all the way through Django before user two's query can even starts.
And this will have to include the insanely slow download speed to the desktop.
Apache (like all the alternatives, lighttpd or nginx) is multi-threaded. The slowest part of the transaction is the download from Apache to the desktop. You don't want Python code (and Django) handling this in a single-threaded manner. Even for just a few users.
Also, you don't what Django serving static media (i.e., CSS and JS library files.)
A single slow spot in your application won't effect the overall system throughput if Apache and mod_wsgi are in place. One request 's output page can be slowly downloading to a PC desktop in parallel with another user's output.
DO NOT USE THIS (the builtin Django webserver) SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.
http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver-port-or-address-port
But you don't have use Apache if you don't want to. You could directly use Spawning, Gunicorn etc.
Cherokee is also easy to setup.
Use nginx + gunicorn.
Nginx: five lines of configuration. Gunicorn: two lines of configuration. That's easy and efficient. For better control you can spawn the gunicorn process using supervisord.
Both gunicorn and supervisord are available to install with pip, and nginx is available in almost any distribution in the default package pool.
The built in Django server was not built for production. There are many reasons why, mainly security and efficiency.
The recommended way is to use mod_wsgi which is covered in the docs here