Skip to content

Commit cdd5015

Browse files
authored
Merge pull request #112 from fykosak/dev-setup-script
Setup script
2 parents 8f4bb64 + 02e82a1 commit cdd5015

File tree

5 files changed

+154
-157
lines changed

5 files changed

+154
-157
lines changed

README.md

Lines changed: 63 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,70 @@
1-
# FYKOS-webs
1+
# FYKOS webs
22

3-
Nette multisite codebase for the [Fyziklani](https://fyziklani.cz) and [Online Physics Brawl](https://online.fyziklani.cz) competitions.
3+
Codebase pro Nette weby pro účely FYKOSu a Výfuk.
44

5-
This project uses modified bootstrap file to support multiple websites sharing components and models.
5+
Weby jsou naprogramovány v PHP pomocí frameworku Nette. Běží pod Apache v rámci
6+
docker kontejnerů.
67

7-
## Requirements
8-
- `PHP 8.1`
9-
- `Apache` with `mod_rewrite`
10-
- sql database
8+
## Vývoj
119

12-
## Development
10+
### Prerekvizity
11+
12+
Aby bylo možné spustit weby, je potřeba mít nainstalovaný docker. Pokud jste na
13+
Linuxu, využijte [návodu na instalaci](https://docs.docker.com/engine/install/).
14+
15+
Pokud jste na Windows, je asi nejlepším způsobem nainstalovat WSL (Ubuntu nebo
16+
Debian) a využít návodu pro Linux. Alternativně lze nainstalovat [Docker
17+
desktop](https://docs.docker.com/desktop/setup/install/windows-install/) přímo
18+
na Windows.
19+
20+
Pro MacOS je možné použít pouze [Docker desktop](https://docs.docker.com/desktop/setup/install/mac-install/).
21+
22+
### První nastavení
23+
24+
V root složce se nachází script `setup.sh`. Ten po spuštění vykoná několik kroků:
25+
26+
- vytvoří konfigurační soubory a doplní do nich přístupové údaje
27+
- vytvoří docker compose soubor a vytvoří příslušné kontejnery
28+
- pomocí kontejneru nainstaluje přes `composer` a `npm` příslušné balíčky a zkompiluje css/js
29+
30+
Postup:
31+
32+
1. Spusťte setup script v **hlavní složce** pomocí `./setup.sh`
33+
2. Vyplňte hodnoty požadované scriptem
34+
3. Běžte do podložky `docker` (`cd docker`)
35+
4. Spusťte docker kontejnery pomocí `docker compose up`
36+
5. V prohlížeči běžte na url `http://localhost:<port>`, kde `<port>` je číslo od 8080 až 8084:
37+
- Výfuk -- [http://localhost:8080](http://localhost:8080)
38+
- FYKOS -- [http://localhost:8081](http://localhost:8081)
39+
- Fyziklání Online -- [http://localhost:8082](http://localhost:8082)
40+
- Fyziklání -- [http://localhost:8083](http://localhost:8083)
41+
- DSEF -- [http://localhost:8084](http://localhost:8084)
42+
6. Spuštěný docker compose v terminálu lze ukončit pomocí CTRL+C
43+
44+
### Následný vývoj
45+
46+
Po prvotním nastavení stačí opakovat kroky 3--6 ze sekce první nastavení. Není
47+
tedy třeba pokaždé spouštět script `setup.sh`.
48+
49+
Pokud chcete, aby vám weby běžely nepřetržitě a nemuseli jste mít otevřený
50+
terminál, ve je docker spuštěn, lze pomocí `docker compose up -d` ve složce
51+
`docker` spustit kontejnery na pozadí. Následně je lze zastavit přes
52+
`docker compose stop`.
53+
54+
Více informací o tom, jak používat docker a jak v něm spouštět příkazy
55+
naleznete v [Docker README](docker/README.md).
56+
57+
## Manuální instalace
58+
59+
Script `setup.sh` provádí úkony, které by bylo nutné udělat jinak ručně. Jedná
60+
se o:
61+
62+
- ve složce `app/config/` zkopírování konfiguračního souboru `config.local.neon.sample` do složky `local` pod názvy `fykos.neon`, `vyfuk.neon`, `fol.neon`, `fof.neon`, `dsef.neon`
63+
- zkopíruje soubor `docker/docker-compose.yml.sample` do `docker/docker-compose.yml` a vyplní `UID` a `GID` uživatele
64+
- buildne kontejnery přes `docker compose build`
65+
- pomocí vytvořeného kontejneru nainstaluje balíčky pro composer a npm a spustí `npm run build`
66+
67+
## Development using podman
1368

1469
As a convenience for `podman` based setups, you can use [just](https://just.systems/)
1570
command runner for your development environment. For `docker` see below. All
@@ -27,148 +82,3 @@ To use `docker` instead of `podman`, instead of `just cmd` write `just runner=do
2782
This is untested, so contact @rkuklik if you run into issues.
2883

2984
To access the websites (after running `just dev`), following links can be used:
30-
31-
- [vyfuk](http://localhost:8080)
32-
- [fykos](http://localhost:8081)
33-
- [fol](http://localhost:8082)
34-
- [fof](http://localhost:8083)
35-
- [dsef](http://localhost:8084)
36-
37-
## Installation
38-
39-
1. Clone the repository `git clone --recurse-submodules ...`
40-
2. Configure the web server (see below for example configuration)
41-
3. Create a configuration files and fill all the necessary information
42-
- `cp app/config/config.local.neon.example app/config/config.fof.local.neon`
43-
- `cp app/config/config.local.neon.example app/config/config.fol.local.neon`
44-
- `cp app/config/config.local.neon.example app/config/config.dsef.local.neon`
45-
4. Follow the build instructions
46-
47-
### Example Apache configuration
48-
Following configuration expects repository located in `/var/www/fykos-webs`.
49-
```apacheconf
50-
<Directory /var/www/fykos-webs>
51-
AllowOverride All
52-
Require all granted
53-
</Directory>
54-
55-
<VirtualHost online.fyziklani.cz.local online.fyziklani.org.local>
56-
ServerName online.fyziklani.cz.local
57-
ServerAlias online.fyziklani.org.local
58-
DocumentRoot /var/www/fykos-webs/www/fol
59-
SetEnv NETTE_DEVEL 1
60-
</VirtualHost>
61-
62-
<VirtualHost fyziklani.cz.local fyziklani.org.local>
63-
ServerName fyziklani.cz.local
64-
ServerAlias fyziklani.org.local
65-
DocumentRoot /var/www/fykos-webs/www/fof
66-
SetEnv NETTE_DEVEL 1
67-
</VirtualHost>
68-
69-
<VirtualHost dsef.cz.local dsef.org.local>
70-
ServerName dsef.cz.local
71-
ServerAlias dsef.org.local
72-
DocumentRoot /var/www/fykos-webs/www/dsef
73-
SetEnv NETTE_DEVEL 1
74-
</VirtualHost>
75-
76-
<VirtualHost fykos.cz.local fykos.org.local>
77-
ServerName fykos.cz.local
78-
ServerAlias fykos.org.local
79-
DocumentRoot /var/www/fykos-webs/www/fykos
80-
SetEnv NETTE_DEVEL 1
81-
</VirtualHost>
82-
```
83-
84-
Do not forget to modify your `/etc/hosts` file to point to the correct IP address of your server.
85-
```etc/hosts
86-
127.0.0.1 online.fyziklani.cz.local
87-
127.0.0.1 online.fyziklani.org.local
88-
127.0.0.1 fyziklani.cz.local
89-
127.0.0.1 fyziklani.org.local
90-
127.0.0.1 dsef.cz.local
91-
127.0.0.1 dsef.org.local
92-
127.0.0.1 fykos.cz.local
93-
127.0.0.1 fykos.org.local
94-
```
95-
96-
These domains need to be configured in `app/config/config.*.local.neon` under `parameters.domains` in order to work.
97-
98-
## Build instructions
99-
100-
*You need `composer` and `npm` with `node` to build this project.*
101-
102-
1. Run `composer install` to install php dependencies.
103-
2. Run `npm install` to install javascript dependencies and build tools.
104-
3. Run `npm run build` to compile css and js files.
105-
106-
You can use `npm run dev` to automatically rebuild files when they are changed.
107-
108-
## Detailed manual for WSL, Ubuntu
109-
110-
Installing Prerequisites
111-
1. open wsl
112-
2. if not installed, install `apache2` (`sudo apt install apache2`)
113-
3. if not installed, install `php8.1` (`sudo apt install php8.1`, you might also need `php8.1-dom` and `php8.1-soap`)
114-
4. if not installed, install `mysql` (google how to do that - e.g. via `sudo apt install mysql-server`)
115-
5. if not installed, install `composer` (google how to do that - sudo apt install composer does not work as of July 2022)
116-
6. if not installed, install `node`, version at least 16. Alternatively, install nvm and then `nvm use 16`
117-
7. if not installed, install `gettext` (via `sudo apt install gettext`), check `locale -a` if you have `cs_CZ` and `en_US` installed, otherwise use `sudo locale-gen cs_CZ`, `sudo locale-gen cs_CZ.UTF-8` and then `sudo update-locale`
118-
8. pull this repository to a location where you want to have it (e.g. `cd C:/data/fykos && git pull <repourl>`)
119-
* Note: you may encounter various problems, e.g. php not being executed (try `sudo apt install libapache2-mod-php` and `sudo a2enmod php8.1`) or with "ERROR: Module mpm_event is enabled" (try `sudo a2dismod mpm_event` and `sudo a2enmod mpm_prefork`, and then `sudo service apache2 restart`)
120-
121-
Configuring Apache
122-
* Explanation: the webserver reads all files from `sites-enabled` and loads the configuration from them.
123-
Other files in these directories are symlinks which point to files in `sites-available` (handy for turning off
124-
the page by deleting the symlink without deleting the actual configuration)
125-
1. open wsl
126-
2. create a new file `sudo nano /etc/apache2/sites-enabled/fykos-webs.conf`
127-
3. paste the example apache configuration (above) into this file
128-
4. change the Directory and all DocumentRoots to be where you have downloaded your repo (e.g. `<Directory /mnt/c/data/fykos/webs>` and `DocumentRoot /mnt/c/data/fykos/webs/www/fol`)
129-
4. save it
130-
131-
Configuring hosts file
132-
* Explanation: Since wsl has its own IP adress, and we use a browser over on Windows, we need
133-
to configure Windows's DNS to route us over to wsl.
134-
1. open the file `C:/Windows/System32/drivers/etc/hosts` as administrator
135-
2. paste the example contents of `/etc/hosts` and save
136-
3. open wsl and type `ip a` and find out the IP adress of WSL (usually something like eth0: ... *inet 172.22.207.240/20* ...)
137-
4. replace all occurrences of `127.0.0.1` in `C:/Windows/System32/drivers/etc/hosts` with the found IP (e.g. `172.22.207.240`)
138-
5. Sadly, steps 3 and 4 need to be repeated everytime wsl gets restarted, because a new IP is generated every time.
139-
140-
141-
Configuring MySql
142-
* Explanation: some parts of the web need an access to a database to work.
143-
1. open wsl
144-
2. start mysql (`sudo mysql`). You may encounter errors, in which case google how to solve them.
145-
3. you are now in mysql shell (the line starts with `mysql>`).
146-
4. Create databases for FOL and FOF: (a) `create database fol ;` (b) `create database fof ;`
147-
5. Create user with a password, e.g.: `CREATE USER 'fykos'@'localhost' IDENTIFIED BY 'password';`
148-
6. Set privileges for the newly created user: (a) `GRANT ALL PRIVILEGES ON fol.* TO 'fykos'@'localhost' WITH GRANT OPTION;` (b) same, only with fof
149-
7. Close mysql by ctrl+D
150-
151-
152-
Inserting tables and data
153-
* Explanation: In FOL, we the database also needs to have some tables and preferably dummy data.
154-
1. open wsl terminal and `sudo mysql`
155-
2. type `use fol` (tells mysql to modify the `fol` database)
156-
3. copy and paste the contents of the file `data/sql/schema_fol.sql` into mysql shell and press Enter (creates tables)
157-
4. copy and paste the contents of the file `data/sql/example_fol.sql` into mysql shell and press Enter (inserts data)
158-
159-
160-
Configuring neon files
161-
* Explanation: these files contain secret data such as passwords and connection strings.
162-
It is something like appsettings.json. These data are then used in the application to
163-
connect to various resources, such as the database.
164-
* `.local` files always override the configurations from files without the `.local`. We only
165-
edit the `.local` files, which are intentionally excluded from git.
166-
1. tell somebody to send you at least the `fksdbDownloader` credentials and fill them out
167-
2. fill out the database name in the connection strings (e.g. `dsn: 'mysql:host=localhost;dbname=fof'`)
168-
3. fill out the `parameters.database` section with the credentials that you've used in MySql (`user: fykos`, `password: password`)
169-
4. set gameApiURL to an empty string: `gameApiURL: ''`
170-
171-
172-
## Troubleshooting
173-
174-
* "could not find driver" ... if this error is shown `Nette\Database\ConnectionException could not find driver Caused by PDOException`, it is likely because you do not have something installed, see https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver

app/config/config.local.neon.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
parameters:
22
# FKSDB API endpoint
33
fksdbDownloader:
4-
login: ''
5-
password: ''
4+
login: '<fksdb-login>'
5+
password: '<fksdb-password>'
66
url: 'https://db.fykos.cz/api/'
77
problemManagerDownloader:
88
login: ''
99
password: ''
10-
url: ''
10+
url: 'https://static.fykos.cz/problems/'
1111

1212
# game API endpoint, used for FOL/FOF live results
1313
#gameApi:

docker/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
docker-compose.yml
12
log
23
temp
34
config/local
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
- ./config/msmtprc:/etc/msmtprc # mail config
2222
ports:
2323
- 8080-8084:8080-8084 # opened ports mapping, not needed with proxy
24-
user: 1000:1000 # expects the main user with uid:pid
24+
user: <uid>:<gid> # expects the main user with uid:pid
2525
extra_hosts:
2626
# enable the hack in apache.conf
2727
- "vyfuk.local:127.0.0.1"

setup.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
COLOR_RED='\033[0;31m'
5+
COLOR_YELLOW='\033[0;33m'
6+
COLOR_GREEN='\033[0;32m'
7+
COLOR_NONE='\033[0m'
8+
9+
heading() {
10+
printf "\n${COLOR_YELLOW}--- $1 ---${COLOR_NONE}\n"
11+
}
12+
13+
check_command() {
14+
if ! command -v $1 >/dev/null 2>&1
15+
then
16+
echo "Error: command $1 could not be found, please install it"
17+
exit 1
18+
else
19+
echo "$1 found"
20+
fi
21+
}
22+
23+
docker_run() {
24+
docker run --user "$(id -u):$(id -g)" -t --volume .:/var/www/html webs $@
25+
}
26+
27+
28+
heading "Check dependencies"
29+
30+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
31+
if [ "$SCRIPT_DIR" != "$(pwd)" ]
32+
then
33+
echo "You are not in the scripts directory, please cd into it"
34+
echo "cd $SCRIPT_DIR"
35+
exit 1
36+
fi
37+
38+
check_command docker
39+
40+
heading "Setting up configuration"
41+
42+
read -r -p "FKSDB login: " fksdb_login
43+
read -r -p "FKSDB password: " -s fksdb_password
44+
echo ""
45+
46+
configDirectory='app/config/local/'
47+
mkdir -p $configDirectory
48+
49+
for service in vyfuk fykos fol fof dsef; do
50+
file="$configDirectory/$service.neon"
51+
echo "Generating configuration for $service"
52+
cp "app/config/config.local.neon.example" $file
53+
sed -i "s/<fksdb-login>/$fksdb_login/" $file
54+
sed -i "s/<fksdb-password>/$fksdb_password/" $file
55+
done
56+
57+
mkdir -p "temp"
58+
59+
60+
heading "Setting up docker"
61+
62+
cp "docker/docker-compose.yml.sample" "docker/docker-compose.yml"
63+
sed -i "s/<uid>/$(id -u)/" "docker/docker-compose.yml"
64+
sed -i "s/<gid>/$(id -g)/" "docker/docker-compose.yml"
65+
66+
67+
heading "Build docker containers"
68+
(cd docker && docker compose build --pull)
69+
70+
71+
heading "Run composer"
72+
docker_run composer install
73+
74+
75+
heading "Run npm"
76+
docker_run npm install
77+
docker_run npm run build
78+
79+
80+
heading "Setup finished"
81+
printf "${COLOR_GREEN}Setup finished successfully${COLOR_NONE}\n"
82+
echo "Next steps:"
83+
echo "- Move to the docker directory and start docker containers by running:"
84+
echo " - cd docker"
85+
echo " - docker compose up"
86+
echo "- When wanting to stop the containers, press CTRL+C"

0 commit comments

Comments
 (0)