'Static files not found Flask on Apache
I have a Flask application deployed on Apache, and the JavaScript files in the static
folder cannot be found
I don't understand what is wrong, here are the files:
Apache conf:
<VirtualHost *:80>
ServerName japanesepractice.local
ServerAlias www.japanesepractice.local
WSGIDaemonProcess japanesepractice user=leonardo group=leonardo threads=5
WSGIScriptAlias / /var/www/japanesepractice.local/japanese.wsgi
<Directory /var/www/japanesepractice.local>
WSGIProcessGroup japanesepractice
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
Alias /static/ /var/www/japanesepractice.local/static
<Directory /var/www/japanesepractice.local/static>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
app.py:
# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request
from service import kanjiservice
app = Flask(__name__)
@app.route('/')
def homepage():
return render_template('index.html')
wsgi file:
#!/usr/bin/python2
activate_this = '/var/www/japanesepractice.local/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/japanesepractice.local')
from app import app as application
template:
<script src="{{ url_for('static', filename='js/responsivevoice.js') }}"></script>
<script src="{{ url_for('static', filename='js/speaking.js') }}"></script>
Could someone help?
Solution 1:[1]
You should only use an Alias
for your /static/
routes if you don't want Apache to use your WSGIScriptAlias
to handle matching requests. This is good for performance, since requests for static files don't need to engage the WSGI application (except creating the URL), but it may be related to your problems.
You can troubleshoot by removing:
Alias /static/ /var/www/japanesepractice.local/static
<Directory /var/www/japanesepractice.local/static>
Order allow,deny
Allow from all
</Directory>
If removing this works, try re-adding it with balanced trailing slashes (/static/
as /static
to match the missing slash /var/www/japanesepractice.local/static
.
Solution 2:[2]
Try adding type="text/javascript"
like below:
`<script type="text/javascript" src="{{ url_for('static', filename='js/responsivevoice.js') }}"> .
</script>`
Solution 3:[3]
/static
not
/static/
The issue is that all the apache/debian and ubuntu forums online are incorrect. Its a major oversight. Logan Bertram above points to the issue as part of his answer.
With this and conda containers using the online tutorials in youtube I was able to expose something that was on localhost:5000 on a public server.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Logan Bertram |
Solution 2 | Mike Weston |
Solution 3 | Eamonn Kenny |