gabrielgio.me @ 9a25224de711803f6b38a1436560f375a9389097

ref: Add new post

Add a new post about how to set up firefly on alpine linux.
  1diff --git a/content/posts/2022-07-18-firefly-install.md b/content/posts/2022-07-18-firefly-install.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..4dfd008cb3afbed1f50049b9cd97e0b93f3cb2ed
  4--- /dev/null
  5+++ b/content/posts/2022-07-18-firefly-install.md
  6@@ -0,0 +1,222 @@
  7+---
  8+title: "Running firefly3 on alpine"
  9+date: 2022-07-18
 10+tags: ['alpine', 'linux', 'php', 'nginx']
 11+---
 12+
 13+**Disclaimer:** before starting be aware that I'm not a sysadmin nor I have a
 14+deep knowledge in security. This is me reporting the steps I did as a learning
 15+experiment, so take this tutorial as your own risk.
 16+
 17+I have a pretty decent knowledge in container tecnology, I maintain several
 18+container on my local server for many applications. However I've decided to
 19+take a step back and learn a bit more how those applications are really
 20+deployed and kept without containers, and first candidate being firefly3[^1]. I
 21+have it currently running on container but let's install in a disctributions.
 22+
 23+For the distro of choice I'll pick alpine, for its small footprint and the use
 24+of OpenRC (nothing against systemd though).
 25+
 26+*I don't want to extend this tutorial to cover every single part, so for the
 27+next steps I'll assume that you have a running instance of PostgreSQL and
 28+Alpine.*
 29+
 30+## Dependencies
 31+
 32+First we need to install all the necessary packages to get firefly running.
 33+Let's go through them and check are they are used for.
 34+
 35+```shell
 36+apk add curl tar gzip
 37+```
 38+
 39+cURL is needed to download the source code from Github and tar gzip are for
 40+extracting the compressed code.
 41+
 42+```shell
 43+apk add composer
 44+```
 45+
 46+ Composer is a dependency manager for PHP. It is required to download the
 47+ dependencies of the project.
 48+
 49+ Now we need to download the list of dependencies list in the site[^2].
 50+
 51+```
 52+ Extra packages
 53+ Install the following PHP modules:
 54+    PHP BCMath Arbitrary Precision Mathematics
 55+    PHP Internationalization extension
 56+    PHP Curl
 57+    PHP Zip
 58+    PHP Sodium
 59+    PHP GD
 60+    PHP XML
 61+    PHP MBString
 62+    PHP whatever database you're gonna use.
 63+```
 64+
 65+For those we have the following alpine packages:
 66+
 67+```shell
 68+apk add \
 69+    php8 \
 70+    php8-curl \
 71+    php8-zip \
 72+    php8-sodium \
 73+    php8-gd \
 74+    php8-xml \
 75+    php8-mbstring \
 76+    php8-bcmath \
 77+    php8-pgsql
 78+```
 79+
 80+But that is not everything, I don't If I lack knowledge in the PHP stack but
 81+the applicatoin will later complain about a lot of missing dependencies, those
 82+being:
 83+
 84+```shell
 85+apk add \
 86+    php8-fileinfo \
 87+    php8-intl \
 88+    php8-session \
 89+    php8-simplexml \
 90+    php8-tokenizer \
 91+    php8-xmlwriter \
 92+    php8-dom \
 93+    php8-pdo_pgsql \
 94+    php8-shmop
 95+```
 96+
 97+A tip that may as well help you later. Some of those not listed packages are
 98+described in the their project for the docker image[^3] and its base image[^4].
 99+It can also help with describing the necessary steps.
100+
101+As the next step we need to install the pieces of software that will actually
102+run the project:
103+
104+```shell
105+apk add nginx php8-fpm
106+```
107+Nginx will act as reverse proxy and php8-fpm will actually run the project. You
108+can use lighttpd as well as some others.
109+
110+## Deploying the code
111+
112+Now we have all necessary packages, lets download the project into on server,
113+grab the latest release from Github, at the time of this writing is `5.7.9`.
114+Download into the `/var/www/firefly`. The folder location is kinda up you, I
115+think nginx itself has another default folder for its sites, but I always use
116+www folder to store the projects.
117+
118+```shell
119+mkdir -p /var/www/firefly
120+```
121+
122+Create the folder then download/extract the source code:
123+
124+```shell
125+curl -SL https://github.com/firefly-iii/firefly-iii/archive/refs/tags/5.7.9.tar.gz | \
126+    tar zxC /var/www/firefly --strip-components 1
127+```
128+
129+This piece of code was taken from the dockerfile[^5].
130+
131+Now move to the `/var/www/firefly` and install its dependencies with composer:
132+```shell
133+cd /var/www/firefly
134+composer install --prefer-dist --no-dev --no-scripts
135+```
136+
137+## Config files
138+
139+### Firefly
140+
141+Firefly makes the process of setting up the connection strings and other
142+configuration quite easy. We'll only need to create an `.env` file with all the
143+information needed. Fill the information according with your setup:
144+
145+```ini
146+# /var/wwww/firefly/.env
147+
148+DB_CONNECTION=pgsql
149+DB_HOST=localhost
150+DB_PORT=5432
151+DB_DATABASE=firefly
152+DB_USERNAME=admin
153+DB_PASSWORD=admin
154+APP_KEY=<RANDON_KEY>
155+```
156+
157+To generate a random key just run:
158+
159+```shell
160+head /dev/urandom | LC_ALL=C tr -dc 'A-Za-z0-9' | head -c 32 && echo
161+```
162+
163+Once you have set it up we need to bootstrap the project:
164+
165+```shell
166+php artisan config:cache
167+```
168+
169+To update the cached configuration. If everything is setup properly the process
170+finish successfully.
171+
172+```shell
173+php artisan firefly-iii:create-database
174+php artisan migrate:refresh --seed
175+php artisan firefly-iii:upgrade-database
176+```
177+
178+To bootstrap the database.
179+
180+### Nginx
181+
182+We will need to edit the nginx config file to find and run the project, add
183+the following server inside of `/etc/nginx/http.d/`, by default nginx will read
184+all `.config` inside of that folder. Just like the www folder this is more a
185+personal choice, you have some room to choose where you want to config this
186+server.
187+
188+```shell
189+# /etc/nginx/http.d/firefly.conf
190+server {
191+    listen 8080;
192+    server_name localhost;
193+
194+
195+    root /var/www/firefly/public;
196+
197+    location ~ \.php$ {
198+        try_files $uri $uri/ =404;
199+        fastcgi_split_path_info ^(.+\.php)(/.+)$;
200+        fastcgi_pass 127.0.0.1:9000;
201+        include fastcgi.conf;
202+    }
203+
204+    location / {
205+      try_files $uri /index.php$is_args$args;
206+    }
207+}
208+```
209+
210+This will set up the process in the port 8080. It is just an exemple, adapt it
211+to your needs.
212+
213+### Services
214+
215+Now that we have everything set up we can start the service to serve firefly:
216+
217+```shell
218+rc-service php-fpm8 start
219+rc-service nginx start
220+```
221+
222+`http://localhot:8080/` (or your server's hostname) should be up and running.
223+
224+[^1]: https://www.firefly-iii.org/
225+[^2]: https://docs.firefly-iii.org/firefly-iii/installation/self_hosted/?mtm_campaign=docu-internal&mtm_kwd=self_hosted
226+[^3]: https://dev.azure.com/Firefly-III/_git/MainImage
227+[^4]: https://dev.azure.com/firefly-iii/_git/BaseImage
228+[^5]: https://dev.azure.com/Firefly-III/MainImage/_git/MainImage?path=/Dockerfile&version=GC520b8f865ea623a8625fe64e9f583406849be91a&line=14&lineEnd=15&lineStartColumn=1&lineEndColumn=1&lineStyle=plain&_a=contents