Matrix server automated install
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

install.sh 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. set -eo pipefail
  3. DOMAIN=$1
  4. if [ -z ${DOMAIN} ]; then
  5. echo "Script usage: ./install.sh <DOMAIN>"
  6. return 1
  7. fi
  8. BASE_DIR=/opt/matrix
  9. # Create directory and copy configs + docker-compose YAML
  10. mkdir -p ${BASE_DIR}/db
  11. cp -R . ${BASE_DIR}
  12. cd ${BASE_DIR}
  13. # Disable "Pending Kernel upgrade" banner
  14. sed -i "s|#\$nrconf{kernelhints} = -1;|\$nrconf{kernelhints} = -1;|g" /etc/needrestart/needrestart.conf
  15. # Disable "Daemon Using Outdated Libraries" banner
  16. sed -i "s|#\$nrconf{restart} = 'i';|\$nrconf{restart} = 'a';|g" /etc/needrestart/needrestart.conf
  17. # Baseline utils
  18. echo -e "Installing baseline utils\n"
  19. apt update
  20. apt upgrade -y
  21. apt install -y ca-certificates curl pwgen nginx python3-certbot-nginx ufw coturn
  22. # Open only needed ports
  23. echo -e "Opening ports and enabling ufw\n"
  24. # SSH
  25. ufw allow 22/tcp
  26. # Nginx (HTTP/HTTPS)
  27. ufw allow 80/tcp
  28. ufw allow 443/tcp
  29. ufw allow 8448/tcp
  30. # Coturn Ports
  31. ufw allow 3478/udp
  32. ufw allow 5443/udp
  33. ufw allow 49152:65535/udp
  34. # Enable firewall
  35. ufw --force enable
  36. # Configure Coturn TURN server
  37. echo -e "Install and configure coturn server\n"
  38. echo "TURNSERVER_ENABLED=1" > /etc/default/coturn
  39. cp config/turnserver.conf /etc/
  40. TURN_PWD=$(pwgen -s 28 -1)
  41. TURN_STATIC_SECRET=$(pwgen -s 64 1)
  42. EXTERNAL_IP=$(curl -s checkip.amazonaws.com)
  43. sed -i "s|DOMAIN|${DOMAIN}|g" /etc/turnserver.conf
  44. sed -i "s|TURN_PWD|${TURN_PWD}|g" /etc/turnserver.conf
  45. sed -i "s|EXTERNAL_IP|${EXTERNAL_IP}|g" /etc/turnserver.conf
  46. sed -i "s|STATIC_SECRET|${TURN_STATIC_SECRET}|g" /etc/turnserver.conf
  47. # Custom coturn SystemD service file to allow coturn access to Letsencrypt SSL certs
  48. cp "${BASE_DIR}/coturn.service" /lib/systemd/system/coturn.service
  49. systemctl daemon-reload
  50. # Add Docker's official GPG key
  51. echo -e "Install docker\n"
  52. install -m 0755 -d /etc/apt/keyrings
  53. curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  54. chmod a+r /etc/apt/keyrings/docker.asc
  55. # Add the repository to APT sources
  56. echo \
  57. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  58. $(. /etc/os-release && echo "${VERSION_CODENAME}") stable" | \
  59. tee /etc/apt/sources.list.d/docker.list > /dev/null
  60. apt update
  61. # Install docker
  62. apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  63. # Create docker network `matrix_server`
  64. echo -e "Create docker network\n"
  65. docker network create --driver=bridge --subnet=10.10.10.0/24 --gateway=10.10.10.1 matrix_server
  66. docker network create --driver=bridge --subnet=10.100.0.0/24 --gateway=10.100.0.1 --internal matrix_db
  67. # Randomly pick a DB password
  68. PG_PASS=$(pwgen -s 28 -1)
  69. # Replace PG_PASS Password and DOMAIN in docker compose YAML
  70. sed -i "s|DOMAIN|${DOMAIN}|g" "${BASE_DIR}/docker-compose.yaml"
  71. sed -i "s|PG_PASS|${PG_PASS}|g" "${BASE_DIR}/docker-compose.yaml"
  72. # Generate synapse file
  73. echo -e "Generating synapse file..\n"
  74. docker compose run --rm synapse_homeserver --generate-config -H ${DOMAIN} -c /data/homeserver.yaml --report-stats=yes
  75. # Replace DB config in Synapse's homeserver.yaml
  76. echo -e "Configuring homeserver.yaml\n"
  77. # Granting all read permissions to cert files
  78. chmod 444 ${BASE_DIR}/config/synapse/${DOMAIN}.*
  79. # Config homeserver.yaml
  80. sed -i '$ d' "${BASE_DIR}/config/synapse/homeserver.yaml"
  81. sed -e '22r homeserver.yaml.db' -e '22,25d' "${BASE_DIR}/config/synapse/homeserver.yaml" > /tmp/homeserver.yaml
  82. cp /tmp/homeserver.yaml "${BASE_DIR}/config/synapse/homeserver.yaml"
  83. # Configure User Directory and TURN
  84. cat <<EOF >> "${BASE_DIR}/config/synapse/homeserver.yaml"
  85. user_directory:
  86. enabled: true
  87. search_all_users: true
  88. prefer_local_users: true
  89. show_locked_users: true
  90. turn_allow_guests: false
  91. turn_user_lifetime: 86400000
  92. turn_shared_secret: "${TURN_STATIC_SECRET}"
  93. turn_uris: [ "turn:${DOMAIN}?transport=udp" ]
  94. suppress_key_server_warning: true
  95. retention:
  96. enabled: true
  97. default_policy:
  98. min_lifetime: 1s
  99. max_lifetime: 1s
  100. allowed_lifetime_min: 1s
  101. allowed_lifetime_max: 1s
  102. EOF
  103. # Replace Password in homeserver.yaml
  104. sed -i "s|PG_PASS|${PG_PASS}|g" "${BASE_DIR}/config/synapse/homeserver.yaml"
  105. # Replace Sliding Sync key
  106. SLIDING_SYNC_KEY=$(openssl rand -hex 32)
  107. sed -i "s|SLIDING_SYNC_KEY|${SLIDING_SYNC_KEY}|g" "${BASE_DIR}/docker-compose.yaml"
  108. # Replace domain in element config
  109. sed -i "s|DOMAIN|${DOMAIN}|g" "${BASE_DIR}/config/element/element-config.json"
  110. # Copy SystemD file and start the service
  111. echo -e "Setting up SystemD service\n"
  112. cp "${BASE_DIR}/matrix.service" /etc/systemd/system/
  113. systemctl daemon-reload
  114. systemctl enable --now matrix.service
  115. # Configure Nginx
  116. echo -e "Configuring nginx\n"
  117. cat <<EOF > /etc/nginx/sites-enabled/default
  118. server {
  119. listen 80;
  120. server_name ${DOMAIN};
  121. # Hardening
  122. add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
  123. add_header Content-Security-Policy "default-src 'self' ${DOMAIN} http: https: data: blob: 'unsafe-inline' 'unsafe-eval'" always;
  124. add_header X-XSS-Protection "1; mode=block";
  125. add_header X-Content-Type-Options nosniff;
  126. add_header X-Frame-Options "SAMEORIGIN";
  127. location /.well-known/matrix/client {
  128. default_type application/json;
  129. add_header Access-Control-Allow-Origin *;
  130. return 200 '{"m.homeserver": {"base_url": "https://${DOMAIN}"}, "org.matrix.msc3575.proxy": {"url": "https://${DOMAIN}"}}';
  131. }
  132. # Admin panel
  133. location /admin/ {
  134. proxy_pass http://10.10.10.6/;
  135. proxy_set_header X-Forwarded-For \$remote_addr;
  136. proxy_set_header X-Forwarded-Proto \$scheme;
  137. proxy_set_header Host \$host;
  138. proxy_http_version 1.1;
  139. }
  140. # Sydent identity server
  141. location ~ ^(/_matrix/identity) {
  142. proxy_pass http://10.10.10.5:8090;
  143. proxy_set_header X-Forwarded-For \$remote_addr;
  144. proxy_set_header X-Forwarded-Proto \$scheme;
  145. proxy_set_header Host \$host;
  146. proxy_http_version 1.1;
  147. }
  148. # Sliding Sync
  149. location ~ ^/(client/|_matrix/client/unstable/org.matrix.msc3575/sync) {
  150. proxy_pass http://10.10.10.7:8008;
  151. proxy_set_header X-Forwarded-For \$remote_addr;
  152. proxy_set_header X-Forwarded-Proto \$scheme;
  153. proxy_set_header Host \$host;
  154. }
  155. # Synapse Backend
  156. location ~ ^(\/_matrix|\/_synapse\/(client|admin)) {
  157. # Synapse Container Network IP
  158. proxy_pass http://10.10.10.4:8008;
  159. proxy_set_header X-Forwarded-For \$remote_addr;
  160. proxy_set_header X-Forwarded-Proto \$scheme;
  161. proxy_set_header Host \$host;
  162. client_max_body_size 50M;
  163. proxy_http_version 1.1;
  164. }
  165. # Hydrogen web
  166. location ~ ^/(hydrogen|assets) {
  167. rewrite /hydrogen/(.*) /\$1 break;
  168. proxy_pass http://10.10.10.8:8080;
  169. proxy_set_header X-Forwarded-For \$remote_addr;
  170. proxy_set_header X-Forwarded-Proto \$scheme;
  171. proxy_set_header Host \$host;
  172. client_max_body_size 50M;
  173. proxy_http_version 1.1;
  174. }
  175. # Element Frontend
  176. location / {
  177. # Element chat Container Network IP
  178. proxy_pass http://10.10.10.3;
  179. proxy_set_header X-Forwarded-For \$remote_addr;
  180. proxy_set_header X-Forwarded-Proto \$scheme;
  181. proxy_set_header Host \$host;
  182. # Nginx by default only allows file uploads up to 1M in size
  183. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  184. client_max_body_size 50M;
  185. # Synapse responses may be chunked, which is an HTTP/1.1 feature.
  186. proxy_http_version 1.1;
  187. }
  188. }
  189. EOF
  190. systemctl restart nginx
  191. systemctl enable --now nginx
  192. echo -e "Generate SSL cert\n"
  193. certbot --nginx -d ${DOMAIN} --agree-tos --register-unsafely-without-email
  194. # Add custom 8448 SSL port for Matrix Federation
  195. sed -i '/listen\ 443\ ssl/a\\tlisten\ 8448\ ssl\;' /etc/nginx/sites-enabled/default
  196. nginx -s reload
  197. # Enable coturn
  198. systemctl enable --now coturn
  199. # Finally, start services
  200. # Ensuring the DB dir is clean before bootstrapping
  201. systemctl enable --now matrix.service
  202. # Add certbot SSL cert renewal to crontab
  203. crontab -l | { cat; echo '43 6 * * * certbot renew --post-hook "systemctl reload nginx"'; } | crontab -