Hoarding programming problems for you!

Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Setup Jenkins On AWS EC2 For Your Python (Django) Application – Part 2

Sharing is caring!

This article assumes you know how to setup an EC2 instance with a running Apache web server and SSL. If you don’t know how to setup an EC2 instance with an Apache web server and a Let’s Encrypt SSL certificate please refer to this link: Migrating WordPress site from a legacy hosting provider to AWS

In Part 1 of this tutorial we covered the following:
• Setup Jenkins in you EC2 Instance
• Setup an Apache VirtualHost to run Jenkins over SSL

In this tutorial we will cover how to run our Django REST service behind an Apache web server on our EC2 instance.

Step 1: Install Python 3
Login to your AWS EC2 instance and run the following:

Then, run the following command to see what is available to install:

To install Python run:

To use pip3 add the following symbolic link ln -s /usr/bin/pip-3.6 /usr/bin/pip3.

Step 2: Start you Django REST application

Normally you would be able to access your Django application locally at http://127.0.0.1:8000.

Step 3: Update the existing Apache VirtualHost to forward traffic to your Django application
Edit your /etc/httpd/conf.d/ssl.conf using $ nano /etc/httpd/conf.d/ssl.conf and make sure you add the following to the end of the file (or update the existing one):

Now you will be able to access your Django application at https://fizzbuzzer.com/api/.

Note: The ProxyPassReverse / http://fizzbuzzer.com/ is required for the following reasons:
The ProxyPassReverse is used to change the headers sent by the app to Apache, before Apache sends it the browser. For example, if the app sits at http://127.0.0.1:8000, and it tries to redirect the browser to, say, /new_location/, then it will respond with a redirect and location header of http://127.0.0.1:8000/new_location/, and Apache will take this and send it off to the browser. Problem is, the browser (assuming it’s somewhere else) then tries to send a request to http://127.0.0.1:8000/new_location/, and gets an error.

What ProxyPassReverse does is intercepts those headers, and rewrites them so that they match what the Apache server that’s doing the proxying looks like. So if my apache server is hosting http://fizzbuzzer.com/ and I have a ProxyPass that points /api to http://127.0.0.1:8000/myapp/, if the application sitting at 127.0.0.1:8000 returns a redirect to http://127.0.0.1:8000/myapp/new_location/, I’ll need to use ProxyPassReverse so that it gets rewritten to http://fizzbuzzer.com/new_location/ by Apache before sending the request back to the browser.

If you aren’t issuing redirects, it’s not going to be an issue, but it doesn’t hurt to have it there in case a 301/302 redirect is returned. As far as mod_rewrite, the RewriteRule applies to the request going to the App, and not the response coming from the App. So they are mutually exclusive events.

Step 4: Restart Apache