Run multiple Flask apps on the same domain - python

I want to run several Flask apps such that...
mydomain.com is running one app in one directory.
mydomain.com/app1 is running another app in another directory.
mydomain.com/app2 is running a third app in a third directory.
I want to specifically avoid having to have a URL structure like app1.mydomain.com
Is this possible under Apache?

Related

Prevent website users to access files in directory of a flask app

I have just deployed my first flask app, however, I have found out that it is really easy to download all the code I spent so long writing. For example, if my app was app.com, users can simply go to the website /app.py and my app.py would get automatically downloaded. Same for the passenger_wsgi.py and every other folder short of a db.
How can I disable that without compromising the integrity of my flask app?

how can i convert django app to desktop app

I have a Django app up and running on DigitalOcean with Nginx and PostgreSQL. But some clients want an offline version of the app so that their data remains on their systems and they don't have to connect to the internet. One solution is to write the whole app from scratch but that would take time and cost.
I was thinking of a solution where I can convert the Django app into a desktop app with minimal changes i.e. replace CDNs with files and remove functionality that requires internet. But I don't know how can I do this.
I was thinking of electron, i,e, elctron will spawn a child process which will start django's server and then the electron will load 127.0.0.1:8000 in a webview.
But how can I package this app into an executable because it would need python installed and configured on the user's system. Or does python itself has any library that can convert the Django app into a desktop app?
Below is the file structure of my Django project
project_folder/
app_1/
app_2/
app_3/
configurations/
templates/
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
media/
staticfiles/
manage.py
Any help would be appreciated.

mod_wsgi: How to run different apps under single domain?

I have a domain example.com and I run a standard Apache there (serving static files and PHP). I want to run Python served pages on subdomain.example.com.
I managed to configure Apache to do this and there is a Flask app running at subdomain.example.com. However, in the virtual host config file, the whole subdomain is tight to this one single app. I would like to go further and run several different apps on this subdomain.
For example:
subdomain.example.com/app1/ would run /var/www/apps/app1/app.wsgi
subdomain.example.com/app2/ would run /var/www/apps/app2/app.wsgi
and so on...
Furthermore I would like this to be fully automatic, that is when I set up new folder in /var/www/apps/, I could reach the app through the Apache without further configuration.
I can see several ways of doing this:
Configure Apache to route every subdomain.example.com request
to a single "meta app" in Python which would run a specific app
based on given URL.
Do some magic with Apache configuration that would take care of
this automatically.
Maybe use nginx? I don't really have much experience with this,
but someone told me this could solve the problem.
Is there any best practice about how to do this? Thank you.
It looks like you should be able to do this by providing a directory path (in your case, /var/www/apps) to the WSGIScriptAlias directive. Read more here:
https://modwsgi.readthedocs.org/en/develop/configuration-directives/WSGIScriptAlias.html
This seems like a neater solution than using a meta app.

Hosting Django and Path to Project Directory

I am using a digital ocean virtual server to host a django web app. when you create the droplet, it creates a default app in the directory /home/django/django_project/django. However seeing as this isn't the app I want to host, I put my application files into the directory /home/myproject/myapp and updated the gunicorn and nginx configs to point there. However once updating the urls for this app, I am trying to see it online and noticed that the 404 looked like this:
which states that the URLconf is defined in django_project.urls which I had since deleted out of that 'home/django directory. I have reloaded and restarted gunicorn and nginx in an attempt to get django to realize that the project django_project doesn’t exist any more, but no luck. Has anyone run into this before or have any suggestions as to what I should try next?

Where is Flask running from on my OS X machine?

I'm used to building my websites with PHP, and on my OS X machine I expect to have to ensure that I have my scripts living in an explicitly specified location that I define as my Apache server's document root. But when I follow the simple instructions for building a Flask website, I magically get a working website, with nothing at all in any of the places on my machine that serve as document roots, regardless of where I have my Flask script. This is especially confusing since I always think if deployment as involving careful duplicating the file structure of my site under document root on the deployment server's document root.
Where is Flask "running from" on my OS X machine? Where do I "put it" when I deploy it (and what to I put)?
It's running from wherever you put it. You surely know where you saved the code: that's where it is.
But your mistake is in thinking that this development environment is running through Apache, or indeed has anything to do with how you'll run it in production. Neither is true. You're using a development server, the separate Werkzeug project, but that is not suitable for running in prod.
When you are ready to deploy, Flask has full instructions on how to either connect it to Apache through mod_wsgi, or set up a separate WSGI server which you'll usually connect to through a reverse proxy such as nginx.
Supposed you have your main.py under /path/to/my_project/, when you run the internal server python main.py, Flask is then running under your project folder.
Of course that built-in server is only good for development, when you're trying to deploy for production, normally Gunicorn (via wsgi app, read more HERE) or other web server is more appropriated (and advised by Flask) itself. And your production folder can be placed wherever you want, just like Apache PHP you may place your folder under /var/www/ (EDITED: as Daniel Roseman pointed out, you may try to change this folder location for security concern), it's the same for Flask, that's nothing stops you placing the folder but rather have the permission set properly.
Hope this helps.

Categories