Configuring Your Self-Hosted Anvil App to Run as a Service

Configuring Your Self-Hosted Anvil App to Run as a Service

Introduction

Welcome to the guide on configuring your self-hosted Anvil app to run as a service!
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. 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.

You can read these excellent guides written by Ryan on how to host anvil apps on your own server. He covers deploying anvil apps to popular cloud services such as Google Compute Engine, DigitalOcean Droplet, Linode and more!

If you have gone through the trouble of hosting anvil app on your own server you might want to configure your app to run as a service so that it starts automatically whenever your server is restarted, ensuring seamless operation without manual intervention. This guide will show you how to set that up!

Prerequisites

Before we begin, ensure you meet the following prerequisites:

  • Comfortable executing commands on a Linux terminal.
  • A running Anvil app on your server. If you have followed Ryan’s guides, you are probably running your app using a command similar to this
~$ cd ~
~$ source env/bin/activate
~$ anvil-app-server --app CRUDnewsappwalkthrough --origin https://<your-app-domain>/

Step 1 - Creating a systemd Configuration File

On Linux systems, systemd is an init system used to bootstrap userspace and manage user processes. At the time of its introduction it was controversial, but now is used by the majority of all modern Linux distributions.

So when you want to automatically run an application at every start of the Linux operating system, you need to create a configuration file.
Open a terminal and execute the following command:

~$ sudo nano /etc/systemd/system/anvild.service

This will create and open anvild.service file in the /etc/systemd/system/ directory with the nano text editor. You can use vi to edit the file if you prefer.

I am naming it anvild.service short for anvil daemon service. Of course, you can name it anything you want.

Type in or paste the following script into the file, replacing placeholders with your specific details:

[Unit]
Description=Anvil App Service

[Service]
User=your_username
WorkingDirectory=/path/to/your/app
ExecStart=/path/to/your/env/bin/anvil-app-server --app your_app_name --origin https://<your-app-domain>/

[Install]
WantedBy=multi-user.target

Hit CTRL + X then y to confirm the write and finally press Enter to save the file and exit out of nano editor.

Here is how my anvild.service looks like:

[Unit]
Description="Anvil App Service"

[Service]
User=bbytelab
WorkingDirectory=/home/bbytelab
ExecStart=/home/bbytelab/env/bin/anvil-app-server --app CRUDnewsappwalkthrough --port 80
Restart=on-failure

[Install]
WantedBy=multi-user.target

Note :
multi-user.target normally defines a system state where all network services are started up and the system will accept logins. If your service is not wanted by anybody or no other service depends on it,
it will not start automatically at boot even if you have it enabled.

Step 2 - Reloading systemd

When you edit any systemd script use the following command to load the changes:

~$ sudo systemctl daemon-reload

Step 3 - Starting Your Service

You can now start your Anvil app service using the following command:

~$ sudo systemctl start anvild

Step 4 - Enabling Auto Start at Boot

To ensure your service starts automatically at boot, run:

~$ sudo systemctl enable anvild

This should give following output indicating the symlink has been created.

Created symlink /etc/systemd/system/multi-user.target.wants/anvild.service → /etc/systemd/system/anvild.service.

Now if you reboot your server your anvil app should start automatically.

Step 5 - Checking App Logs with journalctl

You can use the following command to view and tail your app logs:

~$ journalctl -u anvild.service -f

To learn more about journalctl you can check out this great tutorial at DigitalOcean Community on
How To Use Journalctl to View and Manipulate Systemd Logs

More Interaction with the Service

To check the status of your service, use:

~$ sudo systemctl status anvild

To stop the service, run:

~$ sudo systemctl stop anvild

To disable auto start at boot, run:

~$ sudo systemctl disable anvild

Congratulations! Your Anvil app is now configured to run as a service on your self-hosted server, ensuring automatic startup on system reboot.

If you face any difficulties following this guide, feel free to let me know!

10 Likes

I’m confused about running self-hosted Anvil web applications. Based on the following thread:

Django or Anvil? Which One Should I Pick? - Anvil Q&A - Anvil Community Forum

Anvil self-hosting cannot be used for actual Production applications. The Furthermore, the open source Anvil doesn’t have much contribution from the community and if there is issue, a pull request need to sign CLA, which is unfortunately not available now.

jdbc uses “user” to identify the username, not “username”, the example configura

Furthermore, I am not 100% confident using the open source Anvil in production given the basic issues it has (for example above link). Http-kit, not a production ready server, is used. It seems for dev, not for production.

The self-hosted Anvil cannot be used in Production environments.

Have all of the concerns raised, on that page, been addressed? If so, could you please give a link out to the fix?

Thanks!

Hi This is James, How are you?

Hi James!

I am doing fine, thanks for asking.

What’s going on with you?

Hi @SteelersFan

It’s true that the open-source anvil-runtime doesn’t see as much community contribution, and if there are any issues, a pull request needs to sign a CLA. This remains the case.

However, the Anvil team does maintain it regularly and they are highly skilled at what they do. It’s worth noting that for any issues, fixes may not be as immediate as with Anvil cloud.

I’ve personally been using the self-hosted solution for three of my production apps, and I know several others who do the same. While my apps have a relatively small user base and don’t handle heavy loads, they have been running smoothly so far, and all the services offered in Anvil cloud work perfectly with the self-hosted setup.

The main reason I opted for the self-hosted solution is that my apps don’t generate enough profit to cover the cost of the Anvil business plan, which includes persistent server starts.

Also, the issue of geolocation plays a role; I’m located in South Asia, as are my users. With Anvil cloud likely situated in the UK or US, server calls involve significant latency due to the distance. This latency can render the apps slow and barely usable without persistent servers.

Overall, while there are considerations to be mindful of, the self-hosted solution has worked well for me and others in similar situations.

2 Likes

This post is super useful!!

Would be awesome if the anvil team added this post to Anvil Docs | Cloud Deployment Guides

1 Like