running a flask website in a subdirectory with apache

  • Last Update :
  • Techknowledgy :

I've tried checking the apache doc about virtual hosts but I can't make sense of it, here is my current 'sites-enabled/defaults' configuration file :

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        WSGIDaemonProcess bc user=bc group=bc threads=5
        WSGIScriptAlias / /var/www/bc/bc.wsgi
        <Directory /var/www/bc>
                   WSGIProcessGroup bc
                   WSGIApplicationGroup %{GLOBAL}
                   Order deny,allow
                   Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

my /var/www directory looks like this :

. / index.php

   . / bc /

   . / bc / index.py

Suggestion : 2

I'm using apache on my personal server, and I'm trying to run a 'main' website made with Silex on www.mydomain.com/ and another one made using Flask on www.mydomain.com/something,How can I get the python website to only work in www.mydomain.com/something and let the PHP one run on www.mydomain.com/ ?,It works just fine, I can reach my website at www.mydomain.com/something, but when I go back to www.mydomain.com/ I get a Flask 404 error message., 4 days ago I'm pretty new to programming/web dev in general. I have a Flask site hosted on Heroku and I'm trying to install Wordpress on a subdirectory (ex: /blog). I've created a separate app on Heroku that hosts my blog, but now I'm unsure how to combine the two (everything non /blog is routed using the Flask app and everything /blog is using Wordpress).


<VirtualHost *:80>         ServerAdmin [email protected]         DocumentRoot /var/www         <Directory />                 Options FollowSymLinks                 AllowOverride None         </Directory>         <Directory /var/www/>                 Options Indexes FollowSymLinks MultiViews                 AllowOverride None                 Order allow,deny                 allow from all         </Directory>         ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/         <Directory "/usr/lib/cgi-bin">                 AllowOverride None                 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch                 Order allow,deny                 Allow from all         </Directory>         WSGIDaemonProcess bc user=bc group=bc threads=5         WSGIScriptAlias / /var/www/bc/bc.wsgi         <Directory /var/www/bc>                    WSGIProcessGroup bc                    WSGIApplicationGroup %{GLOBAL}                    Order deny,allow                    Allow from all         </Directory>         ErrorLog ${APACHE_LOG_DIR}/error.log         # Possible values include: debug, info, notice, warn, error, crit,         # alert, emerg.         LogLevel warn         CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 
from flask
import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello world!"
if __name__ == "__main__": app.run()
#! /usr/bin/python3.6  import logging import sys logging.basicConfig(stream=sys.stderr) sys.path.insert(0, '/home/username/ExampleFlask/ExampleFlask') from my_flask_app import app as application application.secret_key = 'anything you wish'
<VirtualHost *:80># Add machine's IP address (use ifconfig command)ServerName 192.168.1.103# Give an alias to to start your website url withWSGIScriptAlias /testFlask /home/username/ExampleFlask/ExampleFlask/my_flask_app.wsgi<Directory /home/username/ExampleFlask/ExampleFlask/>		# set permissions as per apache2.conf file Options FollowSymLinks AllowOverride None Require all granted</Directory>ErrorLog ${APACHE_LOG_DIR}/error.logLogLevel warnCustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Suggestion : 3

August 17, 2015 , Updated: August 17, 2015

    I obviously have all the prerequisites already installed and/or running (Linux, Python, Apache, Flask, libapache2-mod-wsgi, wsgi, etc.). To give you an idea of my environment, this is my file structure:

/var/www / html /
   flaskapp /
   flaskapp.wsgi
flaskapp /
   __init__.py
static /
   templates /

    Basically, what I had to do was to modify my existing /etc/apache2/sites-available/000-default.conf file. I added the following lines in it:

WSGIDaemonProcess flaskapp user=www-data group=www-data threads=5 home=/var/www/html/flaskapp
WSGIScriptAlias /flaskapp /var/www/html/flaskapp/flaskapp.wsgi

<Directory /var/www/html/flaskapp/flaskapp>
    WSGIProcessGroup flaskapp
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptReloading On
    Order deny,allow
    Allow from all
</Directory>

    Note that the .wsgi file must be executable by the user specified in the user parameter in the WSGIDaemonProcess line. Pay special attention to the second line (WSGIScriptAlias), most documentation told me to write it like this:

WSGIScriptAlias / /var/www / html / flaskapp / flaskapp.wsgi

After all the modifications were done, I restarted Apache:

service apache2 restart

Suggestion : 4

I'm using apache on my personal server, and I'm trying to run a 'main' website made with Silex on www.mydomain.com/ and another one made using Flask on www.mydomain.com/something,How can I get the python website to only work in www.mydomain.com/something and let the PHP one run on www.mydomain.com/ ?,It works just fine, I can reach my website at www.mydomain.com/something, but when I go back to www.mydomain.com/ I get a Flask 404 error message.,To run the Flask website I'm using mod_wsgi as explained on their official website (http://flask.pocoo.org/docs/deploying/mod_wsgi/).

I've tried checking the apache doc about virtual hosts but I can't make sense of it, here is my current 'sites-enabled/defaults' configuration file :

<VirtualHost *:80>        ServerAdmin webmaster@localhost        DocumentRoot /var/www        <Directory />                Options FollowSymLinks                AllowOverride None        </Directory>        <Directory /var/www/>                Options Indexes FollowSymLinks MultiViews                AllowOverride None                Order allow,deny                allow from all        </Directory>        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/        <Directory "/usr/lib/cgi-bin">                AllowOverride None                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch                Order allow,deny                Allow from all        </Directory>        WSGIDaemonProcess bc user=bc group=bc threads=5        WSGIScriptAlias / /var/www/bc/bc.wsgi        <Directory /var/www/bc>                   WSGIProcessGroup bc                   WSGIApplicationGroup %{GLOBAL}                   Order deny,allow                   Allow from all        </Directory>        ErrorLog ${APACHE_LOG_DIR}/error.log        # Possible values include: debug, info, notice, warn, error, crit,        # alert, emerg.        LogLevel warn        CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

my /var/www directory looks like this :

. / index.php. / bc / . / bc / index.py