Deploy django project to production

(Comments)

Django is the most popular framework for web application. It is python framework and based on MVT (Models, Views & Templates) similarly to MVC frameworks. Deployment is an important part of the website development process. Deploying to the server is last essential step of every project. Here's an overview of the steps involved.


1. Create python virtual environment
2. Create Django project
3. Configure database
4. Deploy Django app to the server

If you haven't configured apache2 yet then follow Install and configure Apache2, MySQL, PHP and phpMyAdmin on ubuntu.

1. Create python virtual environment-

Here some assumption was made that you have already installed python, pip, and virtualenv on your Ubuntu system. If not run following command-

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install python-pip python-dev build-essential

sudo apt-get install apache2 libapache2-mod-wsgi

sudo pip install virtualenv

To create new python virtual environment type the following in your terminal and hit Enter-

virtualenv myenv

virtualenv

Now activate your virtual enviroment-

Source myenv/bin/activate

activate virtualenv

Now it’s time to install django and other required python modules. Run the following command to install django-

pip install django

It will install the latest version of Django supported to python version you are using.

Install django

2. Create Django project-

django-admin startproject myproject

you will get a folder created by Django named myproject. The folder tree looks like-

project tree

3. Configure database-

# myproject/myproject/setting.py
DATABASES = {

'default': {

'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

}
}

replace above lines with-

DATABASES = {

"default": {

"ENGINE": "django.db.backends.mysql",
"NAME": "Test", #database name
"USER": "root", #database username
"PASSWORD": "password", #database password
"HOST": "",
"PORT": "",
}
}

Also at last of setting.py add-

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Now install MySQL for python-

pip install mysqlclient

Install MySQL

Now Create your django app using the following command-

python manage.py startapp myapp

This will create a folder named myapp inside your myproject folder. Myproject folder tree now looks like-

tree after app create

Now navigate to your project and run-

cd myproject
python manage.py migrate

migrate database

Now run following command to check project is working or not. If you get following then congratulations!! You have done your first step.

congrats django

Now connect your App to your project we have to change some files. Add folloeing to your project’s urls.py-

# myproject/myproject/urls.py

from django.conf.urls import url,include

url(r'^myapp/', include(myapp.urls)),

# file looks like-


from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [

url(r'^admin/', admin.site.urls),
url(r'^myapp/', include('myapp.urls')),
]

Now create urls.py inside your app’s directory and paste following code-

#myproject/myapp/urls.py

from django.conf.urls import url
from views import TestView

urlpatterns = [

url(r'^test/', TestView),
]

Write TestView to your view.py

#myproject/myapp/views.py
# -*- coding:utf-8 _*_
from __future__ import unicode_literals

from django.shortcuts import render, HttpResponse

def TestView(request):

return HttpResponse('This is my test view')

Again run-

python manage.py runserver

and open http://127.0.0.1:8000/myapp/test/ into your browser. You will see the message you have written and return HttpResponse('This is my test view').


Now add domain you want to use in ALLOWED_HOSTS-

myproject/myproject/setting.py
ALLOWED_HOSTS = ['myexample.com']

Create /etc/apache2/sites-available/django.conf using-

sudo vi /etc/apache2/sites-available/django.conf

and add following code inside django.conf

<VirtualHost *:80>

ServerAdmin [email protected]
ServerName myexample.com
ServerAlias www.myexample.com
DocumentRoot /var/www/myproject

Alias /static /var/www/myproject/static
<Directory /var/www/myproject/static>

Require all granted

</Directory>
<Directory /var/www/myproject/myproject>

<Files wsgi.py>

Require all granted

</Files>

</Directory>

WSGIDaemonProcess blogs python-home=/var/www/myenv python-path=/var/www/myproject
WSGIProcessGroup blogs
WSGIScriptAlias / /var/www/myproject/myproject/wsgi.py
ErrorLog /var/www/logs/error.log
CustomLog /var/www/logs/custom.log combined

</VirtualHost>

Add domain name to your hosts-

sudo vi /etc/hosts

and the following line and save the file-

127.0.0.1 myexample.com

Now run the following to enable django.conf and other configurations-

sudo a2enmod rewrite

sudo a2enmod wsgi

sudo a2ensite django

sudo service apache2 restart

Now open http://myexample.com/myapp/test/ and you will see the same page as http://127.0.0.1:8000/myapp/test/

Note: If you are deploying to some domain globally then don’t forget to change DEBUG setting to False and you need to point out your public IP at domain’s DNS setting.

Comments

Join us for latest updates and blog posts

Recent Posts

Archive

2018

Authors

Categories

Tags

Feeds

RSS / Atom