Python/uWSGI Applications

Python web applications are commonly deployed using WSGI. Opalstack provides a simple installer for the popular uWSGI application server configured for Python 3.

Installing a Python/uWSGI Application

1

Create a shell user if you have not done so already. You can use an existing shell user if you wish.

2

Click on Applications in the control panel sidebar.

3

Click the button labeled "+ Add Application" in the upper-left corner of the content area.

4

Select Python/uWsgi as the application type.

5

Enter a name for your application in the Name field.

This name will be used to create a subdirectory in your shell user's apps directory such as /home/username/apps/appname.

6

Select the shell user that will host the application.

If you have multiple servers, be sure to select the shell user that is located on the server from which you intend to serve your site.

7

Click the "Add Application" button to save your new application.

8

A notification will appear in the control panel when the application is ready. This may take a minute or longer.

When the installation is complete, the following files and directories will be present in the application directory:

  • start, stop, and kill: scripts to control the operation of your application.
  • env: a directory containing the Python 3 environment used by your application.
  • tmp: a directory containing temporary files used by your application
  • myapp: a directory containing a simple "hello world" Python WSGI application.
  • uwsgi.ini: the configuration file for your Python/uWSGI application.

Controlling your Python/uWSGI application

You can control the operation of your Python/uWSGI application using the scripts provided by the installer.

  • To start the application: /home/app_user/apps/app_name/start
  • To stop the application: /home/app_user/apps/app_name/stop
  • To kill the application if it has hung: /home/app_user/apps/app_name/kill

Installing Python dependencies for your project

Your Python/uWSGI application includes a Python 3 environment located at /home/app_user/apps/app_name/env into which you can install your project's Python dependencies. To do so:

1

Activate your application's Python environment:

source /home/app_user/apps/app_name/env/bin/activate
2

Install your dependences with pip. You can install single packages...

pip install packagename

...or if you have a requirements file for your project you can install all of the dependencies at once:

pip install -r  requirements.txt

Connecting your Python/uWSGI application to your project

Your Python/uWSGI applications are initially configured to serve a simple "hello world" application. All of the configuration is done via the uwsgi.ini configuration file in the application directory, so you can reconfigure the application to serve your own project by editing that configuration.

For example, if you have a Flask project that you'd like to serve via uWSGI:

1

Create a new Python/uWSGI application as described above.

2

Upload your Flask project directory to your application directory. For this example, the project directory is named flaskapp.

3

Edit your application's uwsgi.ini configuration file (below the "adjust the following to point to your project" comment) to point it at your Flask app. For this example, we'll comment out the existing WSGI handler and use the uWSGI module directive instead.

# adjust the following to point to your project
#wsgi-file = /home/username/apps/appname/myapp/wsgi.py
#touch-reload = /home/username/apps/appname/myapp/wsgi.py
module = flaskapp:app
touch-reload = /home/username/apps/appname/flaskapp/__init__.py
4

Use the application's stop and start commands to restart the application to pick up the new configuration:

/home/username/apps/appname/stop
/home/username/apps/appname/start

At this point, the application is now serving the flaskapp project.

Performance tuning

If your Python/uWSGI application's performance is poor under heavy load, you may be able to improve it by increasing the values of workers and threads in your application's uwsgi.ini configuration file. Increasing those values will increase the ability of the application to handle more traffic by adding more worker processes and by increasing the number of simultaneous requests that each worker can handle.

There is no magic formula for determining the number of workers and threads your application needs. You'll need to spend some time observing your application's performance while you are adjusting the configuration until you find the settings that work best for you.

Other configuration

You can use any uWSGI configuration you need by setting it in your application's uwsgi.ini file. For more information please see: