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 php composer-setup.php --quiet
60 # Move composer to global installation
61 mv composer.phar /usr/local/bin/composer
63 # Install BookStack composer dependencies
64 export COMPOSER_ALLOW_SUPERUSER=1
65 php /usr/local/bin/composer install --no-dev --no-plugins
67 # Copy and update BookStack environment variables
69 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
70 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
71 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
72 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
74 # Generate the application key
75 php artisan key:generate --no-interaction --force
76 # Migrate the databases
77 php artisan migrate --no-interaction --force
79 # Set file and folder permissions
80 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
86 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
90 ServerAdmin webmaster@localhost
91 DocumentRoot /var/www/bookstack/public/
93 <Directory /var/www/bookstack/public/>
94 Options Indexes FollowSymLinks
97 <IfModule mod_rewrite.c>
98 <IfModule mod_negotiation.c>
99 Options -MultiViews -Indexes
104 # Handle Authorization Header
105 RewriteCond %{HTTP:Authorization} .
106 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
108 # Redirect Trailing Slashes If Not A Folder...
109 RewriteCond %{REQUEST_FILENAME} !-d
110 RewriteCond %{REQUEST_URI} (.+)/$
111 RewriteRule ^ %1 [L,R=301]
113 # Handle Front Controller...
114 RewriteCond %{REQUEST_FILENAME} !-d
115 RewriteCond %{REQUEST_FILENAME} !-f
116 RewriteRule ^ index.php [L]
120 ErrorLog \${APACHE_LOG_DIR}/error.log
121 CustomLog \${APACHE_LOG_DIR}/access.log combined
126 a2dissite 000-default.conf
127 a2ensite bookstack.conf
129 # Restart apache to load new config
130 systemctl restart apache2
133 echo "Setup Finished, Your BookStack instance should now be installed."
135 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
137 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"