Set Up a Django Server with Apache Virtual Host and Python Virtual Environment


Hello and welcome back to another episode of Continuous Improvement, the podcast where we explore tips, tricks, and strategies to help you improve your skills and workflows. I’m your host, Victor, and today we’re going to dive into a step-by-step guide on setting up a Django application for production.

But before we begin, a quick reminder to subscribe to our podcast and follow us on social media to stay updated with the latest episodes. Alright, let’s jump right in!

Setting up a Django application for production can be a bit daunting, but don’t worry, I’ve got you covered. I’ve broken down the process into simple steps to make it easier for you to follow along. So let’s get started.

Step one, assuming you already have your CentOS or Ubuntu instance running and Python installed, create a folder for your project and set the appropriate permissions. You can do this by running the following commands:

sudo mkdir /opt/yourpath/projects
sudo chown $USER /opt/yourpath/projects

Step two, if you haven’t already initialized your project, you can do so by installing Django and starting your project with the following command:

python -m pip install Django
django-admin startproject APPNAME /opt/yourpath/projects/APPNAME

Remember to replace ‘APPNAME’ with your desired project name.

Moving on to step three, by default, the Django development server runs on port 8000. You can start the server with the command:

python manage.py runserver

Great! Now that we have our project set up, let’s prepare it for production. Step four involves editing the ‘settings.py’ file with a few configurations. Set ‘DEBUG’ to False, ‘ALLOWED_HOSTS’ to [’*’], ‘STATIC_URL’ to ‘/static/’ and ‘STATIC_ROOT’ to the appropriate directory path.

After making these changes, it’s time for step five. We need to collect and build the static files of our Django project. Run the following command in your project’s directory:

python manage.py collectstatic --noinput

The next step, step six, is to serve your web application via the Apache web server. Assuming you have Apache2 installed, enable virtual hosts for your project and create a virtual host configuration file. You can create the file by running:

touch /opt/yourpath/apache2/conf/vhosts/project-vhost.conf

In this file, you’ll need to specify the WSGIDaemonProcess for your Django application. Make sure to replace all instances of ‘APPNAME’ with your actual project name.

To enable HTTPS, you can create another virtual host configuration file with a similar content structure. Use the command:

touch /opt/yourpath/apache2/conf/vhosts/project-https-vhost.conf

Now that we’ve updated the configurations, it’s time for step seven - restarting the Apache server. This will allow your Django site to become operational.

Lastly, in step eight, we’ll isolate Python dependencies within a virtual environment to avoid any version conflicts or dependency issues. Inside your project directory, run the following commands:

pip install virtualenv
virtualenv venv
source venv/bin/activate

This creates a folder named ‘venv’ that contains all your Python executables. Any subsequent ‘pip install’ commands will affect only this folder.

Once you’ve activated the virtual environment, go back and edit ‘project-vhost.conf’ and ‘project-https-vhost.conf’. Update the ‘python-home’ path in both files to point to the ‘venv’ folder, like this:

WSGIDaemonProcess APPNAME python-home=/opt/yourpath/projects/APPNAME/venv python-path=/opt/yourpath/projects/APPNAME

Make sure to replace ‘APPNAME’ with your project name.

And that’s it! You’ve successfully set up your Django application for production. Now, all that’s left is to navigate to your public IP address and you should see your Django page up and running.

If you encounter any issues along the way, remember to check the Apache server error log for troubleshooting. You can do this by running:

tail /opt/yourpath/apache2/logs/error_log

That wraps up today’s episode on setting up a Django application for production. I hope you found this guide helpful and that it saves you time and effort in the future.

Remember, continuous improvement is all about learning, growing, and finding better ways to do things. If you have any questions or topics you’d like me to cover in future episodes, feel free to reach out to me on social media. Thank you for tuning in to Continuous Improvement, and until next time, keep improving!