]> BookStack Code Mirror - devops/blob - scripts/installation-centos-7.sh
Fixed install script composer setup lines
[devops] / scripts / installation-centos-7.sh
1 #!/bin/sh
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."
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 and remi php repository
19 yum check-update
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
26
27 # Start Apache & Mariadb
28 systemctl start httpd
29 systemctl start mariadb
30 # Set Apache and Mariadb to start on system boot
31 systemctl enable httpd
32 systemctl enable mariadb
33
34 # Set up database
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;"
39
40 # Download BookStack
41 cd /var/www
42 git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch bookstack
43 BOOKSTACK_DIR="/var/www/bookstack"
44 cd $BOOKSTACK_DIR
45
46 # Install composer
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');")
50
51 if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
52 then
53     php composer-setup.php --quiet
54     RESULT=$?
55     rm -f composer-setup.php
56 else
57     >&2 echo 'ERROR: Invalid composer installer signature'
58     rm -f composer-setup.php
59     exit 1
60 fi
61
62 # Install BookStack composer dependencies
63 php composer.phar install
64
65 # Copy and update BookStack environment variables
66 cp .env.example .env
67 sed -i.bak "s@APP_URL=.*\$@APP_URL=http://$DOMAIN@" .env
68 sed -i.bak 's/DB_DATABASE=.*$/DB_DATABASE=bookstack/' .env
69 sed -i.bak 's/DB_USERNAME=.*$/DB_USERNAME=bookstack/' .env
70 sed -i.bak "s/DB_PASSWORD=.*\$/DB_PASSWORD=$DB_PASS/" .env
71
72 # Generate the application key
73 php artisan key:generate --no-interaction --force
74 # Migrate the databases
75 php artisan migrate --no-interaction --force
76
77 # Set BookStack file and folder permissions
78 chown apache:apache -R bootstrap/cache public/uploads storage && chmod -R 755 bootstrap/cache public/uploads storage
79
80 # Set up Apache VirtualHost
81 mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
82 echo "IncludeOptional sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf
83 cat >/etc/httpd/sites-available/bookstack.conf <<EOL
84 <VirtualHost *:80>
85         ServerName ${DOMAIN}
86
87         ServerAdmin webmaster@localhost
88         DocumentRoot /var/www/bookstack/public/
89
90     <Directory /var/www/bookstack/public/>
91         Options Indexes FollowSymLinks
92         AllowOverride None
93         Require all granted
94         <IfModule mod_rewrite.c>
95             <IfModule mod_negotiation.c>
96                 Options -MultiViews -Indexes
97             </IfModule>
98
99             RewriteEngine On
100
101             # Handle Authorization Header
102             RewriteCond %{HTTP:Authorization} .
103             RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
104
105             # Redirect Trailing Slashes If Not A Folder...
106             RewriteCond %{REQUEST_FILENAME} !-d
107             RewriteCond %{REQUEST_URI} (.+)/$
108             RewriteRule ^ %1 [L,R=301]
109
110             # Handle Front Controller...
111             RewriteCond %{REQUEST_FILENAME} !-d
112             RewriteCond %{REQUEST_FILENAME} !-f
113             RewriteRule ^ index.php [L]
114         </IfModule>
115     </Directory>
116
117         ErrorLog /var/log/httpd/bookstack-error.log
118         CustomLog /var/log/httpd/bookstack-access.log combined
119
120 </VirtualHost>
121 EOL
122
123 ln -s /etc/httpd/sites-available/bookstack.conf /etc/httpd/sites-enabled/bookstack.conf
124
125 # Restart apache to load new config
126 systemctl restart httpd
127
128 # Open up the firewall
129 firewall-cmd --permanent --zone=public --add-service=http 
130 firewall-cmd --permanent --zone=public --add-service=https
131 firewall-cmd --reload
132
133 # Update SELinux to allow Apache to write to BookStack locations
134 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/bootstrap/cache
135 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/public/uploads
136 chcon -Rv --type=httpd_sys_rw_content_t /var/www/bookstack/storage
137
138 echo ""
139 echo "Setup Finished, Your BookStack instance should now be installed."
140 echo "You can login with the email '[email protected]' and password of 'password'"
141 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
142 echo ""
143 echo "You should be able to access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"