skip to Main Content

I am having trouble deploying a Flask application on a shared hosting server using cpanel. The home page (subdomain.domain.com) is loading correctly, but when I click on other links such as "Contact Us" or "About Us (subdomain.domain.com/about-us)," I am getting a "404 URL Not Found" error.
The project structure is as follows: the root folder contains a "website" folder, which contains a "Templates," "static," "init.py," "views.py," and "auth.py" files. Additionally, there is an "app.py" and "passenger_wsgi.py" file in the root folder.

in templates i am using {{ url_for(‘views.aboutus’) }} My home page is loading but if i click on other pages i am getting 404 url

my question is similar: Python flask app routing in cpanel: can only access root url
any answer is not working for me
this is my project structure:

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

i tried to change .htaccess file but still it is not working

This is my .htaccess file:-

# 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 file:

import os
import sys


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

app.py file :

from website import createApp

app = createApp()

if __name__ == '__main__':
    app.run()

init.py file inside the website folder:


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

and my views.py is:


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')

when i go to subdomain.domain.com my Home page i loading. but if i click on other page url i am getting 404 url not found, it is working perfectelly on localhost

2

Answers


  1. Chosen as BEST ANSWER

    I just updated my .htaccess file and it resolved the issue

    # 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]
    

  2. I think I can help you.

    Change your passenger_wsgi.py file to:

    import imp
    import os
    import sys
    
    
    sys.path.insert(0, os.path.dirname(__file__))
    
    wsgi = imp.load_source('wsgi', 'app.py')
    application = wsgi.app
    

    and change app.py to:

    from website import create_app
    
    app = create_app()
    application = app
    

    This is because WSGI software usually used to run Python on servers requires the main application variable to be called application. Of course this depends on the hosting server.

    I hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search