Ubuntu server 12.04/apache2

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m (MediaWiki Caching)
 
(24 intermediate revisions by one user not shown)
Line 1: Line 1:
==Configuring an Apache server==
+
[[ubuntu_server_12.04|Ubuntu Server 12.04]]
 
<source lang="bash">
 
<source lang="bash">
apt-get install apache2 libapache2-mod-php5
+
apt-get install apache2 libapache2-mod-php5 libapache2-svn php5-gd php-apc
# enable modrewrite
+
ln -s ../mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
+
 
</source>
 
</source>
  
 +
==Configuring an Apache server==
 
Configuration is in <code>/etc/apache2</code>
 
Configuration is in <code>/etc/apache2</code>
  
 +
===Initial Setup===
 +
<source lang="bash">
 +
cat <<EOF >>/etc/apache2/mods-available/cgi.load
 +
AddHandler cgi-script .cgi
 +
EOF
 +
 +
cat <<EOF >/etc/apache2/mods-available/proxy.conf
 +
<IfModule mod_proxy.c>
 +
  ProxyVia On
 +
  ProxyRequests On
 +
  <Proxy *>
 +
    Order deny,allow
 +
    Deny from all
 +
    Allow from 127.0.0.1
 +
    Allow from .attie.co.uk
 +
  </Proxy>
 +
</IfModule>
 +
EOF
 +
 +
cat <<EOF >>/etc/apache2/conf.d/compressed
 +
<Files *.js.gz>
 +
  AddType text/javascript .gz
 +
  AddEncoding gzip .gz
 +
</Files>
 +
<Files *.jgz>
 +
  AddType text/javascript .jgz
 +
  AddEncoding gzip .jgz
 +
</Files>
 +
<Files *.css.gz>
 +
  AddType text/css .gz
 +
  AddEncoding gzip .gz
 +
</Files>
 +
<Files *.png.gz>
 +
  AddType image/png .gz
 +
  AddEncoding gzip .gz
 +
</Files>
 +
EOF
 +
 +
cat <<EOF >>/etc/apache2/conf.d/security
 +
# prevent access to sensitive files
 +
RedirectMatch 404 /\\.svn(/|$)
 +
RedirectMatch 404 /\\.git(ignore)?(/|$)
 +
RedirectMatch 404 /\\.ht
 +
 +
# prevent directory listing by default
 +
Options -Indexes
 +
EOF
 +
 +
ln -sf ../mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
 +
ln -sf ../mods-available/authz_svn.load /etc/apache2/mods-enabled/authz_svn.load
 +
ln -sf ../mods-available/dav.load /etc/apache2/mods-enabled/dav.load
 +
ln -sf ../mods-available/dav_fs.load /etc/apache2/mods-enabled/dav_fs.load
 +
ln -sf ../mods-available/dav_fs.conf /etc/apache2/mods-enabled/dav_fs.conf
 +
ln -sf ../mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
 +
ln -sf ../mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf
 +
ln -sf ../mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
 +
ln -sf ../mods-available/proxy_ftp.load /etc/apache2/mods-enabled/proxy_ftp.load
 +
ln -sf ../mods-available/proxy_ftp.conf /etc/apache2/mods-enabled/proxy_ftp.conf
 +
ln -sf ../mods-available/proxy_connect.load /etc/apache2/mods-enabled/proxy_connect.load
 +
</source>
 +
 +
===Site Configuration===
 
Store site content in <code>/home/www</code>
 
Store site content in <code>/home/www</code>
 
<source lang="bash">
 
<source lang="bash">
Line 18: Line 79:
 
! Domain / Subdomain !! !! Root Dir
 
! Domain / Subdomain !! !! Root Dir
 
|-
 
|-
|align="right"| <code>www.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/www</code>
+
|align="right"| <code>www.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/htdocs</code>
 
|-
 
|-
 
|align="right"| <code>wiki.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/wiki</code>
 
|align="right"| <code>wiki.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/wiki</code>
 
|-
 
|-
|align="right"| <code>joke.test.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/test/joke</code>
+
|align="right"| <code>joke.test.attie.co.uk</code> || &rarr; || <code>/home/www/attie.co.uk/test.joke</code>
 
|}
 
|}
  
Line 44: Line 105:
 
Add a site file in <code>/etc/apache2/sites-available</code>.
 
Add a site file in <code>/etc/apache2/sites-available</code>.
 
Enable the site by symlinking it into <code>/etc/apache2/sites-enabled</code>.
 
Enable the site by symlinking it into <code>/etc/apache2/sites-enabled</code>.
 +
 +
If there is a reason to load one site before another, then use a prefix other than 50.
 +
<source lang="bash">
 +
ln -s /home/www/attie.co.uk/htdocs/.htinfo/config /etc/apache2/sites-enabled/50-www.attie.co.uk
 +
</source>
  
 
====Sample config====
 
====Sample config====
Line 51: Line 117:
 
   ServerName attie.co.uk
 
   ServerName attie.co.uk
 
   ServerAlias www.attie.co.uk
 
   ServerAlias www.attie.co.uk
   DocumentRoot "/home/www/attie.co.uk/www"
+
   DocumentRoot "/home/www/attie.co.uk/htdocs"
   CustomLog "/home/www/attie.co.uk/www/.htinfo/access.log" combined
+
   CustomLog "/home/www/attie.co.uk/htdocs/.htinfo/access.log" combined
   ErrorLog "/home/www/attie.co.uk/www/.htinfo/error.log"
+
   ErrorLog "/home/www/attie.co.uk/htdocs/.htinfo/error.log"
 
</VirtualHost>
 
</VirtualHost>
 +
</source>
 +
 +
==Site Enable/Disable script==
 +
<source lang="bash">
 +
#!/bin/bash
 +
 +
APACHE_SITE_DIR="/etc/apache2/sites-enabled"
 +
MY_DIR="/home/www"
 +
 +
function usage {
 +
        echo "$0 <enable|disable> <domain>"
 +
        exit 1
 +
}
 +
 +
function validate {
 +
        local me=$(readlink -f "$0")
 +
        local mydir=$(dirname $me)
 +
 +
        if [ "$mydir" != "$MY_DIR" ]; then
 +
                echo "$0: must be run from '$MY_DIR'..."
 +
                exit 1
 +
        fi
 +
 +
        local fqdn=$1
 +
        local tld=$(echo $fqdn | rev | cut -d . -f 1 | rev)
 +
        local tld_n=2
 +
        if [ "$tld" == "uk" ]; then
 +
                tld_n=3
 +
        fi
 +
        tld=$(echo $fqdn | rev | cut -d . -f -$tld_n | rev)
 +
 +
        local subdomain=$(echo $fqdn | sed -re "s/\.?$tld\$//")
 +
        local subdomain_path=$(echo $subdomain | awk -F . '{i=NF;do{printf $(i);if(i>1){printf FS;}else{printf "\n";}}while(--i>0);}')
 +
        if [ "$subdomain" == "" ]; then
 +
                local config="$mydir/$tld/htdocs/.htinfo/config"
 +
        else
 +
                local config="$mydir/$tld/$subdomain_path/.htinfo/config"
 +
        fi
 +
 +
        if [ ! -e "$config" ]; then
 +
                echo "Config file missing... ('$config')"
 +
                return 1
 +
        fi
 +
 +
        CONFIG="$config"
 +
 +
        local config_link=""
 +
        local wcard="$APACHE_SITE_DIR/*-$fqdn"
 +
        for file in "$APACHE_SITE_DIR/$fqdn" $wcard; do
 +
                if [ -e "$file" ]; then
 +
                        config_link=$file
 +
                        break
 +
                fi
 +
        done
 +
 +
        if [ "$config_link" == "" ]; then
 +
                config_link="$APACHE_SITE_DIR/50-$fqdn"
 +
        else
 +
                if [ ! -h "$config_link" ]; then
 +
                        echo "Apache config exists, and is not a symlink... ('$config_link')"
 +
                        exit 1
 +
                fi
 +
        fi
 +
 +
        CONFIG_LINK="$config_link"
 +
 +
        return 0
 +
}
 +
 +
function enable {
 +
        validate $1 || exit 1
 +
        ln -sf "$CONFIG" "$CONFIG_LINK" || {
 +
                echo "Faailed to create link..."
 +
                exit 1
 +
        }
 +
        service apache2 reload || {
 +
                echo "Failed to reload apache..."
 +
        }
 +
        echo "Successfully enabled $1"
 +
}
 +
 +
function disable {
 +
        validate $1 || exit 1
 +
        rm -f "$CONFIG_LINK" || {
 +
                echo "Faailed to remove link..."
 +
                exit 1
 +
        }
 +
        service apache2 reload || {
 +
                echo "Failed to reload apache..."
 +
        }
 +
        echo "Successfully DISABLED $1"
 +
}
 +
 +
if [ "$#" != "2" ]; then
 +
        usage
 +
fi
 +
case "$1" in
 +
        "enable" | "e")
 +
                enable $2
 +
                ;;
 +
        "disable" | "d")
 +
                disable $2
 +
                ;;
 +
esac
 +
</source>
 +
 +
 +
==MediaWiki Caching==
 +
Set the following in <code>LocalSettings.php</code>
 +
<source lang="php">
 +
$wgMainCacheType = CACHE_ACCEL;
 
</source>
 
</source>

Latest revision as of 21:19, 8 August 2012

Ubuntu Server 12.04

apt-get install apache2 libapache2-mod-php5 libapache2-svn php5-gd php-apc

Contents

[edit] Configuring an Apache server

Configuration is in /etc/apache2

[edit] Initial Setup

cat <<EOF >>/etc/apache2/mods-available/cgi.load
AddHandler cgi-script .cgi
EOF
 
cat <<EOF >/etc/apache2/mods-available/proxy.conf
<IfModule mod_proxy.c>
  ProxyVia On
  ProxyRequests On
  <Proxy *>
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Allow from .attie.co.uk
  </Proxy>
</IfModule>
EOF
 
cat <<EOF >>/etc/apache2/conf.d/compressed
<Files *.js.gz>
  AddType text/javascript .gz
  AddEncoding gzip .gz
</Files>
<Files *.jgz>
  AddType text/javascript .jgz
  AddEncoding gzip .jgz
</Files>
<Files *.css.gz>
  AddType text/css .gz
  AddEncoding gzip .gz
</Files>
<Files *.png.gz>
  AddType image/png .gz
  AddEncoding gzip .gz
</Files>
EOF
 
cat <<EOF >>/etc/apache2/conf.d/security
# prevent access to sensitive files
RedirectMatch 404 /\\.svn(/|$)
RedirectMatch 404 /\\.git(ignore)?(/|$)
RedirectMatch 404 /\\.ht
 
# prevent directory listing by default
Options -Indexes
EOF
 
ln -sf ../mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
ln -sf ../mods-available/authz_svn.load /etc/apache2/mods-enabled/authz_svn.load
ln -sf ../mods-available/dav.load /etc/apache2/mods-enabled/dav.load
ln -sf ../mods-available/dav_fs.load /etc/apache2/mods-enabled/dav_fs.load
ln -sf ../mods-available/dav_fs.conf /etc/apache2/mods-enabled/dav_fs.conf
ln -sf ../mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
ln -sf ../mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf
ln -sf ../mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
ln -sf ../mods-available/proxy_ftp.load /etc/apache2/mods-enabled/proxy_ftp.load
ln -sf ../mods-available/proxy_ftp.conf /etc/apache2/mods-enabled/proxy_ftp.conf
ln -sf ../mods-available/proxy_connect.load /etc/apache2/mods-enabled/proxy_connect.load

[edit] Site Configuration

Store site content in /home/www

mkdir /home/www
chown www-data:www-data -R /home/www

Path goes with TLD first, and a few other rules. Access and Error logs are stored in the TLD's directory, with the subdomain prefixed. E.g:

Domain / Subdomain Root Dir
www.attie.co.uk /home/www/attie.co.uk/htdocs
wiki.attie.co.uk /home/www/attie.co.uk/wiki
joke.test.attie.co.uk /home/www/attie.co.uk/test.joke

As the default configuration will deny access to .ht*, we can use .htinfo to store the configuration and logs.

Content ${ROOTDIR}/
Config ${ROOTDIR}/.htinfo/config
Access Log ${ROOTDIR}/.htinfo/access.log
Error Log ${ROOTDIR}/.htinfo/error.log

[edit] Start/Restart

service apache2 restart

[edit] Setup sites

Add a site file in /etc/apache2/sites-available. Enable the site by symlinking it into /etc/apache2/sites-enabled.

If there is a reason to load one site before another, then use a prefix other than 50.

ln -s /home/www/attie.co.uk/htdocs/.htinfo/config /etc/apache2/sites-enabled/50-www.attie.co.uk

[edit] Sample config

<VirtualHost *:80>
  ServerAdmin webmaster@attie.co.uk
  ServerName attie.co.uk
  ServerAlias www.attie.co.uk
  DocumentRoot "/home/www/attie.co.uk/htdocs"
  CustomLog "/home/www/attie.co.uk/htdocs/.htinfo/access.log" combined
  ErrorLog "/home/www/attie.co.uk/htdocs/.htinfo/error.log"
</VirtualHost>

[edit] Site Enable/Disable script

#!/bin/bash
 
APACHE_SITE_DIR="/etc/apache2/sites-enabled"
MY_DIR="/home/www"
 
function usage {
        echo "$0 <enable|disable> <domain>"
        exit 1
}
 
function validate {
        local me=$(readlink -f "$0")
        local mydir=$(dirname $me)
 
        if [ "$mydir" != "$MY_DIR" ]; then
                echo "$0: must be run from '$MY_DIR'..."
                exit 1
        fi
 
        local fqdn=$1
        local tld=$(echo $fqdn | rev | cut -d . -f 1 | rev)
        local tld_n=2
        if [ "$tld" == "uk" ]; then
                tld_n=3
        fi
        tld=$(echo $fqdn | rev | cut -d . -f -$tld_n | rev)
 
        local subdomain=$(echo $fqdn | sed -re "s/\.?$tld\$//")
        local subdomain_path=$(echo $subdomain | awk -F . '{i=NF;do{printf $(i);if(i>1){printf FS;}else{printf "\n";}}while(--i>0);}')
        if [ "$subdomain" == "" ]; then
                local config="$mydir/$tld/htdocs/.htinfo/config"
        else
                local config="$mydir/$tld/$subdomain_path/.htinfo/config"
        fi
 
        if [ ! -e "$config" ]; then
                echo "Config file missing... ('$config')"
                return 1
        fi
 
        CONFIG="$config"
 
        local config_link=""
        local wcard="$APACHE_SITE_DIR/*-$fqdn"
        for file in "$APACHE_SITE_DIR/$fqdn" $wcard; do
                if [ -e "$file" ]; then
                        config_link=$file
                        break
                fi
        done
 
        if [ "$config_link" == "" ]; then
                config_link="$APACHE_SITE_DIR/50-$fqdn"
        else
                if [ ! -h "$config_link" ]; then
                        echo "Apache config exists, and is not a symlink... ('$config_link')"
                        exit 1
                fi
        fi
 
        CONFIG_LINK="$config_link"
 
        return 0
}
 
function enable {
        validate $1 || exit 1
        ln -sf "$CONFIG" "$CONFIG_LINK" || {
                echo "Faailed to create link..."
                exit 1
        }
        service apache2 reload || {
                echo "Failed to reload apache..."
        }
        echo "Successfully enabled $1"
}
 
function disable {
        validate $1 || exit 1
        rm -f "$CONFIG_LINK" || {
                echo "Faailed to remove link..."
                exit 1
        }
        service apache2 reload || {
                echo "Failed to reload apache..."
        }
        echo "Successfully DISABLED $1"
}
 
if [ "$#" != "2" ]; then
        usage
fi
case "$1" in
        "enable" | "e")
                enable $2
                ;;
        "disable" | "d")
                disable $2
                ;;
esac


[edit] MediaWiki Caching

Set the following in LocalSettings.php

$wgMainCacheType = CACHE_ACCEL;
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox