]> BookStack Code Mirror - devops/blob - scripts/installation-centos-7.sh
Added centos install script
[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 dependancies
63 php composer.phar install
64
65 # Copy and update BookStack environment variables
66 cp .env.example .env
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
70 echo "APP_URL="
71 # Generate the application key
72 php artisan key:generate --no-interaction --force
73 # Migrate the databases
74 php artisan migrate --no-interaction --force
75
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
78
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
83 <VirtualHost *:80>
84         ServerName ${DOMAIN}
85
86         ServerAdmin webmaster@localhost
87         DocumentRoot /var/www/bookstack/public/
88
89     <Directory /var/www/bookstack/public/>
90         Options Indexes FollowSymLinks
91         AllowOverride None
92         Require all granted
93         <IfModule mod_rewrite.c>
94             <IfModule mod_negotiation.c>
95                 Options -MultiViews -Indexes
96             </IfModule>
97
98             RewriteEngine On
99
100             # Handle Authorization Header
101             RewriteCond %{HTTP:Authorization} .
102             RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
103
104             # Redirect Trailing Slashes If Not A Folder...
105             RewriteCond %{REQUEST_FILENAME} !-d
106             RewriteCond %{REQUEST_URI} (.+)/$
107             RewriteRule ^ %1 [L,R=301]
108
109             # Handle Front Controller...
110             RewriteCond %{REQUEST_FILENAME} !-d
111             RewriteCond %{REQUEST_FILENAME} !-f
112             RewriteRule ^ index.php [L]
113         </IfModule>
114     </Directory>
115
116         ErrorLog /var/log/httpd/bookstack-error.log
117         CustomLog /var/log/httpd/bookstack-access.log combined
118
119 </VirtualHost>
120 EOL
121
122 ln -s /etc/httpd/sites-available/bookstack.conf /etc/httpd/sites-enabled/bookstack.conf
123
124 # Restart apache to load new config
125 systemctl restart httpd
126
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
131
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
136
137 echo ""
138 echo "Setup Finished, Your BookStack instance should now be installed."
139 echo "You can login with the email '[email protected]' and password of 'password'"
140 echo "MySQL was installed without a root password, It is recommended that you set a root MySQL password."
141 echo ""
142 echo "You should be able to access your BookStack instance at: http://$CURRENT_IP/ or http://$DOMAIN/"