3 echo "This script installs a new BookStack instance on a fresh Ubuntu 22.04 server."
4 echo "This script does not ensure system security."
7 # Check we're running as root and exit if not
10 >&2 echo "ERROR: This script must be ran with root/sudo privileges"
14 # Get the current user running the script
15 SCRIPT_USER="${SUDO_USER:-$USER}"
17 # Get the current machine IP address
18 CURRENT_IP=$(ip addr | grep 'state UP' -A4 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/')
20 # Fetch domain to use from first provided parameter,
21 # Otherwise request the user to input their domain
26 echo "Enter the domain (or IP if not using a domain) you want to host BookStack on and press [ENTER]."
27 echo "Examples: my-site.com or docs.my-site.com or ${CURRENT_IP}"
31 # Ensure a domain was provided otherwise display
32 # an error message and stop the script
35 >&2 echo 'ERROR: A domain must be provided to run this script'
39 # Install core system packages
40 export DEBIAN_FRONTEND=noninteractive
42 apt install -y git unzip apache2 php8.1 curl php8.1-curl php8.1-mbstring php8.1-ldap \
43 php8.1-xml php8.1-zip php8.1-gd php8.1-mysql mysql-server-8.0 libapache2-mod-php8.1
46 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
47 mysql -u root --execute="CREATE DATABASE bookstack;"
48 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PASS';"
49 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
53 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
54 BOOKSTACK_DIR="/var/www/bookstack"
56 # Move into the BookStack install directory
57 cd $BOOKSTACK_DIR || exit
60 EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
61 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
62 ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
64 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
66 >&2 echo 'ERROR: Invalid composer installer checksum'
71 php composer-setup.php --quiet
74 # Move composer to global installation
75 mv composer.phar /usr/local/bin/composer
77 # Install BookStack composer dependencies
78 export COMPOSER_ALLOW_SUPERUSER=1
79 php /usr/local/bin/composer install --no-dev --no-plugins
81 # Copy and update BookStack environment variables
83 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
84 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
85 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
86 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
88 # Generate the application key
89 php artisan key:generate --no-interaction --force
90 # Migrate the databases
91 php artisan migrate --no-interaction --force
93 # Set file and folder permissions
94 # Sets current user as owner user and www-data as owner group then
95 # provides group write access only to required directories.
96 # Hides the `.env` file so it's not visible to other users on the system.
97 chown -R "$SCRIPT_USER":www-data ./
99 chmod -R 775 bootstrap/cache public/uploads storage
102 # Tell git to ignore permission changes
103 git config core.fileMode false
105 # Enable required apache modules
109 # Set-up the required BookStack apache config
110 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
114 ServerAdmin webmaster@localhost
115 DocumentRoot /var/www/bookstack/public/
117 <Directory /var/www/bookstack/public/>
118 Options -Indexes +FollowSymLinks
121 <IfModule mod_rewrite.c>
122 <IfModule mod_negotiation.c>
123 Options -MultiViews -Indexes
128 # Handle Authorization Header
129 RewriteCond %{HTTP:Authorization} .
130 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
132 # Redirect Trailing Slashes If Not A Folder...
133 RewriteCond %{REQUEST_FILENAME} !-d
134 RewriteCond %{REQUEST_URI} (.+)/$
135 RewriteRule ^ %1 [L,R=301]
137 # Handle Front Controller...
138 RewriteCond %{REQUEST_FILENAME} !-d
139 RewriteCond %{REQUEST_FILENAME} !-f
140 RewriteRule ^ index.php [L]
144 ErrorLog \${APACHE_LOG_DIR}/error.log
145 CustomLog \${APACHE_LOG_DIR}/access.log combined
150 # Disable the default apache site and enable BookStack
151 a2dissite 000-default.conf
152 a2ensite bookstack.conf
154 # Restart apache to load new config
155 systemctl restart apache2
158 echo "Setup Finished, Your BookStack instance should now be installed."
160 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
162 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"