In this post, we will see how we can run our Django project using Apache on Ubuntu.

Since I’m lazy enough to write too much text, here are the steps:

  1. I assume that we have our Ubuntu ready.
  2. First we will update Ubuntu packages:
    sudo apt-get update
    sudo apt-get upgrade
  3. Next we will install Apache, Python & Django
    sudo apt-get install apache2
    sudo apt-get install python3-pip
    sudo pip3 install virtualenv

    pip is a Package manage for Python that helps in installing and removing Python packages easily.
    virtualenv allow us to create a virtual environment of Python within our project only. So, no system settings needs to be updated for our project.

  4. Next we will create a directory for our project and setup virtual environment in it.
    Normally Apache has following directory for web application:


    On terminal, move into the above directory and execute following commands step by step:

    mkdir djangoweb
    cd djangoweb
    virtualenv env
    source env/bin/activate

    At this point, our virtual environment for Python is ready inside the djangoweb directory. The third line above creates the virtual environment and fourth line activates it. If you want to de-activate the virtual environment and want to use system settings, simply execute the command:


    You can check your virtual environment with the command:

    python --version
  5. Next, we will install Django in our virtual environment. Execute the command:
    pip3 install django
  6. Now, we have everything ready, let’s create a new project. Execute the following command from djangoweb directory (make sure your virtual environment is active): startproject project .

    Dot here means current directory

  7. Next, run the server to test if our app is ready:
    python runserver

    This will run a virtual server which you can access on the browser:


    Note: If you are running Ubuntu on AWS, you need to create a new Custom TCP Rule in security group for port 8000.

  8. Press Control-C to stop the server. Now, we will run our application on Apache server. For this, we need to install mod_wsgi module. Execute the command:
    sudo apt-get install libapache2-mod-wsgi
  9. Now, we will configure default Apache host to serve our Django application. Execute the command:
    nano /etc/apache2/sites-enabled/000-default

    This will open the file in edit mode. After the line <VirtualHost *:80>, add following:

    WSGIDaemonProcess djangoapp python-path=/var/www/html/djangoweb:/var/www/html/djangoweb/env/lib/python3.4/site-packages
    WSGIProcessGroup djangoapp
    WSGIScriptAlias / /var/www/html/djangoweb/project/

    The first line gives the name to Django process and specifies the location where to find the files. If we are using global Python configuration, then we don’t need python-path
    The second line specifies which process group a WSGI application or set of WSGI applications will be executed in. Here, it should match the name specified in WSGIDaemonProcess.
    The third line tells where to find the WSGI configuration file.

  10. Everything is ready now, restart the Apache server:
    sudo service apache2 restart

    And now visit the site:

  11. Next, we will create a view and test it. First move to the djangoweb directory and execute the command: startapp app
  12. Now create a new directory inside app directory
     cd app
     mkdir templates
  13. Create a new file named about.html inside the templates directory and put some html in it.
  14. Next open the app/ file and add following code in it:
    from django.shortcuts import render
    def about(request):
           return render(request, 'about.html')
  15. Now, we will create URL for this view. Open the file project/ and add following line:
    from app import views


    url(r'^about-us/', views.about)
  16. At the last, we need to tell Django about our views and app. Open the file and do following:

    Add ‘app’ under INSTALLED_APPS at the end

    Under TEMPLATES, modify ‘DIRS’ like this:

    'DIRS': [os.path.join(BASE_DIR, 'app/templates')]

That’s it, now you can access your view from the URL: