Hosting multiple Django instances on a VPS - python

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 :)

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.

I created a bash script that deploys as many websites as you want on your server. It automatically installs all dependencies on your server, creates a virtual environment, configure Gunicorn, Nginx, and a database for Django, etc. Check it out:
https://github.com/jdbit/django-auto-deploy

Related

QuestDB installation on a VPS web infrastructure newbie - moving

I have a fx vol model which I have written in python / questdb on my local machine. The standalone model works as an application in my machine and I also have lots of feeds written which are constantly updating the local machine questdb instance.
Now I want to move to web and have my application and database on the web server away from my machine.
I am not too familiar of web servers and how to install a questdb there.
From my knowledge I will need :
a VPS paid subscription where I have centOS and I have python support.
I need to install questdb here ( using docker ?)
Install all my python model here
Start the questdb in the VPS
Configure my scripts to use the hosted questdb
The model saves output in a table in questdb ; while the scripts also keep on updating new feeds to questdb
Start a webserver which provides a web access to the model results saved in the questdb
7.a I need to provide username and login for the website
7.b I need to use some vitualization
7.c I need the users once they are logged in to run some simulations
What I need some quidance is :
What sort of VPS service to look for
Has any one already installed a questdb in this way
Which webserver is the best for python
Lots of good questions there. Will try to help with all of them (disclaimer, I work for QuestDB as a developer advocate)
What sort of VPS service to look for:
Anything will work, but depending on the volume you are going to ingest/query, you will greatly benefit from a fast drive. In many cases the default disk drive for a VPS/Cloud provider will be slower than the SSD drive on your local development machine. See if the default works for you and otherwise select a better option when starting your virtual machine.
Has any one already installed a questdb in this way
Sure. If you want to install on AWS, Google Cloud, or Digital Ocean, you have detailed instructions at the QuestDB site. If you prefer to use Docker, it is fully supported, but a popular option is installing the binaries and just start QuestDB as a service.
Which webserver is the best for python
This one is tricky, as it really depends on what you will do with it. From your question I am getting the idea you want to run a small user-facing web application that will also run some background jobs. For that scenario a suitable framework can be Flask, which also offers some plugins for user/login management and for background tasks (using redis as a dependency)

How to distribute Django web app to users?

I am developing a Django app to run on every client computer separately. The question is, which technologies should I use to distribute such as docker, virtual machine etc.? How can I protect the Django app's code? How can I prevent to distribute without licenses?
I suggest you look into Heroku. They have a free plan too so you can test it without having to pay first. Their guide with django after you set up an app is good too. You will find everything in their documentation but if you get stuck, I recommend this video by Corey Schafer. Good luck
Edit: Heroku also supports Docker but I'm not too familiar with it. Might be useful to you

Proper way to build web app with Django on remote web server

I'm fairly new to Django so please excuse my ignorance.
I'm starting my first build of a web app on my remote web server. I am currently SSH'ing to the server and have started the Django project. I just launched the development server for the Django project, and it automatically serves at http://127.0.0.1:8000/.
My question is- what is the proper way to build a Django web app remotely on a server? Am I supposed to build the app on my computer and then transfer the project to my web server after it is complete? Or is there a way for me to access the development server without messing with the domains/ip addresses of the websites that are live on my web server?
Thanks!
This isn't really a Django specific question as the same basic methodology goes for any software development project.
Create different settings files for development and production. For Django this will involve setting the ALLOWED_HOSTS in your production settings as you mentioned as well as other settings. This checklist is helpful for this step. Use environment variables to hide secrets and set them in your development and production machines as appropriate.
Use a version control system such as git and push changes locally and then pull them onto your production server then run your Django server.
Do one better and setup a CI/CD pipeline to automate this
Yes, you're supposed to build working project on your computer :)
You probably cant efficiently write something good on the remote server.
Then you will probably create the git repository, for example on gitlab.
Then you will work on your computer building your project.
When you want to see it on the remote server, you will pull your project there.
It will be a little tricky for a first time to correctly deploy it, but it's not really that bad + there are a lot of good instructions out there, for ex:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
I hope I correctly understood your question.

How to deploy Django without mod_wsgi

I'm relatively new to web development, but I have a couple years experience working with python, so I figured I would build my first production site using django.
I've spent the last few days learning the basics and building a test site running on my local machine. Today, I've been trying to deploy my site to production; however, I've hit a pretty large stumbling block.
The django documentation suggests using mod_wsgi for apache deployments. I followed the install instructions here, only to realize that I don't have access to make any changes to apache - I'm currently on a shared hosting plan.
Apparently, to perform the install, I would have to upgrade to a VPS plan, which costs a lot more.
Any advice for a new web developer trying to get a proof-of-concept web app together (preferably with feedback gathered from real users) on a budget?
I think I have two options:
Eat the cost on my current web hosting plan. Try to find a cheaper host that specializes in django hosting. I've been looking at the following (suggestions here would be wonderful):
Heroku
DigitalOcean
A2Hosting
Try some sort of manual deployment. Is this possible or has anybody ever made this work? I can't seem to find any resources about this.
I was able to install django on my web server and my site just seems like a collection of files at this point. Can I simply move this directory tree to my site using an ftp? And handle the database migration manually?
Can I just run django on my web server via ssh instead of from my local machine? Essentially just rebuild my site in production? I know this isn't a smart idea with a normal production site, but as a proof of concept, I don't mind some downtime if my code is buggy.
Thanks!
If you are just starting Heroku is probably perfect for you. It's super easy to set up, you don't have to handle the server configuration yourself and it has a free tier to do some tests until you are ready to go.
If you are OK with doing some more advanced server/service configuration, you can use Google Cloud, Azure or AWS. This last one has a one year free tier. If you go this route, I suggest using gunicorn/uwsgi + nginx instead of Apache mod_wsgi.

Setting up a Python web development environment on OS X

I'm running Mac OS X Leopard and wanted to know what the easy way to setup a web development environment to use Python, MySQL, Apache on my machine which would allow me to develop on my Mac and then easily move it to a host in the future.
I've been trying to get mod_wsgi installed and configured to work with Django and have a headache now. Are there any web hosts that currently use mod_wsgi besides Google, so I could just develop there?
FWIW, we've found virtualenv [http://pypi.python.org/pypi/virtualenv] to be an invaluable part of our dev setup. We typically work on multiple projects that use different versions of Python libraries etc. It's very difficult to do this on one machine without some way to provide a localized, customized Python environment, as virtualenv does.
Most Python applications are moving away from mod_python. It can vary by framework or provider, but most development effort is going into mod_wsgi.
Using the WSGI standard will make your Python application server agnostic, and allow for other nice additions like WSGI middleware. Other providers may only provide CGI (which won't scale well performance wise), or FastCGI.
I've worked with Django using only the included server in the manager.py script and have not had any trouble moving to a production environment.
If you put your application in a host that does the environment configuration for you (like WebFaction) you should not have problems moving from development to production.
I run a Linux virtual machine on my Mac laptop. This allows me to keep my development environment and production environments perfectly in sync (and make snapshots for easy experimentation / rollback). I've found VMWare Fusion works the best, but there are free open source alternatives such as VirtualBox if you just want to get your feet wet.
I share the source folders from the guest Linux operating system on my Mac and edit them with the Mac source editor of my choosing (I use Eclipse / PyDev because the otherwise excellent TextMate doesn't deal well with Chinese text yet). I've documented the software setup for the guest Linux operating system here; it's optimized for serving multiple Django applications (including geodjango).
For extra added fun, you can edit your Mac's /etc/hosts file to make yourdomainname.com resolve to your guest Linux boxes internal IP address and have a simple way to work on / test multiple web projects online or offline without too much hassle.
What you're looking for is Mod_Python. It's an Apache-based interpreter for Python. Check it out here:
http://www.modpython.org/
Google App Engine has done it for you. Some limitations but it works great, and it gives you a path to hosting free.
Of course Mac OS X, in recent versions, comes with Python and Apache. However you may want to have more flexibility in the versions you use, or you may not like the tweaks Apple has made to the way they are configured. A good way to get a more generic set of tools, including MySQL, is to install them anew. This will help your portability issues. The frameworks can be installed relatively easily with one of these open source package providers.
Fink
MacPorts
MAMP
mod_wsgi is really, really simple.
Pyerweb is a really simple (~90 lines including comments/whitespace) WSGI-compliant routing-framework I wrote. Basically the WSGI API is just a function that gets passed environ, and wsgi_start_response, and it returns a string.
envrion is a dict with the request info, for example environ['PATH_INFO'] is the request URI)
wsgi_start_response which is a callable function which you execute to set the headers,:
wsgi_start_response(output_response, output_headers)
output_response is the string containing the HTTP status you wish to send (200 OK etc), and output_headers is a list-of-tuples containing your headers (for example, [("Content-type", "text/html")] would set the content-type)
Then the function returns a string containing your output.. That's all there is to it!
To run it, using spawning you can just do spawn scriptname.my_wsgi_function_nae and it will start listening on port 8080.
To use it via mod_wsgi, it's documentation is good, http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide and there is a django specific section
The up-side to using mod_wsgi is it's the standard for serving Python web-applications. I recently decided to play with Google App Engine, and was surprised when Pyerweb (which I linked to at the start of this answer) worked perfectly on it, completely unintentionally. I was even more impressed when I noticed Django applications run on it too.. Standardisation is a good thing!
You may want to look into web2py. It includes an administration interface to develop via your browser. All you need in one package, including Python.
Check out WebFaction—although I don't use them (nor am I related to / profit from their business in any way). I've read over and over how great their service is and particularly how Django-friendly they are. There's a specific post in their forums about getting up and running with Django and mod_wsgi.
Like others before me in this thread, I highly recommend using Ian Bicking's virtualenv to isolate your development environment; there's a dedicated page in the mod_wsgi documentation for exactly that sort of setup.
I'd also urge you to check out pip, which is basically a smarter easy_install which knows about virtualenv. Pip does two really nice things for virtualenv-style development:
Knows how to install from source control (SVN, Git, etc...)
Knows how to "freeze" an existing development environement's requirements so that you can create that environment somewhere else—very very nice for multiple developers or deployment.

Categories