]> BookStack Code Mirror - devops/blob - scripts/installation-ubuntu-22.04.sh
8576dbf109a7f686df2449aef89af1db325a2bdd
[devops] / scripts / installation-ubuntu-22.04.sh
1 #!/bin/bash
2
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."
5 echo ""
6
7 # Check we're running as root and exit if not
8 if [[ $EUID -gt 0 ]]
9 then
10   >&2 echo "ERROR: This script must be ran with root/sudo privileges"
11   exit 1
12 fi
13
14 # Get the current user running the script
15 SCRIPT_USER="${SUDO_USER:-$USER}"
16
17 # Get the current machine IP address
18 CURRENT_IP=$(ip addr | grep 'state UP' -A4 | grep 'inet ' | awk '{print $2}' | cut -f1  -d'/')
19
20 # Fetch domain to use from first provided parameter,
21 # Otherwise request the user to input their domain
22 DOMAIN=$1
23 if [ -z "$1" ]
24 then
25   echo ""
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}"
28   read -r DOMAIN
29 fi
30
31 # Ensure a domain was provided otherwise display
32 # an error message and stop the script
33 if [ -z "$DOMAIN" ]
34 then
35   >&2 echo 'ERROR: A domain must be provided to run this script'
36   exit 1
37 fi
38
39 # Install core system packages
40 export DEBIAN_FRONTEND=noninteractive
41 apt update
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
44
45 # Set up database
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;"
50
51 # Download BookStack
52 cd /var/www || exit
53 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
54 BOOKSTACK_DIR="/var/www/bookstack"
55
56 # Move into the BookStack install directory
57 cd $BOOKSTACK_DIR || exit
58
59 # Install composer
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');")"
63
64 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
65 then
66     >&2 echo 'ERROR: Invalid composer installer checksum'
67     rm composer-setup.php
68     exit 1
69 fi
70
71 php composer-setup.php --quiet
72 rm composer-setup.php
73
74 # Move composer to global installation
75 mv composer.phar /usr/local/bin/composer
76
77 # Install BookStack composer dependencies
78 export COMPOSER_ALLOW_SUPERUSER=1
79 php /usr/local/bin/composer install --no-dev --no-plugins
80
81 # Copy and update BookStack environment variables
82 cp .env.example .env
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
87
88 # Generate the application key
89 php artisan key:generate --no-interaction --force
90 # Migrate the databases
91 php artisan migrate --no-interaction --force
92
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 ./
98 chmod -R 755 ./
99 chmod -R 775 bootstrap/cache public/uploads storage
100 chmod 740 .env
101
102 # Tell git to ignore permission changes
103 git config core.fileMode false
104
105 # Enable required apache modules
106 a2enmod rewrite
107 a2enmod php8.1
108
109 # Set-up the required BookStack apache config
110 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
111 <VirtualHost *:80>
112         ServerName ${DOMAIN}
113
114         ServerAdmin webmaster@localhost
115         DocumentRoot /var/www/bookstack/public/
116
117   <Directory /var/www/bookstack/public/>
118       Options -Indexes +FollowSymLinks
119       AllowOverride None
120       Require all granted
121       <IfModule mod_rewrite.c>
122           <IfModule mod_negotiation.c>
123               Options -MultiViews -Indexes
124           </IfModule>
125
126           RewriteEngine On
127
128           # Handle Authorization Header
129           RewriteCond %{HTTP:Authorization} .
130           RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
131
132           # Redirect Trailing Slashes If Not A Folder...
133           RewriteCond %{REQUEST_FILENAME} !-d
134           RewriteCond %{REQUEST_URI} (.+)/$
135           RewriteRule ^ %1 [L,R=301]
136
137           # Handle Front Controller...
138           RewriteCond %{REQUEST_FILENAME} !-d
139           RewriteCond %{REQUEST_FILENAME} !-f
140           RewriteRule ^ index.php [L]
141       </IfModule>
142   </Directory>
143
144         ErrorLog \${APACHE_LOG_DIR}/error.log
145         CustomLog \${APACHE_LOG_DIR}/access.log combined
146
147 </VirtualHost>
148 EOL
149
150 # Disable the default apache site and enable BookStack
151 a2dissite 000-default.conf
152 a2ensite bookstack.conf
153
154 # Restart apache to load new config
155 systemctl restart apache2
156
157 echo ""
158 echo "Setup Finished, Your BookStack instance should now be installed."
159 echo "You can login with the email '[email protected]' and password of 'password'"
160 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
161 echo ""
162 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"