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