hosting multiple Flask apps for unique domains - python

I have two unique domains (eg. domain1.com and domain2.com). I am trying to host two unique webpages (via two separate flask apps) on one linode server (eg. 12.34.567.890) using Flask.
Currently, domain1.com works, but domain2.com gives me the Apache2 default page.
Here's what I have so far:
etc\apache2\ports.conf:
Listen 80
Listen 8080
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
etc\apache2\sites-available\000-default.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
etc\apache2\sites-available\FlaskApp.conf:
<VirtualHost *:80>
ServerName domain1.com
ServerAdmin admin#email.com
WSGIScriptAlias / /var/www/AppOne/flaskapp.wsgi
<Directory /var/www/AppOne/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/AppOne/FlaskApp/static
<Directory /var/www/AppOne/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8080>
ServerName domain2.com
ServerAdmin admin#email.com
WSGIScriptAlias / /var/www/AppTwo/flaskapp.wsgi
<Directory /var/www/AppTwo/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/AppTwo/FlaskApp/static
<Directory /var/www/AppTwo/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
__init__.py for each app is the same (but in different directories):
from flask import Flask, render_template, flash, request, url_for, redirect, session
from content_management import Content
#...etc...
import gc
TOPIC_DICT = Content()
app = Flask(__name__)
#app.route('/')
def homepage():
return render_template("home.html")
if __name__ == "__main__":
app.run()
The \var\www\ directory looks like this:
\var\www
|
└─── AppOne
| | flaskapp.wsgi
| |
| └─── FlaskApp
| | __init__.py
| |
| └─── static
| └─── templates
| | home.html
| |
| └─── venv
|
└─── AppTwo #same as AppOne
| flaskapp.wsgi
|
└─── FlaskApp
| __init__.py
|
└─── static
└─── templates
| home.html
|
└─── venv
Sorry if I got a bit verbose, but any guidance would help!

No need to listen on port 8080. WSGI file for AppTwo wasn't point to the right directory.
ports.conf:
Listen 80
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
000-default.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
FlaskApp.conf:
<VirtualHost *:80>
ServerName domain1.com
ServerAdmin admin#email.com
WSGIScriptAlias / /var/www/AppOne/flaskapp.wsgi
<Directory /var/www/AppOne/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/AppOne/FlaskApp/static
<Directory /var/www/AppOne/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
ServerAdmin admin#email.com
WSGIScriptAlias / /var/www/AppTwo/flaskapp.wsgi
<Directory /var/www/AppTwo/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/AppTwo/FlaskApp/static
<Directory /var/www/AppTwo/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
AppOne/flaskapp.wsgi:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/AppOne/")
from FlaskApp import app as application
application.secret_key = 'PasswordGoesHere'
AppTwo/flaskapp.wsgi:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/AppTwo/")
from FlaskApp import app as application
application.secret_key = 'PasswordGoesHere'

Related

Can I create a flask webapp in the same url with other pages(like wordpress)?

I have a self-hosted server, only for my lan, with a Wordpress(miservidor.com) and Owncloud(miservidor.com/owncloud) page, those pages work perfectly, and i recentlly decided to create a webapp with flask under the same domain like miservidor.com/musicdownloader. I have tried to make it works but with no results, my config files are these:
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName miservidor.com
Redirect / https://miservidor.com/
</VirtualHost>
/etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory />
AllowOverride ALL
</Directory>
</VirtualHost>
</IfModule>
/etc/apache2/config-available/musicdownloader.conf
<VirtualHost *:80>
WSGIScriptAlias /musicdownloader /var/www/musicdownloader/musicdownloader.wsgi
<Directory /var/www/musicdownloader/musicdownloader/>
Order allow,deny
Allow from all
</Directory>
Alias /musicdownloader/static /var/www/musicdownloader/musicdownloader/static
<Directory /var/www/musicdownloader/musicdownloader/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/config-available/owncloud.conf
Alias /owncloud "/var/www/owncloud/"
<Directory /var/www/owncloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/owncloud
SetEnv HTTP_HOME /var/www/owncloud
</Directory>
My web app hierarchy is (in /var/www/):
|--musicdownloader/
|----musicdownloader.wgsi
|----musicdownloader/
|------app.py
|------static/
|--------main.css
|------templates/
|--------musicdownloader.html
|--------download.html
musicdownloader.wgsi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/musicdownloader/")
from musicdownloader import app as application
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/musicdownloader', methods=["POST","GET"])
def musicdownloader():
return render_template('musicdownloader.html')
if __name__ == "__main__":
app.run()
Is this the correct way to do it? It is better to use subdomains or different domains with the same ip? Thanks in advance.
I found the solution, I can do it by changing the content I put in the file /etc/apache2/config-available/musicdownloader.conf to:
WSGIScriptAlias /musicdownloader /var/www/musicdownloader/musicdownloader.wgsi
WSGIDaemonProcess musicdownloader python-path=/var/www/musicdownloader:/var/www/musicdownloader/musicdownloader/venv/lib/python3.8/site-packages
WSGIProcessGroup musicdownloader
<Directory /var/www/musicdownloader/musicdownloader/>
Require all granted
</Directory>
Alias /musicdownloader/static /var/www/musicdownloader/musicdownloader/static
<Directory /var/www/musicdownloader/musicdownloader/static/>
Require all granted
</Directory>
Then:
sudo systemctl reload apache2
And now I can have my flask app in:
miservidor.com/musicdownloader

Serve one flask app at root and another at a different path with apache2 and mod_wsgi

I am trying to serve one flask app directly at localhost, and another at localhost/menus.
When my apache configuration is like this:
<VirtualHost *:80>
ServerName localhost
# logs configuration -------------------------------------------------
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# front page ---------------------------------------------------------
WSGIDaemonProcess flask_ac user=#1000 group=#1000 threads=5
WSGIScriptAlias /front /var/www/flask_ac/hookup.wsgi
<Directory /var/www/flask_ac>
WSGIProcessGroup flask_ac
WSGIApplicationGroup flask_ac
Require all granted
</Directory>
# start menus --------------------------------------------------------
WSGIDaemonProcess menus user=#1000 group=#1000 threads=5
WSGIScriptAlias /menus /var/www/flask_ac/projects/menus/hookup.wsgi
<Directory "/var/www/flask_ac/projects/menus/">
WSGIProcessGroup menus
WSGIApplicationGroup menus
Require all granted
</Directory>
It works fine, but it is serving the first app at localhost/front instead of just localhost.
If I change my first WSGIScriptAlias to:
WSGIScriptAlias / /var/www/flask_ac/hookup.wsgi
It will serve the first app correctly at localhost, but the second app at localhost/menus breaks, and returns not found.
I found this answer about a similar issue, but it doesn't address this directly, and I cant seem to figure it out.
Change the order such that the application for the sub URL is first.
WSGIScriptAlias /menus /var/www/flask_ac/projects/menus/hookup.wsgi
...
WSGIScriptAlias / /var/www/flask_ac/hookup.wsgi
Documentation mentioning ordering at:
http://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#the-wsgiscriptalias-directive

Problems with www prefix in flask after setting SERVER_NAME for subdomains

After I set the variable SERVER_NAME to be able to use subdomains with blueprints, all pages that before worked with www. as well does not work anymore.
I set
SERVER_NAME = mydomain.com
my apache sites-available conf files looks like this
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin email#mydomain.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
< VirtualHost *:80>
ServerName subdomain.mydomain.com
ServerAdmin email#mydomain.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Is there a proper way of fixing this, or is the only way some redirects?
Have to added A record for www.
If not please add the same:
goto : DNS -> SELECT DOMAIN -> ADD RECORD -> Select record type "A"
hostname : www
IP Adress : {Your Public IP}

Openstack Kilo dashboard forward

I've setup the Kilo in LAN, which can't be accessed by the external network. Now, I'd like to do a forwarding to make it be accessed from outside. How to set the ProxyPass and ProxyPassReverse?
I asked this because for Kilo dashboard, the login decorator will redirect http://host.ip/dashboard --> http://host.ip/dashboard/auth/login/?next=/dashboard/. And as a result, the access via forwarding will fail.
Can anyone help?
P.S. (apache configuration)
# ************************************
# Vhost template in module puppetlabs-apache
# Managed by Puppet
# ************************************
<VirtualHost *:80>
ServerName xxx.xxx.com
## Vhost docroot
DocumentRoot "/var/www/"
## Alias declarations for resources outside the DocumentRoot
Alias /dashboard/static "/usr/share/openstack-dashboard/static"
## Directories, there should at least be a declaration for /var/www/
<Directory "/var/www/">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
## Logging
ErrorLog "/var/log/httpd/horizon_error.log"
ServerSignature Off
CustomLog "/var/log/httpd/horizon_access.log" combined
## RedirectMatch rules
RedirectMatch permanent ^/$ /dashboard
## Server aliases
ServerAlias 10.xxx.xxx.xxx
ServerAlias xxx.xxx.com
ServerAlias localhost
WSGIDaemonProcess dashboard group=apache processes=3 threads=10 user=apache
WSGIProcessGroup dashboard
WSGIScriptAlias /dashboard "/usr/share/openstack-dashboard/openstack_dashboard/wsgi/django.wsgi"
</VirtualHost>
The issue is now fixed. And it works now. The solution is:
add the external domain name http://external.domain.name/ to virtualHost configuration as a serverAlias as shown below:
ServerAlias external.domain.name

Apache, Web.py and Mod_WSGI Issue - Page Not Found

Please your help to know what is wrong with my configuration Apache + Web.py + Mod_WSGI and my website cannot be loaded, this is my config:
Apache version: 2.7.4
Web.py version -> https://github.com/webpy/webpy
mod_wsgi -> not sure if 3.3 or 3.4
Directory
/var/www/pyapps/bot
web -> link webpy
webpy
code.py
static
templates
Apache
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>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
CustomLog ${APACHE_LOG_DIR}/access.log combine
WSGIScriptAlias /sitea /var/www/pyapps/bot/code.py/
Alias /sitea/static /var/www/pyapps/bot/static
Alias /sitea/templates /var/www/pyapps/bot/templates
AddType text/html .py
<Directory /var/www/pyapps/bot>
Order deny,allow
Allow from All
</Directory>
<Directory /sitea/static>
Options +Indexes
</Directory>
Code.py
import web
urls = (
'/.*', 'hello',
)
class hello:
def GET(self):
return "Hello, world."
application = web.application(urls, globals()).wsgifunc()
Result
Not Found
The requested URL /sitea was not found on this server.
Apache/2.2.22 (Ubuntu) Server at xxx.xxx.xxx.xxx Port 80
You shouldn't have a trailing slash on the end of the WSGIScriptAlias path:
WSGIScriptAlias /sitea /var/www/pyapps/bot/code.py
Also note you shouldn't really have your WSGI application files inside the document root (/var/www). Put them somewhere else (and change the WSGIScriptAlias path accordingly).

Categories

Resources