Skip to content

Instantly share code, notes, and snippets.

@bborysenko
Created September 25, 2013 10:16
Show Gist options
  • Save bborysenko/6697710 to your computer and use it in GitHub Desktop.
Save bborysenko/6697710 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
###
# This script configures Upstart and Nginx to support your OTRS install with
# fcgi.
#
# You should configure the group as which your webserver runs (usually www-data
# for Ubuntu), the homedirbase for your users (usually /home) and the directory
# where you want your sockets created (/var/run/otrs by default).
#
# Author: Mark Schouten <mark@tuxis.nl>
# License: GPL
fatal() {
echo "FATAL ERROR"
echo "$1"
exit 1
}
warning() {
echo "WARNING"
echo "$1"
}
# Your homedir base and socketdir
SOCKETDIR=/var/run/otrs
WWWDATAGROUP=webserv
for USER in $@; do
getent passwd ${USER} > /dev/null
[ $? -eq 0 ] || fatal "User ${USER} not found!"
HOMEDIR=$(getent passwd ${USER} | cut -d':' -f6)
FCGIBIN=${HOMEDIR}/bin/fcgi-bin
HTTPDIR=${HOMEDIR}/var/httpd/htdocs
# Check if the user has OTRS in it's homedir
[ -d ${FCGIBIN} ] || fatal "User ${USER} doesn't have an OTRS dir"
# Fetch the vhostname
if [ ! -r ${HOMEDIR}/vhost ]; then
warning "${USER} doesn't have the /vhost file in it's homedir"
echo -n "What hostname will the vhost of this user use? "
read VHOST
echo ${VHOST} >> ${HOMEDIR}/vhost
fi
VHOST=`cat ${HOMEDIR}/vhost`
[ -z ${VHOST} ] && continue
# Create an initscript for each script in fcgi-bin
for FILE in `ls ${FCGIBIN}`; do
SERVICEDIR=/etc/service/${USER}-${FILE}
# Create a directory for this user in /etc/init/
mkdir -p ${SERVICEDIR}
SERVICERUN=${SERVICEDIR}/run
echo -n > ${SERVICERUN}
[ -x ${FCGIBIN}/${FILE} ] || chmod +x ${FCGIBIN}/${FILE}
if [ "${FILE}" = "installer.pl" ]; then
cat > ${SERVICERUN} <<EOB
#!/bin/bash
# OTRS Service
# description "OTRS Fcgi Service for ${VHOST}/${FILE}"
# author "Mark Schouten <mark@tuxis.nl>"
# Check if socketdir exists
[ -d ${SOCKETDIR} ] || mkdir -p ${SOCKETDIR}
exec /usr/bin/spawn-fcgi -n -s ${SOCKETDIR}/${VHOST}.${FILE}.sock -M 0775 -u ${USER} -g ${WWWDATAGROUP} -G ${WWWDATAGROUP} -n -C 2 -- ${FCGIBIN}/${FILE}
EOB
else
cat > ${SERVICERUN} <<EOB
#!/bin/bash
# OTRS Service
# description "OTRS Fcgi Service for ${VHOST}/${FILE}"
# author "Mark Schouten <mark@tuxis.nl>"
# Check if socketdir exists
[ -d ${SOCKETDIR} ] || mkdir -p ${SOCKETDIR}
exec /usr/bin/spawn-fcgi -n -s ${SOCKETDIR}/${VHOST}.${FILE}.sock -M 0775 -u ${USER} -g ${WWWDATAGROUP} -G ${WWWDATAGROUP} -n -C 2 -- ${FCGIBIN}/${FILE}
EOB
fi
echo "Created service config for $FILE"
chmod +x ${SERVICERUN}
done
echo "Create vhost-config for ${VHOST}"
cat > /etc/nginx/sites-available/${VHOST} <<EOB
server {
listen 80;
server_name ${VHOST};
root ${HTTPDIR};
index index.html;
location /otrs-web {
gzip on;
alias ${HTTPDIR};
}
location ~ ^/otrs/(.*\.pl)(/.*)?$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
fastcgi_pass unix:${SOCKETDIR}/${VHOST}.\$1.sock;
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME ${FCGIBIN}/\$1;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME \$fastcgi_script_name;
fastcgi_param REQUEST_URI \$request_uri;
fastcgi_param DOCUMENT_URI \$document_uri;
fastcgi_param DOCUMENT_ROOT \$document_root;
fastcgi_param SERVER_PROTOCOL \$server_protocol;
fastcgi_param REMOTE_ADDR \$remote_addr;
fastcgi_param REMOTE_PORT \$remote_port;
fastcgi_param SERVER_ADDR \$server_addr;
fastcgi_param SERVER_PORT \$server_port;
fastcgi_param SERVER_NAME \$server_name;
}
}
EOB
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment