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