]> BookStack Code Mirror - devops/blob - scripts/installation-ubuntu-20.04.sh
Changed output of ubuntu 22.04 script, added checks
[devops] / scripts / installation-ubuntu-20.04.sh
1 #!/bin/sh
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.
4
5 # Fetch domain to use from first provided parameter,
6 # Otherwise request the user to input their domain
7 DOMAIN=$1
8 if [ -z "$1" ]
9 then
10 echo ""
11 printf "Enter the domain you want to host BookStack and press [ENTER]\nExamples: my-site.com or docs.my-site.com\n"
12 read -r DOMAIN
13 fi
14
15 # Ensure a domain was provided otherwise display
16 # an error message and stop the script
17 if [ -z "$DOMAIN" ]
18 then
19   >&2 echo 'ERROR: A domain must be provided to run this script'
20   exit 1
21 fi
22
23 # Get the current machine IP address
24 CURRENT_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')
25
26 # Install core system packages
27 export DEBIAN_FRONTEND=noninteractive
28 add-apt-repository universe
29 apt update
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
32
33 # Set up database
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;"
38
39 # Download BookStack
40 cd /var/www || exit
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
44
45 # Install composer
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');")"
49
50 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
51 then
52     >&2 echo 'ERROR: Invalid composer installer checksum'
53     rm composer-setup.php
54     exit 1
55 fi
56
57 php composer-setup.php --quiet
58 rm composer-setup.php
59
60 # Move composer to global installation
61 mv composer.phar /usr/local/bin/composer
62
63 # Install BookStack composer dependencies
64 export COMPOSER_ALLOW_SUPERUSER=1
65 php /usr/local/bin/composer install --no-dev --no-plugins
66
67 # Copy and update BookStack environment variables
68 cp .env.example .env
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
73
74 # Generate the application key
75 php artisan key:generate --no-interaction --force
76 # Migrate the databases
77 php artisan migrate --no-interaction --force
78
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
81
82 # Set up apache
83 a2enmod rewrite
84 a2enmod php7.4
85
86 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
87 <VirtualHost *:80>
88         ServerName ${DOMAIN}
89
90         ServerAdmin webmaster@localhost
91         DocumentRoot /var/www/bookstack/public/
92
93     <Directory /var/www/bookstack/public/>
94         Options Indexes FollowSymLinks
95         AllowOverride None
96         Require all granted
97         <IfModule mod_rewrite.c>
98             <IfModule mod_negotiation.c>
99                 Options -MultiViews -Indexes
100             </IfModule>
101
102             RewriteEngine On
103
104             # Handle Authorization Header
105             RewriteCond %{HTTP:Authorization} .
106             RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
107
108             # Redirect Trailing Slashes If Not A Folder...
109             RewriteCond %{REQUEST_FILENAME} !-d
110             RewriteCond %{REQUEST_URI} (.+)/$
111             RewriteRule ^ %1 [L,R=301]
112
113             # Handle Front Controller...
114             RewriteCond %{REQUEST_FILENAME} !-d
115             RewriteCond %{REQUEST_FILENAME} !-f
116             RewriteRule ^ index.php [L]
117         </IfModule>
118     </Directory>
119
120         ErrorLog \${APACHE_LOG_DIR}/error.log
121         CustomLog \${APACHE_LOG_DIR}/access.log combined
122
123 </VirtualHost>
124 EOL
125
126 a2dissite 000-default.conf
127 a2ensite bookstack.conf
128
129 # Restart apache to load new config
130 systemctl restart apache2
131
132 echo ""
133 echo "Setup Finished, Your BookStack instance should now be installed."
134 echo "You can login with the email '[email protected]' and password of 'password'"
135 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
136 echo ""
137 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"