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