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.
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 # Get the current machine IP address
16 CURRENT_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
18 # Install core system packages
19 export DEBIAN_FRONTEND=noninteractive
20 add-apt-repository universe
22 apt install -y git apache2 curl php7.2-fpm php7.2-curl php7.2-mbstring php7.2-ldap \
23 php7.2-tidy php7.2-xml php7.2-zip php7.2-gd php7.2-mysql mysql-server-5.7 libapache2-mod-php7.2
26 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
27 mysql -u root --execute="CREATE DATABASE bookstack;"
28 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED BY '$DB_PASS';"
29 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
33 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
34 BOOKSTACK_DIR="/var/www/bookstack"
38 EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
39 curl -s https://getcomposer.org/installer > composer-setup.php
40 ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")
42 if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
44 php composer-setup.php --quiet
48 >&2 echo 'ERROR: Invalid composer installer signature'
53 # Install BookStack composer dependancies
54 php composer.phar install
56 # Copy and update BookStack environment variables
58 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
59 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
60 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
62 # Generate the application key
63 php artisan key:generate --no-interaction --force
64 # Migrate the databases
65 php artisan migrate --no-interaction --force
67 # Set file and folder permissions
68 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
74 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
78 ServerAdmin webmaster@localhost
79 DocumentRoot /var/www/bookstack/public/
81 <Directory /var/www/bookstack/public/>
82 Options Indexes FollowSymLinks
85 <IfModule mod_rewrite.c>
86 <IfModule mod_negotiation.c>
87 Options -MultiViews -Indexes
92 # Handle Authorization Header
93 RewriteCond %{HTTP:Authorization} .
94 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
96 # Redirect Trailing Slashes If Not A Folder...
97 RewriteCond %{REQUEST_FILENAME} !-d
98 RewriteCond %{REQUEST_URI} (.+)/$
99 RewriteRule ^ %1 [L,R=301]
101 # Handle Front Controller...
102 RewriteCond %{REQUEST_FILENAME} !-d
103 RewriteCond %{REQUEST_FILENAME} !-f
104 RewriteRule ^ index.php [L]
108 ErrorLog ${APACHE_LOG_DIR}/error.log
109 CustomLog ${APACHE_LOG_DIR}/access.log combined
114 a2dissite 000-default.conf
115 a2ensite bookstack.conf
117 # Restart apache to load new config
118 systemctl restart apache2
121 echo "Setup Finished, Your BookStack instance should now be installed."
123 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
125 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"