Setup Django on Debian 8

This tutorial explains how to setup Django on Debian 8 (jessie). I will show how to use both Python 2 and 3 as well as Nginx and PostgreSQL.

Everything done in this tutorial is done as root.

Install Packages

To start out, we need to install some packages.

For Python 2:

apt-get install python-pip python-dev virtualenv nginx postgresql postgresql-contrib libpq-dev sudo gcc

For Python 3:

apt-get install python3-pip python3-dev virtualenv nginx postgresql postgresql-contrib libpq-dev sudo gcc

Setup Databases

First, we log into the user postgres.

sudo -u postgres -s

Next, 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 dbname

Create 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 dbuser

The 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=# \q

Exit to root.

exit

Enable and start PostgreSQL:

systemctl enable postgresql
systemctl start postgresql

Setup the Virtual Environment

Instead 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/project

We now create the environment. This is different for python 2 and 3 users.

For Python 2:

virtualenv . -p python

For Python 3:

virtualenv . -p python3

Enter the virtual environment.

source bin/activate

Install Django, gunicorn, and psycopg2.

pip install django
pip install gunicorn
pip install psycopg2

If 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.8

We are now done with that for now, so we can deactivate our virtual environment.

deactivate

Project Upload and Configuration

This 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.git

Open 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': ''
    }
}

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/static

Now 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 collectstatic

Configure Gunicorn

Gunicorn 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.target

Enable and start the service that we have created.

systemctl enable django.service
systemctl start django.service

Nginx

You 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/django

Enable and start Nginx.

systemctl enable nginx
systemctl start nginx

Conclusion

Congratulations, you now have a working Django site up on your Debian VPS.

Legg igjen en kommentar

The Rise of Machines: Real World Applications of AI

The Rise of Machines: Real World Applications of AI

Kunstig intelligens er ikke i fremtiden, det er her akkurat i nåtiden I denne bloggen Les hvordan kunstig intelligens-applikasjoner har påvirket ulike sektorer.

DDOS-angrep: en kort oversikt

DDOS-angrep: en kort oversikt

Er du også et offer for DDOS-angrep og forvirret over forebyggingsmetodene? Les denne artikkelen for å løse spørsmålene dine.

Har du noen gang lurt på hvordan hackere tjener penger?

Har du noen gang lurt på hvordan hackere tjener penger?

Du har kanskje hørt at hackere tjener mye penger, men har du noen gang lurt på hvordan tjener de den slags penger? la oss diskutere.

Revolusjonerende oppfinnelser fra Google som vil gjøre livet ditt enkelt.

Revolusjonerende oppfinnelser fra Google som vil gjøre livet ditt enkelt.

Vil du se revolusjonerende oppfinnelser fra Google og hvordan disse oppfinnelsene forandret livet til alle mennesker i dag? Les deretter til bloggen for å se oppfinnelser fra Google.

Fredag ​​Essential: Hva skjedde med AI-drevne biler?

Fredag ​​Essential: Hva skjedde med AI-drevne biler?

Konseptet med selvkjørende biler som skal ut på veiene ved hjelp av kunstig intelligens er en drøm vi har hatt en stund nå. Men til tross for flere løfter, er de ingen steder å se. Les denne bloggen for å lære mer...

Teknologisk singularitet: en fjern fremtid for menneskelig sivilisasjon?

Teknologisk singularitet: en fjern fremtid for menneskelig sivilisasjon?

Ettersom vitenskapen utvikler seg raskt og tar over mye av innsatsen vår, øker også risikoen for å utsette oss for en uforklarlig singularitet. Les hva singularitet kan bety for oss.

Funksjonaliteter til Big Data Reference Architecture Layers

Funksjonaliteter til Big Data Reference Architecture Layers

Les bloggen for å kjenne ulike lag i Big Data Architecture og deres funksjoner på den enkleste måten.

Evolusjon av datalagring – infografikk

Evolusjon av datalagring – infografikk

Lagringsmetodene for dataene har vært i utvikling kan være siden fødselen av dataene. Denne bloggen dekker utviklingen av datalagring på grunnlag av en infografikk.

6 fantastiske fordeler ved å ha smarte hjemmeenheter i livene våre

6 fantastiske fordeler ved å ha smarte hjemmeenheter i livene våre

I denne digitaldrevne verden har smarthusenheter blitt en avgjørende del av livet. Her er noen fantastiske fordeler med smarthusenheter om hvordan de gjør livet vårt verdt å leve og enklere.

macOS Catalina 10.15.4 tilleggsoppdatering forårsaker flere problemer enn å løse

macOS Catalina 10.15.4 tilleggsoppdatering forårsaker flere problemer enn å løse

Nylig lanserte Apple macOS Catalina 10.15.4 en tilleggsoppdatering for å fikse problemer, men det ser ut til at oppdateringen forårsaker flere problemer som fører til muring av mac-maskiner. Les denne artikkelen for å lære mer