To run an app that uses ASGI (minimal basic setup, adjust and expand as needed):
- Go to https://my.opalstack.com/apps/.
- Create a new "Nginx Proxy Port" application and make a note of the app name and port assignment.
- Log in to a SSH session as the app's shell user.
- Execute the following commands to create a new Python environment using uvicorn as the ASGI server.
cd ~/apps/name_of_app
python3.11 -m venv env
source env/bin/activate
pip install gunicorn uvicorn
- Upload your project to the app directory, the rest of this example will use
myproject
as the project name.
- Install your project dependencies.
- Create a file
~/apps/name_of_app/start
with the following contents:
#!/bin/bash
# replace appname with your app name
APP=name_of_app
APPDIR=$HOME/apps/$APP
# set PYTHONPATH to your top level project directory, modify as needed for your project:
PYTHONPATH=$APPDIR/myproject
# set HANDLER to your ASGI handler, eg for `myproject/asgi.py` use:
HANDLER=myproject.asgi
# set PORT to your app's port:
PORT=NNNNN
STARTCMD="$APPDIR/env/bin/gunicorn -D -b 127.0.0.1:$PORT -w 4 -k uvicorn.workers.UvicornWorker $HANDLER:application"
(pgrep -f "$STARTCMD" > /dev/null && echo "$APP already running.") || (PYTHONPATH=$PYTHONPATH $STARTCMD && echo "started $APP.")
- Create a file
~/apps/name_of_app/stop
with the following contents:
#!/bin/bash
# replace appname with your app name
APP=name_of_app
APPDIR=$HOME/apps/$APP
# set HANDLER to your ASGI handler, eg for `myproject/asgi.py` use:
HANDLER=myproject.asgi
# set PORT to your app's port:
PORT=NNNNN
STARTCMD="$APPDIR/env/bin/gunicorn -D -b 127.0.0.1:$PORT -w 4 -k uvicorn.workers.UvicornWorker $HANDLER:application"
pgrep -f "$STARTCMD" > /dev/null && pkill -f "$STARTCMD" && echo "stopped $APP." || echo "$APP not running."
- Run the following commands to make your
start
and stop
scripts executable:
chmod 700 ~/apps/name_of_app/start
chmod 700 ~/apps/name_of_app/stop
- Run
~/apps/name_of_app/start
to start the app.