Search code examples
pythondjangonginxweb-hostinguwsgi

Hosting multiple Django instances on a VPS


I'm moving away from WordPress and into bespoke Python apps.

I've settled on Django as my Python framework, my only problems at the moment are concerning hosting. My current shared hosting environment is great for WordPress (WHM on CloudLinux), but serving Django on Apache/cPanel appears to be hit and miss, although I haven't tried it as yet with my new hosting company. - who have Python enabled in cPanel.

What is the easiest way for me to set up a VPS to run a hosting environment for say, twenty websites? I develop everything in a virtualenv, but I have no experience in running Django in a production environment as yet. I would assume that venv isn't secure enough or has scalability issues? I've read some things about people using Docker to set up separate Django instances on a VPS, but I'm not sure whether they wrote their own management system.

It's my understanding that each instance Python/Django needs uWSGI and Nginx residing within that virtual container? I'm looking for a simple and robust solution to host 20 Django sites on a VPS - is there an out of the box solution? I'm also happy to develop one and set up a VPS if I'm pointed in the right direction.

Any wisdom would be gratefully accepted.

Andy :)


Solution

  • Traditional approach

    • Virtualenv is good enough and perfectly ready for production use. You can have multiple virtualenv for multiple projects on the same VM.
    • If you have multiple database engines for multiple projects. Like, MySQL for one, PostgreSQL for another something like this then you just need to set up each individually.
    • Install Nginx and configure each according to project.
    • Install supervisor to manage(restart/start/stop) each project individually.
    • Anything that required by the project.
      Here it has a huge drawback. Because you can't use different versions on your database engine for a different project in an easy way. So, containerization is highly recommended.

    For simple and robust solution,

    • Use Docker(docker-compose) for local and production deployment.
    • Configure uWsgi with Nginx(Available on docker.)
    • Create a CI/CD pipeline with any tool like Jenkins.
    • Monitor your projects using any good tool like Raygun.

    That's it.