]> BookStack Code Mirror - devops/blob - scripts/installation-ubuntu-20.04.sh
Added domain provided check to ubuntu install script
[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 # Move composer to global installation
58 mv composer.phar /usr/local/bin/composer
59
60 # Install BookStack composer dependencies
61 export COMPOSER_ALLOW_SUPERUSER=1
62 php /usr/local/bin/composer install --no-dev --no-plugins
63
64 # Copy and update BookStack environment variables
65 cp .env.example .env
66 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
67 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
68 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
69 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
70
71 # Generate the application key
72 php artisan key:generate --no-interaction --force
73 # Migrate the databases
74 php artisan migrate --no-interaction --force
75
76 # Set file and folder permissions
77 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
78
79 # Set up apache
80 a2enmod rewrite
81 a2enmod php7.4
82
83 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
84 <VirtualHost *:80>
85         ServerName ${DOMAIN}
86
87         ServerAdmin webmaster@localhost
88         DocumentRoot /var/www/bookstack/public/
89
90     <Directory /var/www/bookstack/public/>
91         Options Indexes FollowSymLinks
92         AllowOverride None
93         Require all granted
94         <IfModule mod_rewrite.c>
95             <IfModule mod_negotiation.c>
96                 Options -MultiViews -Indexes
97             </IfModule>
98
99             RewriteEngine On
100
101             # Handle Authorization Header
102             RewriteCond %{HTTP:Authorization} .
103             RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
104
105             # Redirect Trailing Slashes If Not A Folder...
106             RewriteCond %{REQUEST_FILENAME} !-d
107             RewriteCond %{REQUEST_URI} (.+)/$
108             RewriteRule ^ %1 [L,R=301]
109
110             # Handle Front Controller...
111             RewriteCond %{REQUEST_FILENAME} !-d
112             RewriteCond %{REQUEST_FILENAME} !-f
113             RewriteRule ^ index.php [L]
114         </IfModule>
115     </Directory>
116
117         ErrorLog \${APACHE_LOG_DIR}/error.log
118         CustomLog \${APACHE_LOG_DIR}/access.log combined
119
120 </VirtualHost>
121 EOL
122
123 a2dissite 000-default.conf
124 a2ensite bookstack.conf
125
126 # Restart apache to load new config
127 systemctl restart apache2
128
129 echo ""
130 echo "Setup Finished, Your BookStack instance should now be installed."
131 echo "You can login with the email '[email protected]' and password of 'password'"
132 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
133 echo ""
134 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"