2 echo "This script will install a new BookStack instance on a fresh CentOS 7 server."
3 echo "This script is experimental and does not attend to system security."
5 # Fetch domain to use from first provided parameter,
6 # Otherwise request the user to input their domain
11 printf "Enter the domain you want to host BookStack and press [ENTER]\nExamples: my-site.com or docs.my-site.com\n"
15 # Get the current machine IP address
16 CURRENT_IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
18 # Install core system packages and remi php repository
20 yum install -y git httpd curl wget yum-utils mariadb-server
21 wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
22 wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
23 rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
24 yum-config-manager --enable remi-php73
25 yum install -y php php-cli php-common php-gd php-json php-ldap php-mysqlnd php-mbstring php-tidy php-xml php-zip php-mcrypt php-opcache
27 # Start Apache & Mariadb
29 systemctl start mariadb
30 # Set Apache and Mariadb to start on system boot
31 systemctl enable httpd
32 systemctl enable mariadb
35 DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)"
36 mysql -u root --execute="CREATE DATABASE bookstack;"
37 mysql -u root --execute="CREATE USER 'bookstack'@'localhost' IDENTIFIED BY '$DB_PASS';"
38 mysql -u root --execute="GRANT ALL ON bookstack.* TO 'bookstack'@'localhost';FLUSH PRIVILEGES;"
42 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
43 BOOKSTACK_DIR="/var/www/bookstack"
47 EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
48 curl -s https://getcomposer.org/installer > composer-setup.php
49 ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');")
51 if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
53 php composer-setup.php --quiet
55 rm -f composer-setup.php
57 >&2 echo 'ERROR: Invalid composer installer signature'
58 rm -f composer-setup.php
62 # Install BookStack composer dependancies
63 php composer.phar install
65 # Copy and update BookStack environment variables
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
71 # Generate the application key
72 php artisan key:generate --no-interaction --force
73 # Migrate the databases
74 php artisan migrate --no-interaction --force
76 # Set BookStack file and folder permissions
77 chown apache:apache -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
79 # Set up Apache VirtualHost
80 mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
81 echo "IncludeOptional sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf
82 cat >/etc/httpd/sites-available/bookstack.conf <<EOL
86 ServerAdmin webmaster@localhost
87 DocumentRoot /var/www/bookstack/public/
89 <Directory /var/www/bookstack/public/>
90 Options Indexes FollowSymLinks
93 <IfModule mod_rewrite.c>
94 <IfModule mod_negotiation.c>
95 Options -MultiViews -Indexes
100 # Handle Authorization Header
101 RewriteCond %{HTTP:Authorization} .
102 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
104 # Redirect Trailing Slashes If Not A Folder...
105 RewriteCond %{REQUEST_FILENAME} !-d
106 RewriteCond %{REQUEST_URI} (.+)/$
107 RewriteRule ^ %1 [L,R=301]
109 # Handle Front Controller...
110 RewriteCond %{REQUEST_FILENAME} !-d
111 RewriteCond %{REQUEST_FILENAME} !-f
112 RewriteRule ^ index.php [L]
116 ErrorLog /var/log/httpd/bookstack-error.log
117 CustomLog /var/log/httpd/bookstack-access.log combined
122 ln -s /etc/httpd/sites-available/bookstack.conf /etc/httpd/sites-enabled/bookstack.conf
124 # Restart apache to load new config
125 systemctl restart httpd
127 # Open up the firewall
128 firewall-cmd --permanent --zone=public --add-service=http
129 firewall-cmd --permanent --zone=public --add-service=https
130 firewall-cmd --reload
132 # Update SELinux to allow Apache to write to BookStack locations
133 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/bootstrap/cache
134 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/public/uploads
135 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/storage
138 echo "Setup Finished, Your BookStack instance should now be installed."
140 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
142 echo "You should be able to access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"