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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. set -euo 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. # Baseline utils
  16. echo -e "Installing baseline utils\n"
  17. apt update
  18. apt upgrade -y
  19. apt install -y ca-certificates curl pwgen nginx python3-certbot-nginx ufw coturn
  20. # Open only needed ports
  21. echo -e "Opening ports and enabling ufw\n"
  22. # SSH
  23. ufw allow 22/tcp
  24. # Nginx (HTTP/HTTPS)
  25. ufw allow 80/tcp
  26. ufw allow 443/tcp
  27. ufw allow 8448/tcp
  28. # Coturn Ports
  29. ufw allow 3478/udp
  30. ufw allow 5443/udp
  31. ufw allow 49152:65535/udp
  32. # Enable firewall
  33. ufw --force enable
  34. # Configure Coturn TURN server
  35. echo -e "Install and configure coturn server\n"
  36. echo "TURNSERVER_ENABLED=1" > /etc/default/coturn
  37. cp config/turnserver.conf /etc/
  38. TURN_PWD=$(pwgen -s 28 -1)
  39. TURN_STATIC_SECRET=$(pwgen -s 64 1)
  40. EXTERNAL_IP=$(curl -s checkip.amazonaws.com)
  41. sed -i "s|DOMAIN|${DOMAIN}|g" /etc/turnserver.conf
  42. sed -i "s|TURN_PWD|${TURN_PWD}|g" /etc/turnserver.conf
  43. sed -i "s|EXTERNAL_IP|${EXTERNAL_IP}|g" /etc/turnserver.conf
  44. sed -i "s|STATIC_SECRET|${TURN_STATIC_SECRET}|g" /etc/turnserver.conf
  45. # Custom coturn SystemD service file to allow coturn access to Letsencrypt SSL certs
  46. cp "${BASE_DIR}/coturn.service" /lib/systemd/system/coturn.service
  47. systemctl daemon-reload
  48. # Add Docker's official GPG key
  49. echo -e "Install docker\n"
  50. install -m 0755 -d /etc/apt/keyrings
  51. curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  52. chmod a+r /etc/apt/keyrings/docker.asc
  53. # Add the repository to APT sources
  54. echo \
  55. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  56. $(. /etc/os-release && echo "${VERSION_CODENAME}") stable" | \
  57. tee /etc/apt/sources.list.d/docker.list > /dev/null
  58. apt update
  59. # Install docker
  60. apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  61. # Create docker network `matrix_server`
  62. echo -e "Create docker network\n"
  63. docker network create --driver=bridge --subnet=10.10.10.0/24 --gateway=10.10.10.1 matrix_server
  64. # Generate synapse file
  65. echo -e "Generating synapse file..\n"
  66. docker compose run --rm -e SYNAPSE_SERVER_NAME=${DOMAIN} -e SYNAPSE_REPORT_STATS=yes synapse generate
  67. # Replace DB config in Synapse's homeserver.yaml
  68. echo -e "Configuring homeserver.yaml\n"
  69. # Granting all read permissions to cert files
  70. chmod 444 ${BASE_DIR}/config/synapse/${DOMAIN}.*
  71. # Config homeserver.yaml
  72. sed -i '$ d' "${BASE_DIR}/config/synapse/homeserver.yaml"
  73. sed -e '22r homeserver.yaml.db' -e '22,25d' "${BASE_DIR}/config/synapse/homeserver.yaml" > /tmp/homeserver.yaml
  74. cp /tmp/homeserver.yaml "${BASE_DIR}/config/synapse/homeserver.yaml"
  75. # Configure User Directory and TURN
  76. cat <<EOF >> "${BASE_DIR}/config/synapse/homeserver.yaml"
  77. user_directory:
  78. enabled: true
  79. search_all_users: true
  80. prefer_local_users: true
  81. show_locked_users: true
  82. turn_allow_guests: False
  83. turn_user_lifetime: 86400000
  84. turn_shared_secret: "${TURN_STATIC_SECRET}"
  85. turn_uris: [ "turn:${DOMAIN}?transport=udp" ]
  86. EOF
  87. # Randomly pick a DB password
  88. PG_PASS=$(pwgen -s 28 -1)
  89. # Replace Password in homeserver.yaml
  90. sed -i "s|PG_PASS|${PG_PASS}|g" "${BASE_DIR}/config/synapse/homeserver.yaml"
  91. # Replace PG_PASS Password and DOMAIN in docker compose YAML
  92. sed -i "s|DOMAIN|${DOMAIN}|g" "${BASE_DIR}/docker-compose.yaml"
  93. sed -i "s|PG_PASS|${PG_PASS}|g" "${BASE_DIR}/docker-compose.yaml"
  94. # Replace Sliding Sync key
  95. SLIDING_SYNC_KEY=$(openssl rand -hex 32)
  96. sed -i "s|SLIDING_SYNC_KEY|${SLIDING_SYNC_KEY}|g" "${BASE_DIR}/docker-compose.yaml"
  97. # Replace domain in element config
  98. sed -i "s|DOMAIN|${DOMAIN}|g" "${BASE_DIR}/config/element/element-config.json"
  99. # Copy SystemD file and start the service
  100. echo -e "Setting up SystemD service\n"
  101. cp "${BASE_DIR}/matrix.service" /etc/systemd/system/
  102. systemctl daemon-reload
  103. systemctl enable --now matrix.service
  104. # Configure Nginx
  105. echo -e "Configuring nginx\n"
  106. cat <<EOF > /etc/nginx/sites-enabled/default
  107. server {
  108. listen 80;
  109. listen 8448;
  110. server_name ${DOMAIN};
  111. location /.well-known/matrix/client {
  112. default_type application/json;
  113. add_header Access-Control-Allow-Origin *;
  114. return 200 '{"m.homeserver": {"base_url": "https://${DOMAIN}"}, "org.matrix.msc3575.proxy": {"url": "https://${DOMAIN}"}}';
  115. }
  116. # Admin panel
  117. location /admin/ {
  118. proxy_pass http://10.10.10.6/;
  119. proxy_set_header X-Forwarded-For \$remote_addr;
  120. proxy_set_header X-Forwarded-Proto \$scheme;
  121. proxy_set_header Host \$host;
  122. proxy_http_version 1.1;
  123. }
  124. # Sydent identity server
  125. location ~ ^(/_matrix/identity) {
  126. proxy_pass http://10.10.10.5:8090;
  127. proxy_set_header X-Forwarded-For \$remote_addr;
  128. proxy_set_header X-Forwarded-Proto \$scheme;
  129. proxy_set_header Host \$host;
  130. proxy_http_version 1.1;
  131. }
  132. # Sliding Sync
  133. location ~ ^/(client/|_matrix/client/unstable/org.matrix.msc3575/sync) {
  134. proxy_pass http://10.10.10.7:8008;
  135. proxy_set_header X-Forwarded-For \$remote_addr;
  136. proxy_set_header X-Forwarded-Proto \$scheme;
  137. proxy_set_header Host \$host;
  138. }
  139. # Synapse Backend
  140. location ~ ^(\/_matrix|\/_synapse\/(client|admin)) {
  141. # Synapse Container Network IP
  142. proxy_pass http://10.10.10.4:8008;
  143. proxy_set_header X-Forwarded-For \$remote_addr;
  144. proxy_set_header X-Forwarded-Proto \$scheme;
  145. proxy_set_header Host \$host;
  146. client_max_body_size 50M;
  147. proxy_http_version 1.1;
  148. }
  149. # Element Frontend
  150. location / {
  151. # Element chat Container Network IP
  152. proxy_pass http://10.10.10.3;
  153. proxy_set_header X-Forwarded-For \$remote_addr;
  154. proxy_set_header X-Forwarded-Proto \$scheme;
  155. proxy_set_header Host \$host;
  156. # Nginx by default only allows file uploads up to 1M in size
  157. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  158. client_max_body_size 50M;
  159. # Synapse responses may be chunked, which is an HTTP/1.1 feature.
  160. proxy_http_version 1.1;
  161. }
  162. }
  163. EOF
  164. systemctl restart nginx
  165. systemctl enable --now nginx
  166. echo -e "Generate SSL cert\n"
  167. certbot --nginx -d ${DOMAIN} --agree-tos --register-unsafely-without-email
  168. systemctl enable --now coturn
  169. # Add certbot SSL cert renewal to crontab
  170. crontab -l | { cat; echo '43 6 * * * certbot renew --post-hook "systemctl reload nginx"'; } | crontab -
  171. # Finally, start services
  172. # Ensuring the DB dir is clean before bootstrapping
  173. rm -rf ${BASE_DIR}/db/*
  174. systemctl enable --now matrix.service