Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit ffa21c48e1319c5d1027b26572ab22ccae74baac @postpostmodern committed Sep 29, 2008
Showing with 378 additions and 0 deletions.
  1. +63 −0 README.markdown
  2. +241 −0 a2mksite.sh
  3. +29 −0 templates/apache.conf
  4. +15 −0 templates/logrotate.conf
  5. +30 −0 templates/public/index.html
@@ -0,0 +1,63 @@
+# a2mksite: Apache 2 vhost generator script
+
+I'm not a big fan of using control panels like Plesk and cPanel to manage my Apache sites. But setting up a new site by hand can take _minutes_. I don't have that kind of time! :)
+
+This script allows you to type one command to set up an entire site including:
+
+* Creation of the root html directory (using an optional skeleton structure for a new site) for the site
+* Creation of a site-specific Apache log directory with recommended permissions
+* Creation of empty access.log and error.log files with recommended permissions
+* Creation of an Apache configuration file in your 'sites-available' directory (using an optional user-template)
+* Creation of a logrotate configuration file for rotating your site's Apache logs (using an optional user-template)
+* Symlinking the Apache conf file into the 'sites-enabled' directory via a2ensite
+* Gracefully restarting Apache
+
+__PLEASE NOTE:__ This script is intended to be a __time saver__. It's not a substitute for understanding your server's configuration. You should understand what these configuration files do before using this script.
+
+# BETA WARNING
+
+___This script works fine for me (using Ubuntu 8.04, Apache 2.2 and logrotate on Slicehost), but server setups vary greatly. USE THIS SCRIPT AT YOUR OWN RISK!! It mainly just creates some files and restarts Apache, but there is always risk involved with running a script (esp. using sudo). I AM NOT RESPONSIBLE FOR DAMAGE CAUSED BY THIS SCRIPT.__
+
+If you're a web server admin, you may very well know more about configuring Apache and logrotate than I do. If you find any flaws with this script or have any recommendations as to how this script can be improved, please fork it and send me a pull request.
+
+# Installation
+
+This script was created for use with Apache 2.x on Ubuntu. It will probably work on most other Linux distros with a little tweaking of the configuration at the top of the a2mksite.sh script. See "Affected Files and Directories" below for specifics.
+
+* Place this a2mksite directory somewhere on your server (I like /usr/local)
+* Make sure the a2mksite.sh script is executable by you: `chmod 744 a2mksite/a2mksite.sh`
+* Optionally, create Apache and logrotate configuration templates and site skeleton (see below)
+* You might also want to create an alias to the script.
+
+A bash alias would look like this:
+
+ alias a2mksite='sudo /usr/local/a2mksite/a2mksite.sh'
+
+# Usage
+
+ sudo a2mksite/a2mksite.sh domain.com
+
+This script creates several files owned by root; so, sudo is necessary.
+
+# Affected Files and Directories
+
+By default, these are the directories and files created. This is how I like my web server set up. You may prefer a different structure. The structure can be customized by opening the script and changing the configuration variables near the top of the script.
+
+ /var/www/sites/example.com/
+ /var/www/sites/example.com/public/
+ /var/www/sites/example.com/log/
+ /var/www/sites/example.com/log/access.log
+ /var/www/sites/example.com/log/error.log
+
+ /etc/apache2/sites-available/example.com
+ /etc/apache2/sites-enabled/example.com
+ /etc/logrotate.d/sites
+ /etc/logrotate.d/sites.d/example.com.conf
+
+# Using Configuration File Templates
+
+If you would prefer to customize the config files that are generated by this script, copy the 'templates' directory to your home directory and rename it to 'a2mksite_templates'. Then, open the conf files in your text editor and go to town.
+
+# Using a Custom Site Placeholder
+
+Similarly, the contents of the public directory (your default web documents) can be customized by copying the 'templates' directory to your home directory, renaming it to 'a2mksite_templates', and editing the contents of the public directory within it.
@@ -0,0 +1,241 @@
+#!/bin/bash -
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+if [[ -z "$1" ]]
+then
+ echo "Usage: $0 domain.com"
+ exit
+fi
+
+if [[ -z "$SUDO_USER" ]]
+ then
+ echo "Use sudo"
+ exit
+fi
+
+CHOICES="Overwrite Skip"
+
+# The domain being created
+DOMAIN=$1
+ESCAPED_DOMAIN=${DOMAIN/\./\\\\.}
+
+# This directory holds the templates for the apache and logrotate conf files
+DEFAULT_TEMPLATE_DIR="$(dirname $0)/templates"
+USER_TEMPLATE_DIR="/home/$SUDO_USER/a2mksite_templates"
+
+# This is where we'll create the conf files
+APACHE_CONF_DIR="/etc/apache2/sites-available"
+LOGROTATE_CONF_DIR="/etc/logrotate.d"
+LOGROTATE_SITES_DIR="$LOGROTATE_CONF_DIR/sites.d"
+
+# These are the conf files
+APACHE_CONF="$APACHE_CONF_DIR/$DOMAIN"
+LOGROTATE_CONF="$LOGROTATE_SITES_DIR/$DOMAIN.conf"
+
+# This is where the site itself will be created
+SITES_DIR="/var/www/sites"
+SITE_DIR="$SITES_DIR/$DOMAIN"
+PUBLIC_DIR="$SITE_DIR/public"
+LOG_DIR="$SITE_DIR/log"
+
+# FUNCTIONS ==========================================================
+
+function mk_site {
+ [[ ! -d $SITE_DIR ]] || rm -R "$SITE_DIR"
+ echo "Creating $PUBLIC_DIR..."
+ mkdir -p "$PUBLIC_DIR"
+ echo "done!"
+ if [[ -d $PUBLIC_TEMPLATE ]]
+ then
+ echo "Copying public $PUBLIC_TEMPLATE to $SITE_DIR..."
+ cp -Rp "$PUBLIC_TEMPLATE" "$SITE_DIR"
+ echo "done!"
+ fi
+ chown -R $SUDO_USER:$SUDO_GID "$SITE_DIR"
+}
+
+function mk_logs {
+ if [[ ! -d $LOG_DIR ]]
+ then
+ echo "Creating log dir: $LOG_DIR..."
+ mkdir "$LOG_DIR"
+ echo "done!"
+ fi
+
+ touch "$LOG_DIR/access_log"
+ touch "$LOG_DIR/error_log"
+
+ echo "Chowning log dir to root..."
+ chown -R 0:$SUDO_GID "$LOG_DIR"
+ echo "done!"
+
+ echo "Chmoding log dir to 1750..."
+ chmod -R 750 "$LOG_DIR"
+ chmod 1750 "$LOG_DIR"
+ echo "done!"
+}
+
+function mk_apache_conf {
+ echo "Creating apache config file: $APACHE_CONF..."
+ sed -e "s:LOG_DIR:$LOG_DIR:g"\
+ -e "s:SITE_DIR:$SITE_DIR:g"\
+ -e "s:USER:$SUDO_USER:g"\
+ -e "s:GROUP:$SUDO_GID:g"\
+ -e "s:ESCAPED_DOMAIN:$ESCAPED_DOMAIN:g"\
+ -e "s:THE_DOMAIN:$DOMAIN:g"\
+ "$APACHE_TEMPLATE" > "$APACHE_CONF"
+ echo "done!"
+
+ echo "Chowning apache config file to root..."
+ chown 0:$SUDO_GID "$APACHE_CONF"
+ echo "done!"
+
+ echo "Chmoding apache config file to 740..."
+ chmod 740 "$APACHE_CONF"
+ echo "done!"
+}
+
+function mk_logrotate_conf {
+ echo "Creating logrotate config file: $FILE..."
+ sed -e "s:LOG_DIR:$LOG_DIR:g" "$LOGROTATE_TEMPLATE" > "$LOGROTATE_CONF"
+ echo "done!"
+
+ echo "Chowning logrotate config file to root..."
+ chown 0:$SUDO_GID "$LOGROTATE_CONF"
+ echo "done!"
+
+ echo "Chmoding logrotate file to 740..."
+ chmod 740 "$LOGROTATE_CONF"
+ echo "done!"
+}
+
+# Determine template directories ========================
+
+# Choose the user's public/ template first
+# If it doesn't exist, use the default
+if [[ -d "$USER_TEMPLATE_DIR/public" ]]
+ then PUBLIC_TEMPLATE="$USER_TEMPLATE_DIR/public"
+elif [[ -d "$DEFAULT_TEMPLATE_DIR/public" ]]
+ then PUBLIC_TEMPLATE="$DEFAULT_TEMPLATE_DIR/public"
+else
+ echo "No public/ template can be found. Aborted."
+ exit
+fi
+
+# Choose the user's apache.conf template first
+# If it doesn't exist, use the default
+if [[ -f "$USER_TEMPLATE_DIR/apache.conf" ]]
+ then APACHE_TEMPLATE="$USER_TEMPLATE_DIR/apache.conf"
+elif [[ -f "$DEFAULT_TEMPLATE_DIR/apache.conf" ]]
+ then APACHE_TEMPLATE="$DEFAULT_TEMPLATE_DIR/apache.conf"
+else
+ echo "No apache.conf template can be found. Aborted."
+ exit
+fi
+
+# Choose the user's logrotate.conf template first
+# If it doesn't exist, use the default
+if [[ -f "$USER_TEMPLATE_DIR/logrotate.conf" ]]
+ then LOGROTATE_TEMPLATE="$USER_TEMPLATE_DIR/logrotate.conf"
+elif [[ -f "$DEFAULT_TEMPLATE_DIR/apache.conf" ]]
+ then LOGROTATE_TEMPLATE="$DEFAULT_TEMPLATE_DIR/logrotate.conf"
+else
+ echo "No logrotate.conf template can be found. Aborted."
+ exit
+fi
+
+# Create necessary directories ==========================
+
+if [[ ! -d "$APACHE_CONF_DIR" ]]
+ then
+ mkdir -p "$APACHE_CONF_DIR"
+ chgrp $SUDO_GID "$APACHE_CONF_DIR"
+fi
+if [[ ! -d "$LOGROTATE_SITES_DIR" ]]
+ then
+ mkdir -p "$LOGROTATE_SITES_DIR"
+ chgrp $SUDO_GID "$LOGROTATE_SITES_DIR"
+fi
+if [[ ! -d "$SITES_DIR" ]]
+ then
+ mkdir -p "$SITES_DIR"
+ chown $SUDO_USER:$SUDO_GID "$SITES_DIR"
+fi
+
+# Create logrotate conf file for all sites ===============
+
+if [[ ! -f "$LOGROTATE_CONF_DIR/sites" ]]
+ then
+ echo "include $LOGROTATE_SITES_DIR" > "$LOGROTATE_CONF_DIR/sites"
+ chgrp $SUDO_GID "$LOGROTATE_CONF_DIR/sites"
+fi
+
+
+# Run those functions ====================================
+
+if [[ -d "$SITE_DIR" ]]
+ then
+ echo "$SITE_DIR already exists..."
+ select choice in $CHOICES; do
+ if [ $choice ]; then
+ case $choice in
+ Overwrite)
+ mk_site
+ break;;
+ Skip) break;;
+ esac
+ else
+ echo 'Invalid selection'
+ fi
+ done
+else
+ mk_site
+fi
+
+mk_logs
+
+if [[ -f "$APACHE_CONF" ]]
+ then
+ echo "$APACHE_CONF already exists..."
+ select choice in $CHOICES; do
+ if [ $choice ]; then
+ case $choice in
+ Overwrite)
+ mk_apache_conf
+ break;;
+ Skip) break;;
+ esac
+ else
+ echo 'Invalid selection'
+ fi
+ done
+ else
+ mk_apache_conf
+fi
+
+if [[ -f "$LOGROTATE_CONF" ]]
+ then
+ echo "$LOGROTATE_CONF already exists..."
+ select choice in $CHOICES; do
+ if [ $choice ]; then
+ case $choice in
+ Overwrite)
+ mk_logrotate_conf
+ break;;
+ Skip) break;;
+ esac
+ else
+ echo 'Invalid selection'
+ fi
+ done
+ else
+ mk_logrotate_conf
+fi
+
+
+# FINISH UP ==================================
+echo "Enabling Site..."
+/usr/sbin/a2ensite $DOMAIN
+echo "Restarting Apache..."
+/usr/sbin/apache2ctl graceful
+echo "ALL DONE!"
@@ -0,0 +1,29 @@
+<VirtualHost *>
+
+ ServerName THE_DOMAIN
+ ServerAlias www.THE_DOMAIN
+ DocumentRoot SITE_DIR/public
+
+ RewriteEngine On
+ RewriteCond %{HTTP_HOST} ^www\.ESCAPED_DOMAIN$ [NC]
+ RewriteRule ^(.*)$ http://THE_DOMAIN$1 [R=301,L]
+
+ UseCanonicalName Off
+
+ CustomLog LOG_DIR/access.log combined
+ ErrorLog LOG_DIR/error.log
+
+ <IfModule mod_ssl.c>
+ SSLEngine off
+ </IfModule>
+
+ <Directory "SITE_DIR/public">
+ Options FollowSymLinks
+ AllowOverride All
+ Order allow,deny
+ Allow from all
+ AcceptPathInfo On
+ </Directory>
+
+</VirtualHost>
+
@@ -0,0 +1,15 @@
+LOG_DIR/*.log {
+ weekly
+ missingok
+ rotate 12
+ compress
+ delaycompress
+ notifempty
+ create 640 root adm
+ sharedscripts
+ postrotate
+ if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
+ /etc/init.d/apache2 reload > /dev/null
+ fi
+ endscript
+}
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <style type="text/css" media="screen">
+ body {
+ background: #f0f0f0;
+ font: normal 14px sans-serif;
+ }
+ #content {
+ width: 400px;
+ margin: 50px auto;
+ }
+ h1 {
+ font-size: 18px;
+ }
+
+ </style>
+ <title>Web Site Coming Soon</title>
+</head>
+
+<body>
+<div id="content">
+ <h1>Site Coming Soon</h1>
+ <p>This site has been successfully generated, but there's nothing here yet.</p>
+</div>
+</body>
+</html>

0 comments on commit ffa21c4

Please sign in to comment.