This tutorial explains how to setup Django on Debian 8 (Jessie). I will show how to use both Python 2 and Python 3 as well as Nginx and PostgreSQL.
Everything done in this tutorial is done as root.
To start, we need to install some packages.
For Python 2:
apt-get install python-pip python-dev virtualenv nginx postgresql postgresql-contrib libpq-dev sudo gccFor Python 3:
apt-get install python3-pip python3-dev virtualenv nginx postgresql postgresql-contrib libpq-dev sudo gccFirst, we log in as the user postgres.
sudo -u postgres -sNext, we create a new database. The database name can be whatever you want it to be (dbname is used here), But you must be consistent with the rest of the setup.
createdb dbnameCreate a user for the new database. Again, this can be whatever you desire it to be, but I used dbuser. This will also ask you to set a password.
createuser -P dbuserThe user must now be given access to the database. Just be sure to use the correct database name and user name.
psql
postgres=# GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser;
postgres=# \qExit to root.
exitEnable and start PostgreSQL:
systemctl enable postgresql
systemctl start postgresqlInstead of just using the global Python files, we will be using a virtual environment. We start by creating a directory to hold it all.
mkdir -p /opt/projectWe now create the environment. This is different for Python 2 and Python 3 users.
For Python 2:
virtualenv . -p pythonFor Python 3:
virtualenv . -p python3Enter the virtual environment.
source bin/activateInstall Django, gunicorn, and psycopg2.
pip install django
pip install gunicorn
pip install psycopg2If you need a specific version of Django, change the install command to match the format below. This example installs 1.7.8.
pip install django==1.7.8We are now done with that for now, so we can deactivate our virtual environment.
deactivateThis is the time we upload our project to the server, and we make sure that all of its settings are correct. You can use any method to do this. FTP, SFTP, git, etc, are all ways of doing this. If you are using git to track the project’s code, you can just clone it to the server. This git command will clone the project to the server and place it in /opt/project/project/.
git clone http://example.com:project.gitOpen the settings.py file in any text browser.
First things first, debug mode needs to be off. Look for the DEBUG = True line and change True to False. After this, make sure that you have ALLOWED_HOSTS set to some value.
ALLOWED_HOSTS = ['*']Look for the DATABASES dictionary, and it should look like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': 'password you set',
'HOST': 'localhost',
'PORT': ''
}
}The last step here is to set a static root. Place the following directly below STATIC_URL.
STATIC_ROOT = '/opt/project/static/'Exit the file and create the static root directory.
mkdir -p /opt/project/staticNow migrate the database, create a super user, and collect all static files.
cd /opt/project/project
../bin/python manage.py makemigrations
../bin/python manage.py migrate
../bin/python manage.py createsuperuser
../bin/python manage.py collectstaticGunicorn is the WSGI server that we will be using. Since Debian 8 comes with systemd, we will take advantage of systemd to start and stop the server.
Create the file /etc/systemd/system/django.service and add the following content.
[Unit]
Description=Django with Gunicorn
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/opt/project
ExecStart=/opt/project/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 project.wsgi
[Install]
WantedBy=multi-user.targetEnable and start the service that we have created.
systemctl enable django.service
systemctl start django.serviceYou may have noticed that we bound the Gunicorn server to 127.0.0.1. Now we need a way to access it from outside the server. This is where Nginx comes in.
Create the new file /etc/nginx/sites-available/django and add the following. The domain.example.com part can be set to whatever you need it to be.
server {
listen 80;
server_name domain.example.com;
access_log off;
location /static/ {
alias /opt/project/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}Create a symbolic link to enable this site.
ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled/djangoEnable and start Nginx.
systemctl enable nginx
systemctl start nginxCongratulations, you now have a working Django site up on your Debian VPS.