Ditch Google Analytics for Plausible Analytics on Amazon Lightsail
Tags: webdev, tutorial, opensource, startup
Published: 2022-10-18 20:04:49

Plausible is a simple and privacy-friendly alternative to Google Analytics. It's a small (94% smaller than Google Analytics), open-source tool that allows users to either self-host or pay for the cloud-hosted version. If you want an easy-to-use tool and are concerned about privacy or you don't want Google harvesting your analytics data, this is the tool for you!

## How can I start using Plausible Analytics?
You have two options:

1. Plausible.io - Hosted version, paid monthly or yearly, and fully-managed by Plausible.

![Plausible Pricing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prytzatzyyd7tiwq7v8l.png)

2. Plausible.io - Open-source/self-hosted, free, managed by you.

We will be focusing on how to run the open-source version on Amazon Lightsail

## What is Amazon Lightsail?

Amazon Lightsail offers easy-to-use virtual private server (VPS) instances, containers, storage, databases, and more at a cost-effective monthly price. Lightsail makes it easy to choose an Operating system or an application blueprint to deploy and use. We will get Plausible running on Lightsail in less than 5 minutes. Let's get started!

## Login to Amazon Lightsail
If you do not already have a Lightsail account, head over to **<u>[Amazon Lightsail and create one](https://aws.amazon.com/lightsail/)</u>**.

Once you login to the service, it will look like this:

![Lightsail Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkzhl5z6mersqinbcz3i.png)

We are going to create an instance. Click 'Create instance'

![Create Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg2hlc5gfo1hd6q2c4xq.png)

Choose the region you want this deployed. Currently you have 14 regions globally where your instances can be deployed. I'm using Virginia (US-EAST-1a).

Click the 'OS Only' button and select the latest version of Ubuntu (for me that is 20.04)


![Ubuntu Blueprint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se1chragjekv8gyed5u3.png)

To speed up the deployment and configuration we are going to use a Launch Script, which will install and configure some of the required files before we even log in to the instance.

Click on 'Add Launch Script' and paste the following code: 

```
sudo apt update -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli docker-compose containerd.io -y

sudo mkdir /var/www
cd /var/www
sudo git clone https://github.com/plausible/hosting
cd hosting
sudo git clone https://github.com/TheBrianGraf/AmazonLightsail/
sudo chmod +x ./AmazonLightsail/Plausible-conf-setup.sh
```

The above code does the following:

- Updates available packages repository
- Download's Docker and additional Docker components
- Download's Plausible setup files
- Download's a Plausible config file


![Launch Script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbuloedaaafblyz7psfw.png)

## Choose an Instance Size
Lightsail offers a number of different sized instances to meet the needs of it's users. We'll choose the 4GB/2vCPU instance but you can choose a smaller or larger instance as you require.

![Instance Plans](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbws53t0e0kaf3f0wfs4.png)

Lastly, we'll give the Instance a name. We'll call ours **'Plausible-Analytics'**. Click 'Create instance'

![Create Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka01mzwtbxiy8418ohac.png)

## Add a static IP and Open Port 8000
We want our Lightsail Instance to keep the same IP address even if the instance is rebooted in the future. To add a Static IP, Click on the name of your instance.

![Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq771gul80kn20tepdvd.png)

Click the 'Networking' tab and click 'Attach static IP'

![Instance Networking](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqlabfv4rz30m6lkoq8x.png)

Click 'Create Static IP' and give it a name. We'll name ours 'Plausible-Analytics-IP'. Click 'Create'. 

![Attach Static IP to Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uagiyrnmatcdduzqw7ja.png)

We'll go ahead and open TCP Port 8000 so that we can reach our Plausible server once it's configured. Click on your instance name again and go back to the 'Networking' Tab.

Under IPv4 Firewall, click 'Add rule'. Type '8000' in the Port box and click 'Create'

![Enable Port 8000](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjz0rouemf9vnntusu4f.png)

Now we are ready to login to our instance and run the last few commands to get up and running.

## Login to the instance

Scroll back up to the top of the page and click the 'Connect' tab, then click 'Connect using SSH' this will open a new window that allows you to run commands in your instance.

Copy and paste (or type in) the following code into the SSH terminal:

```
# Run the config setup file
sudo /var/www/hosting/AmazonLightsail/Plausible-conf-setup.sh

# Change directories to the Plausible files
cd /var/www/hosting

# Kick off the Docker container
sudo docker-compose up --detach
# Wait about 10-20 seconds after the above command completes then run the final command. If it gives an error. Wait another 10 seconds and try it again.
# 
sudo docker-compose exec plausible_db psql -U postgres -d plausible_db -c "UPDATE users SET email_verified = true;"

```

Once you've run these commands, you will be able to login to Plausible. Copy the IP address of your instance into your web browser and include port 8000. You should now be able to login with the credentials found in the 'plausible-conf.env' file.

![Plausible Login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/149hnwcax1q92wg741az.png)

## From here you can begin your journey using Plausible instead of Google Analytics. For more information on how to get started with adding your website, you can continue with your setup **[HERE](https://plausible.io/docs/guided-tour)**.