f4eac521311f86f528beb4838c68f9de73b5d8f4
[akkoma] / docs / docs / installation / openbsd_en.md
1 # Installing on OpenBSD
2
3 This guide describes the installation and configuration of akkoma (and the required software to run it) on a single OpenBSD 7.2 server.
4
5 For any additional information regarding commands and configuration files mentioned here, check the man pages [online](https://man.openbsd.org/) or directly on your server with the man command.
6
7 {! installation/generic_dependencies.include !}
8
9 ### Preparing the system
10 #### Required software
11
12 To install them, run the following command (with doas or as root):
13
14 ```
15 pkg_add elixir gmake git postgresql-server postgresql-contrib cmake ffmpeg ImageMagick erlang-wx-25
16 ```
17
18 (Note that the erlang version may change, it was 25 at the time of writing)
19
20 Akkoma requires a reverse proxy, OpenBSD has relayd in base (and is used in this guide) and packages/ports are available for nginx (www/nginx) and apache (www/apache-httpd). Independently of the reverse proxy, [acme-client(1)](https://man.openbsd.org/acme-client) can be used to get a certificate from Let's Encrypt.
21
22 #### Optional software
23
24 Per [`docs/installation/optional/media_graphics_packages.md`](../installation/optional/media_graphics_packages.md):
25 * ImageMagick
26 * ffmpeg
27 * exiftool
28
29 To install the above:
30
31 ```
32 pkg_add ImageMagick ffmpeg p5-Image-ExifTool
33 ```
34
35 #### Creating the akkoma user
36 Akkoma will be run by a dedicated user, \_akkoma. Before creating it, insert the following lines in login.conf:
37 ```
38 akkoma:\
39 :datasize-max=1536M:\
40 :datasize-cur=1536M:\
41 :openfiles-max=4096
42 ```
43 This creates a "akkoma" login class and sets higher values than default for datasize and openfiles (see [login.conf(5)](https://man.openbsd.org/login.conf)), this is required to avoid having akkoma crash some time after starting.
44
45 Create the \_akkoma user, assign it the akkoma login class and create its home directory (/home/\_akkoma/): `useradd -m -L akkoma _akkoma`
46
47 #### Clone akkoma's directory
48 Enter a shell as the \_akkoma user. As root, run `su _akkoma -;cd`. Then clone the repository with `git clone https://akkoma.dev/AkkomaGang/akkoma.git`. Akkoma is now installed in /home/\_akkoma/akkoma/, it will be configured and started at the end of this guide.
49
50 #### PostgreSQL
51 Start a shell as the \_postgresql user (as root run `su _postgresql -` then run the `initdb` command to initialize postgresql:
52 You will need to specify pgdata directory to the default (/var/postgresql/data) with the `-D <path>` and set the user to postgres with the `-U <username>` flag. This can be done as follows:
53
54 ```
55 initdb -D /var/postgresql/data -U postgres
56 ```
57 If you are not using the default directory, you will have to update the `datadir` variable in the /etc/rc.d/postgresql script.
58
59 When this is done, enable postgresql so that it starts on boot and start it. As root, run:
60 ```
61 rcctl enable postgresql
62 rcctl start postgresql
63 ```
64 To check that it started properly and didn't fail right after starting, you can run `ps aux | grep postgres`, there should be multiple lines of output.
65
66 #### httpd
67 httpd will have three fuctions:
68
69 * redirect requests trying to reach the instance over http to the https URL
70 * serve a robots.txt file
71 * get Let's Encrypt certificates, with acme-client
72
73 Insert the following config in httpd.conf:
74 ```
75 # $OpenBSD: httpd.conf,v 1.17 2017/04/16 08:50:49 ajacoutot Exp $
76
77 ext_inet="<IPv4 address>"
78 ext_inet6="<IPv6 address>"
79
80 server "default" {
81 listen on $ext_inet port 80 # Comment to disable listening on IPv4
82 listen on $ext_inet6 port 80 # Comment to disable listening on IPv6
83 listen on 127.0.0.1 port 80 # Do NOT comment this line
84
85 log syslog
86 directory no index
87
88 location "/.well-known/acme-challenge/*" {
89 root "/acme"
90 request strip 2
91 }
92
93 location "/robots.txt" { root "/htdocs/local/" }
94 location "/*" { block return 302 "https://$HTTP_HOST$REQUEST_URI" }
95 }
96
97 types {
98 }
99 ```
100 Do not forget to change *<IPv4/6 address\>* to your server's address(es). If httpd should only listen on one protocol family, comment one of the two first *listen* options.
101
102 Create the /var/www/htdocs/local/ folder and write the content of your robots.txt in /var/www/htdocs/local/robots.txt.
103 Check the configuration with `httpd -n`, if it is OK enable and start httpd (as root):
104 ```
105 rcctl enable httpd
106 rcctl start httpd
107 ```
108
109 #### acme-client
110 acme-client is used to get SSL/TLS certificates from Let's Encrypt.
111 Insert the following configuration in /etc/acme-client.conf:
112 ```
113 #
114 # $OpenBSD: acme-client.conf,v 1.4 2017/03/22 11:14:14 benno Exp $
115 #
116
117 authority letsencrypt-<domain name> {
118 #agreement url "https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf"
119 api url "https://acme-v02.api.letsencrypt.org/directory"
120 account key "/etc/acme/letsencrypt-privkey-<domain name>.pem"
121 }
122
123 domain <domain name> {
124 domain key "/etc/ssl/private/<domain name>.key"
125 domain certificate "/etc/ssl/<domain name>.crt"
126 domain full chain certificate "/etc/ssl/<domain name>.fullchain.pem"
127 sign with letsencrypt-<domain name>
128 challengedir "/var/www/acme/"
129 }
130 ```
131 Replace *<domain name\>* by the domain name you'll use for your instance. As root, run `acme-client -n` to check the config, then `acme-client -ADv <domain name>` to create account and domain keys, and request a certificate for the first time.
132 Make acme-client run everyday by adding it in /etc/daily.local. As root, run the following command: `echo "acme-client <domain name>" >> /etc/daily.local`.
133
134 Relayd will look for certificates and keys based on the address it listens on (see next part), the easiest way to make them available to relayd is to create a link, as root run:
135 ```
136 ln -s /etc/ssl/<domain name>.fullchain.pem /etc/ssl/<IP address>.crt
137 ln -s /etc/ssl/private/<domain name>.key /etc/ssl/private/<IP address>.key
138 ```
139 This will have to be done for each IPv4 and IPv6 address relayd listens on.
140
141 #### relayd
142 relayd will be used as the reverse proxy sitting in front of akkoma.
143 Insert the following configuration in /etc/relayd.conf:
144 ```
145 # $OpenBSD: relayd.conf,v 1.4 2018/03/23 09:55:06 claudio Exp $
146
147 ext_inet="<IPv4 address>"
148 ext_inet6="<IPv6 address>"
149
150 table <akkoma_server> { 127.0.0.1 }
151 table <httpd_server> { 127.0.0.1 }
152
153 http protocol plerup { # Protocol for upstream akkoma server
154 #tcp { nodelay, sack, socket buffer 65536, backlog 128 } # Uncomment and adjust as you see fit
155 tls ciphers "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305"
156 tls ecdhe secp384r1
157
158 # Forward some paths to the local server (as akkoma won't respond to them as you might want)
159 pass request quick path "/robots.txt" forward to <httpd_server>
160
161 # Append a bunch of headers
162 match request header append "X-Forwarded-For" value "$REMOTE_ADDR" # This two header and the next one are not strictly required by akkoma but adding them won't hurt
163 match request header append "X-Forwarded-By" value "$SERVER_ADDR:$SERVER_PORT"
164
165 match response header append "X-XSS-Protection" value "0"
166 match response header append "X-Permitted-Cross-Domain-Policies" value "none"
167 match response header append "X-Frame-Options" value "DENY"
168 match response header append "X-Content-Type-Options" value "nosniff"
169 match response header append "Referrer-Policy" value "same-origin"
170 match response header append "Content-Security-Policy" value "default-src 'none'; base-uri 'none'; form-action 'self'; img-src 'self' data: https:; media-src 'self' https:; style-src 'self' 'unsafe-inline'; font-src 'self'; script-src 'self'; connect-src 'self' wss://CHANGEME.tld; upgrade-insecure-requests;" # Modify "CHANGEME.tld" and set your instance's domain here
171 match request header append "Connection" value "upgrade"
172 #match response header append "Strict-Transport-Security" value "max-age=63072000; includeSubDomains; preload" # Uncomment this only after you get HTTPS working.
173
174 # If you do not want remote frontends to be able to access your Akkoma backend server, comment these lines
175 match response header append "Access-Control-Allow-Origin" value "*"
176 match response header append "Access-Control-Allow-Methods" value "POST, PUT, DELETE, GET, PATCH, OPTIONS"
177 match response header append "Access-Control-Allow-Headers" value "Authorization, Content-Type, Idempotency-Key"
178 match response header append "Access-Control-Expose-Headers" value "Link, X-RateLimit-Reset, X-RateLimit-Limit, X-RateLimit-Remaining, X-Request-Id"
179 # Stop commenting lines here
180 }
181
182 relay wwwtls {
183 listen on $ext_inet port https tls # Comment to disable listening on IPv4
184 listen on $ext_inet6 port https tls # Comment to disable listening on IPv6
185
186 protocol plerup
187
188 forward to <akkoma_server> port 4000 check http "/" code 200
189 forward to <httpd_server> port 80 check http "/robots.txt" code 200
190 }
191 ```
192 Again, change *<IPv4/6 address\>* to your server's address(es) and comment one of the two *listen* options if needed. Also change *wss://CHANGEME.tld* to *wss://<your instance's domain name\>*.
193 Check the configuration with `relayd -n`, if it is OK enable and start relayd (as root):
194 ```
195 rcctl enable relayd
196 rcctl start relayd
197 ```
198
199 #### pf
200 Enabling and configuring pf is highly recommended.
201 In /etc/pf.conf, insert the following configuration:
202 ```
203 # Macros
204 if="<network interface>"
205 authorized_ssh_clients="any"
206
207 # Skip traffic on loopback interface
208 set skip on lo
209
210 # Default behavior
211 set block-policy drop
212 block in log all
213 pass out quick
214
215 # Security features
216 match in all scrub (no-df random-id)
217 block in log from urpf-failed
218
219 # Rules
220 pass in quick on $if inet proto icmp to ($if) icmp-type { echoreq unreach paramprob trace } # ICMP
221 pass in quick on $if inet6 proto icmp6 to ($if) icmp6-type { echoreq unreach paramprob timex toobig } # ICMPv6
222 pass in quick on $if proto tcp to ($if) port { http https } # relayd/httpd
223 pass in quick on $if proto tcp from $authorized_ssh_clients to ($if) port ssh
224 ```
225 Replace *<network interface\>* by your server's network interface name (which you can get with ifconfig). Consider replacing the content of the authorized\_ssh\_clients macro by, for exemple, your home IP address, to avoid SSH connection attempts from bots.
226
227 Check pf's configuration by running `pfctl -nf /etc/pf.conf`, load it with `pfctl -f /etc/pf.conf` and enable pf at boot with `rcctl enable pf`.
228
229 #### Configure and start akkoma
230 Enter a shell as \_akkoma (as root `su _akkoma -`) and enter akkoma's installation directory (`cd ~/akkoma/`).
231
232 Then follow the main installation guide:
233
234 * run `mix deps.get`
235 * run `MIX_ENV=prod mix pleroma.instance gen` and enter your instance's information when asked
236 * copy config/generated\_config.exs to config/prod.secret.exs. The default values should be sufficient but you should edit it and check that everything seems OK.
237 * exit your current shell back to a root one and run `psql -U postgres -f /home/_akkoma/akkoma/config/setup_db.psql` to setup the database.
238 * return to a \_akkoma shell into akkoma's installation directory (`su _akkoma -;cd ~/akkoma`) and run `MIX_ENV=prod mix ecto.migrate`
239
240 As \_akkoma in /home/\_akkoma/akkoma, you can now run `LC_ALL=en_US.UTF-8 MIX_ENV=prod mix phx.server` to start your instance.
241 In another SSH session/tmux window, check that it is working properly by running `ftp -MVo - http://127.0.0.1:4000/api/v1/instance`, you should get json output. Double-check that *uri*'s value is your instance's domain name.
242
243 ##### Starting akkoma at boot
244 An rc script to automatically start akkoma at boot hasn't been written yet, it can be run in a tmux session (tmux is in base).
245
246
247 #### Create administrative user
248
249 If your instance is up and running, you can create your first user with administrative rights with the following command as the \_akkoma user.
250 ```
251 LC_ALL=en_US.UTF-8 MIX_ENV=prod mix pleroma.user new <username> <your@emailaddress> --admin
252 ```
253
254 {! installation/frontends.include !}
255
256 #### Further reading
257
258 {! installation/further_reading.include !}
259
260 {! support.include !}