Search code examples
pythonapacheflaskhttp-status-code-404

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

enter image description here

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

  • 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.