Django¶
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Note
For this guide you should be familiar with the basic concepts of
Python and its package manager pip
License¶
All relevant legal information can be found here
Prerequisites¶
Your URL needs to be setup:
[isabell@stardust ~]$ uberspace web domain list
isabell.uber.space
[isabell@stardust ~]$
uWSGI¶
Install the required uwsgi package with pip.
[isabell@stardust ~]$ pip3.6 install uwsgi --user
[isabell@stardust ~]$
After that, continue with setting it up as a service.
Create ~/etc/services.d/uwsgi.ini
with the following content:
[program:uwsgi]
command=uwsgi --master --emperor %(ENV_HOME)s/uwsgi/apps-enabled
autostart=true
autorestart=true
stderr_logfile = ~/uwsgi/err.log
stdout_logfile = ~/uwsgi/out.log
stopsignal=INT
Create needed folders and files for uwsgi:
[isabell@stardust ~]$ mkdir -p ~/uwsgi/apps-enabled
[isabell@stardust ~]$ touch ~/uwsgi/err.log
[isabell@stardust ~]$ touch ~/uwsgi/out.log
[isabell@stardust ~]$
After creating the configuration, tell supervisord to refresh its configuration and start the service:
[isabell@stardust ~]$ supervisorctl reread
SERVICE: available
[isabell@stardust ~]$ supervisorctl update
SERVICE: added process group
[isabell@stardust ~]$ supervisorctl status
SERVICE RUNNING pid 26020, uptime 0:03:14
[isabell@stardust ~]$
If it’s not in state RUNNING
, check the logs.
Installation¶
Install django
[isabell@stardust ~]$ pip3.6 install django --user
[isabell@stardust ~]$
Hint
Depending on your database configuration, additional modules like mysqlclient
might be required.
Create a django project. We will use “MyDjangoProject” during this guide.
[isabell@stardust ~]$ django-admin startproject MyDjangoProject
[isabell@stardust ~]$
Migrate database
[isabell@stardust ~]$ python3.6 ~/MyDjangoProject/manage.py migrate
[isabell@stardust ~]$
Configuration¶
Configure Hostname¶
Edit ~/MyDjangoProject/MyDjangoProject/settings.py
and edit the line ALLOWED_HOSTS = []
to add your host name.
ALLOWED_HOSTS = ['isabell.uber.space']
If you need to add multiple host names, separate them with commas like this:
ALLOWED_HOSTS = ['isabell.uber.space', 'www.isabell.example']
Configure web server¶
Note
Django is running on port 8000.
To make the application accessible from the outside, configure a web backend:
[isabell@stardust ~]$ uberspace web backend set / --http --port <port>
Set backend for / to port <port>; please make sure something is listening!
You can always check the status of your backend using "uberspace web backend list".
[isabell@stardust ~]$
Setup daemon¶
To deploy your application with uwsgi, create a file at ~/uwsgi/apps-enabled/myDjangoProject.ini
with the following content:
Warning
Replace <username>
with your username! (4 times)
Warning
Ensure that static-map
matches the path configured in django’s STATIC_ROOT
. Otherwise all images, stylesheets and javascript will be missing from your site.
[uwsgi]
base = /home/<username>/MyDjangoProject/MyDjangoProject
chdir = /home/<username>/MyDjangoProject
http = :8000
master = true
wsgi-file = %(base)/wsgi.py
touch-reload = %(wsgi-file)
static-map = /static=%(base)/static
app = wsgi
#virtualenv = %(chdir)/venv
plugin = python
uid = <username>
gid = <username>
Test installation¶
Perform a CURL request to djangos port to see if your installation succeeded:
[isabell@stardust ~]$ curl -I https://isabell.uber.space
HTTP/1.1 200 OK
Content-Type: text/html
X-Frame-Options: SAMEORIGIN
Content-Length: 16348
[isabell@stardust ~]$
If you don’t see HTTP/1.1 200 OK
check your installation.
Finishing installation¶
Point your browser to URL and create a user account.
Best practices¶
Security¶
Change all default passwords. Look at folder permissions. Don’t get hacked!
Tested with Django 2.0.5, Uberspace 7.1.6
Written by: Finn <mail@f1nn.eu>