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
27 export DEBIAN_FRONTEND=noninteractive
28 add-apt-repository universe
30 apt install -y git unzip apache2 php7.4 curl php7.4-fpm php7.4-curl php7.4-mbstring php7.4-ldap \
31 php7.4-tidy php7.4-xml php7.4-zip php7.4-gd php7.4-mysql mysql-server-8.0 libapache2-mod-php7.4
34 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
35 mysql -u root --execute="CREATE DATABASE bookstack;"
36 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PASS';"
37 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
41 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
42 BOOKSTACK_DIR="/var/www/bookstack"
43 cd $BOOKSTACK_DIR || exit
46 EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
47 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
48 ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
50 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
52 >&2 echo 'ERROR: Invalid composer installer checksum'
57 # Move composer to global installation
58 mv composer.phar /usr/local/bin/composer
60 # Install BookStack composer dependencies
61 export COMPOSER_ALLOW_SUPERUSER=1
62 php /usr/local/bin/composer install --no-dev --no-plugins
64 # Copy and update BookStack environment variables
66 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
67 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
68 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
69 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
71 # Generate the application key
72 php artisan key:generate --no-interaction --force
73 # Migrate the databases
74 php artisan migrate --no-interaction --force
76 # Set file and folder permissions
77 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
83 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
87 ServerAdmin webmaster@localhost
88 DocumentRoot /var/www/bookstack/public/
90 <Directory /var/www/bookstack/public/>
91 Options Indexes FollowSymLinks
94 <IfModule mod_rewrite.c>
95 <IfModule mod_negotiation.c>
96 Options -MultiViews -Indexes
101 # Handle Authorization Header
102 RewriteCond %{HTTP:Authorization} .
103 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
105 # Redirect Trailing Slashes If Not A Folder...
106 RewriteCond %{REQUEST_FILENAME} !-d
107 RewriteCond %{REQUEST_URI} (.+)/$
108 RewriteRule ^ %1 [L,R=301]
110 # Handle Front Controller...
111 RewriteCond %{REQUEST_FILENAME} !-d
112 RewriteCond %{REQUEST_FILENAME} !-f
113 RewriteRule ^ index.php [L]
117 ErrorLog \${APACHE_LOG_DIR}/error.log
118 CustomLog \${APACHE_LOG_DIR}/access.log combined
123 a2dissite 000-default.conf
124 a2ensite bookstack.conf
126 # Restart apache to load new config
127 systemctl restart apache2
130 echo "Setup Finished, Your BookStack instance should now be installed."
132 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
134 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"