production.md 11.3 KB
Newer Older
1
2
# Production guide

Chocobozzz's avatar
Chocobozzz committed
3
  * [Installation](#installation)
4
  * [Upgrade](#upgrade)
Chocobozzz's avatar
Chocobozzz committed
5

6
7
## Installation

Chocobozzz's avatar
Chocobozzz committed
8
Please don't install PeerTube for production on a device behind a low bandwidth connection (example: your ADSL link).
Rigel Kent's avatar
Rigel Kent committed
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).
Chocobozzz's avatar
Chocobozzz committed
10

Chocobozzz's avatar
Chocobozzz committed
11
### :hammer: Dependencies
12

Chocobozzz's avatar
Chocobozzz committed
13
Follow the steps of the [dependencies guide](dependencies.md).
14

Chocobozzz's avatar
Chocobozzz committed
15
### :construction_worker: PeerTube user
16

17
Create a `peertube` user with `/var/www/peertube` home:
18

Chocobozzz's avatar
Chocobozzz committed
19
```bash
20
$ sudo useradd -m -d /var/www/peertube -s /bin/bash -p peertube peertube
Thomas Citharel's avatar
Thomas Citharel committed
21
22
23
```

Set its password:
Chocobozzz's avatar
Chocobozzz committed
24
```bash
Chocobozzz's avatar
Chocobozzz committed
25
$ sudo passwd peertube
26
27
```

28
29
30
31
32
33
Ensure the peertube root directory is traversable by nginx:

```bash
$ ls -ld /var/www/peertube # Should be drwxr-xr-x
```

34
35
**On FreeBSD**

Chocobozzz's avatar
Chocobozzz committed
36
```bash
37
38
39
40
41
$ sudo pw useradd -n peertube -d /var/www/peertube -s /usr/local/bin/bash -m
$ sudo passwd peertube
```
or use `adduser` to create it interactively.

Chocobozzz's avatar
Chocobozzz committed
42
### :card_file_box: Database
43

Thomas Citharel's avatar
Thomas Citharel committed
44
Create the production database and a peertube user inside PostgreSQL:
45

Chocobozzz's avatar
Chocobozzz committed
46
```bash
47
$ cd /var/www/peertube
Chocobozzz's avatar
Chocobozzz committed
48
$ sudo -u postgres createuser -P peertube
49
50
```

Chocobozzz's avatar
Chocobozzz committed
51
52
Here you should enter a password for PostgreSQL `peertube` user, that should be copied in `production.yaml` file.
Don't just hit enter else it will be empty.
53

Chocobozzz's avatar
Chocobozzz committed
54
```bash
55
$ sudo -u postgres createdb -O peertube -E UTF8 -T template0 peertube_prod
56
57
```

Chocobozzz's avatar
Chocobozzz committed
58
59
Then enable extensions PeerTube needs:

Chocobozzz's avatar
Chocobozzz committed
60
```bash
Chocobozzz's avatar
Chocobozzz committed
61
62
63
64
$ sudo -u postgres psql -c "CREATE EXTENSION pg_trgm;" peertube_prod
$ sudo -u postgres psql -c "CREATE EXTENSION unaccent;" peertube_prod
```

Chocobozzz's avatar
Chocobozzz committed
65
### :page_facing_up: Prepare PeerTube directory
66

67
68
Fetch the latest tagged version of Peertube:

Chocobozzz's avatar
Chocobozzz committed
69
```bash
Thomas Citharel's avatar
Thomas Citharel committed
70
71
$ 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"
```
72

73
74
75

Open the peertube directory, create a few required directories:

Chocobozzz's avatar
Chocobozzz committed
76
```bash
Chocobozzz's avatar
Chocobozzz committed
77
78
$ cd /var/www/peertube
$ sudo -u peertube mkdir config storage versions
Chocobozzz's avatar
Chocobozzz committed
79
$ sudo -u peertube chmod 750 config/
Thomas Citharel's avatar
Thomas Citharel committed
80
```
81

82
83
84

Download the latest version of the Peertube client, unzip it and remove the zip:

Chocobozzz's avatar
Chocobozzz committed
85
```bash
Chocobozzz's avatar
Chocobozzz committed
86
$ cd /var/www/peertube/versions
87
$ # Releases are also available on https://builds.joinpeertube.org/release
Thomas Citharel's avatar
Thomas Citharel committed
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
Thomas Citharel's avatar
Thomas Citharel committed
90
```
91

92

David Libeau's avatar
David Libeau committed
93
Install Peertube:
94

Chocobozzz's avatar
Chocobozzz committed
95
```bash
Chocobozzz's avatar
Chocobozzz committed
96
97
$ cd /var/www/peertube
$ 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
```

Chocobozzz's avatar
Chocobozzz committed
101
### :wrench: PeerTube configuration
102

Chocobozzz's avatar
Chocobozzz committed
103
104
105
Copy the default configuration file that contains the default configuration provided by PeerTube.
You **must not** update this file.

Chocobozzz's avatar
Chocobozzz committed
106
```bash
Chocobozzz's avatar
Chocobozzz committed
107
108
$ cd /var/www/peertube
$ sudo -u peertube cp peertube-latest/config/default.yaml config/default.yaml
Chocobozzz's avatar
Chocobozzz committed
109
110
111
```

Now copy the production example configuration:
112

Chocobozzz's avatar
Chocobozzz committed
113
```bash
Chocobozzz's avatar
Chocobozzz committed
114
115
$ cd /var/www/peertube
$ 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
Chocobozzz's avatar
Chocobozzz committed
119
120
and database configuration (`webserver`, `database`, `redis`, `smtp` and `admin.email` sections in particular).
Keys defined in `config/production.yaml` will override keys defined in `config/default.yaml`.
121

Chocobozzz's avatar
Chocobozzz committed
122
**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.
123

Chocobozzz's avatar
Chocobozzz committed
124
### :truck: Webserver
125

126
127
We only provide official configuration files for Nginx.

128
129
Copy the nginx configuration template:

Chocobozzz's avatar
Chocobozzz committed
130
```bash
131
$ sudo cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube
132
133
```

Chocobozzz's avatar
Chocobozzz committed
134
135
Then set the domain for the webserver configuration file.
Replace `[peertube-domain]` with the domain for the peertube server.
136

Chocobozzz's avatar
Chocobozzz committed
137
```bash
138
$ sudo sed -i 's/${WEBSERVER_HOST}/[peertube-domain]/g' /etc/nginx/sites-available/peertube
139
$ sudo sed -i 's/${PEERTUBE_HOST}/127.0.0.1:9000/g' /etc/nginx/sites-available/peertube
140
141
```

Chocobozzz's avatar
Chocobozzz committed
142
143
Then modify the webserver configuration file. Please pay attention to the `alias` keys of the static locations.
It should correspond to the paths of your storage directories (set in the configuration file inside the `storage` key).
144

Chocobozzz's avatar
Chocobozzz committed
145
```bash
146
147
148
$ sudo vim /etc/nginx/sites-available/peertube
```

Chocobozzz's avatar
Chocobozzz committed
149
150
Activate the configuration file:

Chocobozzz's avatar
Chocobozzz committed
151
```bash
Chocobozzz's avatar
Chocobozzz committed
152
153
154
155
156
$ sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/peertube
```

To generate the certificate for your domain as required to make https work you can use [Let's Encrypt](https://letsencrypt.org/):

Chocobozzz's avatar
Chocobozzz committed
157
```bash
Chocobozzz's avatar
Chocobozzz committed
158
$ sudo systemctl stop nginx
G4dter's avatar
G4dter committed
159
$ sudo certbot certonly --standalone --post-hook "systemctl restart nginx"
160
$ sudo systemctl reload nginx
161
162
```

Chocobozzz's avatar
Chocobozzz committed
163
164
165
Certbot should have installed a cron to automatically renew your certificate.
Since our nginx template supports webroot renewal, we suggest you to update the renewal config file to use the `webroot` authenticator:

Chocobozzz's avatar
Chocobozzz committed
166
```bash
Chocobozzz's avatar
Chocobozzz committed
167
$ # Replace authenticator = standalone by authenticator = webroot
G4dter's avatar
G4dter committed
168
$ # Add webroot_path = /var/www/certbot
Chocobozzz's avatar
Chocobozzz committed
169
170
171
$ sudo vim /etc/letsencrypt/renewal/your-domain.com.conf
```

172
173
174
**FreeBSD**
On FreeBSD you can use [Dehydrated](https://dehydrated.io/) `security/dehydrated` for [Let's Encrypt](https://letsencrypt.org/)

Chocobozzz's avatar
Chocobozzz committed
175
```bash
176
177
178
$ sudo pkg install dehydrated
```

Chocobozzz's avatar
Chocobozzz committed
179
### :alembic: TCP/IP Tuning
180
181
182

**On Linux**

Chocobozzz's avatar
Chocobozzz committed
183
```bash
184
185
186
187
188
189
190
191
$ sudo cp /var/www/peertube/peertube-latest/support/sysctl.d/30-peertube-tcp.conf /etc/sysctl.d/
$ sudo sysctl -p /etc/sysctl.d/30-peertube-tcp.conf
```

Your distro may enable this by default, but at least Debian 9 does not, and the default FIFO
scheduler is quite prone to "Buffer Bloat" and extreme latency when dealing with slower client
links as we often encounter in a video server.

Chocobozzz's avatar
Chocobozzz committed
192
### :bricks: systemd
193

Chocobozzz's avatar
Chocobozzz committed
194
If your OS uses systemd, copy the configuration template:
195

Chocobozzz's avatar
Chocobozzz committed
196
```bash
197
$ sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
198
199
```

Chocobozzz's avatar
Chocobozzz committed
200
Check the service file (PeerTube paths and security directives):
201

Chocobozzz's avatar
Chocobozzz committed
202
```bash
Chocobozzz's avatar
Chocobozzz committed
203
$ sudo vim /etc/systemd/system/peertube.service
204
205
206
207
208
```


Tell systemd to reload its config:

Chocobozzz's avatar
Chocobozzz committed
209
```bash
Chocobozzz's avatar
Chocobozzz committed
210
$ sudo systemctl daemon-reload
211
212
```

Chocobozzz's avatar
Chocobozzz committed
213
214
If you want to start PeerTube on boot:

Chocobozzz's avatar
Chocobozzz committed
215
```bash
taziden's avatar
taziden committed
216
$ sudo systemctl enable peertube
Chocobozzz's avatar
Chocobozzz committed
217
218
```

Chocobozzz's avatar
Chocobozzz committed
219
Run:
220

Chocobozzz's avatar
Chocobozzz committed
221
```bash
Chocobozzz's avatar
Chocobozzz committed
222
223
$ sudo systemctl start peertube
$ sudo journalctl -feu peertube
224
225
```

Chocobozzz's avatar
Chocobozzz committed
226
227
**FreeBSD**
On FreeBSD, copy the startup script and update rc.conf:
Gérald Niel's avatar
Gérald Niel committed
228

Chocobozzz's avatar
Chocobozzz committed
229
```bash
230
231
$ sudo install -m 0555 /var/www/peertube/peertube-latest/support/freebsd/peertube /usr/local/etc/rc.d/
$ sudo sysrc peertube_enable="YES"
Gérald Niel's avatar
Gérald Niel committed
232
233
```

Chocobozzz's avatar
Chocobozzz committed
234
Run:
Gérald Niel's avatar
Gérald Niel committed
235

Chocobozzz's avatar
Chocobozzz committed
236
```bash
Gérald Niel's avatar
Gérald Niel committed
237
238
239
$ sudo service peertube start
```

Chocobozzz's avatar
Chocobozzz committed
240
### :bricks: OpenRC
241
242
243

If your OS uses OpenRC, copy the service script:

Chocobozzz's avatar
Chocobozzz committed
244
```bash
245
246
247
248
249
$ sudo cp /var/www/peertube/peertube-latest/support/init.d/peertube /etc/init.d/
```

If you want to start PeerTube on boot:

Chocobozzz's avatar
Chocobozzz committed
250
```bash
251
252
253
254
255
$ sudo rc-update add peertube default
```

Run and print last logs:

Chocobozzz's avatar
Chocobozzz committed
256
```bash
257
258
259
260
$ sudo /etc/init.d/peertube start
$ tail -f /var/log/peertube/peertube.log
```

Chocobozzz's avatar
Chocobozzz committed
261
### :technologist: Administrator
262

263
The administrator username is `root` and the password is automatically generated. It can be found in PeerTube
Chocobozzz's avatar
Chocobozzz committed
264
logs (path defined in `production.yaml`). You can also set another password with:
265

Chocobozzz's avatar
Chocobozzz committed
266
```bash
267
$ cd /var/www/peertube/peertube-latest && NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run reset-password -- -u root
268
269
```

270
271
272
Alternatively you can set the environment variable `PT_INITIAL_ROOT_PASSWORD`,
to your own administrator password, although it must be 6 characters or more.

Chocobozzz's avatar
Chocobozzz committed
273
### :tada: What now?
274
275

Now your instance is up you can:
Chocobozzz's avatar
Chocobozzz committed
276

Steffen Möller's avatar
Steffen Möller committed
277
 * Add your instance to the public PeerTube instances index if you want to: https://instances.joinpeertube.org/
278
 * Check [available CLI tools](/support/doc/tools.md)
Chocobozzz's avatar
Chocobozzz committed
279

280
281
## Upgrade

Chocobozzz's avatar
Chocobozzz committed
282
### PeerTube instance
283
284
285

**Check the changelog (in particular BREAKING CHANGES!):** https://github.com/Chocobozzz/PeerTube/blob/develop/CHANGELOG.md

286
#### Auto
287

288
289
The password it asks is PeerTube's database user password.

Chocobozzz's avatar
Chocobozzz committed
290
```bash
291
$ cd /var/www/peertube/peertube-latest/scripts && sudo -H -u peertube ./upgrade.sh
292
$ sudo systemctl restart peertube # Or use your OS command to restart PeerTube if you don't use systemd
293
294
```

Chocobozzz's avatar
Chocobozzz committed
295
296
#### Manually

297
Make a SQL backup
298

Chocobozzz's avatar
Chocobozzz committed
299
```bash
300
$ SQL_BACKUP_PATH="backup/sql-peertube_prod-$(date -Im).bak" && \
301
    cd /var/www/peertube && sudo -u peertube mkdir -p backup && \
302
    sudo -u postgres pg_dump -F c peertube_prod | sudo -u peertube tee "$SQL_BACKUP_PATH" >/dev/null
303
304
```

305
Fetch the latest tagged version of Peertube:
306

Chocobozzz's avatar
Chocobozzz committed
307
```bash
308
$ 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"
309
310
```

311
Download the new version and unzip it:
312

Chocobozzz's avatar
Chocobozzz committed
313
```bash
314
$ cd /var/www/peertube/versions && \
315
    sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" && \
316
317
318
319
    sudo -u peertube unzip -o peertube-${VERSION}.zip && \
    sudo -u peertube rm peertube-${VERSION}.zip
```

Chocobozzz's avatar
Chocobozzz committed
320
Install node dependencies:
321

Chocobozzz's avatar
Chocobozzz committed
322
```bash
Chocobozzz's avatar
Chocobozzz committed
323
$ cd /var/www/peertube/versions/peertube-${VERSION} && \
324
    sudo -H -u peertube yarn install --production --pure-lockfile
325
326
```

Chocobozzz's avatar
Chocobozzz committed
327
Copy new configuration defaults values and update your configuration file:
328

Chocobozzz's avatar
Chocobozzz committed
329
```bash
Chocobozzz's avatar
Chocobozzz committed
330
$ sudo -u peertube cp /var/www/peertube/versions/peertube-${VERSION}/config/default.yaml /var/www/peertube/config/default.yaml
Chocobozzz's avatar
Chocobozzz committed
331
$ diff -u /var/www/peertube/versions/peertube-${VERSION}/config/production.yaml.example /var/www/peertube/config/production.yaml
332
333
```

Chocobozzz's avatar
Chocobozzz committed
334
Change the link to point to the latest version:
335

Chocobozzz's avatar
Chocobozzz committed
336
```bash
Chocobozzz's avatar
Chocobozzz committed
337
$ cd /var/www/peertube && \
338
    sudo unlink ./peertube-latest && \
Chocobozzz's avatar
Chocobozzz committed
339
    sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
340
341
```

342
343
344
345
346
347
348
349
350
### Configuration

You can check for configuration changes, and report them in your `config/production.yaml` file:

```bash
$ cd /var/www/peertube/versions
$ diff -u "$(ls --sort=t | head -2 | tail -1)/config/production.yaml.example" "$(ls --sort=t | head -1)/config/production.yaml.example"
```

351
352
353
354
### nginx

Check changes in nginx configuration:

Chocobozzz's avatar
Chocobozzz committed
355
```bash
356
$ cd /var/www/peertube/versions
Chocobozzz's avatar
Chocobozzz committed
357
$ diff -u "$(ls --sort=t | head -2 | tail -1)/support/nginx/peertube" "$(ls --sort=t | head -1)/support/nginx/peertube"
358
359
360
361
362
363
```

### systemd

Check changes in systemd configuration:

Chocobozzz's avatar
Chocobozzz committed
364
```bash
365
$ cd /var/www/peertube/versions
Chocobozzz's avatar
Chocobozzz committed
366
$ diff -u "$(ls --sort=t | head -2 | tail -1)/support/systemd/peertube.service" "$(ls --sort=t | head -1)/support/systemd/peertube.service"
367
368
369
370
371
372
```

### Restart PeerTube

If you changed your nginx configuration:

Chocobozzz's avatar
Chocobozzz committed
373
```bash
374
375
376
377
$ sudo systemctl reload nginx
```

If you changed your systemd configuration:
Chocobozzz's avatar
Chocobozzz committed
378

Chocobozzz's avatar
Chocobozzz committed
379
```bash
380
381
382
383
384
$ sudo systemctl daemon-reload
```

Restart PeerTube and check the logs:

Chocobozzz's avatar
Chocobozzz committed
385
```bash
386
$ sudo systemctl restart peertube && sudo journalctl -fu peertube
387
388
```

389
### Things went wrong?
390

391
Change `peertube-latest` destination to the previous version and restore your SQL backup:
392

Chocobozzz's avatar
Chocobozzz committed
393
```bash
394
$ OLD_VERSION="v0.42.42" && SQL_BACKUP_PATH="backup/sql-peertube_prod-2018-01-19T10:18+01:00.bak" && \
395
    cd /var/www/peertube && sudo -u peertube unlink ./peertube-latest && \
396
    sudo -u peertube ln -s "versions/peertube-$OLD_VERSION" peertube-latest && \
397
    sudo -u postgres pg_restore -c -C -d postgres "$SQL_BACKUP_PATH" && \
398
399
    sudo systemctl restart peertube
```