1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-11-23 03:51:35 +00:00
How_To_Linux/src/day_4/tasks.md

196 lines
8.4 KiB
Markdown
Raw Normal View History

2022-08-25 00:16:00 +00:00
# Tasks
## Compilation in containers
> 📍 : This task should be done on the Contabo server after connecting with SSH to the user that you did create yesterday on the server (not admin).
We want to practice compilation and containers, so lets compile in a container!
In this task, we want to compile the program `tmate`.
1. Start an Ubuntu container with `podman run -it --rm --name tmate-compiler ubuntu:latest bash`.
2022-08-26 14:42:14 +00:00
1. Go to the [website of `tmate`](https://tmate.io/) and find out how to compile from source (there are instructions for compiling on Ubuntu).
2022-08-25 00:16:00 +00:00
1. Follow the compilation instructions in the container.
1. After compilation, you will find the binary `tmate` in the directory of the git repository.
1. Don't exit the container yet, otherwise you will lose what you have done in it. Now open a new terminal (tab) and copy the binary `tmate` from the container to the directory `bin` in your home directory. Use the command `podman cp CONTAINERNAME:SRC_PATH DESTINATION_PATH`.
1. Verify that the binary `tmate` was copied to `DESTINATION_PATH` and then exit the container in the first terminal (tab).
Now write a script called `compile_tmate.sh` that automates what you have done in the container to compile `tmate`. Just copy all the commands that you used in the container to a script.
Add to the end of the script `mv PATH_TO_TMATE_BINARY_IN_CONTAINER /volumes/bin` to copy the binary to the directory `/volumes/bin` after compilation.
Create a directory called `scripts` and put the script in it.
Now write a second script in the parent directory of the directory `scripts`. The second script should automate creating the container that runs the first script.
Do the following in the second script:
1. Check if `scripts/compile_tmate.sh` does NOT exist. In this case you should print a useful message that explains why the script terminates and then exit with error code 1.
1. Make sure that `scripts/compile_tmate.sh` is executable for the user.
1. Create a directory called `bin` (next to the directory `scripts`) if it does not already exist.
1. Use the following snippet:
```bash
podman run -it --rm \
--name tmate-compiler \
-v ./scripts:/volumes/scripts:Z,ro \
-v ./bin:/volumes/bin:Z \
docker.io/library/ubuntu:latest \
/volumes/scripts/compile_tmate.sh
```
It creates a container that runs the script `compile_tmate.sh` and is removed afterwards (because of `--rm`).
The `scripts` directory is mounted to be able to give the container access to the script `compile_tmate.sh`. The directory is mounted as _read only_ (`ro`) because it will not be modified.
2022-08-26 14:42:14 +00:00
The `bin` directory is mounted to be able to transfer the binary into it before the container exits.
2022-08-25 00:16:00 +00:00
After running the second script, you should see the container compiling and then exiting. At the end, you should find the binary `tmate` in the `bin` directory.
Now that you have the program `tmate`, find out what it does! Try it with a second person.
##### Tipps:
2022-08-26 14:42:14 +00:00
- On Debian based distributions like Ubuntu, the package manager is `apt`. Before that you can install any packages with `apt`, you have to run `apt update`. This does not run system updates like `dnf upgrade`. `apt update` does only synchronize the repositories which is needed before installations.
2022-08-25 00:16:00 +00:00
- Test if a file exists in bash:
```bash
if [ -f FILE_PATH ]
then
(...)
fi
```
Replace `(...)` with your code. For more information on the option `-f` and other useful options for bash conditions, read the man page of the program `test`: `man test`.
To test if a file does NOT exist, replace `-f` with `! -f`.
- Exit a bash script with error code 1:
```bash
exit 1
```
## Task: Static website
> 📍 : In this task, you should connect as the user `admin` to the Contabo server. **Don't do this task as the user that you did create on the server!** ⚠️
> 📍 : Starting with this task: Asking you to replace `N` means to enter the number that you are using in the URL `ttydN.mo8it.xyz`.
In this task, you will host a static website which is a website that does not have a backend. A static website is just a set of HTML, CSS (and optionally JavaScript) files.
To host the website, we need a web server. In this task, we will use the Nginx web server.
Create a directory `~/nginxN` (replace `N`) with two directories in it: `website` and `conf`.
Place these two files:
1. `~/nginxN/conf/nginxN.jinext.xyz.conf` (replace `N`):
```
server {
listen 80;
2022-08-25 12:43:53 +00:00
server_name nginxN.jinext.xyz;
2022-08-25 00:16:00 +00:00
location / {
root /volumes/website;
index index.html;
}
}
```
2022-08-25 12:43:53 +00:00
Replace `N` also in `server_name`!
2022-08-25 00:16:00 +00:00
1. `~/nginxN/website/index.html` (replace `N`):
```
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Demo</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
```
Create a Nginx container with the following options:
- Name: `nginxN`. Replace `N`!
- Timezone `tz`: `local`.
- Network: `traefik`.
- Volumes:
2022-08-25 12:45:38 +00:00
- `~/nginxN/website:/volumes/website` with labels `Z,ro`.
- `~/nginxN/conf:/etc/nginx/conf.d` with labels `Z,ro`.
2022-08-25 00:16:00 +00:00
- Label: `io.containers.autoupdate=registry`
- Image: `docker.io/library/nginx:alpine`
Create the systemd file for the container above.
Move the systemd file to `~/.config/systemd/user`.
Enable and start the container as user services with `systemctl --user enable --now container-nginxN`. Replace `N`!
Visit [https://nginxN.jinext.xyz](https://nginxN.jinext.xyz) to see if everything did work! Replace `N`!
Now, you can edit `index.html` and add your own HTML content.
You can also add more files to the directory `website`. If you add a file `test.html` for example, then you should see it under [https://nginxN.jinext.xyz/test](https://nginxN.jinext.xyz/test).
## Task: Nextcloud
> 📍 : In this task, you should connect as the user `admin` to the Contabo server. **Don't do this task as the user that you did create on the server!** ⚠️
In this task you will deploy your own cloud on the server: Nextcloud!
To do so, we will install Nextcloud as a container using `podman`.
To connect as `admin` again, change the user for the host `linux-lab` in `~/.ssh/config` back to `admin` or use `ssh admin@linux-lab` instead of only `ssh linux-lab`.
You can find more information about the Nextcloud container here: https://hub.docker.com/\_/nextcloud
Create a directory called `nextcloudN` (replace `N`) in the home directory of the user `admin`.
Create a directory called `nextcloudN-db` (replace `N`) for the database container.
Create a container for the database with the following options:
- Container name: `nextcloudN-db`. Replace `N`!
- Timezone `tz`: `local`
- Network: `traefik`
- Volume: Mount the directory `nextcloudN-db` (replace `N`) that you did create into `/var/lib/postgresql/data` in the container. Use the label `Z`!
- The following environment variables:
- `POSTGRES_DB=nextcloud`
- `POSTGRES_USER=nextcloud`
- `POSTGRES_PASSWORD=DB_PASSWORD`. Replace `DB_PASSWORD` with a good password!
- Label: `io.containers.autoupdate=registry`
- Image: `docker.io/library/postgres:alpine`
Create the actual Nextcloud container with the following options:
- Container name: `nextcloudN`. `N` at the end stands for the number that you are using in the url to connect to the browser terminal `ttydN.mo8it.xyz`.
- Timezone `tz`: `local`
- Network: `traefik`
- Volume: Mount the directory `nextcloudN` that you did create into `/var/www/html` in the container. Use the label `Z`!
- The same environment variables as for the other container! Use the same `DB_PASSWORD`. Add one more environment variable:
- `POSTGRES_HOST=nextcloudN-db`. Replace `N`!
- Label: `io.containers.autoupdate=registry`
- Image: `docker.io/library/nextcloud:24-apache`
Create the systemd files for the two containers above.
Move the systemd files to `~/.config/systemd/user`.
Enable and start the two containers as user services with `systemctl --user enable --now container-nextcloudN-db` and `systemctl --user enable --now container-nextcloudN`. Replace `N`!
Visit [https://nextcloudN.jinext.xyz](https://nextcloudN.jinext.xyz) to see if everything did work! Replace `N`!
## Task: Vim game
In this task, we are going to play a game! 🎮️
The game is an educational game that lets you learn the very basics of the navigation in Vim/Neovim.
Play the game on this website: [https://vim-adventures.com](https://vim-adventures.com/)