# Running Other Services

## Running Services

### Hosting a web page with nginx

* `nginx` is a great web server for these things, but you can pick whatever you want, or are more familiar with
* On Ubuntu, this is pretty simple: apt install nginx
* Enable it and start it: `sudo systemctl enable nginx`, `sudo systemctl start nginx`
* Open up your browser and head to <http://host/> — you should see a page there
* Edit files in /var/www/html/..., etc: try replacing index.html? with "hello world"
* check <http://host/> again

Here's a pretty cool, simple website. Let's try and deploy it:

{% @github-files/github-code-block url="<https://github.com/PaulineLabaisse/Motherfuckingwebsite/blob/master/Motherfuckerwebsite.html>" %}

### Running a cron job that sends us a message via telegram

One of the best things about a server is that it's meant to run 24/7, something that our laptops or desktops aren't great at doing. This means it's really good at running things constantly at a scheduled interval. To do such a thing, we'll use something known as cron jobs.

* Cron is a scheduling daemon that executes tasks at specified intervals.
* These tasks are called cron jobs and are mostly used to automate system maintenance or administration.

#### Cron jobs

The crontab command allows you to install, view , or open a crontab file for editing:

* `crontab -e` - Edit crontab file, or create one if it doesn’t already exist.
* `crontab -l` - Display crontab file contents.
* `crontab -r` - Remove your current crontab file.
* `crontab -i` - Remove your current crontab file with a prompt before removal.
* `crontab -u` - Edit other user crontab file. This option requires system administrator privileges.

#### Cron Syntax

* <https://crontab.guru/>

  ```
  Syntax:
  * * * * * command(s)
  - - - - -
  | | | | |
  | | | | ----- Day of week (0 - 7) (Sun=0 or 7)
  | | | ------- Month (1 - 12)
  | | --------- Day of month (1 - 31)
  | ----------- Hour (0 - 23)
  ------------- Minute (0 - 59)

  Example:
  */5 * * * * /path/to/script.sh # Run every 5 minutes
  ```

#### Creating a telegram bot with cronjob

We have an API that gives us the 24 hour weather forecast in Singapore. We want to:

* Check this forecast every morning
* If it's about to rain, send us an alert. For simplicity, lets send a telegram message!

Here's a bash script that sends a telegram message if it's going to rain that day:

<pre class="language-bash"><code class="lang-bash"><strong>#!/bin/bash
</strong>
# Replace these with your actual Telegram bot token and chat ID
TELEGRAM_BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"
TELEGRAM_API_URL="https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage"

# Replace this with your actual curl command that fetches the JSON weather data
response=$(curl -s "https://api.data.gov.sg/v1/environment/24-hour-weather-forecast")

# Define an array of rain-related keywords
rain_keywords=("Rain" "Showers" "Thundery Showers" "Heavy Thundery Showers")

# Extract the general weather forecast without jq
general_forecast=$(echo "$response" | grep -o '"forecast":"[^"]*' | sed 's/"forecast":"//')

# Initialize a flag to check if rain is found
rain_found=0

# Check for rain-related keywords in the general forecast
for keyword in "${rain_keywords[@]}"; do
    if [[ "$general_forecast" == *"$keyword"* ]]; then
        rain_found=1
        break
    fi
done

# If rain is detected, send an alert
if [ "$rain_found" -eq 1 ]; then
    message="Weather Alert: Rain expected! General forecast is '$general_forecast'. Stay prepared!"
    
    # Send the message to the Telegram bot
    curl -s -X POST $TELEGRAM_API_URL \
        -d chat_id=$CHAT_ID \
        -d text="$message" > /dev/null

    echo "Alert sent: $message"
else
    echo "No rain expected. General forecast is '$general_forecast'."
fi

</code></pre>

* To get a telegram bot id, just go to @BotFather, and create a new bot, enter the token we receive inside
* To get your chat id, just go to @getmyid\_bot and copy your chat id there
* You'll need to start a chat with your bot first, go to your bot and do '/start' before you try running the script

Here are some more local APIs you can try to automate/script!

{% embed url="<https://github.com/jackveiga/singapore-apis>" %}

These should give you a very simple idea of hosting some services on our own servers. There's so much more we can't cover in a 2 hour workshop but here are some resources if you're fast/want to learn more!

### Syncing your files with Syncthing

Syncthing is an open-source file synchronization tool that allows you to effortlessly synchronize files across multiple devices. It runs on various platforms, providing a seamless experience for syncing files securely and privately over local networks or the internet.

{% embed url="<https://www.atlantic.net/dedicated-server-hosting/how-to-install-syncthing-on-ubuntu-22-04/>" %}

### Hosting your own VPN over your own server

There are plenty of reasons why you should [never use or trust public VPN providers](https://gist.github.com/joepie91/5a9909939e6ce7d09e29). We can set up our own to ensure that we can browse the internet securely or hide our IP addresses by routing our network traffic through our servers instead.

{% embed url="<https://www.cyberciti.biz/faq/ubuntu-20-04-set-up-wireguard-vpn-server/>" %}
