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