Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Python Falcon on Apache2 with mod_wsgi

Scenario: I'm using Python Falcon framework for my REST API service. The application is initially run by Gunicorn. Right now when I call https://example.com:8000/, I get a proper response that the API logic dictates.

Goal: I want to deploy it on Apache2 along with mod_wsgi on my personal development server. The API would still be running under Gunicorn, and Apache should interact with Gunicorn whenever a request is made on that end-point.

Directory structure:

| - abc_service
        | - abc_service
                | - __init__.py
                | - app.py
        | - wsgi.py
        | - .abc_venv

The source code for app.py looks something like this:

import falcon
from .abc import ABC

api = application = falcon.API() # pylint: disable=invalid-name

# Creating a unique GET resource from the ABC class
test_handler_resource = ABC() # pylint: disable=invalid-name

# Mapping the HTTP GET endpoint with the unique resource
api.add_route('/abc', test_handler_resource)

# Mapping the HTTP POST endpoint with the unique resource
api.add_route('/abc/filters', test_handler_resource)

In my wsgi.py, I have the following contents:

from abc_service import application

The configuration for Apache in /etc/apache2/sites-available/000-default.conf is as follows:

# WSGI
WSGIDaemonProcess python-path=/home/uname/abc-service/src/abc_service/.abc_venv/lib/python3.5/site-packages
WSGIScriptAlias /abc /home/uname/abc-service/src/abc_service/wsgi.py

ProxyPass /abc http://localhost:8000/abc
ProxyPassReverse /abc http://localhost:8000/abc
<Location /abc>
AuthType None
Require all granted
# Always set these headers.
Header always set Access-Control-Allow-Origin "https://example.com"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
</Location>

I can't find any helpful resource on the web regarding the same on the web that uses this combination of Falcon, Gunicorn, mod_wsgi, and Apache. So, I can't figure out what I'm doing wrong.

I appreciate your time and help. Thanks!

like image 238
Rushiraj Nenuji Avatar asked Apr 23 '26 20:04

Rushiraj Nenuji


1 Answers

So, a solution that worked for me was to add the WSGIPythonHome path to the Apache configuration.

WSGIPythonHome /var/local/abc-service/src/abc_service/.venv

The final Apache configuration looks like this:

# At the top of the Apache Config file
WSGIPythonHome /var/local/abc-service/src/abc_service/.venv

...
...
...

# WSGI
WSGIDaemonProcess python-path=/home/uname/abc-service/src/abc_service/.abc_venv/lib/python3.5/site-packages
WSGIScriptAlias /abc /home/uname/abc-service/src/abc_service/wsgi.py

ProxyPass /abc http://localhost:8000/abc
ProxyPassReverse /abc http://localhost:8000/abc
<Location /abc>
AuthType None
Require all granted
# Always set these headers.
Header always set Access-Control-Allow-Origin "https://example.com"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
</Location>
like image 186
Rushiraj Nenuji Avatar answered Apr 26 '26 09:04

Rushiraj Nenuji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!