Wednesday, 24 January 2024

python deployment with apache2 wsgi, django wsgi utilization, docker

what is WSGI

Web Server Gateway Interface


python cant handle http request natively, it allows server like ngix and apache2 to communicate with python by using WSGI

https://www.reddit.com/r/learnpython/comments/1szfcf/what_exactly_is_wsgi/?onetap_auto=true&one_tap=true


https://stackoverflow.com/questions/22394900/capacity-of-django-development-server 

why u should not use python manage.py runserver (the deployment server) for production 


you should definitely switch, django dev server is pretty much a toy, a simple single threaded server easily used for development, there are very few steps involved with serving your django application using apache –

If someone uploads a large image, the server will block all other requests during that time, I believe that alone is good enough reason to switch servers.


Additionally, advice from the first page tutorial in django documentation:

Now’s a good time to note: don’t use this server in anything resembling a production environment. It’s intended only for use while developing. (We’re in the business of making Web frameworks, not Web servers.)



If you are using apache2, you can directly install in apache2 and enable wsgi.module:

 

https://modwsgi.readthedocs.io/en/master/



If you are using python, pythin-alpine image for docker, and using httpd docker image(apache2) as stand alone container, you might want to install mod_wsgi-standalone in python, and do mod_wsgi-express start-server

offcial documentation:

https://pypi.org/project/mod-wsgi/

mod_wsgi-express start-server will start apache2 server at port 8000, 

How to integrate with python and django natively :

1) first approach :

https://pypi.org/project/mod-wsgi/

2) second approach(considering that front end and static files also need to be deployed and wrapped in docker container):

install wsgi module in python-alpine image where u use to for django, in DockerFile:

# expat-dev is required for mod_wsgi-standalone

https://pkgs.alpinelinux.org/package/edge/main/x86/expat-dev

RUN apk add expat-dev

RUN pip install mod_wsgi-standalone

Then in your dcoker-compose 

app:

    user: "${uID}:${gID}"

    build:

      context: .

    ports:

      - "8000:8000"

    command: >

      sh -c "

            sleep 3

            python manage.py migrate &&

            mod_wsgi-express start-server /myapp/myapp/wsgi.py"

in your httpd.conf for your httpd container, do a reverse proxy to your IP at port 8000

        ######## Reverse Proxy ##############

        RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}

        RequestHeader set X-Forwarded-SSL expr=%{HTTPS}

ProxyPassMatch .*/myendpoint/.* http://myIP:8000

    ProxyPassReverse .*/myendpoint/.* http://myIP:8000

In this way, 2) approach, you will have httdp webserver for external request, then reverse proxy api request to your apache wsgi server where your django is


Second approach is very desired as httpd container can be used for static files and front end and https connects as httpd is very fast in serving front end files:


  • The front-end server is generally very efficient at serving static files, and your Python app server is not, and so you configure URL routing rules whereby the front-end server handles those requests itself and only delegates to the app server for dynamic requests, lowering the load on the app server.

https://www.reddit.com/r/learnpython/comments/1szfcf/what_exactly_is_wsgi/

No comments:

Post a Comment