2 # This script will install a new BookStack instance on a fresh Ubuntu 20.04 server.
3 # This script is experimental and does not ensure any security.
5 # Fetch domain to use from first provided parameter,
6 # Otherwise request the user to input their domain
11 printf "Enter the domain you want to host BookStack and press [ENTER]\nExamples: my-site.com or docs.my-site.com\n"
15 # Ensure a domain was provided otherwise display
16 # an error message and stop the script
19 >&2 echo 'ERROR: A domain must be provided to run this script'
23 # Get the current machine IP address
24 CURRENT_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
26 # Install core system packages
28 apt install -y software-properties-common
29 export DEBIAN_FRONTEND=noninteractive
30 add-apt-repository universe
31 add-apt-repository -yu ppa:ondrej/php
32 apt install -y git unzip apache2 curl php8.2 php8.2-curl php8.2-mbstring php8.2-ldap \
33 php8.2-xml php8.2-zip php8.2-gd php8.2-mysql mysql-server-8.0 libapache2-mod-php8.2
36 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
37 mysql -u root --execute="CREATE DATABASE bookstack;"
38 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PASS';"
39 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
43 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
44 BOOKSTACK_DIR="/var/www/bookstack"
45 cd $BOOKSTACK_DIR || exit
48 EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
49 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
50 ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
52 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
54 >&2 echo 'ERROR: Invalid composer installer checksum'
59 php composer-setup.php --quiet
62 # Move composer to global installation
63 mv composer.phar /usr/local/bin/composer
65 # Install BookStack composer dependencies
66 export COMPOSER_ALLOW_SUPERUSER=1
67 php /usr/local/bin/composer install --no-dev --no-plugins
69 # Copy and update BookStack environment variables
71 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
72 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
73 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
74 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
76 # Generate the application key
77 php artisan key:generate --no-interaction --force
78 # Migrate the databases
79 php artisan migrate --no-interaction --force
81 # Set file and folder permissions
82 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
88 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
92 ServerAdmin webmaster@localhost
93 DocumentRoot /var/www/bookstack/public/
95 <Directory /var/www/bookstack/public/>
96 Options Indexes FollowSymLinks
99 <IfModule mod_rewrite.c>
100 <IfModule mod_negotiation.c>
101 Options -MultiViews -Indexes
106 # Handle Authorization Header
107 RewriteCond %{HTTP:Authorization} .
108 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
110 # Redirect Trailing Slashes If Not A Folder...
111 RewriteCond %{REQUEST_FILENAME} !-d
112 RewriteCond %{REQUEST_URI} (.+)/$
113 RewriteRule ^ %1 [L,R=301]
115 # Handle Front Controller...
116 RewriteCond %{REQUEST_FILENAME} !-d
117 RewriteCond %{REQUEST_FILENAME} !-f
118 RewriteRule ^ index.php [L]
122 ErrorLog \${APACHE_LOG_DIR}/error.log
123 CustomLog \${APACHE_LOG_DIR}/access.log combined
128 a2dissite 000-default.conf
129 a2ensite bookstack.conf
131 # Restart apache to load new config
132 systemctl restart apache2
135 echo "Setup Finished, Your BookStack instance should now be installed."
136 echo "You can login with the email 'admin@admin.com' and password of 'password'"
137 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
139 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"