Monday, September 5, 2011

Install Aegir Master Slave with Pressflow Platform on CentOS 5 in EC2

Most of the documentation found on this subject are targeted towards Ubuntu. I had quite a time rounding up data for a complete CentOS configuration and ended up writing two scripts for handling this task.

I am currently setting this up in Amazon EC2, complete with auto scaling. I will post more of the advanced configuration when I have that auto scaling portion of this project complete.

The first script is one that can be run on a base CentOS 5.4 ami and will need to be run on both the master and slave instances. Changing this to accomodate CentOS 5.6 or 6.0 is as easy as modifying a few of the paths in the below scripts.

I am using Zend Server Enterprise for my PHP stack, and am therefore pointing to the appropriate php.ini in my scripts as well as the Zend php binaries for path creation.

Upon execution of these scripts on a CentOS system, you will have the following:

- Apache Server
- Zend Server Enterprise Edition
- Varnish
- Aegir Master
- Aegir Slave(s)



Application Server Setup Script

- For this to work properly, you will need to first create an rsa key pair and store it on Amazon S3. You will need to update the last portion of the below script to match your S3 repo.
- You will also need the keys from your Amazon account in order to download these keys automagically to this instance.

#!/bin/sh
# Aegir-Base-Install.sh
#
#
# Created by Shawn LoPresto on 9/1/11.


export WEBHOME=/var/aegir


#Verify that this is a RedHat system
if ! [ -s /etc/redhat-release ] #assuming this is sufficient
then
echo " ERR: This is not a Redhat based distribution. Quitting"
exit1
fi


#Global Master/Slave Setup#
#Aegir#
#CentOS 5.4#


#setup aegir user with new home dir and permissions
echo " INFO: User creation"
useradd --home-dir $WEBHOME aegir
gpasswd -a aegir apache
chmod -R 755 $WEBHOME
! [ -d $WEBHOME ] && mkdir $WEBHOME
chown -R aegir:apache $WEBHOME


# Install Zend-Server
#Add the following repo
cat >> /etc/yum.repos.d/zend.repo <> /etc/profile < /dev/null
if ! [ $? -eq 0 ]
then
echo "alias apachectl='/usr/local/zend/bin/apachectl'" >> $WEBHOME/.bashrc
fi
. $WEBHOME/.bashrc

source $WEBHOME/.bashrc

#enable apache at boot
chkconfig httpd on

#setup aegir user with new home dir and permissions
echo " INFO: User creation"
useradd --home-dir $WEBHOME aegir
gpasswd -a aegir apache
chmod -R 755 $WEBHOME
! [ -d $WEBHOME ] && mkdir $WEBHOME
chown -R aegir:apache $WEBHOME

#grant permission to apachectl for aegir user
grep aegir /etc/sudoers > /dev/null
if ! [ $? -eq 0 ]
then
echo "aegir ALL=NOPASSWD: /usr/sbin/apachectl" >> /etc/sudoers
sed -i 's/^Defaults requiretty/#Defaults requiretty/g' /etc/sudoers
fi

if ! [ -d /etc/httpd/conf.d/aegir.conf ]
then
ln -s $WEBHOME/config/apache.conf /etc/httpd/conf.d/aegir.conf
fi

######################
###POSTFIX CONFIG#####
######################

#Setup postfix relay
cat >> /etc/postfix/main.cf <
> /etc/postfix/sasl_passwd < websmtp.myserver.com:587 myemailuser:mypassword
EOF

postmap /etc/postfix/sasl_passwd
/etc/init.d/postfix restart

######################
##Aegir User SSH Key##
######################

su - aegir
mkdir ~/.ssh
cd ~/.ssh
s3cmd --configure
s3cmd get s3://Aegir_Setup/aegir_keys/id_rsa ~/.ssh/id_rsa
s3cmd get s3://Aegir_Setup/aegir_keys/id_rsa.pub ~/.ssh/authorized_keys
chmod 700 /var/aegir/.ssh
chmod 600 /var/aegir/.ssh/*

exit

Varnish



** Do not waste time trying to make Varnish cache your https pages.  HTTPS requests should be sent directly to your web server, where the certificate can be verified.


#!/bin/sh
#  
#
#  Created by Shawn LoPresto on 9/3/11.
#make sure varnish is installed
yum -y install varnish

# Create varnish config
echo " Creating Varnish Config"
cat >> /etc/varnish/default.vcl <<EOF
backend default {
.host = "127.0.0.1";
.port = "8082";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
}
sub vcl_recv {
// Remove has_js and Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
// Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
// Remove empty cookies.
if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}

// Skip the Varnish cache for install, update, and cron
if (req.url ~ "install\.php|update\.php|cron\.php") {
return (pass);
}

// Cache all requests by default, overriding the
// standard Varnish behavior.
// if (req.request == "GET" || req.request == "HEAD") {
//   return (lookup);
// }
}
sub vcl_hash {
if (req.http.Cookie) {
set req.hash += req.http.Cookie;
}
}
EOF

#Edit default port in /etc/sysconfig/varnish
sed -i 's/DAEMON_OPTS="-a\ :6081/DAEMON_OPTS="-a\ :80/' /etc/sysconfig/varnish

##Should read
#DAEMON_OPTS="-a :80 \
#-T localhost:6082 \
#-f /etc/varnish/default.vcl \
#-u varnish -g varnish \
##Next switch limits size of the cache
#-s file,/var/lib/varnish/varnish_storage.bin,1G"

##Change Apache default listening ports
sed -i 's/Listen\ 80/Listen\ 8082/' /etc/httpd/conf/httpd.conf

#Listen 8082
chkconfig varnish on
chkconfig httpd on
/etc/init.d/httpd restart
/etc/init.d/varnish restart

#####Aegir Specific#######
##Each Aegir server will need to be configured with a default port of 8082 for http (within the UI configuration).##
##########################

###Remainder of configuration is done on Aegir Master and requires making some additions to the settings.php#########

Aegir Master

The below script will require some user input for the initial Aegir configuration, but will walk you through the complete install.

#!/bin/sh
#  Aegir Master Install
#  
#
#  Created by Shawn LoPresto on 9/1/11.
######################
#####MASTER ONLY######
######################

#Setup Profile Variables
echo " INFO: Switching to aegir user"
su -c -l aegir '
export DRUSH_VERSION=7.x-4.5
export WEBHOME=/var/aegir
export HOME=$WEBHOME
export drush="$HOME/bin/drush/drush" 
export DRUPAL_VER=6.x

export AEGIR_DB_HOST=aegir-db.mydomain.com ##Update with your DB host FQDN
export AEGIR_HOST=aegir-master.mydomain.com ##Update with the FQDN for the Aegir master
export AEGIR_DB_USER=aegirmaster ###update with privileged MySQL admin account

mkdir ~/bin/
cd $HOME/bin
gunzip -c drush-$DRUSH_VERSION.tar.gz | tar -xf -
rm -rf drush-$DRUSH_VERSION.tar.gz
mkdir $WEBHOME/make-files

#Create Pressflow Make file
cat >> $WEBHOME/make-files/pressflow.make <<EOF
; DRUPAL VERSION
core = 6.x

; CORE MODULES
projects[pressflow][type] = "core"
projects[pressflow][download][type] = "get"
projects[pressflow][download][url] = "http://files.pressflow.org/pressflow-6-current.tar.gz"

; DEVELOPMENT
projects[devel][subdir] = "contrib"
projects[backup_migrate][subdir] = "contrib"

; PERFORMANCE
projects[memcache][subdir] = "contrib"
projects[varnish][subdir] = "contrib"

; UTILITY
projects[apachesolr][subdir] = "contrib"
projects[libraries][subdir] = "contrib"
projects[jquery_ui][subdir] = "contrib"
projects[modalframe][subdir] = "contrib"

; UI
projects[admin][subdir] = "contrib"
projects[admin][version] = "2.0-beta3"
projects[vertical_tabs][subdir] = "contrib"
projects[wysiwyg][subdir] = "contrib"

; KEY MODULES
projects[cck][subdir] = "contrib"
projects[views][subdir] = "contrib"
projects[token][subdir] = "contrib"
projects[pathauto][subdir] = "contrib"

; FILE/IMAGE HANDLING
projects[filefield][subdir] = "contrib"
projects[imagefield][subdir] = "contrib"
projects[imagecache][subdir] = "contrib"
projects[imageapi][subdir] = "contrib"
projects[transliteration][subdir] = "contrib"

; THEMES

projects[tao][location] = http://code.developmentseed.org/fserver
projects[rubik][location] = http://code.developmentseed.org/fserver

; OTHER FILES

; ApacheSolr
libraries[SolrPhpClient][download][type] = "get"
libraries[SolrPhpClient][directory_name] = "SolrPhpClient"
libraries[SolrPhpClient][destination] = "modules/contrib/apachesolr"

; jQuery UI
libraries[jquery_ui][download][type] = "get"
libraries[jquery_ui][download][url] = "http://jquery-ui.googlecode.com/files/jquery.ui-1.6.zip"
libraries[jquery_ui][directory_name] = "jquery.ui"
libraries[jquery_ui][destination] = "modules/contrib/jquery_ui"

; TinyMCE 
libraries[tinymce][download][type] = "get"
libraries[tinymce][directory_name] = "tinymce"
EOF

#set up the alias for drush
grep drush $HOME/.bashrc > /dev/null
if ! [ $? -eq 0 ]
then
echo "alias drush='/var/aegir/bin/drush/drush'" >> $HOME/.bashrc
fi
. $HOME/.bashrc

source $HOME/.bashrc

cd $HOME

#get the latest version of drush before starting 
$drush self-update

echo " INFO: Installing Drupal $DRUPAL_VER"

$drush --destination=$HOME dl drupal-$DRUPAL_VER

echo " INFO: Installing drupal module : provision"
$drush dl --destination=$HOME/.drush provision-6.x

echo  " INFO: Running hostmaster install"
$drush --aegir_db_host=$AEGIR_DB_HOST --aegir_db_user=$AEGIR_DB_USER hostmaster-install 

#install update module and run updates
echo " INFO: Updating Drupal installation"
##enable the update module
$drush -y -r /var/aegir/hostmaster-6.x-1.3 -l http://$AEGIR_HOST enable update
##do the update
$drush -y -r /var/aegir/hostmaster-6.x-1.3 -l http://$AEGIR_HOST up

#update drush make
echo " INFO: Updating drush make:
cd /var/aegir/.drush
tar xzvf drush_make-6.x-2.3.tar.gz
rm -f drush_make-6.x-2.3.tar.gz

echo " INFO: Installation complete. Please visit http://$AEGIR_HOST to verify"
'

That is it for the install. Now go to your browser and hit the newly created URL to begin configuring Aegir.

I will create another post with the additional changes needed to actually utilize Varnish for your Drupal sites.


No comments:

Post a Comment