Configuring the Django project on the VM and making it accessible online: Part 4 of 5


This is part four of a five-part series on the “Development of a Django web app from testing to production and hosting onto Microsoft Azure’s virtual machine”.
The series will be split into the following topics:

  1. Part 1: Developing the Django Web App: Here, we improved the “Restful A.P.I.s in the Django Project” we built by building its UI/Web platform as it was only accessible via postman, to access the system via web interface with more features. Click here to go through the article.
  2. Part 2: Acquire a VM from Microsoft Azure: Here, we went through choosing the best VM with regards to cost and performance. Click here to go through the article.
  3. Part 3: Setting up the VM and uploading of Project using GIT:  Click here to go through the article.
  4. Part 4: Configuring the Django project on the VM and making it accessible online
  5. Part 5: Monitoring of the Django Web App using Microsoft Azure Tools

Intended audience: Who the article is for

This article will take the user through making the Django web app run on port 80 and ensure that it’s always running even after a server restart.

Case Study: What the article is going to cover

We are going to look into installing Apache and configuring it to work with Django and setting it to always startup as a service when the VM is restarted.


  1. Putty – a software used to log into virtual machines, get it here
  2. A virtual machine in Microsoft Azure, here’s how to get one

Configuring the Django project

  1. Log into the VM from putty. Here is how to do so
  2. Cd into the Django web app:
    • cd series_1/djangorestapis
  3. Create a venv:
    • python3 -m venv venv
  4. Activate venv:
    • source venv/bin/activate
  5. Install the requirements:
    • pip install -r requirements.txt
  6. Cd into the djangowebapp folder that has the “” file
    • cd djangorestapis
  7. Open the “” file using the command.
    • nano
    • Insert pic 2
  1. Using the arrow keys, scroll down to the code “import os” and just below it, type the code below. , mind the indentation, this code points to a file we will create to hold our Gmail log in credentials for us to use Gmail as an email server. Here is how to obtain these credentials.
  2. Using the arrow keys, scroll down to the variable labelled STATIC_URL and below it, type the code below. It is to allow us to collect all static files into one folder that’s easy to access and manage
  3. Using the arrow keys, scroll down to the bottom of the “ file”. You will see the code below
  4. Change the above code to the one below by uncommenting the first two python variables and deleting the last two python variables

This informs the web app to use Gmail credentials in the “config.json” file located in the etc folder. We will create this file below.

After making the changes above, press ctrl+O to save, then press enter, then press ctrl+X to exit

  1. We will now create the “config.json” file that’s to be located inside the etc folder.
  2. Enter the command “pwd” to view the directory you are currently in
  1. You should be inside the following directory: “/home/djangoweb/series_1/djangorestapis/djangorestapis”
  2. Navigate out of this directory and back to the parent Django web app directory, type the command below
    • cd ..
    • If you type the command “pwd” you should be in the following directory “/home/djangoweb/series_1/djangorestapis”
  3. Inside this folder type the command below:
    • sudo nano /etc/config.json
    • You will see the terminal window below:

21. Enter the JSON code below with the appropriate values for the gmail account we will use for emailing service

After making the changes above, press ctrl+O to save, enter, and ctrl+X to exit.

  1. We now configure the Django web app to save static files inside a folder in the STATIC_ROOT variable we created above. This is to ensure that static files such as css files and images are stored in an accessible folder in the VM.
    • We navigate back into the Django web app folder first, type the command: “cd series_1/djangorestapis”
    • This command will move the static files to appropriate place in the VM, type the command: “python collectstatic”
    • You will be informed of the number of files copied to the static folder.
  1. Create DB
    • python makemigrations
  2. Apply DB Changes:
    • python migrate
  3. Create a superuser by typing the command
    • python createsuperuser
    • Enter the username
    • Enter the email
    • Enter the password
    • Remember these superuser credentials, they will be needed when logging into the Django admin interface.
  4. Run the server:
    • python runserver
  5. The server will now be accessible via: http://VM_Public_IP_Address:8000 e.g. once we activate the port
  6. We now need to activate or open port 8000

Enabling port 8000 on the Virtual Machine

The virtual machine only gave us the option of enabling the HTTP, HTTPS and SSH ports, so for us to test if the Django web app is running successfully, we need to open or enable port 8000 on the virtual machine via the Azure portal. Below is how to do so:

  1. Log in to Azure
  2. You Virtual machine will be under “Recent resources.”
  1. Click on it to access its features.
  2. On the virtual machine’s page, select the networking menu located on the left side.
  1. On the networking page click on the button labelled “Add inbound port rule.”
  1. The pop window labelled “Add inbound security rule” will appear, under Destination port ranges, enter “8000”, set the “Priority” to 100, then scroll to the end of the pop up window and under “Name” enter port_8000
  1. The server will now be accessible via: http://VM_Public_IP_Address:8000 e.g.

Making the Virtual Machine Accessible Online via Apache

Apache is a web service used to host websites on servers; we will install this service and link the Django web app to the service. We are doing this to ensure that the website is always up even when restarted so that we don’t have to start the Django web app every time we restart the server and enable Django to run in the background.

  1. Follow the steps below to learn how to do so:
  2. Stop the Django server from running by hitting ctrl+c on the terminal.
  3. Navigate out of the Django web app directory to the VM’s user home directory, type the command
    • cd /home/djangoweb
  4. You should now be in your VM’s user home directory, e.g. /home/djangoweb/”
  5. We will install the apache server.
    • sudo apt-get install apache2 -y
  6. We then install a library that allows apache to communicate with Django Python.
    • sudo apt-get install libapache2-mod-wsgi-py3 -y
  7. Navigate to this apache folder to perform configurations on apache
    • cd /etc/apache2/sites-available/
  8. Copy Apache’s configuration file (000-default.conf) to the project conf
    • sudo cp 000-default.conf djangorestapis.conf
  9. Open the djangorestapis.conf for us to configure apache to redirect port 80 to our Django web app.
    • sudo nano djangorestapis.conf
  10. We will copy the commands below into the djangorestapis.conf inside VirtuaHost Tags, so paste the code below just above the last VirtuaHost

After making the changes above, press ctrl+O to save, press enter, then press ctrl+X to exit

  1. Then run the code below: to command Apache to enable the djangorestapis.conf file we created
    • sudo a2ensite djangorestapis
  2. Disable the default site: the conf file we copied to djangorestapis
    • sudo a2dissite 000-default.conf
  3. Navigate back to the home directory
    • cd /home/djangoweb
  4. Provide apache read and write access to files and database related to the Django web app
    • sudo chown :www-data series_1/djangorestapis/db.sqlite3
    • sudo chmod 664 series_1/djangorestapis/db.sqlite3
    • sudo chmod 775 series_1/
    • sudo chmod 775 series_1/djangorestapis/
    • sudo chown :www-data series_1/
    • sudo chown :www-data series_1/djangorestapis/
  5. Restart apache service
    • sudo service apache2 restart
    • sudo systemctl reload apache2
  6. The site can now be accessed from http://VM_Public_IP_Address, e.g.
  7. You can now go back to Microsoft Azure’ Virtual Machine page and the networking page and delete the port 8000 inbound port rule to close the port from accessibility.


You should now have a basic understanding of how to link a Django web app to run on the Apache web service and be accessible online.

Please let me know how your experience was, Thank you.

You May Also Like