Running a server¶
Pydap comes with a lightweight and scalable OPeNDAP server, implemented as a WSGI application. Being a WSGI application, Pydap can run on a variety of servers, and frameworks including Apache, Nginx, IIS, uWSGI, Flask or as a standalone Python process. It can also be seamless combined with different middleware for authentication/authorization, GZip compression, and much more.
There is no one right way to run Pydap server; your application requirements and software stack will inform your deployment decisions. In this chapter we provide a few examples to try and get you on the right track.
In order to distribute your data first you need to install a proper handler, that will convert the data format to the Pydap data model.
If you just want to quickly test the Pydap server, you can run it as a standalone Python application using the server that comes with Python Paste and gunicorn. To run the server, first make sure that you have installed Pydap with the server extras dependencies:
$ pip install Pydap[server]
and then just run the
pydap script that pip installs into your bin directory:
$ pydap --data /path/to/my/data/files --port 8080
To change the default directory listing, the help page and the HTML form, you can point a switch to your template directory
$ pydap --data /path/to/my/data/files --templates /path/to/my/templates.
Pydap follows the WSGI specification, and most web servers gateways simply require a WSGI callable and a small amount of boiler plate code. Pydap provides the
DapServer class which is a WSGI callable located in the
pydap.wsgi.app module. A simple WSGI application script file would be something like this:
from pydap.wsgi.app import DapServer application = DapServer('/path/to/my/data/files')
The Flask framework simply requires a couple more function calls to spin up an application server. A simple server would looks something like this (your mileage may vary):
from flask import Flask from pydap.wsgi.app import DapServer application = DapServer('/path/to/my/data/files') app = Flask(__name__) app.wsgi_app = application app.run('0.0.0.0', 8000)
For a robust deployment you can run Pydap with Apache, using mod_wsgi. After installing mod_wsgi, create a sandbox in a directory outside your DocumentRoot, say
/var/www/pydap/, using a virtual environment:
$ mkdir /var/www/pydap $ python3 -m venv /var/www/pydap/env
If you want the sandbox to use your system installed packages (like Numpy, e.g.) you can use the
$ python3 -m venv --system-site-packages /var/www/pydap/env
Now let’s activate the sandbox and install Pydap – this way the module and its dependencies can be isolated from the system libraries:
$ source /var/www/pydap/env/bin/activate.sh (env)$ pip install Pydap
Create a WSGI script file somewhere convenient (e.g. /var/www/pydap/server/apache/pydap.wsgi) that reads something like this:
import site # force mod_wsgi to use the Python modules from the sandbox site.addsitedir('/var/www/pydap/env/lib/pythonX.Y/site-packages') from pydap.wsgi.app import DapServer application = DapServer('/path/to/my/data/files')
Now create an entry in your Apache configuration pointing to the
pydap.wsgi file you just edited. To mount the server on the URL
/pydap, for example, you should configure it like this:
WSGIScriptAlias /pydap /var/www/pydap/server/apache/pydap.wsgi <Directory /var/www/pydap/server/apache> Order allow,deny Allow from all </Directory>
This is the file I use for the test.pydap.org virtualhost:
<VirtualHost *:80> ServerAdmin email@example.com ServerName test.pydap.org DocumentRoot /var/www/sites/test.pydap.org/server/data <Directory /var/www/sites/test.pydap.org/server/data> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /var/www/sites/test.pydap.org/server/apache/pydap.wsgi <Directory /var/www/sites/test.pydap.org/server/apache> Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/test.pydap.org.error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/test.pydap.org.access.log combined ServerSignature On </VirtualHost>
You can find more information on the mod_wsgi configuration guide. Just remember that Pydap is a WSGI application like any other else, so any information on WSGI applications applies to it as well.
uWSGI is a “fast, self-healing and developer/sysadmin-friendly application container server coded in pure C” that can run Pydap. This is the recommended way to run Pydap if you don’t have to integrate it with other web applications. Simply install uWSGI, follow the instructions in the last section in order to create a virtualenv and Pydap installation:
$ mkdir /var/www/pydap $ python virtualenv.py /var/www/pydap/env $ source /var/www/pydap/env/bin/activate.sh (env)$ pip install Pydap uWSGI (env)$ cd /var/www/pydap
Create a WSGI application file myapp.wsgi as above
Now create a file in
/etc/init/pydap.conf with the content:
description "uWSGI server for Pydap" start on runlevel  stop on runlevel [!2345] respawn exec /var/www/pydap/env/bin/uwsgi \ --http-socket 0.0.0.0:80 \ -H /var/www/pydap/env \ --master --processes 4 \ --wsgi-file /var/www/pydap/myapp.wsgi
In order to make it run automatically during boot on Linux you can type:
$ sudo initctl reload-configuration
Users have reported success deploying Pydap with a docker image built with nginx + uWSGI + Flask (based on https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/. A full configuration is somewhat beyond the scope of this documentation (since it will depend on your requirements and your software stack), but it is certainly possible.