Merge branch 'develop' of github.com:Chocobozzz/PeerTube into develop
[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 (example: a Raspberry PI behind your ADSL link) 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. If you're using CentOS7, do not forget to activate the devtoolset-6 software collection:
55 ```
56 $ scl enable devtool-6 bash
57 ```
58 And after that, follow the step as usual. Do not forget to exit the environment after installing Peertube.
59 ```
60 $ cd ../ && sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
61 $ cd ./peertube-latest && sudo -H -u peertube yarn install --production --pure-lockfile
62 ```
63
64 ### PeerTube configuration
65
66 Copy example configuration:
67
68 ```
69 $ cd /var/www/peertube && sudo -u peertube cp peertube-latest/config/production.yaml.example config/production.yaml
70 ```
71
72 Then edit the `config/production.yaml` file according to your webserver
73 configuration.
74
75 **PeerTube does not support webserver host change**. Keep in mind your domain name is definitive after your first PeerTube start.
76
77 ### Webserver
78
79 We only provide official configuration files for Nginx.
80
81 Copy the nginx configuration template:
82
83 ```
84 $ sudo cp /var/www/peertube/peertube-latest/support/nginx/peertube /etc/nginx/sites-available/peertube
85 ```
86
87 Then modify the webserver configuration file. Please pay attention to the `alias` keys of the static locations.
88 It should correspond to the paths of your storage directories (set in the configuration file inside the `storage` key).
89
90 ```
91 $ sudo vim /etc/nginx/sites-available/peertube
92 ```
93
94 Activate the configuration file:
95
96 ```
97 $ sudo ln -s /etc/nginx/sites-available/peertube /etc/nginx/sites-enabled/peertube
98 ```
99
100 To generate the certificate for your domain as required to make https work you can use [Let's Encrypt](https://letsencrypt.org/):
101
102 ```
103 $ sudo systemctl stop nginx
104 $ sudo vim /etc/nginx/sites-available/peertube # Comment ssl_certificate and ssl_certificate_key lines
105 $ sudo certbot --authenticator standalone --installer nginx --post-hook "systemctl start nginx"
106 $ sudo vim /etc/nginx/sites-available/peertube # Uncomment ssl_certificate and ssl_certificate_key lines
107 $ sudo systemctl reload nginx
108 ```
109
110 Remember your certificate will expire in 90 days, and thus needs renewal.
111
112 Now you have the certificates you can reload nginx:
113
114 ```
115 $ sudo systemctl reload nginx
116 ```
117
118 ### systemd
119
120 If your OS uses systemd, copy the configuration template:
121
122 ```
123 $ sudo cp /var/www/peertube/peertube-latest/support/systemd/peertube.service /etc/systemd/system/
124 ```
125
126 Update the service file:
127
128 ```
129 $ sudo vim /etc/systemd/system/peertube.service
130 ```
131
132
133 Tell systemd to reload its config:
134
135 ```
136 $ sudo systemctl daemon-reload
137 ```
138
139 If you want to start PeerTube on boot:
140
141 ```
142 $ sudo systemctl enable peertube
143 ```
144
145 Run:
146
147 ```
148 $ sudo systemctl start peertube
149 $ sudo journalctl -feu peertube
150 ```
151
152 ### FreeBSD
153
154 If you're using FreeBSD, copy the startup script and update rc.conf:
155
156 ```
157 $ sudo cp /var/www/peertube/peertube-latest/support/freebsd/peertube /usr/local/etc/rc.d/
158 $ sudo chmod +x /usr/local/etc/rc.d/peertube
159 $ sudo echo peertube_enable="YES" >> /etc/rc.conf
160 ```
161
162 Run:
163
164 ```
165 $ sudo service peertube start
166 ```
167
168 ### Administrator
169
170 The administrator password is automatically generated and can be found in the
171 logs. You can set another password with:
172
173 ```
174 $ cd /var/www/peertube/peertube-latest && NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run reset-password -- -u root
175 ```
176
177 Now you can subscribe to the mailing list for PeerTube administrators: https://framalistes.org/sympa/subscribe/peertube-admin
178
179 ## Upgrade
180
181 #### Auto (minor versions only)
182
183 ```
184 $ cd /var/www/peertube/peertube-latest/scripts && sudo -u peertube ./upgrade.sh
185 $ sudo systemctl restart peertube && sudo journalctl -fu peertube
186 ```
187
188 #### Manually
189
190 Make a SQL backup
191
192 ```
193 $ SQL_BACKUP_PATH="backup/sql-peertube_prod-$(date -Im).bak" && \
194     cd /var/www/peertube && sudo -u peertube mkdir -p backup && \
195     sudo pg_dump -U peertube -W -h localhost -F c peertube_prod -f "$SQL_BACKUP_PATH"
196 ```
197
198 Fetch the latest tagged version of Peertube:
199
200 ```
201 $ 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"
202 ```
203
204 Download the new version and unzip it:
205
206 ```
207 $ cd /var/www/peertube/versions && \
208     sudo -u peertube wget -q "https://github.com/Chocobozzz/PeerTube/releases/download/${VERSION}/peertube-${VERSION}.zip" && \
209     sudo -u peertube unzip -o peertube-${VERSION}.zip && \
210     sudo -u peertube rm peertube-${VERSION}.zip
211 ```
212
213 Install node dependencies:
214
215 ```
216 $ cd /var/www/peertube/versions/peertube-${VERSION} && \
217     sudo -u peertube yarn install --production --pure-lockfile
218 ```
219
220 Copy new configuration defaults values and update your configuration file:
221
222 ```
223 $ sudo -u peertube cp /var/www/peertube/versions/peertube-${VERSION}/config/default.yaml /var/www/peertube/config/default.yaml
224 $ diff /var/www/peertube/versions/peertube-${VERSION}/config/production.yaml.example /var/www/peertube/config/production.yaml
225 ```
226
227 Change the link to point to the latest version:
228
229 ```
230 $ cd /var/www/peertube && \
231     sudo unlink ./peertube-latest && \
232     sudo -u peertube ln -s versions/peertube-${VERSION} ./peertube-latest
233 ```
234
235
236 Restart PeerTube:
237 ```
238 $ sudo systemctl restart peertube
239 ```
240
241 ### Things went wrong? 
242
243 Change `peertube-latest` destination to the previous version and restore your SQL backup:
244
245 ```
246 $ OLD_VERSION="v0.42.42" && SQL_BACKUP_PATH="backup/sql-peertube_prod-2018-01-19T10:18+01:00.bak" && \
247     cd /var/www/peertube && unlink ./peertube-latest && \
248     sudo -u peertube ln -s "versions/peertube-$OLD_VERSION" peertube-latest && \
249     pg_restore -U peertube -W -h localhost -c -d peertube_prod "$SQL_BACKUP_PATH"
250     sudo systemctl restart peertube
251 ```