]> BookStack Code Mirror - devops/blob - scripts/installation-debian-13.sh
Started a debian 13 script
[devops] / scripts / installation-debian-13.sh
1 #!/bin/bash
2
3 echo "THIS SCRIPT IS NOT CONSIDERED OFFICIALLY SUPPORTED!"
4 echo "Only our Ubuntu LTS scripts are considered supported. This is community"
5 echo "maintained and provided for convenience. It may not be up-to-date or may"
6 echo "have unresolved issues. Use with caution."
7 echo ""
8
9 echo "This installs a new BookStack instance on a fresh Debian 13 (Trixie) server."
10 echo "This script does not ensure system security."
11 echo ""
12
13 # Generate a path for a log file to output into for debugging
14 LOGPATH=$(realpath "bookstack_install_$(date +%s).log")
15
16 # Get the current user running the script
17 SCRIPT_USER="${SUDO_USER:-$USER}"
18
19 # Get the current machine IP address
20 CURRENT_IP=$(ip addr | grep 'state UP' -A4 | grep 'inet ' | awk '{print $2}' | cut -f1  -d'/')
21
22 # Generate a password for the database
23 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
24
25 # The directory to install BookStack into
26 BOOKSTACK_DIR="/var/www/bookstack"
27
28 # Get the domain from the arguments (Requested later if not set)
29 DOMAIN=$1
30
31 # Prevent interactive prompts in applications
32 export DEBIAN_FRONTEND=noninteractive
33
34 # Echo out an error message to the command line and exit the program
35 # Also logs the message to the log file
36 function error_out() {
37   echo "ERROR: $1" | tee -a "$LOGPATH" 1>&2
38   exit 1
39 }
40
41 # Echo out an information message to both the command line and log file
42 function info_msg() {
43   echo "$1" | tee -a "$LOGPATH"
44 }
45
46 # Run some checks before installation to help prevent messing up an existing
47 # web-server setup.
48 function run_pre_install_checks() {
49   # Check we're running as root and exit if not
50   if [[ $EUID -gt 0 ]]
51   then
52     error_out "This script must be ran with root/sudo privileges"
53   fi
54
55   # Check if Apache appears to be installed and exit if so
56   if [ -d "/etc/apache2/sites-enabled" ]
57   then
58     error_out "This script is intended for a fresh server install, existing apache config found, aborting install"
59   fi
60
61   # Check if MySQL/MariaDB appears to be installed and exit if so
62   if [ -d "/var/lib/mysql" ]
63   then
64     error_out "This script is intended for a fresh server install, existing MySQL data found, aborting install"
65   fi
66 }
67
68 # Fetch domain to use from first provided parameter,
69 # Otherwise request the user to input their domain
70 function run_prompt_for_domain_if_required() {
71   if [ -z "$DOMAIN" ]
72   then
73     info_msg ""
74     info_msg "Enter the domain (or IP if not using a domain) you want to host BookStack on and press [ENTER]."
75     info_msg "Examples: my-site.com or docs.my-site.com or ${CURRENT_IP}"
76     read -r DOMAIN
77   fi
78
79   # Error out if no domain was provided
80   if [ -z "$DOMAIN" ]
81   then
82     error_out "A domain must be provided to run this script"
83   fi
84 }
85
86 # Install core system packages
87 function run_package_installs() {
88   apt update
89   apt install -y git unzip apache2 curl mariadb-server php8.4 \
90   php8.4-fpm php8.4-curl php8.4-mbstring php8.4-ldap php8.4-xml php8.4-zip php8.4-gd php8.4-mysql
91 }
92
93 # Set up database
94 function run_database_setup() {
95   # Ensure database service has started
96   systemctl start mariadb.service
97   sleep 3
98
99   # Create the required user database, user and permissions in the database
100   mysql -u root --execute="CREATE DATABASE bookstack;"
101   mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PASS';"
102   mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
103 }
104
105 # Download BookStack
106 function run_bookstack_download() {
107   cd /var/www || exit
108   git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
109 }
110
111 # Install composer
112 function run_install_composer() {
113   EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
114   php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
115   ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
116
117   if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
118   then
119       >&2 echo 'ERROR: Invalid composer installer checksum'
120       rm composer-setup.php
121       exit 1
122   fi
123
124   php composer-setup.php --quiet
125   rm composer-setup.php
126
127   # Move composer to global installation
128   mv composer.phar /usr/local/bin/composer
129 }
130
131 # Install BookStack composer dependencies
132 function run_install_bookstack_composer_deps() {
133   cd "$BOOKSTACK_DIR" || exit
134   export COMPOSER_ALLOW_SUPERUSER=1
135   php /usr/local/bin/composer install --no-dev --no-plugins
136 }
137
138 # Copy and update BookStack environment variables
139 function run_update_bookstack_env() {
140   cd "$BOOKSTACK_DIR" || exit
141   cp .env.example .env
142   sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
143   sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
144   sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
145   sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
146   # Generate the application key
147   php artisan key:generate --no-interaction --force
148 }
149
150 # Run the BookStack database migrations for the first time
151 function run_bookstack_database_migrations() {
152   cd "$BOOKSTACK_DIR" || exit
153   php artisan migrate --no-interaction --force
154 }
155
156 # Set file and folder permissions
157 # Sets current user as owner user and www-data as owner group then
158 # provides group write access only to required directories.
159 # Hides the `.env` file so it's not visible to other users on the system.
160 function run_set_application_file_permissions() {
161   cd "$BOOKSTACK_DIR" || exit
162   chown -R "$SCRIPT_USER":www-data ./
163   chmod -R 755 ./
164   chmod -R 775 bootstrap/cache public/uploads storage
165   chmod 740 .env
166
167   # Tell git to ignore permission changes
168   git config core.fileMode false
169 }
170
171 # Setup apache with the needed modules and config
172 function run_configure_apache() {
173   # Enable required apache modules and config
174   a2enmod rewrite proxy_fcgi setenvif
175   a2enconf php8.4-fpm
176
177   # Set-up the required BookStack apache config
178   cat >/etc/apache2/sites-available/bookstack.conf <<EOL
179 <VirtualHost *:80>
180   ServerName ${DOMAIN}
181
182   ServerAdmin webmaster@localhost
183   DocumentRoot /var/www/bookstack/public/
184
185   <Directory /var/www/bookstack/public/>
186       Options -Indexes +FollowSymLinks
187       AllowOverride None
188       Require all granted
189       <IfModule mod_rewrite.c>
190           <IfModule mod_negotiation.c>
191               Options -MultiViews -Indexes
192           </IfModule>
193
194           RewriteEngine On
195
196           # Handle Authorization Header
197           RewriteCond %{HTTP:Authorization} .
198           RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
199
200           # Redirect Trailing Slashes If Not A Folder...
201           RewriteCond %{REQUEST_FILENAME} !-d
202           RewriteCond %{REQUEST_URI} (.+)/$
203           RewriteRule ^ %1 [L,R=301]
204
205           # Handle Front Controller...
206           RewriteCond %{REQUEST_FILENAME} !-d
207           RewriteCond %{REQUEST_FILENAME} !-f
208           RewriteRule ^ index.php [L]
209       </IfModule>
210   </Directory>
211
212   ErrorLog \${APACHE_LOG_DIR}/error.log
213   CustomLog \${APACHE_LOG_DIR}/access.log combined
214
215 </VirtualHost>
216 EOL
217
218   # Disable the default apache site and enable BookStack
219   a2dissite 000-default.conf
220   a2ensite bookstack.conf
221
222   # Restart apache to load new config
223   systemctl restart apache2
224   # Ensure php-fpm service has started
225   systemctl start php8.4-fpm.service
226 }
227
228 info_msg "This script logs full output to $LOGPATH which may help upon issues."
229 sleep 1
230
231 run_pre_install_checks
232 run_prompt_for_domain_if_required
233 info_msg ""
234 info_msg "Installing using the domain or IP \"$DOMAIN\""
235 info_msg ""
236 sleep 1
237
238 info_msg "[1/9] Installing required system packages... (This may take several minutes)"
239 run_package_installs >> "$LOGPATH" 2>&1
240
241 info_msg "[2/9] Preparing MySQL database..."
242 run_database_setup >> "$LOGPATH" 2>&1
243
244 info_msg "[3/9] Downloading BookStack to ${BOOKSTACK_DIR}..."
245 run_bookstack_download >> "$LOGPATH" 2>&1
246
247 info_msg "[4/9] Installing Composer (PHP dependency manager)..."
248 run_install_composer >> "$LOGPATH" 2>&1
249
250 info_msg "[5/9] Installing PHP dependencies using composer..."
251 run_install_bookstack_composer_deps >> "$LOGPATH" 2>&1
252
253 info_msg "[6/9] Creating and populating BookStack .env file..."
254 run_update_bookstack_env >> "$LOGPATH" 2>&1
255
256 info_msg "[7/9] Running initial BookStack database migrations..."
257 run_bookstack_database_migrations >> "$LOGPATH" 2>&1
258
259 info_msg "[8/9] Setting BookStack file & folder permissions..."
260 run_set_application_file_permissions >> "$LOGPATH" 2>&1
261
262 info_msg "[9/9] Configuring apache server..."
263 run_configure_apache >> "$LOGPATH" 2>&1
264
265 info_msg "----------------------------------------------------------------"
266 info_msg "Setup finished, your BookStack instance should now be installed!"
267 info_msg "- Default login email: [email protected]"
268 info_msg "- Default login password: password"
269 info_msg "- Access URL: http://$CURRENT_IP/ or http://$DOMAIN/"
270 info_msg "- BookStack install path: $BOOKSTACK_DIR"
271 info_msg "- Install script log: $LOGPATH"
272 info_msg "---------------------------------------------------------------"