In the previous part of this tutorial, Deploy Django to DreamCompute on Ubuntu with Apache – Part 3, we’ve successfully installed and configured our Django project to run on a virtual environment.

With everything now set up and working correctly with Django, it is time to install and configure Apache to run our site.


First, let’s deactivate our virtual environment. We can do so by running the following:

(env_name) ubuntu@instance_name:~/domain.com/projectname$ deactivate

Next, we want to install Apache alongside a Python 3 WSGI adapter module. This module will help us run Django with Apache. Run the following command to install the packages:

ubuntu@instance_name:~/domain.com/projectname$ sudo apt-get install apache2 libapache2-mod-wsgi-py3

You will be prompted to say Yes or No. Press Y and hit Enter to begin installing the packages.

Once installed, we’d need to configure our virtual host file, so it picks up our Django project and its static directory.

ubuntu@instance_name:~/domain.com/projectname$ sudo nano /etc/apache2/sites-available/000-default.conf

Running the command above will allow you to edit the virtual host file. Go ahead and delete everything within the file. You can then copy the following template, make your edits, and paste it into the terminal then press Ctrl + X, Y, then Enter to save.

<VirtualHost *:80>
	ServerName domain.com
	ServerAdmin na**@do****.com

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	Alias /static /home/ubuntu/domain.com/public/static
    	<Directory /home/ubuntu/domain.com/public/static>
        	Require all granted
    	</Directory>

    	<Directory /home/ubuntu/domain.com/projectname/projectname>
        	<Files wsgi.py>
            		Require all granted
        	</Files>
    	</Directory>

    	WSGIDaemonProcess projectname python-path=/home/ubuntu/domain.com/projectname python-home=/home/ubuntu/domain.com/env
    	WSGIProcessGroup projectname
    	WSGIScriptAlias / /home/ubuntu/domain.com/projectname/projectname/wsgi.py
</VirtualHost>

Replace all instances of domain.com with your domain and projectname with your project name.

The last step we would need to do is to restart Apache. We can restart Apache with the following command:

ubuntu@instance_name:~/domain.com/projectname$ sudo systemctl restart apache2

That’s it! Now if we go to our domain, we should see our Django site fully operational.

Note: The configuration we have above only runs on port 80. Port 80 usually is how we would access a site through a non-secure HTTP connection. We’ll talk about SSL certificates and HTTPS in the next part of the tutorial.

Author

My name is Tony, and I’m an Experience Designer with 8+ years of experience in design and development. At heart, I am a developer first and a designer second. I enjoy creating interactive experiences, but I also enjoy designing and learning about the user’s experiences.

Write A Comment