Slowbro used tail whip, Your defences were lowered

Scripting Virtual Hosts From Apache To Nginx

A project at work came up in which we are now migrating out sites from apache to nginx. And when you have 50+ wordpress sites hosted, as well as the company website on the same box, you kinda want to get it right the first time.
So after a couple minutes, searching ddg (because privacy is needed), and could only find apache to nginx.conf pre-written scripts, and next to nothing to do with virtual-hosts. I was gob smacked, could not believe that no-one has had this problem. So i set out to instead of taking ten days to manually do it, i’ll take one day and whip up a script!

Apache-to-Nginx-hosts.sh:

#!/bin/bash
# Apache virtual hosts => Nginx Virtual Hosts
# Usage: ./apache-to-nginx-hosts.sh /etc/apache2/sites-available/www.slowb.ro > /etc/nginx/sites-available/www.slowb.ro
# Author: Slowpoke
# Website: www.slowb.ro
# Download script: http://sprunge.us/AdXH or http://pastebin.com/RbTqBnsm

# Global Vars
port=8080
pattern=".*#.*"

# One function to rule them all...
# We run this after we have read a file into Memory
function setvars {
    mainnowwwhost=`echo $mainhost | sed 's/www.//'`
    phpfooter="\tlocation ~ \.php$ {\n\t\tinclude /etc/nginx/fastcgi.conf;\n\t\tfastcgi_pass unix:/tmp/php.socket;\n\t}\n}"
    logging="\taccess_log  /var/log/nginx/$servername-access.log;\n\terror_log  /var/log/nginx/$servername-error.log;\n"
    location="\tlocation / {\n\t\tindex  index.html index.htm index.php;\n\t\tssi on;\n\t\tssi_silent_errors on;\n\t"
    ssi_shtml="\n\t\tlocation  ~* \.shtml$ {
                    \n\t\t\tadd_header Content-Type  text/html;\n\t\t\t}"

    statslocation="\n\tlocation  /stats {\n\t\tauth_basic_user_file  $wwwdir/passwd;\n\t}"
    root="\troot /var/www/$servername/$wordpress;"
    stats="\trewrite  "^/stats$" /stats/ redirect;\n\trewrite ^/stats/(.*) http://$servername/awstats/awstats.pl?config=$servername permanent;"
    header="server {\n\tlisten\t$port;"
    server_name="\n\tserver_name $servername;"
}

# Function to readin all the required data to set it up properly
function readfile {
IFS=$'\n'
    for line in `cat $1`; do
        # Check for commented out lines
        if [[ ! $line =~ $pattern ]];then
            #echo $line #DEBUG
        case "$line" in
           *wordpress*)
                wordpress="wordpress";
            ;;
            *ServerName*)
                testcase=`echo $line | awk '{print $2}'`
                # We define and check for www in the hostname.
                if [[ $testcase =~ .*"www".* ]]; then
                    servername=$testcase
                    mainhost=$testcase
                else
                    servername="www.$testcase"
                    mainhost=$testcase
                fi
            ;;
            *ServerAlias*)
                newline="`echo $line | awk '{print $2}' | sed 's/www.//'`"
                found=false
                # We check for duplicates when reading in the variables.
                for i in ${serveralias[@]}; do
                    if [[ $i == $newline ]]; then
                        found=true
                    fi
                done
                if [[ $found == false ]]; then
                # This += creates/adds on to the existing array of values.
                    serveralias+=($newline)
                fi
            ;;
            *DocumentRoot*)
                wwwdir=`echo $line | awk '{print $2}'`
            ;;
            *AddType*)
                if [[ $line =~ .*"shtml".* ]]; then
                    ssi=true
                fi
            ;;
            *Redirect*)
                if [[ `echo $line | awk '{print $2}'` =~ .*"/stats".* ]]; then
                    stats=true
                fi
            ;;
        esac
        fi
    done
    # Now that everything is setup for the file:
    setvars
}
# Call the function with reading the file parsed. TODO: Try Catch on file/$1 existting
readfile $1

# Set up redirects to www hostnames. and then www redirect for non main -> main domain.
for host in "${serveralias[@]}"
do
    if [[ "www.$host" != "$mainhost" ]]; then
        echo -e "server {\n\t\tlisten 8080;\n\t\tserver_name $host;\n\t\trewrite ^(.*)$ http://www.$host\$1;\n} "
        echo -e "server {\n\t\tlisten 8080;\n\t\tserver_name www.$host;\n\t\trewrite ^(.*)$ http://$mainhost\$1;\n}"
    else
        echo -e "server {\n\t\tlisten 8080;\n\t\tserver_name $host;\n\t\trewrite ^(.*)$ http://www.$host\$1;\n} "
    fi
done
# echo Header
echo -e $header
# echo the root folder
echo -e $root
# echo the server_name
echo -e $server_name
# echo the Logging
echo -e $logging
# Location is left open to add other checks / file types
echo -e $location
if [[ $ssi == true ]]; then
    echo -e $ssi_shtml
fi
# Close Location
echo -e "}"
# Stats
if [[ stats == true ]]; then
    echo -e $statslocation
fi
if [ -z $wordpress ]; then
    echo -e $stats
fi
# Footer
echo -e $phpfooter

I hope that this ends up making someones day. This will work for the basic of apache configurations.

Dat Side Note: As an FYI, we do not use apache/nginx to do redirects besides from hostnames, as this server only hosts wordpress sites, and wordpress does all its redirecting internally and through the .htaccess file i did not need and re-direct support. If I end up needing it ill come back to it, but for the foreseeable future I should not.