Skip to content

Instantly share code, notes, and snippets.

@airbornelamb
Created May 19, 2017 01:54
Show Gist options
  • Save airbornelamb/5f4530715d74d6bddf9620b0165dd1c6 to your computer and use it in GitHub Desktop.
Save airbornelamb/5f4530715d74d6bddf9620b0165dd1c6 to your computer and use it in GitHub Desktop.
Flarum install script for Ubuntu 14.04 or Debian 7.11
#!/bin/bash
flarum_install="/home/$USER/flarum"
webuser_group='www-data'
OS_version=$(lsb_release -a 2> /dev/null | grep Description | tr -d \\t | cut -f2 -d:)
sudo gpasswd -a $USER www-data
# Add repo and key for php5.6
# Support for Debian 7.10 and Ubuntu 14.04
if [ "$OS_version" == "Debian GNU/Linux 7.11 (wheezy)" ]
then
grep -R dotdeb /etc/apt/sources.list /etc/apt/sources.list.d/* &> /dev/null
if [ $? != 0 ];
then
if [ ! -f dotdeb.gpg ]; then wget https://www.dotdeb.org/dotdeb.gpg -O dotdeb.gpg; fi
sudo apt-key add dotdeb.gpg && rm dotdeb.gpg
sudo tee << EOF /etc/apt/sources.list.d/dotdeb.list
deb http://packages.dotdeb.org wheezy-php56 all
deb-src http://packages.dotdeb.org wheezy-php56 all
EOF
fi
elif [ "$OS_version" == "Ubuntu 14.04.5 LTS" ]
then
sudo add-apt-repository ppa:ondrej/php
#else
# echo "No valid version detected"
# echo "Press Enter if sure php version is valid
# echo "Or push Ctrl+C to exit"
# read -p '[] '
fi
# Update lists
echo 'Updating...'
sudo apt-get update 1> /dev/null
# Install packages
for package in nginx mysql-server mysql-client php5-curl php5-gd php5-cli php5-mysql php5-fpm pwgen git
do
dpkg -s "$package" &> /dev/null || packages="$packages $package"
done
sudo apt-get install $packages
# Install composer
if [ ! -e /usr/bin/composer ]
then
php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php
sudo php composer-setup.php --install-dir=/usr/bin --filename composer
php -r "unlink('composer-setup.php');"
fi
# Install flarum
echo Installing flarum..
composer create-project flarum/flarum "$flarum_install" --stability=beta
# Change group and permissions
chmod 775 "$flarum_install"
chmod -R 775 "$flarum_install/assets" "$flarum_install/storage"
sudo chgrp "$webuser_group" "$flarum_install"
sudo chgrp -R "$webuser_group" "$flarum_install/assets" "$flarum_install/extensions" "$flarum_install/storage"
### Write php5-fpm pool config
echo writing php5-fpm config
sudo tee << 'EOF' /etc/php5/fpm/pool.d/flarum.conf &> /dev/null
[flarum]
# auto-generated
listen = /var/run/php5-fpm.$pool.sock
listen.allowed_clients = 127.0.0.1
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 10
pm.max_requests = 5000
pm.process_idle_timeout = 60s
chdir = /
EOF
### Write Nginx config
echo writing nginx config
sudo tee << EOF /etc/nginx/sites-available/flarum &> /dev/null
server { # autogenerated
listen 80;
root $flarum_install;
index index.php index.html index.htm;
error_log /var/log/nginx/error.log error;
server_name _;
EOF
sudo tee --append << 'EOF' /etc/nginx/sites-available/flarum &> /dev/null
location / { try_files $uri $uri/ /index.php?$query_string; }
location /api { try_files $uri $uri/ /api.php?$query_string; }
location /admin { try_files $uri $uri/ /admin.php?$query_string; }
location /flarum {
deny all;
return 404;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.flarum.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.html$ {
expires -1;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 1M;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types application/atom+xml
application/javascript
application/json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/svg+xml
image/x-icon
text/css
text/plain
text/xml;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
}
EOF
# Enable flarum site
sudo ln -s /etc/nginx/sites-available/flarum /etc/nginx/sites-enabled/flarum
# Remove default nginx welcome page
if [ -f /etc/nginx/sites-enabled/default ]
then
echo '/etc/nginx/sites-enabled/default exists - Remove? (y/n)'
read answer
if [ "${answer,,}" == y ]
then
echo 'Removing /etc/nginx/sites-enabled/default'
sudo rm /etc/nginx/sites-enabled/default
fi
fi
#Stop apache2 if running and start other services
sudo service apache2 stop
sudo service php5-fpm stop; sudo service php5-fpm start
sudo service nginx configtest && sudo service nginx restart
# Database creation
# Generate Password
generated_mysql_pass=$(pwgen 20 1)
read -sp 'Enter mysql root password ' mysql_pass
mysql -uroot -p"$mysql_pass" -e 'CREATE DATABASE IF NOT EXISTS `flarum_database` ;'
mysql -uroot -p"$mysql_pass" -e "CREATE USER 'flarum_database'@'localhost' IDENTIFIED BY '$generated_mysql_pass';"
mysql -uroot -p"$mysql_pass" -e "GRANT USAGE ON * . * TO 'flarum_database'@'localhost' IDENTIFIED BY '$generated_mysql_pass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;"
mysql -uroot -p"$mysql_pass" -e 'GRANT ALL PRIVILEGES ON `flarum_database` . * TO 'flarum_database'@'localhost';'
echo ""
echo 'Flarum username -> flarum_database'
echo 'Flarum database -> flarum_database'
echo 'Flarum database password ->' "$generated_mysql_pass"
unset mysql_pass generated_mysql_pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment