How VPS - How to use/setup VPS
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Debian

Setup Django on Debian 8

How VPS by How VPS
November 1, 2019
in Debian
0
0
SHARES
26
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Install Packages
  2. Setup Databases
  3. Setup the Virtual Environment
  4. Project Upload and Configuration
  5. Configure Gunicorn
  6. Nginx
  7. Conclusion
  8. Want to contribute?


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.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
How VPS

How VPS

Related Posts

Debian

How to Install WonderCMS on Debian 9

November 1, 2019
Debian

Using MySQL Views on Debian 7

November 1, 2019
Debian

How to Install and Configure TaskBoard on Debian 9

November 1, 2019
Next Post

Simple Mailserver With Postfix, Dovecot, and Sieve on Debian 9

How to Install Apache Cassandra 3.11.x on Debian 9

How to Install ARK: Survival Evolved on Debian 9

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

  • 121 Followers
  • 87.2k Followers

Recommended

How to Encrypt and Decrypt Files and Directories Using Tar and OpenSSL

4 years ago

How to Install Osclass on CentOS 7

3 years ago

Setup RethinkDB Cluster On Ubuntu 14

3 years ago
How to install Let's Encrypt on Plesk

Let’s Encrypt on Plesk

3 years ago

Instagram

    Please install/update and activate JNews Instagram plugin.

Categories

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel

Topics

Apache Web Server Bluehost Review 2019 Bluehost Review 2020 Bluehost Review 2021 Centmin Mod CentminMod centos install htop fsck htop install HTTP DoS attack Install Snort on an Ubuntu install Zabbix on CentOS install Zabbix on CentOS 7 Linux Commands linux guide linux install htop linux vps setup guide MariaDB MariaDB Error Mysql mysqld error optimize MariaDB optimize Mysql snort Ubuntu
No Result
View All Result

Highlights

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Webmin Reviews

Virtualmin Reviews

CentOS Web Panel Reviews

Ajenti Reviews

ISPConfig Reviews

Trending

Failed to download metadata for repo 'appstream' on Centos 8
CentOS

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

by How VPS
February 25, 2022
0

I tried to update some extensions by use yum on centOs which I specified in Dockerfile. After...

How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020
Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

Top Free Web Hosting Control Panels To Manage VPS/Dedicated Servers

February 17, 2020
Webmin Reviews

Webmin Reviews

February 17, 2020
How VPS – How to use/setup VPS

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Visit our landing page to see all features & demos.
LEARN MORE »

Recent News

  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8 February 25, 2022
  • How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”? November 17, 2020
  • How to optimize Mysql or MariaDB November 3, 2020

Categories

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel

[mc4wp_form]

© 2018 JNews - City News Magazine WordPress theme. All rights belong to their respective owners.
JNews is a top selling 2018 WordPress News, Blog, Newspaper & Magazine Theme.

No Result
View All Result
  • Home

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.