使用cpanel在共享主机上部署简单的flask应用,但只有root url可以访问



我在使用cpanel在共享主机服务器上部署Flask应用程序时遇到了麻烦。主页(subdomain.domain.com)加载正常,但当我点击其他链接时,如"联系我们"。或"关于我们(subdomain.domain.com/about-us),"我得到一个"404 URL未找到"错误。项目结构如下:根文件夹包含一个"website";文件夹,其中包含"模板"、";"静,"init.py,"views.py,"one_answers";auth.py"文件。此外,还有一个"app.py"one_answers";passenger_wsgi.py">

在模板我使用{{url_for('views.aboutus')}}我的主页正在加载,但如果我点击其他页面,我得到404 url

我的问题是类似的:Python flask应用路由在cpanel:只能访问根url任何答案对我都没用这是我的项目结构:

|-->root folder
|--->website
|-->Templates
|-->static
|-->__init__.py
|-->views.py
|-->auth.py
|-->app.py
|-->passenger_wsgi.py

我试图改变。htaccess文件,但仍然不工作

这是我的。htaccess文件:-
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/user/public_html/subdomain.domain.com"
PassengerBaseURI "/"
PassengerPython "/home/user/virtualenv/public_html/subdomain.domain.com/3.8/bin/python"
PassengerAppLogFile "/home/user/public_html/subdomain.domain.com/error.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
RewriteRule ^(static/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

passenger_wsgi.py文件:

import os
import sys

sys.path.insert(0, os.path.dirname(__file__))
from app import app as application

app.py文件:

from website import createApp
app = createApp()
if __name__ == '__main__':
app.run()

init.py文件在website文件夹下:


import os
from flask import Flask

def createApp():
app = Flask(__name__)

# static folder route
app.static_folder = os.path.join(os.path.dirname(__file__), 'static/')

# temlates folder route
app.template_folder = os.path.join(os.path.dirname(__file__), 'templates/')
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/auth/')
return app

和my views.py是:


from flask import Blueprint, render_template
views = Blueprint('views', __name__)

@views.route('/')
def home():
return render_template('index.html')
# return "<h4>Hello</h4>"

@views.route('/about-us',methods=['GET'])
def aboutus():
# return render_template('about.html')
return "<h4>Hello</h4>"


@views.route('services/')
def services():
return render_template('services.html')

当我去subdomain.domain.com我的主页正在加载。但如果我点击其他页面的url,我得到404 url没有找到,它是完美的工作在本地主机

我想我能帮你。

将您的passenger_wsgi.py文件更改为:

import imp
import os
import sys

sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'app.py')
application = wsgi.app
并将app.py改为:
from website import create_app
app = create_app()
application = app

这是因为通常用于在服务器上运行Python的WSGI软件需要将主应用程序变量称为application。当然,这取决于主机服务器。

我希望这对你有帮助

我刚刚更新了我的。htaccess文件,它解决了这个问题

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/user/public_html/subdomain.domain.com"
PassengerBaseURI "/"
PassengerPython "/home/user/virtualenv/public_html/subdomain.domain.com/3.8/bin/python"
PassengerAppLogFile "/home/user/public_html/subdomain.domain.com/error.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
RewriteEngine on
RewriteRule ^(static/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [L]

最新更新