Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 53 additions & 48 deletions ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ apt-packages-repository() {

# Add a Launchpad PPA as a software source.
apt-packages-ppa() {
apt-packages-repository \
"deb http://ppa.launchpad.net/$1/ubuntu lucid main" \
"deb-src http://ppa.launchpad.net/$1/ubuntu lucid main" \
"$2" "$3"
which 'add-apt-repository' >/dev/null || (apt-packages-update; apt-packages-install 'python-software-properties')
add-apt-repository "$1"
}

# Perform a non-interactive `apt-get` command.
Expand All @@ -98,6 +96,11 @@ apt-packages-update() {
apt-non-interactive update
}

# Upgrade `aptitude` packages without any prompts.
apt-packages-upgrade() {
apt-non-interactive upgrade
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already available as system-upgrade which I've tweaked in e879932. Be careful as it's known to break VirtualBox Guest Additions when a new kernel is installed and the DKMS module is not re-compiled. dotless-de/vagrant-vbguest is good at handling this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like having both, since you may want to perform a system upgrade, but not a dist upgrade, or maybe indeed a full dist upgrade. Not necessarily as relevant to vagrant (since you could just change your VagrantFile to reference a newer dist image), but relevant within the scope of general server administration if this script were used while managing non-vagrant boxes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[..] relevant within the scope of general server administration if this script were used while managing non-vagrant boxes.

Makes sense, however I don't intend for this script to be used for server administration. A full distribution upgrade would also likely involve prompts/dialogs so running non-interactive is going to break the system.


# Perform an unattended installation of package(s).
apt-packages-install() {
apt-non-interactive install "$@"
Expand Down Expand Up @@ -217,37 +220,21 @@ apache-sites-create() {
local apache_site_user
local apache_site_group
local apache_site_config
local cgi_action
local cgi_apache_path
local cgi_system_path
local code_block
apache_site_name="$1"
apache_site_path="${2:-/$apache_site_name}"
apache_site_user="${3:-$apache_site_name}"
apache_site_group="${4:-$apache_site_user}"
apache_site_config="/etc/apache2/sites-available/$apache_site_name"
cgi_apache_path="/cgi-bin/"
cgi_system_path="$apache_site_path/.cgi-bin/"
# Create the /.cgi-bin/ directory and set permissions for SuExec.
$SUDO mkdir -p "$cgi_system_path"
$SUDO chmod 0755 "$cgi_system_path"
# Define a new virtual host with mod_fastcgi configured to use SuExec.
# Define a new virtual host
code_block=$( cat <<-EOD
<IfModule mod_fastcgi.c>
FastCgiWrapper /usr/lib/apache2/suexec
FastCgiConfig -pass-header HTTP_AUTHORIZATION -autoUpdate -killInterval 120 -idle-timeout 30
</IfModule>

<VirtualHost *:80>
DocumentRoot ${apache_site_path}

LogLevel debug
ErrorLog /var/log/apache2/error.${apache_site_name}.log
CustomLog /var/log/apache2/access.${apache_site_name}.log combined

SuexecUserGroup ${apache_site_user} ${apache_site_group}
ScriptAlias ${cgi_apache_path} ${cgi_system_path}

# Do not use kernel sendfile to deliver files to the client.
EnableSendfile Off

Expand All @@ -262,30 +249,29 @@ EOD
cgi_action="php-fcgi"
code_block=$( cat <<-EOD
${code_block}

<IfModule mod_fastcgi.c>
<Location ${cgi_apache_path}${cgi_action}>
SetHandler fastcgi-script
Options +ExecCGI +FollowSymLinks
Order Allow,Deny
Allow from all
<FilesMatch \.php$>
SetHandler php5-fcgi
</FilesMatch>
<Location "/fastcgiphp">
Order Deny,Allow
Deny from All
# Prevent accessing this path directly
Allow from env=REDIRECT_STATUS
</Location>
AddHandler ${cgi_action} .php
Action ${cgi_action} ${cgi_apache_path}${cgi_action}
Action php5-fcgi /fastcgiphp
Alias /fastcgiphp /usr/local/bin/${apache_site_name}.fpm_external
FastCgiExternalServer /usr/local/bin/${apache_site_name}.fpm_external -socket /var/run/php5-fpm-${apache_site_name}.sock -pass-header Authorization
</IfModule>
EOD
)
$SUDO cat > "$cgi_system_path$cgi_action" <<-EOD
#!/bin/bash

export PHP_FCGI_CHILDREN=4
export PHP_FCGI_MAX_REQUESTS=200

export PHPRC="${cgi_system_path}php.ini"

exec ${PHP}
EOD
$SUDO chmod 0755 "$cgi_system_path$cgi_action"
# Run PHP-FPM as the selected user and group.
$SUDO sed \
-e 's#^\(\[[A-Za-z0-9-]\+\]\)$#['"$apache_site_name"']#g' \
-e 's#^\(user\)\s*=\s*[A-Za-z0-9-]\+#\1 = '"$apache_site_user"'#g' \
-e 's#^\(group\)\s*=\s*[A-Za-z0-9-]\+#\1 = '"$apache_site_group"'#g' \
-e 's#^\(listen\)\s*=\s*.\+$#\1 = '/var/run/php5-fpm-"$apache_site_name"'.sock#g' \
<'/etc/php5/fpm/pool.d/www.conf' >'/etc/php5/fpm/pool.d/'"$apache_site_name"'.conf'
fi
code_block=$( cat <<-EOD
${code_block}
Expand All @@ -295,11 +281,6 @@ EOD
)
# Write site configuration to Apache.
echo "$code_block" | $SUDO tee "$apache_site_config" > /dev/null
# Configure permissions for /.cgi-bin/ and SuExec.
$SUDO chown -R "$apache_site_user":"$apache_site_group" "$cgi_system_path"
# Update SuExec to accept the new document root for this website.
grep "$apache_site_path" '/etc/apache2/suexec/www-data' > /dev/null || \
( $SUDO sed -e '1s#^#'"$apache_site_path""\n"'#' -i '/etc/apache2/suexec/www-data' > /dev/null )
}

# Restart the Apache server and reload with new configuration.
Expand Down Expand Up @@ -396,18 +377,23 @@ ${code_block}

# Pass PHP scripts to PHP-FPM.
location ~ \.php\$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_param PATH_INFO \$fastcgi_path_info;
fastcgi_param PATH_TRANSLATED \$document_root\$fastcgi_path_info;
fastcgi_param HTTP_AUTHORIZATION \$http_authorization;
fastcgi_pass unix:/var/run/php5-fpm-${nginx_site_name}.sock;
}
EOD
)
# Run PHP-FPM as the selected user and group.
$SUDO sed \
-e 's#^\(\[[A-Za-z0-9-]\+\]\)$#['"$nginx_site_name"']#g' \
-e 's#^\(user\)\s*=\s*[A-Za-z0-9-]\+#\1 = '"$nginx_site_user"'#g' \
-e 's#^\(group\)\s*=\s*[A-Za-z0-9-]\+#\1 = '"$nginx_site_group"'#g' \
-i '/etc/php5/fpm/pool.d/www.conf'
-e 's#^\(listen\)\s*=\s*.\+$#\1 = '/var/run/php5-fpm-"$nginx_site_name"'.sock#g' \
<'/etc/php5/fpm/pool.d/www.conf' >'/etc/php5/fpm/pool.d/'"$nginx_site_name"'.conf'
fi
code_block=$( cat <<-EOD
${code_block}
Expand Down Expand Up @@ -460,6 +446,11 @@ php-pecl-install() {
done
}

# Restart the php5-fpm server and reload with new configuration.
php5-fpm-restart() {
system-service php5-fpm restart
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 3458e89 as php-fpm-restart as I wish to keep version names out of function names.


# }}}

# {{{ MySQL
Expand All @@ -470,6 +461,20 @@ mysql-database-create() {
mysql -u root -e "CREATE DATABASE IF NOT EXISTS \`$1\` CHARACTER SET ${2:-utf8} COLLATE '${3:-utf8_general_ci}'"
}

# Grant access to database (creates user if not exist)
# mysql-database-add-user databasename username password fromhost
mysql-database-add-user() {
log-operation "$FUNCNAME" "$@"
mysql -u root -e "GRANT ALL PRIVILEGES ON \`$1\`.* TO '$2'@'${4:-localhost}' IDENTIFIED BY '$3' WITH GRANT OPTION; FLUSH PRIVILEGES;"
}

# Load data from SQL file
# mysql-load-data databasename sqlfile
mysql-load-data() {
log-operation "$FUNCNAME" "$@"
mysql -u root "$1" < "$2"
}

# Restore a MySQL database from an archived backup.
mysql-database-restore() {
log-operation "$FUNCNAME" "$@"
Expand Down