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