Jump to content

How to use Flask on Plesk


Krydos

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:

    Options +ExecCGI
    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.

Edited by Krydos
Added Options +ExecCGI
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

This particular example is for Flask in a subdirectory, but it is possible to run flask at the webroot so it's just domain.com instead of domain.com/subdirectory. You have to make some slight changes to .htaccess, flask.wsgi, and myapp.py. The reason the default example uses a subdirectory is so people can host different projects in different subdirectories rather than dedicating the whole domain to one Flask app.

Let me know if you'd like to see the changes, and I can post the code that you would use to make Flask run at the web root.

Link to comment
Share on other sites

  • 3 weeks later...

Hi Krydos. I am still working my way through dealing with my database, currently sqlite (and the differences in Python versions - 3.10 on my development machine, vs 3.7). Although the ability to run multiple apps is attractive, I would be interested in how you could run an app at web root, too, if you could explain? Thanks.

Link to comment
Share on other sites

For anyone interested, here's a zip file with Krydos' Flask example: https://kairion.helioho.st/flasktest.zip

Just download it and extract on httpdocs (Plesk's new account) or public_html (cPanel's transferred account), open flask.wsgi (which is inside the directory flasktest that'll be created when extracting the downloaded zip file) and find:

sys.path.append("/home/YOUR_DOMAIN_HERE/httpdocs/flasktest");

And change YOUR_DOMAIN_HERE with your actual domain, e.g.:

sys.path.append("/home/kairion.helioho.st/httpdocs/flasktest");

And then you can access your Flask example: https://kairion.helioho.st/flasktest/

On 1/20/2023 at 11:56 PM, krsmith28p said:

Hi Krydos. I am still working my way through dealing with my database, currently sqlite (and the differences in Python versions - 3.10 on my development machine, vs 3.7). Although the ability to run multiple apps is attractive, I would be interested in how you could run an app at web root, too, if you could explain? Thanks.

@krsmith28p Here's a zip file with Krydos' Flask example adapted to run on web root (httpdocs for Plesk's new account or public_html for cPanel's transferred account): https://kairion.helioho.st/flasktest_on_webroot.zip

Just extract it on your web root (as indicated above), remove all index files that it may have (index.html, index.htm, index.php etc.), open the file flask.wsgi and find:

sys.path.append("/home/YOUR_DOMAIN_HERE/httpdocs");

And change YOUR_DOMAIN_HERE with your actual domain, e.g.:

sys.path.append("/home/kairion.helioho.st/httpdocs");

It will work the same way as Krydos' original example. You can read these three files and check the differences between them and Krydos' originals. It's actually really easy: I just found all references for "flasktest" (the directory we make in the original example) and removed them (or changed them to a simple "/").

I'm not a Python/Django/Flask expert, but feel free to ask any questions and if I can help, I'll be glad to.

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

×
×
  • Create New...