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