Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

How to use Anvil’s open-source App Server on Google Cloud

Introduction

Anvil is a platform for building full-stack web apps with nothing but Python. No need to wrestle with JS, HTML, CSS, Python, SQL and all their frameworks – just build it all in Python.

Anvil also offers one-click deployment to the cloud. Anvil cloud deployment is, in the vast majority of scenarios, the quickest and easiest way to get apps up and running. However, we understand that there are scenarios in which you require your app to be deployed to your own server. To deploy your app to your own server, Anvil provides the open source Anvil App Server.

In this guide, we will demonstrate how to deploy an Anvil app to Google Compute Engine. We will start by creating an environment on your server and installing the Anvil App Server. Then, we will configure our domain name for the server. Next, we will clone your Anvil app from the Anvil editor. We will then run your Anvil app and access it via your domain.

Prerequisites

Initial Server Setup

For this guide, we will use a Compute Engine instance with a fresh Ubuntu 20.04 server install.

I will also be using the provided superuser that comes with a fresh Compute Engine instance, which in my case is ryan. We can use any user that has sudo privileges.

Cloud Engine creation

On this server we are using Python version 3.8.2, although any Python version >=2.7 or >=3.7 will work with the Anvil App Server.

Updating the server package list

To start, we are going to run:

~$ sudo apt-get update

This updates the package list on my server. For more information on apt-get update, there is a good summary provided here.

Installing the Java virtual machine and Python virtual environment package

Next, we need to install the Java virtual machine for the Anvil App Server and the Python venv (virtual environment) package. On our Cloud Engine instance, we can run:

~$ sudo apt-get install python3-venv openjdk-8-jdk

Allowing our user to create a service on 443

The next commands require root, so let’s start by changing to our user to root:

~$ su root

On many Linux systems unprivileged users cannot create services on port 443. This is a somewhat archaic restriction, and there are a few ways around it. The simplest is to turn it off, with the commands:

~# echo 'net.ipv4.ip_unprivileged_port_start=0' > /etc/sysctl.d/50-unprivileged-ports.conf
~# sysctl --system

(There are all sorts of other configurations you can use for HTTPS; see the README file for all the gory details.)

We have now installed and configured everything we need as the root user. It’s time to switch back to the user we logged onto our server with and install the App Server in a virtual environment in their home directory.

Creating your virtual environment

Let’s start by switching to the user we logged onto our server with:

~# su ryan

We are going to start by creating a virtual environment for the Anvil Standalone Runtime to be contained in. The directory I’m creating my environment in is my users home directory. To navigate there we can run:

~$ cd ~

We create a Python virtual environment by running:

~$ python3 -m venv env

Creating a virtual environment provides us with an isolated environment separate from any other apps we may install or develop on this server. Our virtual environment will contain the Anvil App Server, its dependencies and any other Python packages we want to install for our Anvil app.

To activate our virtual environment, we run the following command:

~$ source env/bin/activate

We are now ready to install the Anvil App Server, along with its dependencies and packages.

Installing the Anvil App Server

Next we are going to install the Anvil App Server. The Anvil app server is available on PyPI, so we can install it with pip:

(env) ~$ pip install anvil-app-server

If you want to use additional Python packages (e.g. numpy, pandas) in your app’s server-side code, you should make sure these are installed too!

Now the Anvil App Server is installed, let’s configure our domain name.

Adding your own domain name

To set up a domain name, we first need to purchase a domain name from a domain name registrar.

Once we have purchased the domain name, we need to update the nameservers used by the domain registrar to use Google Cloud Engine instead. Each registrar should have documentation on how to change your domain’s name servers.

Cloud Engines name servers can be found by creating a Cloud DNS managed zone in Google Cloud Network services.

Once our DNS zone is created, the name servers can be found in the data column of the Zone details page. Copy the name servers and add them to your domain name registrar.

Google Cloud DNS zone

Our next step is to create the type A DNS records required by Anvil, this can be done by using the Add record set button in the zone details page. For this guide I’m going to add two A type DNS records.

Type     DNS name IP Address TTL
A         www.ryans-test-app.xyz             server_IP             5 minutes    
A     ryans-test-app.xyz server_IP 5 minutes


Here is an example of what my DNS records look like:

DNS Records

For more information, Google provides a useful guide to DNS management here.

DNS changes take a while to propagate around the internet, so you may need to wait up to 48 hours before your changes fully take effect.

Once the changes have been made, we’re ready to start the app server.

Adding your Anvil app

Creating an Anvil App

For this guide, we are going to use the News Aggregator app from one of our tutorials. If you already have an Anvil app you want to deploy to this server, you can skip straight to Cloning your app to the server with git.

Click this link to clone the finished News Aggregator app to your Anvil account; you’ll need to sign up for a free Anvil account if you don’t have one yet.

Cloning your app to the server with git

For this example, I will be cloning the News Aggregator app from my Anvil account to my Cloud Engine instance.

Each Anvil app is represented by a Git repository. If we click the Clone with Git button in the Version History dialog, we can see the command to clone our desired app onto our server.

Paste the command into the command line window to clone the desired app onto your server. It will look something like this:

# Clone your Anvil app onto your machine
$ git clone ssh://ryan%40anvil.works@anvil.works:2222/AOYS4NM7A3XTUODP.git CRUDnewsappwalkthrough

You will be asked for your Anvil account password to clone the app.

Alternatively, you can set up an SSH key for your server and add it to your anvil app settings by following this guide.

When cloned, the Git repository will be placed in a directory named using the app’s package name. The example above would clone the app into a directory named CRUDnewsappwalkthrough.

Make our app accessible to the world!

Now it’s time to deploy our app to our Anvil App Server for the whole world to access. The best part is, with the Anvil App Server, we can do it all with one command:

(env)$ anvil-app-server --app CRUDnewsappwalkthrough --origin https://ryans-test-app.xyz/

Thanks to Anvil’s built-in integration with Let’s Encrypt, we can specify an https:// origin, and Anvil will take care of the rest.

Let’s Encrypt enforces rate limits for certificates. To avoid exceeding these limits while you are testing your setup, you can deploy your app with the --letsencrypt-staging flag. This flag produces an invalid ‘staging’ certificate that is not subject to rate limits.

Once you are finished testing your deployment, simply remove the flag to deploy with a production certificate.

We can now browse to our app via our domain and it’s ready to go!

Edit your app

Once you’ve launched the web server with the anvil-app-server command, you can simply refresh your browser to see changes you make locally - there’s no need to restart the web server!