Flask routing throw 404 on production server - python

on my production server, my flask application throw 404 when i tried to access www.example.com/admin however, if i tried to access root content, everything works fine. I am using apache for my web server with python 3.4
#app.route('/')
def index():
return render_template("search.html")
. python functions in between
.
.
.
#app.route("/admin")
def admin():
return render_template("adminAuth.html", error=False)
#app.route("/admin", methods=["POST"])
def adminAuth():
# Intentionally return None
return None
Remarks: On my test environment, (PyCharm with Flask) everything works fine, I can access /admin
This is my WSGI conf
<VirtualHost *:80>
ServerName NAME
ServerAdmin admin#mywebsite.com
WSGIScriptAlias / /path/to/app.wsgi
<Directory /path/to/webapp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /path/to/webapp/static
<Directory /path/to/webapp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/path/to/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/path/to/access.log combined
</VirtualHost>

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

404 Not Found when deploying Flask on WSGI

I've been using Flask for a while for development but recently decided to address the "This server should not be used for production" warning. I've spent several hours trying to get wsgi to work to no avail.
Here's the structure of my /var/www:
webApp
webApp
static
templates
__init__.py
webapp.wsgi
My webapp.wsgi file:
#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/webApp/")
from webApp import app as application
application.secret_key = 'Add your secret key'
My webApp.conf file:
<VirtualHost *:80>
ServerName my_vps_ip
ServerAdmin email#mywebsite.com
WSGIScriptAlias / /var/www/webApp/webapp.wsgi
<Directory /var/www/webApp/webApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/webApp/webApp/static
<Directory /var/www/webApp/webApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And finally, my __init__.py file:
from flask import Flask
app = Flask(__name__)
#app.route('/', methods = ['GET'])
def test():
return 'test'
if __name__ == '__main__':
app.run()
And this happens when I visit the server ip:
Why could this be happening? I've spent all day trying to figure this out, so any help is appreciated!
Can You please check the serverName is correct and should be a registered domain.

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

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

Serving static files through apache

I am new to the whole mod_wsgi and serving files through apache. I am really comfortable with flask but this is something i can't get my head around. I did the hello-world program and successfully displayed hello world!
Now i wanted to display a image file. So I updated my hello-world.py to:
from flask import *
yourflaskapp = Flask(__name__)
#yourflaskapp.route("/")
def hello():
file="203.jpg"
return render_template("hello.html",file=file)
# return"HEY"
if __name__ == "__main__":
yourflaskapp.run()
my directory structure is something like:/var/www/hello-world
/hello-world
test.py
yourflaskapp.wsgi
/static
-203.jpg
/templates
-hello.html
My template is simple:
<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>
and my apache conf file is:
<VirtualHost *:80>
WSGIDaemonProcess yourflaskapp
WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
Alias /static/ /var/www/hello-world/static
Alias /templates/ /var/www/hello-world/templates
<Directory /var/www/hello-world>
WSGIProcessGroup yourflaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/hello-world/static>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/hello-world/templates>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Though when i open the browser and head to my ip, it doesn't show the image file.
What am i doing wrong? Is there any other approach i should follow?
and if anyone could recommend any good links from where i can understand working with flask+mod_wsgi+apache2
It is generally always a good idea to have trailing slash balanced when mounting static files at a sub URL. So instead of:
Alias /static/ /var/www/hello-world/static
use:
Alias /static /var/www/hello-world/static

Categories

Resources