Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting Django app with Waitress

Tags:

python

django

I'm trying to host a Django app on my Ubuntu VPS. I've got python, django, and waitress installed and the directories moved over.

I went to the Waitress site ( http://docs.pylonsproject.org/projects/waitress/en/latest/ ) and they said to use it like this:

from waitress import serve
serve(wsgiapp, host='5.5.5.5', port=8080)

Do I put my app name in place of of 'wsiapp'? Do I need to run this in the top-level Django project directory?

like image 217
chrislarson Avatar asked Jan 12 '14 23:01

chrislarson


People also ask

How do you use waitress in Django?

You could just install Waitress itself, and run the ``waitress-serve`` command it provides, pointing to your ``wsgi.py`` file; the only thing that this project does is provide a Django management command, and remove the need for a ``wsgi.py`` in your project. <span property="dct:title">cookiecutter-django</span>.

Can Django be hosted on shared server?

This is one of the options that you can avail to deploy your Django project. The advantage of shared hosting is that it is cheap. The disadvantage is that you might not be able to deploy some advanced projects because you can not install a software on your shared host.


2 Answers

Tested with Django 1.9 and Waitress 0.9.0

You can use waitress with your django application by creating a script (e.g., server.py) in your django project root and importing the application variable from wsgi.py module:

yourdjangoproject project root structure

├── manage.py
├── server.py
├── yourdjangoproject
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py

wsgi.py (Updated January 2021 w/ static serving)

This is the default django code for wsgi.py:

import os
    
from django.core.wsgi import get_wsgi_application
    
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourdjangoproject.settings")
    
application = get_wsgi_application()

If you need static file serving, you can edit wsgi.py use something like whitenoise or dj-static for static assets:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourdjangoproject.settings")

"""
YOU ONLY NEED ONE OF THESE.
Choose middleware to serve static files. 
WhiteNoise seems to be the go-to but I've used dj-static 
successfully in many production applications.
"""

# If using WhiteNoise:
from whitenoise import WhiteNoise
application = WhiteNoise(get_wsgi_application())

# If using dj-static:
from dj_static import Cling
application = Cling(get_wsgi_application())

server.py

from waitress import serve
    
from yourdjangoproject.wsgi import application
    
if __name__ == '__main__':
    serve(application, port='8000')

Usage

Now you can run $ python server.py

like image 129
aboutaaron Avatar answered Sep 20 '22 12:09

aboutaaron


I managed to get it working by using a bash script instead of a python call. I made a script called 'startserver.sh' containing the following (replace yourprojectname with your project name obviously):

#!/bin/bash
waitress-serve --port=80 yourprojectname.wsgi:application

I put it in the top-level Django project directory.

Changed the permissions to execute by owner:

chmod 700 startserver.sh

Then I just execute the script on the server:

sudo ./startserver.sh

And that seemed to work just fine.

like image 22
chrislarson Avatar answered Sep 18 '22 12:09

chrislarson