Django on Debian 9

Linode Staff

I am interested in testing my local Django project on a Linode running Debian 9. How would I go about setting this up?

3 Replies

The quick and dirty way to get started would be to use the Django Development Server (this should be okay just for testing).

These instructions will walk you through creating a test project and configuring the Django Development Server. You'd want to adjust these settings for your situation, though.

To get started, first we want to install Python, Django, and PIP

For Debian based distributions (like Ubuntu):

$ apt update && apt upgrade
$ apt install python python-django python-pip

For RHEL based distributions (like CentOS):

$ yum install epel-release
$ yum update
$ yum install --enablerepo=epel python python-django python-pip

Django should be installed now, but just to be safe, we can make sure:

$ pip install django

If all went well, it should say something like Requirement already satisfied: django in /usr/lib/python2.7/dist-packages

Now, we can set up a working directory for Django. For the sake of simplicity, I chose /var/django:

$ mkdir /var/django/
$ cd /var/django/

Now we can create a test project named mytestsite

$ django-admin startproject mytestsite
$ cd mytestsite

We should be in the /var/django/mytestsite/ directory (you can run pwd to be sure). Since we're good to go, we can migrate the initial database schema and create an administrator (you'll need to supply a username, email, and password).

$ python manage.py migrate
$ python manage.py createsuperuser

That's it for the Django project -- now we just need to add the IP address to the ALLOWED_HOSTS section of the projects settings file. This file should be located in /var/django/mytestsite/mytestsite. From where we are now, we should be able to open the file this way:

$ nano mytestsite/settings.py

Now, locate: ALLOWED_HOSTS = [] and add your IP address in single quotes. For an example, if your IP address was 8.8.8.8:

/var/django/mytestsite/mytestsite/settings.py

ALLOWED_HOSTS = ['8.8.8.8']

Hit CTRL + O and ENTER, then CTRL + X to save and quit.
Finally, start up the server:

$ python manage.py runserver 0.0.0.0:8000

Open up a browser and navigate to your IP address. (Using 8.8.8.8 as an example):

http://8.8.8.8:8000/
http://8.8.8.8:8000/admin

When you're done, be sure to go back to your SSH session and hit CTRL+C to terminate the server.

Let's say I get a fully qualified domain name called 'bubsy3d.comand put it in my Django project'sALLOWED_DOMAINSlist. Do I just execute therunservercommand and leave it? I'm thinking of how I'd set things up so that users can go tohttps://bubsy.com` to see my site.

Or do I need to get involved with WSGI, Apache, and such?

I wouldn't recommend the Django Development Server for a production environment (or one that would be publicly available). For something like that, you might want to consider Django + Gunicorn/uWSGI + Nginx/Apache.

These articles might be useful -- they're based on Ubuntu 16.04 but I don't think it would be difficult to get this going on a Debian 9 Linode:
Part 1: http://rahmonov.me/posts/run-a-django-app-with-gunicorn-in-ubuntu-16-04/
Part 2: http://rahmonov.me/posts/run-a-django-app-with-nginx-and-gunicorn/

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct