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:
- 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.
- 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.
- Part 3: Setting up the VM and uploading of Project using GIT: Click here to go through the article.
- Part 4: Configuring the Django project on the VM and making it accessible online
- 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.
- Putty – a software used to log into virtual machines, get it here
- A virtual machine in Microsoft Azure, here’s how to get one
Configuring the Django project
- Log into the VM from putty. Here is how to do so
- Cd into the Django web app:
- cd series_1/djangorestapis
- Create a venv:
- python3 -m venv venv
- Activate venv:
- source venv/bin/activate
- Install the requirements:
- pip install -r requirements.txt
- Cd into the djangowebapp folder that has the “settings.py” file
- cd djangorestapis
- Open the “settings.py” file using the command.
- nano settings.py
- Insert pic 2
- 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.
- 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
- Using the arrow keys, scroll down to the bottom of the “settings.py file”. You will see the code below
- 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
- We will now create the “config.json” file that’s to be located inside the etc folder.
- Enter the command “pwd” to view the directory you are currently in
- You should be inside the following directory: “/home/djangoweb/series_1/djangorestapis/djangorestapis”
- 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”
- 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.
- 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 manage.py collectstatic”
- You will be informed of the number of files copied to the static folder.
- Create DB
- python manage.py makemigrations
- Apply DB Changes:
- python manage.py migrate
- Create a superuser by typing the command
- python manage.py 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.
- Run the server:
- python manage.py runserver 0.0.0.0:8000
- The server will now be accessible via: http://VM_Public_IP_Address:8000 e.g. http://126.96.36.199:8000 once we activate the port
- 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:
- Log in to Azure
- You Virtual machine will be under “Recent resources.”
- Click on it to access its features.
- On the virtual machine’s page, select the networking menu located on the left side.
- On the networking page click on the button labelled “Add inbound port rule.”
- 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
- The server will now be accessible via: http://VM_Public_IP_Address:8000 e.g. http://188.8.131.52:8000
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.
- Follow the steps below to learn how to do so:
- Stop the Django server from running by hitting ctrl+c on the terminal.
- Navigate out of the Django web app directory to the VM’s user home directory, type the command
- cd /home/djangoweb
- You should now be in your VM’s user home directory, e.g. /home/djangoweb/”
- We will install the apache server.
- sudo apt-get install apache2 -y
- We then install a library that allows apache to communicate with Django Python.
- sudo apt-get install libapache2-mod-wsgi-py3 -y
- Navigate to this apache folder to perform configurations on apache
- cd /etc/apache2/sites-available/
- Copy Apache’s configuration file (000-default.conf) to the project conf
- sudo cp 000-default.conf djangorestapis.conf
- Open the djangorestapis.conf for us to configure apache to redirect port 80 to our Django web app.
- sudo nano djangorestapis.conf
- 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
- Then run the code below: to command Apache to enable the djangorestapis.conf file we created
- sudo a2ensite djangorestapis
- Disable the default site: the conf file we copied to djangorestapis
- sudo a2dissite 000-default.conf
- Navigate back to the home directory
- cd /home/djangoweb
- 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/
- Restart apache service
- sudo service apache2 restart
- sudo systemctl reload apache2
- The site can now be accessed from http://VM_Public_IP_Address, e.g. http://184.108.40.206/
- 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.