]> BookStack Code Mirror - devops/blob - scripts/installation-ubuntu-20.04.sh
Debian 13 script: Fixed mysql use for mariadb, removed composer use
[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 apt update
28 apt install -y software-properties-common
29 export DEBIAN_FRONTEND=noninteractive
30 add-apt-repository universe
31 add-apt-repository -yu ppa:ondrej/php
32 apt install -y git unzip apache2 curl php8.2 php8.2-curl php8.2-mbstring php8.2-ldap \
33   php8.2-xml php8.2-zip php8.2-gd php8.2-mysql mysql-server-8.0 libapache2-mod-php8.2
34
35 # Set up database
36 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
37 mysql -u root --execute="CREATE DATABASE bookstack;"
38 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DB_PASS';"
39 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
40
41 # Download BookStack
42 cd /var/www || exit
43 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
44 BOOKSTACK_DIR="/var/www/bookstack"
45 cd $BOOKSTACK_DIR || exit
46
47 # Install composer
48 EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
49 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
50 ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
51
52 if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
53 then
54     >&2 echo 'ERROR: Invalid composer installer checksum'
55     rm composer-setup.php
56     exit 1
57 fi
58
59 php composer-setup.php --quiet
60 rm composer-setup.php
61
62 # Move composer to global installation
63 mv composer.phar /usr/local/bin/composer
64
65 # Install BookStack composer dependencies
66 export COMPOSER_ALLOW_SUPERUSER=1
67 php /usr/local/bin/composer install --no-dev --no-plugins
68
69 # Copy and update BookStack environment variables
70 cp .env.example .env
71 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
72 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
73 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
74 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
75
76 # Generate the application key
77 php artisan key:generate --no-interaction --force
78 # Migrate the databases
79 php artisan migrate --no-interaction --force
80
81 # Set file and folder permissions
82 chown www-data:www-data -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
83
84 # Set up apache
85 a2enmod rewrite
86 a2enmod php8.2
87
88 cat >/etc/apache2/sites-available/bookstack.conf <<EOL
89 <VirtualHost *:80>
90         ServerName ${DOMAIN}
91
92         ServerAdmin webmaster@localhost
93         DocumentRoot /var/www/bookstack/public/
94
95     <Directory /var/www/bookstack/public/>
96         Options Indexes FollowSymLinks
97         AllowOverride None
98         Require all granted
99         <IfModule mod_rewrite.c>
100             <IfModule mod_negotiation.c>
101                 Options -MultiViews -Indexes
102             </IfModule>
103
104             RewriteEngine On
105
106             # Handle Authorization Header
107             RewriteCond %{HTTP:Authorization} .
108             RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
109
110             # Redirect Trailing Slashes If Not A Folder...
111             RewriteCond %{REQUEST_FILENAME} !-d
112             RewriteCond %{REQUEST_URI} (.+)/$
113             RewriteRule ^ %1 [L,R=301]
114
115             # Handle Front Controller...
116             RewriteCond %{REQUEST_FILENAME} !-d
117             RewriteCond %{REQUEST_FILENAME} !-f
118             RewriteRule ^ index.php [L]
119         </IfModule>
120     </Directory>
121
122         ErrorLog \${APACHE_LOG_DIR}/error.log
123         CustomLog \${APACHE_LOG_DIR}/access.log combined
124
125 </VirtualHost>
126 EOL
127
128 a2dissite 000-default.conf
129 a2ensite bookstack.conf
130
131 # Restart apache to load new config
132 systemctl restart apache2
133
134 echo ""
135 echo "Setup Finished, Your BookStack instance should now be installed."
136 echo "You can login with the email 'admin@admin.com' and password of 'password'"
137 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
138 echo ""
139 echo "You can access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"
Morty Proxy This is a proxified and sanitized view of the page, visit original site.