You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Ubuntu服务器优先运行__init__.py而非index.html作为首页

Troubleshooting: Flask Route Returns "Hello World" But Shows index.html Instead

Hey there, let's work through why your Flask route isn't behaving as expected and you're seeing index.html instead of your "Hello World" message. Here are the most likely fixes to try:

1. Fix the Module Import Case in Your WSGI File

Looking at your flaskapp.wsgi code, you have this line:

from Flaskapp import app as application

On Linux, both Python module names and filesystem paths are case-sensitive. Since your config references paths like /var/www/flaskapp/flaskapp/ (all lowercase), this uppercase Flaskapp import will fail. When Apache can't load your Flask app, it falls back to serving default static content like index.html.

Quick Fix: Update the import line to match your directory name exactly:

from flaskapp import app as application

After making this change, restart Apache to apply the update:

sudo service apache2 restart

2. Prevent Apache from Serving Default Static Index Files

Apache might be prioritizing an existing index.html file over your Flask route. Let's adjust your config to block this:

  • First, confirm there's no accidental index.html file in /var/www/flaskapp/flaskapp/ (the root of your Flask app directory).
  • Add a DirectoryIndex disabled line inside your <VirtualHost> block to tell Apache not to look for default index files:
<VirtualHost *:80>
    # ... your existing config lines ...
    DirectoryIndex disabled
</VirtualHost>

Restart Apache again after modifying the config.

3. Double-Check Your Project Structure

Make sure your project matches the structure your config expects:

/var/www/flaskapp/
├── flaskapp/
│   ├── __init__.py
│   └── static/
└── flaskapp.wsgi

If your __init__.py isn't nested inside the /var/www/flaskapp/flaskapp/ subdirectory, the WSGI script won't be able to import your Flask app correctly.

4. Dig Into Apache Error Logs for Clues

If the above steps don't resolve the issue, check the Apache error log to uncover hidden problems like permission issues or other import errors:

tail -f ${APACHE_LOG_DIR}/error.log

Look for lines mentioning "import error" or "permission denied"—these will point you directly to the root cause.

After trying these steps, reload your website and see if the "Hello World" message appears as you expected.


内容的提问来源于stack exchange,提问作者Evgenis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:31:37