2 # This script will install a new BookStack instance on a fresh Ubuntu 18.04 server.
3 # This script is experimental and does not ensure any security.
6 printf "Enter the domain you want to host BookStack and press [ENTER]\nExamples: my-site.com or docs.my-site.com\n"
9 CURRENT_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
11 export DEBIAN_FRONTEND=noninteractive
13 apt install -y git apache2 curl php7.2-fpm php7.2-curl php7.2-mbstring php7.2-ldap \
14 php7.2-tidy php7.2-xml php7.2-zip php7.2-gd php7.2-mysql mysql-server-5.7 libapache2-mod-php7.2
17 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
18 mysql -u root --execute="CREATE DATABASE bookstack;"
19 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED BY '$DB_PASS';"
20 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
24 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
25 BOOKSTACK_DIR="/var/www/bookstack"
29 EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
30 curl -s https://getcomposer.org/installer > composer-setup.php
31 ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")
33 if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
35 php composer-setup.php --quiet
39 >&2 echo 'ERROR: Invalid composer installer signature'
44 # Install BookStack composer dependancies
45 php composer.phar install
47 # Copy and update BookStack environment variables
49 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
50 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
51 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
53 # Generate the application key
54 php artisan key:generate --no-interaction --force
55 # Migrate the databases
56 php artisan migrate --no-interaction --force
58 # Set file and folder permissions
59 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
65 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
69 ServerAdmin webmaster@localhost
70 DocumentRoot /var/www/bookstack/public/
72 <Directory /var/www/bookstack/public/>
73 Options Indexes FollowSymLinks
76 <IfModule mod_rewrite.c>
77 <IfModule mod_negotiation.c>
78 Options -MultiViews -Indexes
83 # Handle Authorization Header
84 RewriteCond %{HTTP:Authorization} .
85 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
87 # Redirect Trailing Slashes If Not A Folder...
88 RewriteCond %{REQUEST_FILENAME} !-d
89 RewriteCond %{REQUEST_URI} (.+)/$
90 RewriteRule ^ %1 [L,R=301]
92 # Handle Front Controller...
93 RewriteCond %{REQUEST_FILENAME} !-d
94 RewriteCond %{REQUEST_FILENAME} !-f
95 RewriteRule ^ index.php [L]
99 ErrorLog ${APACHE_LOG_DIR}/error.log
100 CustomLog ${APACHE_LOG_DIR}/access.log combined
105 a2dissite 000-default.conf
106 a2ensite bookstack.conf
108 # Restart apache to load new config
109 systemctl restart apache2
112 echo "Setup Finished, Your BookStack instance should now be installed."
114 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
116 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"