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.)
- Create a new shell user via your Opalstack dashboard. You can use an existing shell user if you want.
- Create a new uwsgi app via your Opalstack dashboard and attach it to whatever website.
- 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
- Upload your flask project directory into
~/apps/myapp
. What follows is a simply project directory myproject
set up as a Python module that exposes an app
attribute:
# myproject/hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
# myproject/__init__.py
from .hello import app
- 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
mount = /=myproject:app
manage-script-name = True
touch-reload = /home/myuser/apps/myapp/myproject/hello.py
- 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 mount
directive in your uwsgi.ini
for your own project, but it will usually be projectdir:app
.
If your Flask app isn't organized like a Python module then feel free to post here to let me know what you've got and I'll help you wire it up. 🙂