[Fixed]-Deploying django application in production?

1đź‘Ť

âś…

I’ll start out by saying that I’m assuming you’re using the set up you describe for specific reasons and I won’t try to steer you towards anything that might be more admin-friendly. If you’re not and could use a friendly nudge, you can try Googling around for different people’s pros/cons and examples or I would put in a good word for Gunicorn and an LTS version of Ubuntu.

Anyway.

When I was reading your question the first thing that popped into my head was that you could use something like Fabric or even just a deployment script that could do a string substitution in your wsgi.py file, putting in the correct path for you along with handling everything else in your deployment. I would say automated deployments are something of a standard and you might like the freedom of being able to throw out a server and spin up a new one in 5 minutes, but they can be a lot to learn if you’re doing other things at the time.

Figuring that you probably didn’t want to mess with all the automation rigmarole, I then popped over to Django’s mod_wsgi deployment documentation (which is pretty good) because messing around with the Python path seemed weird and like something you shouldn’t have to do. Sure enough, they recommend running mod_wsgi in daemon mode, which gives you an opportunity to set the Python path in the Apache config and keep it separate from the code you distribute. Then you can just have different boxes use different config files based on their needs.

That same documentation has notes on using a virtual environment (virtualenv) which you also want to probably do — that virtualenv would then be portable and would handle your concerns with point 3.

With point 2 you should probably just think of the git distribution as a feature — no need to run a build process and upload WAR files and all the etceteras. Java people only put up with that because they theoretically get something from having their code compiled into bytecode; Python, for better or for worse, doesn’t do any compilation so you can deploy your code transparently and use git to handle your files at all points in the application’s life without worrying about keeping the artifact versions straight and all that.

EDIT:

You could also probably avoid any issues with PYTHONPATH by adding your ~/.local/bin folder to your path (export PATH=~/.local/bin:$PATH from command line) and then cding into your site’s directory and just running mod_wsgi-express start-server mysite/wsgi.py. By adding the local bin folder to your PATH you gain the ability to reference any applications in it implicitly from any directory while still having everything rooted in the directory you cd into, which should keep Python from getting confused.

I would still prefer to link mod_wsgi into your main Apache installation and run it in daemon mode so that you can control bringing it up/down from systemd/systemctl instead of forking processes from your terminal and having to look up the process ID in ps to kill it.

0đź‘Ť

Better something like this:
import os
import sys
sys.path.append(“add/Your/Path”)
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”,”mysite.settings”)
import django
django.setup
import django.core.handlers.wsgi
application = get_wsgi_application()
application = django.core.handlers.wsgi.WSGIHandler()

check that You project is named a mysite, If You project have other name change mysite on name of You django-project. You will use to production apache?

👤user5243788

0đź‘Ť

import os 
import sys 
sys.path.append("add/Your/Path")
os.environ.setdefault("DJANGO_SETTINGS_MODULE","mysite.settings") 
import django 
django.setup()
import django.core.handlers.wsgi 
application = get_wsgi_application() 
application = django.core.handlers.wsgi.WSGIHandler()
👤user5243788

Leave a comment