Thursday, 30 April 2015

Deploy Django on Apache with Virtualenv + mod_wsgi + mysql

 How to Deploy Django on Apache with Virtualenv + mod_wsgi + mysql


In this post I am going to explain how to deploy a Django app on apache with virtualenv, mod_wsgi and mysql. I went through certain blogs, posts, tutorials to learn how to deploy Django app and after a bit of struggle, I decided to summarize the process in a concrete way. Following are the 10 easy steps to get your Django up and running on a VPS.

Alternate Source :
Following video link that explains the below process.
How to deploy Django app on vps : Part 1 - Video  


How to deploy Django app on vps : Part 2 - Video  



Prerequisites

A VPS with ubuntu 12.04x64 installed.


1. Run update and upgrade & install git


sudo apt-get update  
sudo apt-get upgrade 

If you want to use git to clone the Django app from git repository than you need to install git otherwise you can leave the following command.


sudo apt-get install git


2. Install pip


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

Upgrade to the latest version of pip


sudo pip install --upgrade pip


3. install mysqldb for python and mysql-server


sudo apt-get install python-mysqldb


sudo apt-get install mysql-server-5.5


4. Install and configure Apache

Run the following command to install apache


sudo apt-get install apache2

Now install mod_wsgi which will serve python app from apache server


sudo apt-get install libapache2-mod-wsgi

Restart apache


sudo service apache2 restart


5. Setup virtualenv

virtualenv is a tool for creating isolated python environments. On top of it we have virtualenvwrapper which is a set of extension that helps in easy management of virtualenv.

Install virtualenvwrapper via pip and virtualenv will get installed automatically.




pip install virtualenvwrapper

Set location where virtual environments will live and the location of script virtualenvwrapper.sh so that we can access it later.
Add following lines to .Bash_profile
(The .profile may be named as .Bash_profile in some linux distribution)




sudo nano ~/.profile

This will open .profile file, now copy and paste the below two lines in this file.


export WORKON_HOME=/var/www/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Reload this file using the following command.


source ~/.profile


6. Creating virtual environment for the project

There are two ways to create virtualenv, either make with --system-site-package or with --no-site-packages. The difference is --system-site-packages can use the system-wide settings/installations. We have installed mysql outside the virtualenv, hence our Django app can access mysql if we make virtualenv using --system-site-packages.


mkvirtualenv exampleenv --system-site-packages

Here exampleenv is the name of virtualenv we have created for our django app.


7. Upload your project and install dependencies

First we need to activate the virtualenv by the following command.


workon exampleenv

The name of the virtualenv i.e. exampleenv will be appear at the initial of the terminal prompt which means that virtualenv is activated.

Install Django by the following command


pip install Django

We are going to install our app in /var/www directory. Go to the www directory by the following command.


cd /var/www

Generate requirements in a text file.


pip freeze > requirements.txt

Upload the project
You can copy your project folder or you can clone it from git. I am cloning it from git.


git clone https://github.com/AjeetK/example

Install dependencies


pip install -r requirements.txt

Now deactivate the virtualenv by the following command.


deactivate


8. Create a virutal host

To create a virtual host, we need to create a .conf file in /etc/apache2/sites-available/ directory. Create example.conf with the following command.


sudo nano /etc/apache2/sites-available/example.conf

This will create and open example.conf file, now copy paste the following code in this file.


<VirtualHost *:80>
ServerAdmin webmaster@mydomain.com
ServerName example.redirectme.net
ServerAlias www.example.redirectme.net
WSGIScriptAlias / /var/www/example.wsgi
 
Alias /static/ /var/www/example/static/
        <Location "/static/">
            Options -Indexes
        </Location> 
 
<Directory /var/www/example>
Order deny,allow    
Allow from all
</Directory>
</VirtualHost>

Note : Don't forget to enter your own url in ServerName field. You can put IP of the vps if you don't have a domain name. You can also get the domain name from "no-ip" website.
Save the file and exit from the editor.
Now enable this configuration using the following command.


a2ensite example.conf


9. Create wsgi file

Create a wsgi file according to the path specified in example.conf file.


sudo nano /var/www/example.wsgi

This will create a example.wsgi file in /var/www/ directory, now copy and paste the following content in this file.
Note : Edit the path and name of directory according to you project.


import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/var/www/.virtualenvs/exampleenv/local/lib/python2.7/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/example')
sys.path.append('/var/www/example/example')
os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'
# Activate your virtual env
activate_env=os.path.expanduser("/var/www/.virtualenvs/exampleenv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Save the file and exit from the editor.


 10. synchronize the Database

First we need to create the Database with the name we have used in our project.
Login to mysql by the following command.


mysql -u root -p

Enter the password you gave while installing mysql.
After login, mysql prompt will appear.


create Database example_db;

This will create the Database example_db. The Database name should be same as you have used in your project. Now exit from the mysql.


exit;

Now go to /var/www/example directory. "example" is the project directory in which manage.py is present.


cd /var/www/example

We need to synchronize the Database with our app by the following command.


python manage.py syncdb

Now the final step is to restart the apache server.


sudo service apache2 restart

 In this project, there were no static file. If you have static files in your project, you can add that by the following steps.


STATIC_ROOT = '/var/www/mydomain.com/static/'
STATIC_URL = '/static/'
Congratulations!!! you have successfully deployed a Django app on a VPS. Type the url in the url-addressbar and the app is up and running.

I hope this post is helpful and easy to understand. If you find any error or query, feel free to ask.

Keep Learning & Sharing

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. static files like js and css not loading.

    ReplyDelete
    Replies
    1. Hello Surendra, Please add path of your static files directory in settings.py file of your project like this:
      STATIC_ROOT = "/path/of/static/directory>"
      and add static URL for this:
      STATIC_URL = ""
      and restart apache.

      Delete
  3. hello ,
    I followed this tutorial but it showing Internal Server Error

    ReplyDelete
    Replies
    1. same with me have you found any solution

      Delete
  4. can you please share your project file as well. I want to check the settings.py file please.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel