Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running django and flask on same apache server

I am trying to run to run django and flask on the same apache server.

WSGISocketPrefix /var/www/wsgi
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName  domain.com
        ServerAlias www.domain.com
        DocumentRoot /var/www/
        LogLevel warn
        WSGIDaemonProcess apache processes=2 maximum-requests=500 threads=1
        WSGIProcessGroup apache
        Alias /media /var/www/media/

        WSGIScriptAlias / /var/www/djangoapps/django.wsgi
        WSGIScriptAlias /app1 /var/www/flaskapps/app.wsgi
</VirtualHost>
  1. The first WSGIScriptAlias runs a django app in the root: domain.com.
  2. The second instance of WSGIScriptAlias needs to run a flask app in a subdomain: app1.

But since the main site sits over django, when I try to hit: domain.com/app1, django's urls.py tries to handle that url command. But urls.py should not handle it, since its an independent flask app.

Any ideas how can I go about it?

like image 532
zengr Avatar asked Feb 20 '12 06:02

zengr


People also ask

Can Flask and Django be used together?

Application dispatching is the process of combining multiple Flask applications on the WSGI level. You can not only combine Flask applications into something larger but any WSGI application. This would even allow you to run a Django and a Flask application in the same interpreter side by side if you want.

Can Flask run on Apache server?

Create a python virtual environment for your application and check that you can run your application. Configure your Web Server Gateway Interface (WSGI) file. This is how your Flask application will talk to Apache. Set up the Apache configuration for your site.

Can I use Django with Apache?

Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You'll probably want to start with the installation and configuration documentation.

Does Flask use Apache or Nginx?

Flask is just a web framework and not a web server. Thus to serve a flask application, a web server such as Gunicorn, Nginx or Apache is required to accept HTTP requests.


2 Answers

I'm not sure if this would solve the problem, but have you tried changing the order of your script alias so that /app1 is found before / ?

WSGISocketPrefix /var/www/wsgi
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName  domain.com
        ServerAlias www.domain.com
        DocumentRoot /var/www/
        LogLevel warn
        WSGIDaemonProcess apache processes=2 maximum-requests=500 threads=1
        WSGIProcessGroup apache
        Alias /media /var/www/media/
        WSGIScriptAlias /app1 /var/www/flaskapps/app.wsgi
        WSGIScriptAlias / /var/www/djangoapps/django.wsgi

</VirtualHost>
like image 101
danspants Avatar answered Sep 23 '22 16:09

danspants


For anyone who want to achieve the same in 2018 this really helped me:

https://www.phusionpassenger.com/library/deploy/apache/deploy/python/

I know it is off topic, but I found this question like 20 searches before I found the link to the Description from Passenger....

How ev's here is an excert from the tutorial:

<VirtualHost *:80>
    ServerName www.phusion.nl
    DocumentRoot /websites/phusion/public
<Directory /websites/phusion>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    #Require all granted
</Directory>

Alias /subapp /websites/secondapp/public
<Location /subapp>
    PassengerBaseURI /subapp
    PassengerAppRoot /websites/secondapp

    PassengerAppType wsgi
    PassengerStartupFile passenger_wsgi.py
</Location>
<Directory /websites/secondapp/public>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    #Require all granted
</Directory>

like image 23
nyx00 Avatar answered Sep 23 '22 16:09

nyx00