How to setup django with lighttpd? - django

I have a live website that is running under lighttpd. I now have a sub-domain that I want to run under django. I do not want to move to Apache as I don't currently have the resources to support two production web servers. The django part is actually working well with the django builtin development server so now I just need to port it into production.
It seems that all documentation to setup django with lighttpd is over 10 years old and uses fastcgi that from what I can see was deprecated and removed from django. The new docs point to use wscgi - scgi under lighttpd. But even that is being hard to get any documentation.
Do I need to run uwsgi and use that to serve the pages to mod_scgi? Or should I use gunicorn? I am sort of at a loss on what to do.
Suggestions welcome!

lighttpd supports uwsgi with mod_scgi and scgi.protocol = "uwsgi"
scgi.protocol = "uwsgi"
scgi.server = ( "/" =>
(
(
"socket" => "/tmp/scgi.sock",
"check-local" => "disable",
"fix-root-scriptname" => "enable"
)
)
)
Using mod_scgi and the uwsgi protocol to communicate with the Django server will likely be faster and use fewer resources than using gunicorn (and communicating using lighttpd mod_proxy)
How to use Django with uWSGI
The uWSGI docs linked there recommend configuring the web server to serve static files rather than sending those requests to Django.

There are a few different options you can consider for serving a Django application under Lighttpd. One popular option is to use mod_proxy to forward requests to a separate process running your Django application, such as Gunicorn or uWSGI.
Gunicorn can be easily integrated with Lighttpd using mod_proxy. You can run Gunicorn in the background as a daemon, and configure Lighttpd to forward requests to it using mod_proxy
uWSGI can also be integrated with Lighttpd using mod_proxy and the main advantage of uWSGI over Gunicorn is its scalability and performance, but it can be more complex to set up.
And if you are new to this, I would recommend trying to set up Gunicorn first as it is the simpler option, and then moving on to uWSGI if you need more performance!
UPDATE:
As you mentioned in the comment, you want to use mod_fastcgi to connect Lighttpd to your Django application, so you will need to start Django using the FastCGI process manager provided by flup.
How to start your Django app using flup:
#!/bin/bash
# activate your virtual environment
source /path/to/venv/bin/activate
# Start the FastCGI process manager
python -m flup.server.fcgi -d -m django.core.servers.fastcgi
You will also need to configure Lighttpd to use mod_fastcgi and point it to the socket created by flup in order to connect to the Django application
fastcgi.server = ( "/" =>
( "localhost" =>
(
"socket" => "/tmp/fcgi.sock",
"check-local" => "disable",
)
)
)

Related

Deploy Django\Tornado on Heroku

I want to deploy some app on Heroku, it's Django with Tornado(Tornadio2) server for implement WebSockets with socket.io protocol.
So, at now it's working fine on my VPS server,
I use Nginx with using location section for routing requests for Django or Tornado.
Nginx config looks like this:
location /socket.io {
# Tornado app
proxy_pass http://localhost:8088;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
...
}
location / {
# Django app
...
}
So, how simple way to routing requests for Django/Tornado on Heroku?
Looks like necessary use custom Buildpack for install Nginx?
Or may be have good way to implement async socket.io in Django to avoid having to route requests?
Let me start with your last question:
Or may be have good way to implement async socket.io in Django to avoid having to route requests?
Django is essentially a library for processing of HTTP requests into appropriate HTTP responses. It does not provide an execution context like uWSGI, Apache mod_wsgi, Tornado, Flask, gunicorn etc. So you can not really use django to serve web sockets; there will always be an execution context around django.
When you deploy a django site to Heroku, it will normally use gunicorn as an execution environment. Performance wise this is not so cool: performance of python servers. Because the poor performance of gunicorn has to do with blocking I/O, some people install nginx as a non-blocking layer in front of gunicorn. This has led to the nginx buildpack.
I don't think this fits your needs. Nginx is an awesome web server, but does not contain a python execution environment. So you end up introducing a third server into your stack:
nginx for HTTP
gunicorn for django
server #3 for the websockets
My best suggestion is to leave gunicorn and nginx and to bring everything together in Tornado: web server, WSGI execution context for django, async context for websockets.
This link shows how to run Tornado on Heroku. And the next link shows how to run django in Tornado.

Visit webpage hosted on ubuntu server in a local network

I have a ubuntu server hosting a web page driven by Python Django, I can access that page by using the following command:elinks http:// 127.0.0.1:8000.
Now if I want to access that same web page on a macbook sharing the same home router with my ubuntu server(local ip: 10.0.0.9), how would I do it? Typing in elinks http:// 10.0.0.9:8000 wouldn't work.
Thanks a lot,
ZZ
Are you running the development server using manage.py?
If so, you should start the server using:
python manage.py runserver 0.0.0.0:8000
This will allow the development server to be visited by ips on all interfaces instead of just localhost.
You need to serve it. There are a number of ways to do this, but my preferred method is to use Nginx as a reverse proxy server for gunicorn. This is a good tutorial for that.

What is the purpose of NGINX and Gunicorn running in parallel?

A lot of Django app deployments over Amazon's EC2 use HTTP servers NGINX and Gunicorn.
I was wondering what they actually do and why both are used in parallel. What is the purpose of running them both in parallel?
They aren't used in parallel. NGINX is a reverse proxy. It's first in line. It accepts incoming connections and decides where they should go next. It also (usually) serves static media such as CSS, JS and images. It can also do other things such as encryption via SSL, caching etc.
Gunicorn is the next layer and is an application server. NGINX sees that the incoming connection is for www.domain.com and knows (via configuration files) that it should pass that connection onto Gunicorn. Gunicorn is a WSGI server which is basically a:
simple and universal interface between web servers and web applications or frameworks
Gunicorn's job is to manage and run the Django instance(s) (similar to using django-admin runserver during development)
The contrast to this setup is to use Apache with the mod_wsgi module. In this situation, the application server is actually a part of Apache, running as a module.
Nginx and Gunicorn are not used in parrallel.
Gunicorn, is a Web Server Gateway Interface (WSGI) server
implementation that is commonly used to run Python web applications.
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
Nginx responsible for serving static content, gzip compression, ssl,
proxy_buffers and other HTTP stuff.While gunicorn is a Python HTTP server that interfaces with both nginx and your actual python web-app code to serve dynamic content.
The following diagrams shows how nginx and Gunicorn interact.

Why do we need uwsgi for hosting Django on nGINX

Lets see:
Django is WSGI compatible.
WSGI is Web Server Gateway Interface
Now, Nginx is a server. So we should be able to communicate with Django. So then why do we need uWSGI in between??
All say that uWSGI is a server which speaks wsgi protocol.
Then what is uwsgi protocol. How does it differ from WSGI (which is a protocol/specification)
And again, why do we find the combination Django + uWSGI + Nginx ??
Cant I speak WSGI between nginx & django?? Coz WSGI itself means to be an specification between WebServer (nginx) and Web Applications (django)
WSGI is specifically a Python interface, whereas Nginx is a general webserver. So at a minimum you need something between Nginx and Django that translates the standard http request into a WSGI one.
uWSGI is just one of several popular WSGI servers. Others include gunicorn and mod_wsgi (an Apache module which necessitates having Apache installed too). uWSGI happens to be my preferred one and nginx now has native support for its protocol, so you won't go to far wrong by using it.

django + nginx + fastcgi - how to keep webserver up?

I'm running nginx as a reverse proxy to fastcgi for django. I'm using the following command (will switch later to using sockets to avoid tcp overhead):
python /home/ubuntu/system/sites/manage.py runfcgi host=127.0.0.1 port=8080 pidfile=/home/ubuntu/system/logs/fastcgi.pid maxspare=2
I'm not too familiar with django, but how do I reload my project after making changes? I've made some changes to my views.py file however the website isn't updating with those changes so I assume there is some caching going on.