Serving Django applications with Apache Server

Hosting a Django application is one of the challenging tasks that many developer face. So let’s start hosting your app with a step by step tutorial. Lets start installing apache on your machine by typing following commands in your terminal.

If your application is built with python 2.

sudo apt update
sudo apt install apache2 libapache2-mod-wsgi

If your application is built with python 3.

sudo apt update
sudo apt install apache2 libapache2-mod-wsgi-py3 

Since libapache2-mod-wsgi and libapache2-mod-wsgi-py3 cannot be installed together on same machine, It is not possible to serve both python 2 and 3 applications together on a same server.

You need to give permission to apache2 in order for networking and to access files.

sudo ufw allow in "Apache Full"

Next stop is to edit the apache config to set up wsgi settings.

sudo nano /etc/apache2/sites-available/000-default.conf

copy and paste follwing code and replace project path accordingly.

<VirtualHost *:80>
  DocumentRoot /var/www/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  Alias /static /path/to/folder_static_files
  <Directory /path/to/folder_static_files>
          Require all granted
  <Directory /path/to/folder_containing_wsgi>
              Require all granted
  WSGIDaemonProcess myproject python-path=/path/to/folder_containing_managepy python-home=/path/to/env_folder
  WSGIProcessGroup myproject
  WSGIScriptAlias / /path/to/folder_containing_wsgi/

Save the configurations and change permission to project folder so that you wont get forbidden page when you access url.

sudo chown :www-data /project_folder

Now restart apache server.

sudo service apache2 restart

If you are getting Import error like below.

ImportError: No module named project.settings

It means that your project structure is quite different then try the following code by adding it to


If your are facing Forbidden error then it is due to permission to project is proper try giving proper permisions.

sudo chown :www-data /project_folder
#you may also try without :
sudo chown www-data /project_folder

Set up free ssl to convert from http to https by using LetsEncrypt follow the link below and install according to your os.

402 total views, 2 views today

Posts created 6

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top