Add warning in production guide
[oweals/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 small device behind a low bandwidth connection because it could slow down the fediverse.**
9
10 ### Dependencies
11
12 Follow the steps of the [dependencies guide](dependencies.md).
13
14 ### PeerTube user
15
16 Create a `peertube` user with `/var/www/peertube` home:
17
18 ```
19 $ sudo useradd -m -d /var/www/peertube -s /bin/bash -p peertube peertube
20 ```
21
22 Set its password:
23 ```
24 $ sudo passwd peertube
25 ```
26
27 ### Database
28
29 Create the production database and a peertube user inside PostgreSQL:
30
31 ```
32 $ sudo -u postgres createuser -P peertube
33 $ sudo -u postgres createdb -O peertube peertube_prod
34 ```
35
36 ### Prepare PeerTube directory
37
38 Fetch the latest tagged version of Peertube
39 ```
40 $ 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"
41 ```
42
43 Open the peertube directory, create a few required directories
44 ```
45 $ cd /var/www/peertube && sudo -u peertube mkdir config storage versions && cd versions
46 ```
47
48 Download the latest version of the Peertube client, unzip it and remove the zip
49 ```
50 $ sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip"
51 $ sudo -u peertube unzip peertube-${VERSION}.zip && sudo -u peertube rm peertube-${VERSION}.zip
52 ```
53
54 Install Peertube
55 ```
56 $ cd ../ && sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
57 $ cd ./peertube-latest && sudo -H -u peertube yarn install --production --pure-lockfile
58 ```
59
60 ### PeerTube configuration
61
62 Copy example configuration:
63
64 ```
65 $ cd /var/www/peertube && sudo -u peertube cp peertube-latest/config/production.yaml.example config/production.yaml
66 ```
67
68 Then edit the `config/production.yaml` file according to your webserver
69 configuration.
70
71 ### Webserver
72
73 Copy the nginx configuration template:
74
75 ```
76 $ sudo cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube
77 ```
78
79 Then modify the webserver configuration file. Please pay attention to the `alias` keys of the static locations.
80 It should correspond to the paths of your storage directories (set in the configuration file inside the `storage` key).
81
82 ```
83 $ sudo vim /etc/nginx/sites-available/peertube
84 ```
85
86 If you want to set https with Let's Encrypt please follow the steps of [this guide](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04).
87
88 An example of the nginx configuration could be:
89
90 ```
91 server {
92   listen 80;
93   listen [::]:80;
94   server_name peertube.example.com;
95
96   access_log /var/log/nginx/peertube.example.com.access.log;
97   error_log /var/log/nginx/peertube.example.com.error.log;
98
99   rewrite ^ https://$server_name$request_uri? permanent;
100 }
101
102 server {
103   listen 443 ssl http2;
104   listen [::]:443 ssl http2;
105   server_name peertube.example.com;
106
107   # For example with Let's Encrypt
108   ssl_certificate      /etc/letsencrypt/live/peertube.example.com/fullchain.pem;
109   ssl_certificate_key  /etc/letsencrypt/live/peertube.example.com/privkey.pem;
110   ssl_trusted_certificate /etc/letsencrypt/live/peertube.example.com/chain.pem;
111
112   access_log /var/log/nginx/peertube.example.com.access.log;
113   error_log /var/log/nginx/peertube.example.com.error.log;
114
115   location ^~ '/.well-known/acme-challenge' {
116     default_type "text/plain";
117     root /var/www/certbot;
118   }
119
120   location ~ ^/client/(.*\.(js|css|woff2|otf|ttf|woff|eot))$ {
121     add_header Cache-Control "public, max-age=31536000, immutable";
122
123     alias /var/www/peertube/peertube-latest/client/dist/$1;
124   }
125
126   location ~ ^/static/(thumbnails|avatars)/(.*)$ {
127     add_header Cache-Control "public, max-age=31536000, immutable";
128
129     alias /var/www/peertube/storage/$1/$2;
130   }
131
132   location / {
133     proxy_pass http://localhost:9000;
134     proxy_set_header X-Real-IP $remote_addr;
135     proxy_set_header Host $host;
136     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
137
138     # For the video upload
139     client_max_body_size 8G;
140     proxy_connect_timeout       600;
141     proxy_send_timeout          600;
142     proxy_read_timeout          600;
143     send_timeout                600;
144   }
145
146   # Bypass PeerTube webseed route for better performances
147   location /static/webseed {
148     if ($request_method = 'OPTIONS') {
149       add_header 'Access-Control-Allow-Origin' '*';
150       add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
151       add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
152       add_header 'Access-Control-Max-Age' 1728000;
153       add_header 'Content-Type' 'text/plain charset=UTF-8';
154       add_header 'Content-Length' 0;
155       return 204;
156     }
157
158     if ($request_method = 'GET') {
159       add_header 'Access-Control-Allow-Origin' '*';
160       add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
161       add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
162
163       # Don't spam access log file with byte range requests
164       access_log off;
165     }
166
167     alias /var/www/peertube/storage/videos;
168   }
169
170   # Websocket tracker
171   location /tracker/socket {
172     # Peers send a message to the tracker every 15 minutes
173     # Don't close the websocket before this time
174     proxy_read_timeout 1200s;
175     proxy_set_header Upgrade $http_upgrade;
176     proxy_set_header Connection "upgrade";
177     proxy_http_version 1.1;
178     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
179     proxy_set_header Host $host;
180     proxy_pass http://localhost:9000;
181   }
182 }
183 ```
184
185
186 Activate the configuration file:
187
188 ```
189 $ sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/peertube
190 $ sudo systemctl reload nginx
191 ```
192
193 ### Systemd
194
195 Copy the nginx configuration template:
196
197 ```
198 $ sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
199 ```
200
201 Update the service file:
202
203 ```
204 $ sudo vim /etc/systemd/system/peertube.service
205 ```
206
207 It should look like this:
208
209 ```
210 [Unit]
211 Description=PeerTube daemon
212 After=network.target
213
214 [Service]
215 Type=simple
216 Environment=NODE_ENV=production
217 Environment=NODE_CONFIG_DIR=/var/www/peertube/config
218 User=peertube
219 Group=peertube
220 ExecStart=/usr/bin/npm start
221 WorkingDirectory=/var/www/peertube/peertube-latest
222 StandardOutput=syslog
223 StandardError=syslog
224 SyslogIdentifier=peertube
225 Restart=always
226
227 [Install]
228 WantedBy=multi-user.target
229 ```
230
231
232 Tell systemd to reload its config:
233
234 ```
235 $ sudo systemctl daemon-reload
236 ```
237
238 If you want to start PeerTube on boot:
239
240 ```
241 $ sudo systemctl enable peertube
242 ```
243
244 ### Run
245
246 ```
247 $ sudo systemctl start peertube
248 $ sudo journalctl -feu peertube
249 ```
250
251 ### Administrator
252
253 The administrator password is automatically generated and can be found in the
254 logs. You can set another password with:
255
256 ```
257 $ cd /var/www/peertube/peertube-latest && NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run reset-password -- -u root
258 ```
259
260 ## Upgrade
261
262 Make a SQL backup
263
264 ```
265 $ SQL_BACKUP_PATH="backup/sql-peertube_prod-$(date -Im).bak" && \
266     cd /var/www/peertube && sudo -u peertube mkdir -p backup && \
267     sudo pg_dump -U peertube -W -h localhost -F c peertube_prod -f "$SQL_BACKUP_PATH"
268 ```
269
270 Fetch the latest tagged version of Peertube:
271
272 ```
273 $ 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"
274 ```
275
276 Download the new version and unzip it:
277
278 ```
279 $ cd /var/www/peertube/versions && \
280     sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" && \
281     sudo -u peertube unzip -o peertube-${VERSION}.zip && \
282     sudo -u peertube rm peertube-${VERSION}.zip
283 ```
284
285 Install node dependencies:
286
287 ```
288 $ cd /var/www/peertube/versions/peertube-${VERSION} && \
289     sudo -u peertube yarn install --production --pure-lockfile
290 ```
291
292 Copy new configuration defaults values and update your configuration file:
293
294 ```
295 $ sudo -u peertube cp /var/www/peertube/versions/peertube-${VERSION}/config/default.yaml /var/www/peertube/config/default.yaml
296 $ diff /var/www/peertube/versions/peertube-${VERSION}/config//production.yaml.example /var/www/peertube/config/production.yaml
297 ```
298
299 Change the link to point to the latest version:
300
301 ```
302 $ cd /var/www/peertube && \
303     sudo rm ./peertube-latest && \
304     sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
305 ```
306
307
308 Restart PeerTube:
309 ```
310 $ sudo systemctl restart peertube
311 ```
312
313 ### Things went wrong? 
314
315 Change `peertube-latest` destination to the previous version and restore your SQL backup:
316
317 ```
318 $ OLD_VERSION="v0.42.42" && SQL_BACKUP_PATH="backup/sql-peertube_prod-2018-01-19T10:18+01:00.bak" && \
319     cd /var/www/peertube && rm ./peertube-latest && \
320     sudo -u peertube ln -s "versions/peertube-$OLD_VERSION" peertube-latest && \
321     pg_restore -U peertube -W -h localhost -c -d peertube_prod "$SQL_BACKUP_PATH"
322     sudo systemctl restart peertube
323 ```