• Tutorials
  • HOWTO run Flask under uwsgi on Opalstack

Here's how to spin up a basic Flask app using uwsgi on your Opalstack server:

(This example uses myuser as the shell user name and myapp as the app name.)

  1. Create a new shell user via your Opalstack dashboard. You can use an existing shell user if you want.
  2. Create a new uwsgi app via your Opalstack dashboard and attach it to whatever website.
  3. SSH to the server as your shell user and run the following commands to install flask:
cd ~/apps/myapp
source env/bin/activate
pip install flask
  1. Upload your flask project directory into ~/apps/myapp. The rest of this example assumes the project directory name is "myproject" containing an empty __init__.py file and a hello.py containing the Flask app:
# myproject/hello.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
  1. Edit your `uwsgi.ini to change this part...
# adjust the following to point to your project
wsgi-file = /home/myuser/apps/myapp/myproject/wsgihandler.py
touch-reload =  /home/myuser/apps/myapp/myproject/wsgihandler.py

... to this:

# adjust the following to point to your project
python-path = /home/myuser/apps/myapp/myproject/
wsgi = hello:app
  1. Stop the app and restart it:
~/apps/myapp/stop
sleep 2
~/apps/myapp/start

That's it, you're done :-)

The only tricky part here is figuring out the wsgi directive in your uwsgi.ini for your own project, but it will usually be projectdir:app.

If your Flask app isn't organized like the example above then feel free to post here to let me know what you've got and I'll help you wire it up. 🙂

Edit: I've sorted it now by adding
pythonpath= /home/path/to/my/python/files
to uwsgi.ini and re-starting the server


Thanks for this but I am struggling now with the fact that some of my modules are not loading e.g.
mounting myapp:app on /
Traceback (most recent call last):
File "./myapp/__init__.py", line 12, in <module>
from SHDatabase import SHDatabase
ModuleNotFoundError: No module named 'SHDatabase'

SHDatabase is in the same directory as __init__.py so I'm a little confused.

The fact it is trying shows that __init__.py is found at least.

    ssherlock What about from .SHDatabase import SHDatabase? I've seen some issues with importing modules in the same directory which can be solved by being explicit about it being a same-directory import.

      kylotan Thanks, I will try and remember that, but since adding to PYTHONPATH all is working fine now.
      Cheers

      Okay, but obviously be aware that relying on a change to PYTHONPATH will mean it only works in that particular context - whereas if you can fix it inside the package, it would work everywhere. 🙂

      2 months later

      I am somewhat new to flask, but I believe I am following the tutorial exactly step by step, but I keep getting the following errors in the uwsgi.log

      *** Operational MODE: preforking+threaded ***
      mounting myproject:app on /
      Traceback (most recent call last):
      File "./myproject/init.py", line 4, in <module>
      from hello import app
      ModuleNotFoundError: No module named 'hello'
      unable to load app 0 (mountpoint='/') (callable not found or import error)
      *** no app loaded. going in full dynamic mode ***

      • sean replied to this.

        23f23f check two things:

        • Ensure that your hello directory contains a file named __init__.py
        • Ensure that the parent directory that contains hello is on your Python search path, ie python-path = /home/you/apps/appname/dirname in your uwsgi.ini.

        Restart your app after making any necessary changes.

        I am wondering if the following is possible:

        I can get things to run fine with this uwsgi.ini file:

        [uwsgi]
        master = True
        http = 127.0.0.1:31267
        virtualenv = /home/MYUSERNAME/apps/OPALTEST/env/
        daemonize = /home/MYUSERNAME/logs/apps/OPALTEST/uwsgi.log
        pidfile = /home/MYUSERNAME/apps/OPALTEST/tmp/OPALTEST.pid
        workers = 2
        threads = 2
        
        # adjust the following to point to your project
        python-path = /home/MYUSERNAME/some/other/folder/MYFLASKAPP/
        mount = /=myapp:app
        manage-script-name = True
        touch-reload = /home/MYUSERNAME/some/other/folder/MYFLASKAPP/myapp/hello.py

        However, this uses python 3.6. I have a separate env in /home/MYUSERNAME/some/other/folder/MYFLASKAPP/venv/bin/activate, but I can't seem to get it working with the new python3.8 version.

        • sean replied to this.

          23f23f

          1. Change the virtualenv in uwsgi.ini to /home/MYUSERNAME/some/other/folder/MYFLASKAPP/venv
          2. Ensure that you've installed uwsgi into your other virtualenv (ie pip install uwsgi
          3. Edit the app's start and stop scripts to run /home/MYUSERNAME/some/other/folder/MYFLASKAPP/venv/bin/uwsgi instead of /home/MYUSERNAME/apps/OPALTEST/env/bin/uwsgi.
          2 years later

          I've had a flask app running for a couple years without issue using these instructions. I was just informed that the site it powers is down (not sure how long it's been down it's low-traffic) and when I investigate, it seems like most of the errors are maybe path-related? (ModuleNotFound errors, and the main one posted below.) Has anything changed on the servers recently that would explain this? (vps91)

          --- no python application found, check your startup logs for errors ---

          UPDATE
          I had to newly add a sub-path to my python-path declarations in uwsgi.ini, and it's working again now.

          • sean replied to this.

            Danos2000 No, there have been no changes on your server that would cause this.

            To troubleshoot, do a full stop/start cycle of the app then immediately open your uwsgi log to see what errors are logged immediately before the most recent "no python application found" error. That should give you an indication of the module that can't be found and you can then adjust your python-path in uwsgi.ini accordingly.

            If you're unsure how to proceed after viewing the log then please email our support team with the details and we'll look into it further.

            8 months later
            a year later

            I've edited the original post to simplify it a bit, mainly:

            • The __init__.py file is now blank.
            • The uwsgi.ini has been changed to use python-path and wsgi directives instead of the mount and force-script-name directives
            a month later

            Went over the first post, still works in Sep 2024, thanks.

            Mastodon