]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - support/doc/production.md
dd57e912088228c1169abee58559b66d9a248a2a
[github/Chocobozzz/PeerTube.git] / support / doc / production.md
1 # Production guide
2
3 * [Installation](#installation)
4 * [Upgrade](#upgrade)
5
6 ## Installation
7
8 Please don't install PeerTube for production on a device behind a low bandwidth connection (example: your ADSL link).
9 If you want information about the appropriate hardware to run PeerTube, please see the [FAQ](https://joinpeertube.org/en_US/faq#should-i-have-a-big-server-to-run-peertube).
10
11 ### :hammer: Dependencies
12
13 Follow the steps of the [dependencies guide](dependencies.md).
14
15 ### :construction_worker: PeerTube user
16
17 Create a `peertube` user with `/var/www/peertube` home:
18
19 ```bash
20 $ sudo useradd -m -d /var/www/peertube -s /bin/bash -p peertube peertube
21 ```
22
23 Set its password:
24 ```bash
25 $ sudo passwd peertube
26 ```
27
28 Ensure the peertube root directory is traversable by nginx:
29
30 ```bash
31 $ ls -ld /var/www/peertube # Should be drwxr-xr-x
32 ```
33
34 **On FreeBSD**
35
36 ```bash
37 $ sudo pw useradd -n peertube -d /var/www/peertube -s /usr/local/bin/bash -m
38 $ sudo passwd peertube
39 ```
40 or use `adduser` to create it interactively.
41
42 ### :card_file_box: Database
43
44 Create the production database and a peertube user inside PostgreSQL:
45
46 ```bash
47 $ cd /var/www/peertube
48 $ sudo -u postgres createuser -P peertube
49 ```
50
51 Here you should enter a password for PostgreSQL `peertube` user, that should be copied in `production.yaml` file.
52 Don't just hit enter else it will be empty.
53
54 ```bash
55 $ sudo -u postgres createdb -O peertube -E UTF8 -T template0 peertube_prod
56 ```
57
58 Then enable extensions PeerTube needs:
59
60 ```bash
61 $ sudo -u postgres psql -c "CREATE EXTENSION pg_trgm;" peertube_prod
62 $ sudo -u postgres psql -c "CREATE EXTENSION unaccent;" peertube_prod
63 ```
64
65 ### :page_facing_up: Prepare PeerTube directory
66
67 Fetch the latest tagged version of Peertube:
68
69 ```bash
70 $ VERSION=$(curl -s https://api.github.com/repos/chocobozzz/peertube/releases/latest | grep tag_name | cut -d '"' -f 4) && echo "Latest Peertube version is $VERSION"
71 ```
72
73
74 Open the peertube directory, create a few required directories:
75
76 ```bash
77 $ cd /var/www/peertube
78 $ sudo -u peertube mkdir config storage versions
79 $ sudo -u peertube chmod 750 config/
80 ```
81
82
83 Download the latest version of the Peertube client, unzip it and remove the zip:
84
85 ```bash
86 $ cd /var/www/peertube/versions
87 $ # Releases are also available on https://builds.joinpeertube.org/release
88 $ sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip"
89 $ sudo -u peertube unzip -q peertube-${VERSION}.zip && sudo -u peertube rm peertube-${VERSION}.zip
90 ```
91
92
93 Install Peertube:
94
95 ```bash
96 $ cd /var/www/peertube
97 $ sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
98 $ cd ./peertube-latest && sudo -H -u peertube yarn install --production --pure-lockfile
99 ```
100
101 ### :wrench: PeerTube configuration
102
103 Copy the default configuration file that contains the default configuration provided by PeerTube.
104 You **must not** update this file.
105
106 ```bash
107 $ cd /var/www/peertube
108 $ sudo -u peertube cp peertube-latest/config/default.yaml config/default.yaml
109 ```
110
111 Now copy the production example configuration:
112
113 ```bash
114 $ cd /var/www/peertube
115 $ sudo -u peertube cp peertube-latest/config/production.yaml.example config/production.yaml
116 ```
117
118 Then edit the `config/production.yaml` file according to your webserver and database configuration. In particular:
119 * `webserver`: Reverse proxy public information
120 * `secrets`: Secret strings you must generate manually (PeerTube version >= 5.0)
121 * `database`: PostgreSQL settings
122 * `redis`: Redis settings
123 * `smtp`: If you want to use emails
124 * `admin.email`: To correctly fill `root` user email
125
126 Keys defined in `config/production.yaml` will override keys defined in `config/default.yaml`.
127
128 **PeerTube does not support webserver host change**. Even though [PeerTube CLI can help you to switch hostname](https://docs.joinpeertube.org/maintain-tools?id=update-hostjs) there's no official support for that since it is a risky operation that might result in unforeseen errors.
129
130 ### :truck: Webserver
131
132 We only provide official configuration files for Nginx.
133
134 Copy the nginx configuration template:
135
136 ```bash
137 $ sudo cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube
138 ```
139
140 Then set the domain for the webserver configuration file.
141 Replace `[peertube-domain]` with the domain for the peertube server.
142
143 ```bash
144 $ sudo sed -i 's/${WEBSERVER_HOST}/[peertube-domain]/g' /etc/nginx/sites-available/peertube
145 $ sudo sed -i 's/${PEERTUBE_HOST}/127.0.0.1:9000/g' /etc/nginx/sites-available/peertube
146 ```
147
148 Then modify the webserver configuration file. Please pay attention to the `alias` keys of the static locations.
149 It should correspond to the paths of your storage directories (set in the configuration file inside the `storage` key).
150
151 ```bash
152 $ sudo vim /etc/nginx/sites-available/peertube
153 ```
154
155 Activate the configuration file:
156
157 ```bash
158 $ sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/peertube
159 ```
160
161 To generate the certificate for your domain as required to make https work you can use [Let's Encrypt](https://letsencrypt.org/):
162
163 ```bash
164 $ sudo systemctl stop nginx
165 $ sudo certbot certonly --standalone --post-hook "systemctl restart nginx"
166 $ sudo systemctl reload nginx
167 ```
168
169 Certbot should have installed a cron to automatically renew your certificate.
170 Since our nginx template supports webroot renewal, we suggest you to update the renewal config file to use the `webroot` authenticator:
171
172 ```bash
173 $ # Replace authenticator = standalone by authenticator = webroot
174 $ # Add webroot_path = /var/www/certbot
175 $ sudo vim /etc/letsencrypt/renewal/your-domain.com.conf
176 ```
177
178 If you plan to have many concurrent viewers on your PeerTube instance, consider increasing `worker_connections` value: https://nginx.org/en/docs/ngx_core_module.html#worker_connections.
179
180 **FreeBSD**
181 On FreeBSD you can use [Dehydrated](https://dehydrated.io/) `security/dehydrated` for [Let's Encrypt](https://letsencrypt.org/)
182
183 ```bash
184 $ sudo pkg install dehydrated
185 ```
186
187 ### :alembic: TCP/IP Tuning
188
189 **On Linux**
190
191 ```bash
192 $ sudo cp /var/www/peertube/peertube-latest/support/sysctl.d/30-peertube-tcp.conf /etc/sysctl.d/
193 $ sudo sysctl -p /etc/sysctl.d/30-peertube-tcp.conf
194 ```
195
196 Your distro may enable this by default, but at least Debian 9 does not, and the default FIFO
197 scheduler is quite prone to "Buffer Bloat" and extreme latency when dealing with slower client
198 links as we often encounter in a video server.
199
200 ### :bricks: systemd
201
202 If your OS uses systemd, copy the configuration template:
203
204 ```bash
205 $ sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
206 ```
207
208 Check the service file (PeerTube paths and security directives):
209
210 ```bash
211 $ sudo vim /etc/systemd/system/peertube.service
212 ```
213
214
215 Tell systemd to reload its config:
216
217 ```bash
218 $ sudo systemctl daemon-reload
219 ```
220
221 If you want to start PeerTube on boot:
222
223 ```bash
224 $ sudo systemctl enable peertube
225 ```
226
227 Run:
228
229 ```bash
230 $ sudo systemctl start peertube
231 $ sudo journalctl -feu peertube
232 ```
233
234 **FreeBSD**
235 On FreeBSD, copy the startup script and update rc.conf:
236
237 ```bash
238 $ sudo install -m 0555 /var/www/peertube/peertube-latest/support/freebsd/peertube /usr/local/etc/rc.d/
239 $ sudo sysrc peertube_enable="YES"
240 ```
241
242 Run:
243
244 ```bash
245 $ sudo service peertube start
246 ```
247
248 ### :bricks: OpenRC
249
250 If your OS uses OpenRC, copy the service script:
251
252 ```bash
253 $ sudo cp /var/www/peertube/peertube-latest/support/init.d/peertube /etc/init.d/
254 ```
255
256 If you want to start PeerTube on boot:
257
258 ```bash
259 $ sudo rc-update add peertube default
260 ```
261
262 Run and print last logs:
263
264 ```bash
265 $ sudo /etc/init.d/peertube start
266 $ tail -f /var/log/peertube/peertube.log
267 ```
268
269 ### :technologist: Administrator
270
271 The administrator username is `root` and the password is automatically generated. It can be found in PeerTube
272 logs (path defined in `production.yaml`). You can also set another password with:
273
274 ```bash
275 $ cd /var/www/peertube/peertube-latest && NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run reset-password -- -u root
276 ```
277
278 Alternatively you can set the environment variable `PT_INITIAL_ROOT_PASSWORD`,
279 to your own administrator password, although it must be 6 characters or more.
280
281 ### :tada: What now?
282
283 Now your instance is up you can:
284
285 * Add your instance to the public PeerTube instances index if you want to: https://instances.joinpeertube.org/
286 * Check [available CLI tools](/support/doc/tools.md)
287
288 ## Upgrade
289
290 ### PeerTube instance
291
292 **Check the changelog (in particular the *IMPORTANT NOTES* section):** https://github.com/Chocobozzz/PeerTube/blob/develop/CHANGELOG.md
293
294 #### Auto
295
296 The password it asks is PeerTube's database user password.
297
298 ```bash
299 $ cd /var/www/peertube/peertube-latest/scripts && sudo -H -u peertube ./upgrade.sh
300 $ sudo systemctl restart peertube # Or use your OS command to restart PeerTube if you don't use systemd
301 ```
302
303 #### Manually
304
305 Make a SQL backup
306
307 ```bash
308 $ SQL_BACKUP_PATH="backup/sql-peertube_prod-$(date -Im).bak" && \
309 cd /var/www/peertube && sudo -u peertube mkdir -p backup && \
310 sudo -u postgres pg_dump -F c peertube_prod | sudo -u peertube tee "$SQL_BACKUP_PATH" >/dev/null
311 ```
312
313 Fetch the latest tagged version of Peertube:
314
315 ```bash
316 $ VERSION=$(curl -s https://api.github.com/repos/chocobozzz/peertube/releases/latest | grep tag_name | cut -d '"' -f 4) && echo "Latest Peertube version is $VERSION"
317 ```
318
319 Download the new version and unzip it:
320
321 ```bash
322 $ cd /var/www/peertube/versions && \
323 sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" && \
324 sudo -u peertube unzip -o peertube-${VERSION}.zip && \
325 sudo -u peertube rm peertube-${VERSION}.zip
326 ```
327
328 Install node dependencies:
329
330 ```bash
331 $ cd /var/www/peertube/versions/peertube-${VERSION} && \
332 sudo -H -u peertube yarn install --production --pure-lockfile
333 ```
334
335 Copy new configuration defaults values and update your configuration file:
336
337 ```bash
338 $ sudo -u peertube cp /var/www/peertube/versions/peertube-${VERSION}/config/default.yaml /var/www/peertube/config/default.yaml
339 $ diff -u /var/www/peertube/versions/peertube-${VERSION}/config/production.yaml.example /var/www/peertube/config/production.yaml
340 ```
341
342 Change the link to point to the latest version:
343
344 ```bash
345 $ cd /var/www/peertube && \
346 sudo unlink ./peertube-latest && \
347 sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
348 ```
349
350 ### Configuration
351
352 You can check for configuration changes, and report them in your `config/production.yaml` file:
353
354 ```bash
355 $ cd /var/www/peertube/versions
356 $ diff -u "$(ls --sort=t | head -2 | tail -1)/config/production.yaml.example" "$(ls --sort=t | head -1)/config/production.yaml.example"
357 ```
358
359 ### nginx
360
361 Check changes in nginx configuration:
362
363 ```bash
364 $ cd /var/www/peertube/versions
365 $ diff -u "$(ls --sort=t | head -2 | tail -1)/support/nginx/peertube" "$(ls --sort=t | head -1)/support/nginx/peertube"
366 ```
367
368 ### systemd
369
370 Check changes in systemd configuration:
371
372 ```bash
373 $ cd /var/www/peertube/versions
374 $ diff -u "$(ls --sort=t | head -2 | tail -1)/support/systemd/peertube.service" "$(ls --sort=t | head -1)/support/systemd/peertube.service"
375 ```
376
377 ### Restart PeerTube
378
379 If you changed your nginx configuration:
380
381 ```bash
382 $ sudo systemctl reload nginx
383 ```
384
385 If you changed your systemd configuration:
386
387 ```bash
388 $ sudo systemctl daemon-reload
389 ```
390
391 Restart PeerTube and check the logs:
392
393 ```bash
394 $ sudo systemctl restart peertube && sudo journalctl -fu peertube
395 ```
396
397 ### Things went wrong?
398
399 Change `peertube-latest` destination to the previous version and restore your SQL backup:
400
401 ```bash
402 $ OLD_VERSION="v0.42.42" && SQL_BACKUP_PATH="backup/sql-peertube_prod-2018-01-19T10:18+01:00.bak" && \
403 cd /var/www/peertube && sudo -u peertube unlink ./peertube-latest && \
404 sudo -u peertube ln -s "versions/peertube-$OLD_VERSION" peertube-latest && \
405 sudo -u postgres pg_restore -c -C -d postgres "$SQL_BACKUP_PATH" && \
406 sudo systemctl restart peertube
407 ```