Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created April 27, 2012 15:03
Show Gist options
  • Save StanAngeloff/2509979 to your computer and use it in GitHub Desktop.
Save StanAngeloff/2509979 to your computer and use it in GitHub Desktop.
Quick and dirty Apache + PHP 4.4.x provisioning for Vagrant/Ubuntu 10.04
#!/usr/bin/env bash
set -e
PHP_VERSION='4.4.9'
MYSQL_DBNAME="$1"
# {{{ Functions
# Perform an unattended installation of package(s).
install() {
sudo \
DEBIAN_FRONTEND=noninteractive \
apt-get \
-o Dpkg::Options::='--force-confdef' \
-o Dpkg::Options::='--force-confold' \
-f -y -q \
install \
$*
}
# Set up an IP as a DNS name server if not already present in 'resolv.conf'.
nameserver() {
grep "$1" '/etc/resolv.conf' > /dev/null || \
( echo "nameserver $1" | sudo tee -a '/etc/resolv.conf' > /dev/null )
}
# Set up a specific two-letter country code as the preferred `aptitude` mirror.
apt-mirror() {
sudo sed -i -e "s#\w\+\.archive\.ubuntu#$1.archive.ubuntu#g" /etc/apt/sources.list
}
# Create /opt and set owner, if missing.
create-opt-directory() {
local logged_user
logged_user="$( whoami )"
sudo mkdir -p /opt
sudo chown "$logged_user":"$logged_user" /opt
}
# Download a version of PHP as an archive.
php-download() {
local download_file
download_file="$( mktemp -d -t php-XXXXXXXX )/php-$1.tar.gz"
wget -c -O "$download_file" "http://www.php.net/get/php-$1.tar.gz/from/www.php.net/mirror"
echo "$download_file"
}
# Configure and install a PHP version in /opt.
php-install() {
local download_file
local extract_path
local sources_path
local opt_install
download_file="$( php-download "$1" )"
extract_path="$( mktemp -d -t php-XXXXXXXX )"
tar -zxf "$download_file" -C "$extract_path"
sources_path="$extract_path/php-$1"
opt_install="/opt/php-$1"
create-opt-directory
( cd "$sources_path" && \
./configure \
--prefix="$opt_install" \
--with-config-file-path="$opt_install/etc" \
--with-config-file-scan-dir="$opt_install/etc/conf.d" \
--enable-fastcgi \
--enable-force-cgi-redirect \
${@:2} && \
make && \
make install
)
cp "$sources_path/php.ini-recommended" "$opt_install/etc/php.ini"
}
# Create a new Apache site for /vagrant and set up PHP as Fast-CGI.
setup-vagrant-site() {
local cgi_action
cgi_action="php-${1//./-}-fcgi"
local code_block
code_block=$( cat <<-EOD
<VirtualHost *:80>
DocumentRoot /vagrant
LogLevel debug
ErrorLog /var/log/apache2/error.log
<Directory /vagrant>
Options All
AllowOverride All
</Directory>
<IfModule mod_fastcgi.c>
<Location /.$cgi_action>
SetHandler fastcgi-script
Options ExecCGI FollowSymLinks
Order Allow,Deny
Allow from all
</Location>
AddHandler $cgi_action .php
Action $cgi_action /.$cgi_action
</IfModule>
</VirtualHost>
EOD
)
echo "$code_block" | sudo tee '/etc/apache2/sites-available/vagrant' > /dev/null
cat > "/vagrant/.$cgi_action" <<-EOD
#!/bin/bash
export PHPRC="/opt/php-$1/etc"
export PHP_FCGI_MAX_REQUESTS=499
exec /opt/php-$1/bin/php
EOD
chmod +x "/vagrant/.$cgi_action"
}
# }}}
# Use Google Public DNS for resolving domain names.
# The default is host-only DNS which may not be installed.
nameserver '8.8.8.8'
nameserver '8.8.4.4'
# Use a local Ubuntu mirror, results in faster downloads.
apt-mirror 'bg'
# Update packages cache.
sudo apt-get -q update
# Install VM packages.
install build-essential flex apache2-mpm-worker mysql-server-5.1 libcurl4-openssl-dev libjpeg-dev libpng-dev libmcrypt-dev libmhash-dev libmysqlclient-dev libapache2-mod-fastcgi
php-install "$PHP_VERSION" '--with-zlib --with-openssl --with-curl --with-gd --with-iconv --enable-mbstring --with-mcrypt --with-mhash --with-mysql'
# Replace the default Apache site.
setup-vagrant-site "$PHP_VERSION"
sudo a2dissite default default-ssl
sudo a2ensite vagrant
# Allow modules for Apache.
sudo a2enmod actions
# Restart Apache web service.
sudo service apache2 restart
# Restore MySQL from latest available backup (if any).
mysql -u root -e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DBNAME\` CHARACTER SET utf8 COLLATE 'utf8_general_ci'"
if [ -d '/backups/' ]; then
MYSQL_BACKUP=$( find /backups/ -maxdepth 1 -type f -regextype posix-basic -regex '^.*[0-9]\{8\}-[0-9]\{4\}.tar.gz$' | \
sort -g | \
tail -1 )
if [ ! -z "$MYSQL_BACKUP" ]; then
tar -zxf "$MYSQL_BACKUP" -O | mysql -u root "$MYSQL_DBNAME"
fi
fi
@StanAngeloff
Copy link
Author

What's wrong with sh?

Chef is unnecessary boilerplate which makes you rely on other people's recipes to configure your own server. Also, Ruby for provisioning a Linux box, where's the fun?

@skanev
Copy link

skanev commented Apr 28, 2012

Fair enough.

It took me weeks to figure out how to use chef. But it was totally worth it in the end. Although, I understand that you don't want to invest.

But come now, there can be fun in Ruby too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment