Jump to content

How to use Flask on Plesk


Krydos
 Share

Recommended Posts

We will update our wiki article on how to get started with Flask eventually, but just posting a quick example here so people can comment and ask questions on this thread.

  1. Create a directory on your main domain called flasktest. If you were transferred from cPanel your main domain will be parked on the public_html directory. If you created a new account on Plesk your directory will be httpdocs.

  2. Create an .htaccess file inside the flasktest directory with these contents:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(media/.*)$ - [L]
    RewriteRule ^(admin_media/.*)$ - [L]
    RewriteRule ^(flask\.wsgi/.*)$ - [L]
    RewriteRule ^(.*)$ flasktest/flask.wsgi/$1 [QSA,PT,L]
  3. Create a file named flask.wsgi inside the flasktest directory with these contents:
    import os, sys
    
    # edit your path below
    sys.path.append("/home/domain.helioho.st/httpdocs/flasktest");
    
    sys.path.insert(0, os.path.dirname(__file__))
    from myapp import app as application
    
    # set this to something harder to guess
    application.secret_key = 'secret'
  4. Create a file named myapp.py inside the flasktest directory with these contents:
    import sys
    
    from flask import Flask, __version__
    app = Flask(__name__)
    application = app
    
    @app.route("/")
    def hello():
      return """
    
        Flask is working on HelioHost.<br><br>
        <a href="/flasktest/python/version/">Python version</a><br>
        <a href="/flasktest/flask/version/">Flask version</a>
    
      """
    
    @app.route("/python/version/")
    def p_version():
      return "Python version %s<br><br><a href='/flasktest/'>back</a>" % sys.version
    
    @app.route("/flask/version/")
    def f_version():
      return "Flask version %s<br><br><a href='/flasktest/'>back</a>" % __version__
    
    if __name__ == "__main__":
      app.run()
  5. Make sure your directory structure and files look like this:
    flasktest/
    ├── flask.wsgi
    ├── .htaccess
    └── myapp.py
    
    0 directories, 3 files
  6. If you did everything right it should look like this: https://krydos.heliohost.org/flasktest/

*** Please note that since wsgi uses server side caching your changes might not appear immediately, and in some cases might take several hours to update. We recommend developing your Flask app on your home computer and hosting the production copy on the server.

Link to comment
Share on other sites

  • 1 month later...

I've followed the guide on gtom.helioho.st/flasktest/ but it returns 

Forbidden

You don't have permission to access /flasktest/flask.wsgi/ on this server.

 

I was missing in .htaccess

Options +ExecCGI
AddHandler cgi-script .py

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...