Created
July 17, 2013 09:00
-
-
Save urbels/6018964 to your computer and use it in GitHub Desktop.
thx to slowpoke Slowpoke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment