Created
October 10, 2017 17:25
-
-
Save lcherone/c85a10e9792cbdc5b18d3d014446c70f to your computer and use it in GitHub Desktop.
c9-lamp-prep.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Cloud9 Prep & LAMP Server | |
# Apache2 (rewrite, headers enabled), PHP7, MariaDB, Git, Composer, nodejs & npm | |
# | |
# Set envioroment, for cloud-init | |
set -e | |
export DEBIAN_FRONTEND=noninteractive | |
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' | |
export HOME='/root' | |
# Set working vars, change these to suit | |
# | |
# Application Name | |
appname="Cloud9 Prep & LAMP Server" | |
webroot="/var/www/html" | |
timezone="Europe/London" | |
# | |
# Database - root | |
rootdbpass=$(date +%s%N | sha256sum | base64 | head -c 32 ; echo) | |
# | |
# Database - app | |
dbname="app" | |
dbuser="app" | |
dbpass=$(date +%s%N | sha256sum | base64 | head -c 32 ; echo) | |
# | |
# | |
installed=`date +%Y-%m-%d`; | |
# Apache2 | |
write_info() { | |
if ! echo " | |
============================== | |
= $appname | |
= | |
= Installed Date: $installed | |
= | |
= Cloud9 | |
= - Webroot: $webroot | |
= | |
= MariaDB (mysql) Credentials | |
= - Root Password: $rootdbpass | |
= - | |
= - Database: $dbname | |
= - User: $dbuser | |
= - Password: $dbpass | |
" > $webroot/../install-info.txt | |
then | |
echo "There was an error when creating install-info.txt" | |
exit; | |
else | |
echo "File install-info.txt created." | |
fi | |
} | |
# Setup & Install Dependencys | |
setup_system() { | |
# | |
# set timezone | |
echo $timezone > /etc/timezone | |
dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>/dev/null | |
# | |
# Update System | |
sudo apt-get update | |
sudo apt-get -yq upgrade | |
# | |
# Install system packages | |
sudo apt-get -yq install curl wget | |
sudo apt-get -yq install unzip | |
sudo apt-get -yq install nano | |
sudo apt-get -yq install htop | |
sudo apt-get -yq install git | |
} | |
# Apache2 | |
install_apache() { | |
# | |
# Install Apache2 | |
sudo apt-get -yq install apache2 | |
# | |
# Enable apache modules | |
sudo a2enmod headers | |
sudo a2enmod rewrite | |
sudo awk '/<Directory \/var\/www\/>/,/AllowOverride None/{sub("None", "All",$0)}{print}' /etc/apache2/apache2.conf > tmp.conf && mv tmp.conf /etc/apache2/apache2.conf | |
# | |
# Restart apache2 | |
# | |
sudo service apache2 restart | |
# | |
# Empty the webroot | |
if [ -f /var/www/html/index.html ]; then | |
sudo rm /var/www/html/index.html | |
fi | |
} | |
# Install PHP | |
install_php() { | |
# | |
# Import distibution variables | |
. /etc/lsb-release | |
# | |
# Is PHP5? | |
if [ $DISTRIB_RELEASE = "12.04" ] || [ $DISTRIB_RELEASE = "14.04" ] || [ $DISTRIB_RELEASE = "15.04" ]; then | |
phpver="5" | |
fi | |
# | |
# Is PHP7? | |
if [ $DISTRIB_RELEASE = "16.04" ] || [ $DISTRIB_RELEASE = "16.10" ] || [ $DISTRIB_RELEASE = "17.04" ] || [ $DISTRIB_RELEASE = "17.10" ]; then | |
phpver="7" | |
fi | |
# | |
# Install PHP5 | |
if [ $phpver = "5" ]; then | |
# | |
echo "Installing PHP5.5.9" | |
sudo apt-get -yq install php5 php5-cli | |
sudo apt-get -yq install php5-{curl,gd,mcrypt,json,mysql,sqlite} | |
# | |
sudo apt-get -yq install libapache2-mod-php5 | |
# | |
# enable mods | |
sudo php5enmod mcrypt | |
fi | |
# | |
# Install PHP7 | |
if [ $phpver = "7" ]; then | |
# | |
echo "Installing PHP7.0" | |
sudo apt-get -yq install php7.0 php7.0-cli | |
sudo apt-get -yq install php7.0-{mbstring,curl,gd,mcrypt,json,xml,mysql,sqlite} | |
# | |
sudo apt-get -yq install libapache2-mod-php7.0 | |
fi | |
} | |
# Install MariaDB | |
install_mariadb() { | |
# | |
# Preset Selections | |
sudo debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password password $rootdbpass" | |
sudo debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password_again password $rootdbpass" | |
# | |
# Install | |
sudo apt-get -yqq install mariadb-server | |
sudo apt-get -yqq install libmariadbclient-dev | |
# | |
sudo service mysql start | |
# | |
# Set root user password | |
sudo mysql -u root -p$rootdbpass -e "CREATE DATABASE $dbname /*\!40100 DEFAULT CHARACTER SET utf8 */;" | |
sudo mysql -u root -p$rootdbpass -e "CREATE USER $dbuser@'%' IDENTIFIED BY '$dbpass';" | |
sudo mysql -u root -p$rootdbpass -e "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'%';" | |
sudo mysql -u root -p$rootdbpass -e "GRANT ALL PRIVILEGES on *.* to 'root'@'localhost' IDENTIFIED BY '$rootdbpass';" | |
sudo mysql -u root -p$rootdbpass -e "FLUSH PRIVILEGES;" | |
# | |
sudo service mysql restart | |
} | |
# Install composer (globally) | |
install_composer() { | |
# | |
# Install composer | |
sudo curl -sS https://getcomposer.org/installer | sudo php | |
sudo mv composer.phar /usr/local/bin/composer | |
sudo ln -s /usr/local/bin/composer /usr/bin/composer | |
} | |
# Install Node.js | |
install_nodejs() { | |
# | |
# Install nodejs | |
sudo apt -yq install nodejs nodejs-legacy npm | |
} | |
# Install Application | |
#install_project() { | |
# # | |
# # Place install code here specific for the application | |
#} | |
# | |
# Main | |
# | |
main() { | |
# | |
setup_system | |
# | |
install_php | |
# | |
install_composer | |
# | |
install_apache | |
# | |
install_mariadb | |
# | |
install_nodejs | |
# | |
write_info | |
# | |
#install_project | |
# | |
echo "Install finished." | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment