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:
- I assume that we have our Ubuntu ready.
- First we will update Ubuntu packages:
sudo apt-get update sudo apt-get upgrade
- 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.
- 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:
- Next, we will install Django in our virtual environment. Execute the command:
pip3 install django
- 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):
django-admin.py startproject project .
Dot here means current directory
- Next, run the server to test if our app is ready:
python manage.py runserver 0.0.0.0:8000
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.
- 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
- Now, we will configure default Apache host to serve our Django application. Execute the command:
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/wsgi.py
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.
- Everything is ready now, restart the Apache server:
sudo service apache2 restart
And now visit the site:
- Next, we will create a view and test it. First move to the djangoweb directory and execute the command:
django-admin.py startapp app
- Now create a new directory inside app directory
cd app mkdir templates
- Create a new file named about.html inside the templates directory and put some html in it.
- Next open the app/views.py file and add following code in it:
from django.shortcuts import render def about(request): return render(request, 'about.html')
- Now, we will create URL for this view. Open the file project/urls.py and add following line:
from app import views
- At the last, we need to tell Django about our views and app. Open the file settings.py 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: