Debian Codegraph @

docs/deploying.rst

raw
1 .. _deploying:
2
3 =====================
4 Deploying Datasette
5 =====================
6
7 The quickest way to deploy a Datasette instance on the internet is to use the ``datasette publish`` command, described in :ref:`publishing`. This can be used to quickly deploy Datasette to a number of hosting providers including Heroku, Google Cloud Run and Vercel.
8
9 You can deploy Datasette to other hosting providers using the instructions on this page.
10
11 .. _deploying_fundamentals:
12
13 Deployment fundamentals
14 =======================
15
16 Datasette can be deployed as a single ``datasette`` process that listens on a port. Datasette is not designed to be run as root, so that process should listen on a higher port such as port 8000.
17
18 If you want to serve Datasette on port 80 (the HTTP default port) or port 443 (for HTTPS) you should run it behind a proxy server, such as nginx, Apache or HAProxy. The proxy server can listen on port 80/443 and forward traffic on to Datasette.
19
20 .. _deploying_systemd:
21
22 Running Datasette using systemd
23 ===============================
24
25 You can run Datasette on Ubuntu or Debian systems using ``systemd``.
26
27 First, ensure you have Python 3 and ``pip`` installed. On Ubuntu you can use ``sudo apt-get install python3 python3-pip``.
28
29 You can install Datasette into a virtual environment, or you can install it system-wide. To install system-wide, use ``sudo pip3 install datasette``.
30
31 Now create a folder for your Datasette databases, for example using ``mkdir /home/ubuntu/datasette-root``.
32
33 You can copy a test database into that folder like so::
34
35 cd /home/ubuntu/datasette-root
36 curl -O https://latest.datasette.io/fixtures.db
37
38 Create a file at ``/etc/systemd/system/datasette.service`` with the following contents:
39
40 .. code-block:: ini
41
42 [Unit]
43 Description=Datasette
44 After=network.target
45
46 [Service]
47 Type=simple
48 User=ubuntu
49 Environment=DATASETTE_SECRET=
50 WorkingDirectory=/home/ubuntu/datasette-root
51 ExecStart=datasette serve . -h 127.0.0.1 -p 8000
52 Restart=on-failure
53
54 [Install]
55 WantedBy=multi-user.target
56
57 Add a random value for the ``DATASETTE_SECRET`` - this will be used to sign Datasette cookies such as the CSRF token cookie. You can generate a suitable value like so::
58
59 $ python3 -c 'import secrets; print(secrets.token_hex(32))'
60
61 This configuration will run Datasette against all database files contained in the ``/home/ubuntu/datasette-root`` directory. If that directory contains a ``metadata.yml`` (or ``.json``) file or a ``templates/`` or ``plugins/`` sub-directory those will automatically be loaded by Datasette - see :ref:`config_dir` for details.
62
63 You can start the Datasette process running using the following::
64
65 sudo systemctl daemon-reload
66 sudo systemctl start datasette.service
67
68 You will need to restart the Datasette service after making changes to its ``metadata.json`` configuration or adding a new database file to that directory. You can do that using::
69
70 sudo systemctl restart datasette.service
71
72 Once the service has started you can confirm that Datasette is running on port 8000 like so::
73
74 curl 127.0.0.1:8000/-/versions.json
75 # Should output JSON showing the installed version
76
77 Datasette will not be accessible from outside the server because it is listening on ``127.0.0.1``. You can expose it by instead listening on ``0.0.0.0``, but a better way is to set up a proxy such as ``nginx`` - see :ref:`deploying_proxy`.
78
79 .. _deploying_openrc:
80
81 Running Datasette using OpenRC
82 ===============================
83 OpenRC is the service manager on non-systemd Linux distributions like `Alpine Linux <https://www.alpinelinux.org/>`__ and `Gentoo <https://www.gentoo.org/>`__.
84
85 Create an init script at ``/etc/init.d/datasette`` with the following contents:
86
87 .. code-block:: sh
88
89 #!/sbin/openrc-run
90
91 name="datasette"
92 command="datasette"
93 command_args="serve -h 0.0.0.0 /path/to/db.db"
94 command_background=true
95 pidfile="/run/${RC_SVCNAME}.pid"
96
97 You then need to configure the service to run at boot and start it::
98
99 rc-update add datasette
100 rc-service datasette start
101
102 .. _deploying_buildpacks:
103
104 Deploying using buildpacks
105 ==========================
106
107 Some hosting providers such as `Heroku <https://www.heroku.com/>`__, `DigitalOcean App Platform <https://www.digitalocean.com/docs/app-platform/>`__ and `Scalingo <https://scalingo.com/>`__ support the `Buildpacks standard <https://buildpacks.io/>`__ for deploying Python web applications.
108
109 Deploying Datasette on these platforms requires two files: ``requirements.txt`` and ``Procfile``.
110
111 The ``requirements.txt`` file lets the platform know which Python packages should be installed. It should contain ``datasette`` at a minimum, but can also list any Datasette plugins you wish to install - for example::
112
113 datasette
114 datasette-vega
115
116 The ``Procfile`` lets the hosting platform know how to run the command that serves web traffic. It should look like this::
117
118 web: datasette . -h 0.0.0.0 -p $PORT --cors
119
120 The ``$PORT`` environment variable is provided by the hosting platform. ``--cors`` enables CORS requests from JavaScript running on other websites to your domain - omit this if you don't want to allow CORS. You can add additional Datasette :ref:`settings` options here too.
121
122 These two files should be enough to deploy Datasette on any host that supports buildpacks. Datasette will serve any SQLite files that are included in the root directory of the application.
123
124 If you want to build SQLite files or download them as part of the deployment process you can do so using a ``bin/post_compile`` file. For example, the following ``bin/post_compile`` will download an example database that will then be served by Datasette::
125
126 wget https://fivethirtyeight.datasettes.com/fivethirtyeight.db
127
128 `simonw/buildpack-datasette-demo <https://github.com/simonw/buildpack-datasette-demo>`__ is an example GitHub repository showing a Datasette configuration that can be deployed to a buildpack-supporting host.
129
130 .. _deploying_proxy:
131
132 Running Datasette behind a proxy
133 ================================
134
135 You may wish to run Datasette behind an Apache or nginx proxy, using a path within your existing site.
136
137 You can use the :ref:`setting_base_url` configuration setting to tell Datasette to serve traffic with a specific URL prefix. For example, you could run Datasette like this::
138
139 datasette my-database.db --setting base_url /my-datasette/ -p 8009
140
141 This will run Datasette with the following URLs:
142
143 - ``http://127.0.0.1:8009/my-datasette/`` - the Datasette homepage
144 - ``http://127.0.0.1:8009/my-datasette/my-database`` - the page for the ``my-database.db`` database
145 - ``http://127.0.0.1:8009/my-datasette/my-database/some_table`` - the page for the ``some_table`` table
146
147 You can now set your nginx or Apache server to proxy the ``/my-datasette/`` path to this Datasette instance.
148
149 Nginx proxy configuration
150 -------------------------
151
152 Here is an example of an `nginx <https://nginx.org/>`__ configuration file that will proxy traffic to Datasette::
153
154 daemon off;
155
156 events {
157 worker_connections 1024;
158 }
159 http {
160 server {
161 listen 80;
162 location /my-datasette {
163 proxy_pass http://127.0.0.1:8009/my-datasette;
164 proxy_set_header Host $host;
165 }
166 }
167 }
168
169 You can also use the ``--uds`` option to Datasette to listen on a Unix domain socket instead of a port, configuring the nginx upstream proxy like this::
170
171 daemon off;
172 events {
173 worker_connections 1024;
174 }
175 http {
176 server {
177 listen 80;
178 location /my-datasette {
179 proxy_pass http://datasette/my-datasette;
180 proxy_set_header Host $host;
181 }
182 }
183 upstream datasette {
184 server unix:/tmp/datasette.sock;
185 }
186 }
187
188 Then run Datasette with ``datasette --uds /tmp/datasette.sock path/to/database.db --setting base_url /my-datasette/``.
189
190 Apache proxy configuration
191 --------------------------
192
193 For `Apache <https://httpd.apache.org/>`__, you can use the ``ProxyPass`` directive. First make sure the following lines are uncommented::
194
195 LoadModule proxy_module lib/httpd/modules/mod_proxy.so
196 LoadModule proxy_http_module lib/httpd/modules/mod_proxy_http.so
197
198 Then add these directives to proxy traffic::
199
200 ProxyPass /my-datasette/ http://127.0.0.1:8009/my-datasette/
201 ProxyPreserveHost On
202
203 A live demo of Datasette running behind Apache using this proxy setup can be seen at `datasette-apache-proxy-demo.datasette.io/prefix/ <https://datasette-apache-proxy-demo.datasette.io/prefix/>`__. The code for that demo can be found in the `demos/apache-proxy <https://github.com/simonw/datasette/tree/main/demos/apache-proxy>`__ directory.
204
205 Using ``--uds`` you can use Unix domain sockets similar to the nginx example::
206
207 ProxyPass /my-datasette/ unix:/tmp/datasette.sock|http://localhost/my-datasette/
208
209 The `ProxyPreserveHost On <https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypreservehost>`__ directive ensures that the original ``Host:`` header from the incoming request is passed through to Datasette. Datasette needs this to correctly assemble links to other pages using the :ref:`datasette_absolute_url` method.