Updated Installing Peertube in a FreeBSD Jail (markdown)
[oweals/peertube.wiki.git] / Installing-Peertube-in-a-FreeBSD-Jail.md
1 # A complementary information
2
3 :information_source: we're going to start with a working jail, with network up and access to pkg archive.
4
5
6 :information_source: if you have a poudriere, __DON'T USE IT__. It's better to use pre-compiled package in this case.
7
8 ## Read and apply the dependencies instructions.
9
10 Please read and apply the instructions provided in [dependencies](https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/dependencies.md) page.
11
12 ## Go to the production page
13
14 The main instructions are available in the [production](https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/production.md) page.
15
16 Most of the instruction MUST be done before we continue with specific instructions:
17
18 - create the peertube user
19 - create the database
20
21 :warning: the command for knowing the latest available version works with `bash`, but not with `csh`, which is the default `root` shell on FreeBSD. We have to use a different method (changes are very small).
22
23 ```
24 set 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"
25 ```
26 Then we use the command to download and extract Peertube as visible in the production page.
27
28 ## Peertube configuration
29
30 Nothing change in this part, please read the documentation.
31
32 ## Webserver
33
34 :warning: this is the most different part.
35
36 The `/usr/local/etc/nginx/sites_available` and `/usr/local/etc/nginx/sites_enabled` does not exist by default, we have to create them:
37
38 ```
39 # mkdir /usr/local/etc/nginx/sites_{available,enabled}
40 ```
41 Then we copy the sample nginx configuration file exactly as explained in the official documentation.
42
43 ### The certificate problem
44
45 We are going to suppose that you want to host several web services, each of them in a jail. It will be very difficult to maintain the *let's encrypt* certificates for each of those jail. We let the main host to deal with the certificate for ALL the jails.
46
47 Please read the `dehydraded` documentation in order to generate your Peertube instance certificate.
48
49 :information_source: I used to use certbot. My configuration is a little bit different from the dehydraded one.
50
51 ## ON THE HOST ###
52
53 We need to create a nginx configuration. I named it `peertube-jail.conf` and put it in the `sites_available` folder..
54
55 :information_source: remember to replace `example.com` by your own FQDN.
56
57 :information_source: remember to replace `w.x.y.z` by your jail IP address.
58
59 ```
60 server {
61
62     # First, as for all webserver, we listen to 80 port
63     listen 80;
64
65     # give our server_name
66     server_name peertube.example.com;
67
68     # create some logfiles
69     access_log /var/log/nginx/peertube_access.log;
70     error_log /var/log/nginx/peertube_error.log;
71
72     # redirect permantly to https
73     rewrite ^ https://$server_name/$request_uri permanent;
74 }
75
76 server{
77
78     # The https part
79     listen 443 ssl http2;
80
81     # The server-name again
82     server_name peertube.example.com;
83
84     # We use the same log files as below
85     access_log /var/log/nginx/peertube_access.log;
86     error_log /var/log/nginx/peertube_error.log;
87
88     # We activate the ssl engine and give it the path to the fullchain certificate
89     # and the private key
90     ssl on;
91     ssl_certificate /usr/local/etc/letsencrypt/live/peertube.example.com/fullchain.pem;
92     ssl_certificate_key /usr/local/etc/letsencrypt/live/peertube.example.com/privkey.pem;
93
94     # The root location (/) will be redirect
95     # We add some header and VERY IMPORTANT, the client_max_body_size
96     # set to 4G (the maximum size peertube video)
97     location / {
98         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
99         proxy_pass http://w.x.y.z/;
100         proxy_set_header Host $host;
101         proxy_set_header X-Real-IP $remote_addr;
102         client_max_body_size 4G;
103     }
104 ```
105
106 We move a part of the jail FROM nginx configuration file TO the host configuration file (line 106 to 117):
107
108 ```
109     # We also let the host to deal with the websocket
110     # and transfer it to the jail on port 9000 (the peertube port)
111
112     location /tracker/socket {
113         # Peers send a message to the tracker every 15 minutes
114         # Don't close the websocket before this time
115         proxy_read_timeout 1200s;
116         proxy_set_header Upgrade $http_upgrade;
117         proxy_set_header Connection "upgrade";
118         proxy_http_version 1.1;
119         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
120         proxy_set_header Host $host;
121         proxy_pass http://w.x.y.z:9000;
122     }
123
124 }
125 ```
126
127 Save the file, make the link to have it in `sites_enabled` folder:
128
129 ```
130 # ln -s /usr/local/etc/nginx/sites_available/peertube-jail.conf /usr/local/etc/nginx/sites_enabled
131 ```
132
133 Check the nginx configuration (nginx do a check when restarting. but I prefer do it before)
134
135 ```
136 # nginx -t
137 nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
138 nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
139 ```
140
141 If it's you can reload nginx configuration:
142
143 ```
144 # nginx -s reload
145 ```
146
147 ## BACK TO THE JAIL ##
148
149
150 On the jails we are going to make a lot of changes in the nginx configuration.
151
152 - remove all the ssl configuration (line 16 to 34):
153
154 ```
155 server {
156   listen 443 ssl http2;
157   listen [::]:443 ssl http2;
158   server_name peertube.example.com;
159
160   # For example with certbot (you need a certificate to run https)
161   ssl_certificate      /etc/letsencrypt/live/peertube.example.com/fullchain.pem;
162   ssl_certificate_key  /etc/letsencrypt/live/peertube.example.com/privkey.pem;
163
164   # Security hardening (as of 11/02/2018)
165   ssl_protocols TLSv1.2; # TLSv1.3, TLSv1.2 if nginx >= 1.13.0
166   ssl_prefer_server_ciphers on;
167   ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
168   # ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0, not compatible with import-videos script
169   ssl_session_timeout  10m;
170   ssl_session_cache shared:SSL:10m;
171   ssl_session_tickets off; # Requires nginx >= 1.5.9
172   ssl_stapling on; # Requires nginx >= 1.3.7
173   ssl_stapling_verify on; # Requires nginx => 1.3.7
174 ```
175
176 - remove the websocket block too (line 106 to 117). Remember, we already moved this part in the host nginx configuration file.
177
178 ```
179   # Websocket tracker
180   location /tracker/socket {
181     # Peers send a message to the tracker every 15 minutes
182     # Don't close the websocket before this time
183     proxy_read_timeout 1200s;
184     proxy_set_header Upgrade $http_upgrade;
185     proxy_set_header Connection "upgrade";
186     proxy_http_version 1.1;
187     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
188     proxy_set_header Host $host;
189     proxy_pass http://localhost:9000;
190   }
191 ```
192
193 Our nginx configuration file is now a little bit smaller and will only listen on port 80. Here is mine:
194
195 ```
196 server {
197   listen 80;
198   server_name peertube.example.com;
199
200   access_log /var/log/nginx/peertube.access.log;
201   error_log /var/log/nginx/peertube.error.log;
202
203   add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
204   add_header X-Content-Type-Options nosniff;
205   add_header X-XSS-Protection "1; mode=block";
206   add_header X-Robots-Tag none;
207
208   location ^~ '/.well-known/acme-challenge' {
209    default_type "text/plain";
210    root /var/www/certbot;
211   }
212
213   location ~ ^/client/(.*\.(js|css|woff2|otf|ttf|woff|eot))$ {
214     add_header Cache-Control "public, max-age=31536000, immutable";
215
216     alias /var/www/peertube/peertube-latest/client/dist/$1;
217   }
218
219   location ~ ^/static/(thumbnails|avatars)/(.*)$ {
220     add_header Cache-Control "public, max-age=31536000, immutable";
221
222     alias /var/www/peertube/storage/$1/$2;
223   }
224
225   location / {
226     proxy_pass http://localhost:9000;
227     proxy_set_header X-Real-IP $remote_addr;
228     proxy_set_header Host $host;
229     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
230
231     # Hard limit, PeerTube does not support videos > 4GB
232     client_max_body_size 4G;
233     proxy_connect_timeout       600;
234     proxy_send_timeout          600;
235     proxy_read_timeout          600;
236     send_timeout                600;
237   }
238
239   # Bypass PeerTube webseed route for better performances
240   location /static/webseed {
241     # Clients usually have 4 simultaneous webseed connections, so the real limit is 3MB/s per client
242     limit_rate 800k;
243
244     if ($request_method = 'OPTIONS') {
245       add_header 'Access-Control-Allow-Origin' '*';
246       add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
247       add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
248       add_header 'Access-Control-Max-Age' 1728000;
249       add_header 'Content-Type' 'text/plain charset=UTF-8';
250       add_header 'Content-Length' 0;
251       return 204;
252     }
253
254     if ($request_method = 'GET') {
255       add_header 'Access-Control-Allow-Origin' '*';
256       add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
257       add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
258
259       # Don't spam access log file with byte range requests
260       access_log off;
261     }
262
263     alias /var/www/peertube/storage/videos;
264   }
265
266   # Websocket tracker
267
268   ## Moved in host nginx config
269 }
270 ```
271
272 ## Last words
273
274 Be sure to save and keep your configuration files, a Peertube update could crush them.
275
276 ## Thanks
277 Thanks to Chocobozzz who created Peertube, to Framasoft for being part of Peertube popularity, to friends who help me to understand some tricky with jail network and to reread actors.
278
279 If you find useful this documentation, please make a donation to [Framasoft](https://soutenir.framasoft.org/en//?f=nav)
280