Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Installing BookStack (Debian 11) (2022-03)

Installation: Installing BookStack (Debian 11)

  1. Install dependencies.

    apt install -y git unzip apache2 php7.4 curl php7.4-fpm php7.4-curl php7.4-mbstring php7.4-ldap \
    php7.4-tidy php7.4-xml php7.4-zip php7.4-gd php7.4-mysql libapache2-mod-php7.4 \
  2. Setup database.

    DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
    mysql -u root --execute="CREATE DATABASE bookstack;"
    mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password AS PASSWORD('$DB_PASS');"
    mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
    # TODO: Consider moving away from 'mysql_native_password' to a more secure authentication mechanism.
  3. Install PHP package manager.

    # Install composer
    EXPECTED_CHECKSUM="$(php -r 'copy("", "php://stdout");')"
    php -r "copy('', 'composer-setup.php');"
    ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
        >&2 echo 'ERROR: Invalid composer installer checksum'
        rm composer-setup.php
        exit 1
    php composer-setup.php --quiet
    rm composer-setup.php
    # Move composer to global installation
    mv composer.phar /usr/local/bin/composer
  4. Download BookStack PHP webapp files.

    cd /var/www
    git clone --branch release --single-branch bookstack
    cd bookstack
  5. Configure BookStack.

    # Install BookStack composer dependencies
    php /usr/local/bin/composer install --no-dev --no-plugins
    # Copy and update BookStack environment variables
    cp .env.example .env
    sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
    sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
    sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
    sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
    # Generate the application key
    php artisan key:generate --no-interaction --force
    # Migrate the databases
    php artisan migrate --no-interaction --force
    # Set file and folder permissions
    chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
  6. Configure Apache to serve new BookStack app.

    # Set up apache
    a2enmod rewrite
    a2enmod php7.4
    # Create apache config file to serve webapp
    vim /etc/apache2/sites-available/bookstack.conf
        <VirtualHost *:80>
                ServerName           # Set to static IP otherwise define hostname in DNS entry for it to work.
                ServerAdmin webmaster@localhost
                DocumentRoot /var/www/bookstack/public/
            <Directory /var/www/bookstack/public/>
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
                <IfModule mod_rewrite.c>
                    <IfModule mod_negotiation.c>
                        Options -MultiViews -Indexes
                    RewriteEngine On
                    # Handle Authorization Header
                    RewriteCond %{HTTP:Authorization} .
                    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
                    # Redirect Trailing Slashes If Not A Folder...
                    RewriteCond %{REQUEST_FILENAME} !-d
                    RewriteCond %{REQUEST_URI} (.+)/$
                    RewriteRule ^ %1 [L,R=301]
                    # Handle Front Controller...
                    RewriteCond %{REQUEST_FILENAME} !-d
                    RewriteCond %{REQUEST_FILENAME} !-f
                    RewriteRule ^ index.php [L]
                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined
  7. Verify config file and enable it

    apachectl configtest
    a2dissite 000-default.conf              # Disable default website
    a2ensite bookstack.conf
    systemctl restart apache2
  8. Login into Bookstack: Browser > $BOOKSTACK_IP > Login('', 'password')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment