Last active
August 29, 2015 14:00
-
-
Save williamsodell/11276724 to your computer and use it in GitHub Desktop.
StackScript to install the LAMP stack plus phusion passenger on apache2 and rvm defaulting to ruby 2.0.0 and rails 4
This file contains hidden or 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 | |
# StackScript: 8644 | |
############################################################## | |
# Generic functions | |
############################################################## | |
function system_update { | |
yum -y update | |
} | |
function install_basics { | |
yum -y install jwhois rsync openssh-clients wget | |
} | |
function postfix_install_loopback_only { | |
# Installs postfix and configure to listen only on the local interface. Also | |
# allows for local mail delivery | |
yum -y install postfix | |
yum -y remove sendmail | |
touch /tmp/restart-postfix | |
} | |
function goodstuff { | |
yum -y install vim-enhanced git | |
} | |
function restartServices { | |
# restarts services that have a file in /tmp/needs-restart/ | |
for service in $(ls /tmp/restart-* | cut -d- -f2); do | |
/etc/init.d/$service restart | |
rm -f /tmp/restart-$service | |
done | |
} | |
############################################################## | |
# MySQL functions | |
############################################################## | |
function mysql_install { | |
# $1 - the mysql root password | |
if [ ! -n "$1" ]; then | |
echo "mysql_install() requires the root pass as its first argument" | |
return 1; | |
fi | |
yum -y install mysql-server | |
/etc/init.d/mysqld start | |
echo "Sleeping while MySQL starts up for the first time..." | |
sleep 20 | |
#Remove anonymous users | |
echo "DELETE FROM mysql.user WHERE User='';" | mysql -u root | |
#Remove remote root | |
echo "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';" | mysql -u root | |
#Remove test db | |
echo "DROP DATABASE test;" | mysql -u root | |
#Set root password | |
echo "UPDATE mysql.user SET Password=PASSWORD('$1') WHERE User='root';" | mysql -u root | |
#Flush privs | |
echo "FLUSH PRIVILEGES;" | mysql -u root | |
} | |
function mysql_tune { | |
# Tunes MySQL's memory usage to utilize the percentage of memory you specify, defaulting to 40% | |
# $1 - the percent of system memory to allocate towards MySQL | |
if [ ! -n "$1" ]; | |
then PERCENT=40 | |
else PERCENT="$1" | |
fi | |
MEM=$(get_physical_memory) | |
MYMEM=$((MEM*PERCENT/100)) # how much memory we'd like to tune mysql with | |
MYMEMCHUNKS=$((MYMEM/4)) # how many 4MB chunks we have to play with | |
# mysql config options we want to set to the percentages in the second list, respectively | |
OPTLIST=(key_buffer sort_buffer_size read_buffer_size read_rnd_buffer_size myisam_sort_buffer_size query_cache_size) | |
DISTLIST=(75 1 1 1 5 15) | |
for opt in ${OPTLIST[@]}; do | |
sed -i -e "/\[mysqld\]/,/\[.*\]/s/^$opt/#$opt/" /etc/my.cnf | |
done | |
for i in ${!OPTLIST[*]}; do | |
val=$(echo | awk "{print int((${DISTLIST[$i]} * $MYMEMCHUNKS/100))*4}") | |
if [ $val -lt 4 ] | |
then val=4 | |
fi | |
config="${config}\n${OPTLIST[$i]} = ${val}M" | |
done | |
sed -i -e "s/\(\[mysqld\]\)/\1\n$config\n/" /etc/my.cnf | |
touch /tmp/restart-mysqld | |
} | |
function mysql_create_database { | |
# $1 - the mysql root password | |
# $2 - the db name to create | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_database() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_database() requires the name of the database as the second argument" | |
return 1; | |
fi | |
echo "CREATE DATABASE $2;" | mysql -u root -p"$1" | |
} | |
function mysql_create_user { | |
# $1 - the mysql root password | |
# $2 - the user to create | |
# $3 - their password | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_user() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_user() requires username as the second argument" | |
return 1; | |
fi | |
if [ ! -n "$3" ]; then | |
echo "mysql_create_user() requires a password as the third argument" | |
return 1; | |
fi | |
echo "CREATE USER '$2'@'localhost' IDENTIFIED BY '$3';" | mysql -u root -p"$1" | |
} | |
function mysql_grant_user { | |
# $1 - the mysql root password | |
# $2 - the user to bestow privileges | |
# $3 - the database | |
if [ ! -n "$1" ]; then | |
echo "mysql_create_user() requires the root pass as its first argument" | |
return 1; | |
fi | |
if [ ! -n "$2" ]; then | |
echo "mysql_create_user() requires username as the second argument" | |
return 1; | |
fi | |
if [ ! -n "$3" ]; then | |
echo "mysql_create_user() requires a database as the third argument" | |
return 1; | |
fi | |
echo "GRANT ALL PRIVILEGES ON $3.* TO '$2'@'localhost';" | mysql -u root -p"$1" | |
echo "FLUSH PRIVILEGES;" | mysql -u root -p"$1" | |
} | |
############################################################## | |
# PHP functions | |
############################################################## | |
function php_install_with_apache { | |
install_testing_repo | |
yum -y install php php-mysql php-cli mod_php php-cli php-gd | |
touch /tmp/restart-httpd | |
} | |
function php_tune { | |
# Tunes PHP to utilize up to nMB per process, 32 by default | |
if [ ! -n "$1" ]; | |
then MEM="32" | |
else MEM="${1}" | |
fi | |
sed -i'-orig' "s/memory_limit = [0-9]\+M/memory_limit = ${MEM}M/" /etc/php.ini | |
touch /tmp/restart-httpd | |
} | |
############################################################## | |
# Apache functions | |
############################################################## | |
function apache_install { | |
# installs the system default apache2 MPM | |
yum -y install httpd | |
sed -i -e 's/^#NameVirtualHost \*:80$/NameVirtualHost *:80/' /etc/httpd/conf/httpd.conf | |
mkdir -p /etc/httpd/{sites-enabled,sites-available} | |
echo " | |
Include sites-enabled/*" >> /etc/httpd/conf/httpd.conf | |
mv /var/www/html /var/www/public | |
if [ ! -n "$1" ]; then | |
sed -i "s/root@localhost/$1/g" /etc/httpd/conf/httpd.conf | |
fi | |
sed -i "s/\/var\/www\/html/\/var\/www\/public/g" /etc/httpd/conf/httpd.conf | |
touch /tmp/restart-httpd | |
echo "#!/bin/bash | |
# Configures a VirtualHost | |
# \$1 - required - the hostname of the virtualhost to create | |
if [ ! -n \"\$1\" ]; then | |
echo \"apache_virtualhost() requires the hostname as the first argument\" | |
return 1; | |
fi | |
if [ -e \"/etc/httpd/sites-enabled/\${1}\" ]; then | |
echo /etc/httpd/sites-enabled/\${1} already exists | |
return; | |
fi | |
mkdir -p /var/www/\$1/htdocs /var/log/httpd/\$1 | |
echo \"<VirtualHost *:80>\" > /etc/httpd/sites-available/\${1} | |
echo \" ServerName \$1\" >> /etc/httpd/sites-available/\${1} | |
echo \" DocumentRoot /var/www/\$1/htdocs/\" >> /etc/httpd/sites-available/\${1} | |
echo \" <Directory /var/www/\$1/htdocs/>\" >> /etc/httpd/sites-available/\${1} | |
echo \" AllowOverride All\" >> /etc/httpd/sites-available/\${1} | |
echo \" </Directory>\" >> /etc/httpd/sites-available/\${1} | |
echo \" ErrorLog /var/log/httpd/\$1/error.log\" >> /etc/httpd/sites-available/\${1} | |
echo \" CustomLog /var/log/httpd/\$1/access.log combined\" >> /etc/httpd/sites-available/\${1} | |
echo \"</VirtualHost>\" >> /etc/httpd/sites-available/\${1} | |
ln -s /etc/httpd/sites-available/\${1} /etc/httpd/sites-enabled/\${1} | |
ln -s /var/log/httpd/\$1 /var/www/\$1/logs | |
touch /tmp/restart-httpd" > /usr/local/bin/apache_virtualhost | |
chmod a+x /usr/local/bin/apache_virtualhost | |
} | |
function apache_tune { | |
# Tunes Apache's memory to use the percentage of RAM you specify, defaulting to 40% | |
# $1 - the percent of system memory to allocate towards Apache | |
if [ ! -n "$1" ]; | |
then PERCENT=40 | |
else PERCENT="$1" | |
fi | |
yum -y install httpd | |
PERPROCMEM=10 # the amount of memory in MB each apache process is likely to utilize | |
MEM=$(get_physical_memory) | |
MAXCLIENTS=$((MEM*PERCENT/100/PERPROCMEM)) # calculate MaxClients | |
MAXCLIENTS=${MAXCLIENTS/.*} # cast to an integer | |
sed -i -e "s/\(^[ \t]*\(MaxClients\|ServerLimit\)[ \t]*\)[0-9]*/\1$MAXCLIENTS/" /etc/httpd/conf/httpd.conf | |
touch /tmp/restart-httpd | |
} | |
function apache_virtualhost_from_rdns { | |
# Configures a VirtualHost using the rdns of the first IP as the ServerName | |
apache_virtualhost $(get_rdns_primary_ip) | |
} | |
############################################################## | |
# Ruby functions | |
############################################################## | |
function rvm_install { | |
if [ ! -n "$1" ]; | |
then RUBYVERSION=2.0.0 | |
else RUBYVERSION="$1" | |
fi | |
curl https://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable | |
source /etc/profile | |
rvm requirements | |
rvm install "$RUBYVERSION" | |
rvm use "$RUBYVERSION" --default | |
} | |
function passenger_install { | |
gem install passenger | |
yum -y install curl-devel httpd-devel | |
passenger-install-apache2-module --auto | |
PATH=`gem environment gemdir` | |
echo "LoadModule passenger_module $PATH/gems/passenger-4.0.41/buildout/apache2/mod_passenger.so | |
<IfModule mod_passenger.c> | |
PassengerRoot $PATH/gems/passenger-4.0.41 | |
PassengerDefaultRuby $PATH/wrappers/ruby | |
</IfModule>" > /etc/httpd/conf.d/passenger.conf | |
} | |
############################################################## | |
# Utility functions | |
############################################################## | |
function system_primary_ip { | |
# returns the primary IP assigned to eth0 | |
echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }') | |
} | |
function get_rdns { | |
# calls host on an IP address and returns its reverse dns | |
if [ ! -e /usr/bin/host ]; then | |
yum -y install bind-utils > /dev/null | |
fi | |
echo $(host $1 | awk '/pointer/ {print $5}' | sed 's/\.$//') | |
} | |
function get_rdns_primary_ip { | |
# returns the reverse dns of the primary IP assigned to this system | |
echo $(get_rdns $(system_primary_ip)) | |
} |
This file contains hidden or 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 | |
source <ssinclude StackScriptID="8644"> | |
# <UDF name="db_password" Label="MySQL root Password" /> | |
# <UDF name="db_name" Label="Create Database" default="" example="Optionally create this database" /> | |
# <UDF name="db_user" Label="Create MySQL User" default="" example="Optionally create this user" /> | |
# <UDF name="db_user_password" Label="MySQL User's Password" default="" example="User's password" /> | |
# <UDF name="apache_server_admin" Label="Apache Server Admin Email" example="Server Admin Email" /> | |
logfile="/root/log.txt" | |
export logfile | |
system_update >> $logfile | |
install_basics >> $logfile | |
echo "System Updated" >> $logfile | |
postfix_install_loopback_only >> $logfile | |
echo "postfix_install_loopback_only" >> $logfile | |
mysql_install "$DB_PASSWORD" >> $logfile | |
mysql_tune 40 >> $logfile | |
echo "Mysql installed" >> $logfile | |
mysql_create_database "$DB_PASSWORD" "$DB_NAME" >> $logfile | |
mysql_create_user "$DB_PASSWORD" "$DB_USER" "$DB_USER_PASSWORD" >> $logfile | |
mysql_grant_user "$DB_PASSWORD" "$DB_USER" "$DB_NAME" >> $logfile | |
php_install_with_apache >> $logfile | |
php_tune >> $logfile | |
echo "Php installed" >> $logfile | |
apache_install "$APACHE_SERVER_ADMIN" >> $logfile | |
apache_tune 40 >> $logfile | |
apache_virtualhost_from_rdns >> $logfile | |
apache_mod_deflate_config >> $logfile | |
echo "apache installed" >> $logfile | |
goodstuff >> $logfile | |
echo "goodstuff installed" >> $logfile | |
#installing ruby | |
rvm_install >> $logfile | |
echo "Installed rvm" >> $logfile | |
gem install rails >> $logfile | |
echo "Installed rails" >> $logfile | |
passenger_install >> $logfile | |
echo "Built apache2 passenger module" >> $logfile | |
restartServices >> $logfile | |
echo "StackScript Finished!" >> $logfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment