Skip to content

Instantly share code, notes, and snippets.

@hangingman
Last active May 5, 2020 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hangingman/7b7df9ddcb25c43078e1 to your computer and use it in GitHub Desktop.
Save hangingman/7b7df9ddcb25c43078e1 to your computer and use it in GitHub Desktop.
fswikiの構築自動化スクリプト
#!/bin/bash +x
# launch here fswiki
FSWIKI_SETUP_DIR=/home/www/app
FSWIKI_HOME=$FSWIKI_SETUP_DIR/fswiki
# launch here fswiki static data
DOCUMENT_ROOT=/var/www/html
# static data access path
CONST_DATA_PATH=data
FSWIKI_DOCUMENT_ROOT=${DOCUMENT_ROOT}/${CONST_DATA_PATH}
# apache user name
[ -e /etc/redhat-release ] && APACHE_USER='apache' || APACHE_USER='www-data'
# apachectl
[ -e /etc/redhat-release ] && APACHECTL='apachectl' || APACHECTL='apache2ctl'
# service name
[ -e /etc/redhat-release ] && SERVICE='httpd' || SERVICE='apache2'
if [ $# -ne 1 ]; then
echo "usage: ./fswiki_setup.sh <option>" 1>&2
echo "" 1>&2
echo "--install download or reinstall fswiki" 1>&2
echo "--reconf re setup fsiki config " 1>&2
exit 1
fi
if [ `whoami` != 'root' ]; then
echo '[ERROR] Please exec this script by root'
exit 1
fi
### update sources routine
function update()
{
# install apache2
if [ -e /etc/redhat-release ]; then
yum install -y httpd
yum install -y epel-release
yum install -y mod_perl perl-CGI
else
apt-get install libapache2-mod-perl2 apache2
fi
# if not exist, create dir
if [ ! -d $FSWIKI_SETUP_DIR ]; then
mkdir -p $FSWIKI_SETUP_DIR;
chgrp -R ${APACHE_USER} ${FSWIKI_SETUP_DIR};
chmod g=rxs,o= $FSWIKI_SETUP_DIR;
fi
cd $FSWIKI_SETUP_DIR
# remove file if exist
if [ -d fswiki ]; then
rm -rf fswiki/ *.zip
fi
# git checkout fswiki sources
if git --version >/dev/null 2>&1; then
git clone https://github.com/hangingman/fswiki.git
else
echo "[ERROR] git is not found, aborting..."
exit 1
fi
# change group and permission
chgrp $APACHE_USER fswiki
chmod g=rxs,o= fswiki
cd fswiki
}
### update config routine
function update_config()
{
# set apache2 config file
CONF_NAME=fswiki
# apache2 version check
APACHE_VER=`${APACHECTL} -v | awk 'NR%2==1' | awk '{print $3}'`
echo "Your server uses:" ${APACHE_VER}
# distribution check
if [[ `uname -a` = *Ubuntu* ]]; then
# this is Ubuntu
CONF_PATH=/etc/apache2/conf-available/${CONF_NAME}.conf
ubuntu_config || exit 1
# enable module
a2enmod perl
elif [ -e /etc/redhat-release ]; then
# this is Centos
CONF_PATH=/etc/httpd/conf.d/${CONF_NAME}.conf
centos_config || exit 1
else
# this is Debian
CONF_PATH=/etc/apache2/conf.d/${CONF_NAME}.conf
debian_config || exit 1
# enable module
a2enmod perl
fi
# setup
bash -x ${FSWIKI_HOME}/setup.sh ${FSWIKI_HOME}
# move static data DocumentRoot
if [ ! -d ${FSWIKI_DOCUMENT_ROOT} ]; then
mkdir -p ${FSWIKI_DOCUMENT_ROOT};
chgrp -R ${APACHE_USER} ${FSWIKI_DOCUMENT_ROOT};
chmod g=rxs,o= ${FSWIKI_DOCUMENT_ROOT};
cd ${FSWIKI_DOCUMENT_ROOT};
rm -rf *;
fi
for dir in config data docs plugin theme
do
mv ${FSWIKI_HOME}/${dir} ${FSWIKI_DOCUMENT_ROOT}/${dir};
sed -i 's/.\/${dir}/.\/${CONST_DATA_PATH}\/${dir}/g' ${FSWIKI_HOME}/setup.dat
done
service ${SERVICE} restart
}
### configure for ubuntu
function ubuntu_config ()
{
# invalidate config file
a2disconf ${CONF_NAME}
echo "# fswiki - web " > ${CONF_PATH}
echo "Alias /fswiki \"${FSWIKI_SETUP_DIR}/fswiki\" " >> ${CONF_PATH}
echo "PerlSetEnv FSWIKI_HOME ${FSWIKI_SETUP_DIR}/fswiki" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "<Directory \"${FSWIKI_SETUP_DIR}/fswiki\"> " >> ${CONF_PATH}
echo " AddHandler perl-script .cgi " >> ${CONF_PATH}
echo " PerlHandler ModPerl::PerlRun " >> ${CONF_PATH}
echo " PerlSendHeader On " >> ${CONF_PATH}
echo " Options +ExecCGI -Indexes " >> ${CONF_PATH}
echo " AllowOverride all " >> ${CONF_PATH}
if [[ ${APACHE_VER} = *2.4* ]]; then
echo "use Require all granted";
echo " Require all granted " >> ${CONF_PATH};
fi
echo " <FilesMatch \"\.(pm|dat|wiki|log)$\"> " >> ${CONF_PATH}
echo " deny from all " >> ${CONF_PATH}
echo " </FilesMatch> " >> ${CONF_PATH}
echo "</Directory> " >> ${CONF_PATH}
# validate config file
a2enconf ${CONF_NAME}
}
### configure for debian
function debian_config()
{
echo "# fswiki - web " > ${CONF_PATH}
echo "Alias /fswiki \"${FSWIKI_SETUP_DIR}/fswiki\" " >> ${CONF_PATH}
echo "PerlSetEnv FSWIKI_HOME ${FSWIKI_SETUP_DIR}/fswiki" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "<Directory \"${FSWIKI_SETUP_DIR}/fswiki\"> " >> ${CONF_PATH}
echo " AddHandler perl-script .cgi " >> ${CONF_PATH}
echo " PerlHandler ModPerl::PerlRun " >> ${CONF_PATH}
echo " PerlSendHeader On " >> ${CONF_PATH}
echo " Options +ExecCGI -Indexes " >> ${CONF_PATH}
echo " AllowOverride all " >> ${CONF_PATH}
if [[ ${APACHE_VER} = *2.4* ]]; then
echo "use Require all granted";
echo " Require all granted " >> ${CONF_PATH};
fi
echo " <FilesMatch \"\.(pm|dat|wiki|log)$\"> " >> ${CONF_PATH}
echo " deny from all " >> ${CONF_PATH}
echo " </FilesMatch> " >> ${CONF_PATH}
echo "</Directory> " >> ${CONF_PATH}
}
### configure for centos
function centos_config()
{
echo "# fswiki - web " > ${CONF_PATH}
echo "Alias /fswiki \"${FSWIKI_SETUP_DIR}/fswiki\" " >> ${CONF_PATH}
echo "PerlSetEnv FSWIKI_HOME ${FSWIKI_SETUP_DIR}/fswiki" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "" >> ${CONF_PATH}
echo "<Directory \"${FSWIKI_SETUP_DIR}/fswiki\"> " >> ${CONF_PATH}
echo " AddHandler perl-script .cgi " >> ${CONF_PATH}
echo " PerlHandler ModPerl::PerlRun " >> ${CONF_PATH}
echo " PerlSendHeader On " >> ${CONF_PATH}
echo " Options +ExecCGI -Indexes " >> ${CONF_PATH}
echo " AllowOverride all " >> ${CONF_PATH}
if [[ ${APACHE_VER} = *2.4* ]]; then
echo "use Require all granted";
echo " Require all granted " >> ${CONF_PATH};
fi
echo " <FilesMatch \"\.(pm|dat|wiki|log)$\"> " >> ${CONF_PATH}
echo " deny from all " >> ${CONF_PATH}
echo " </FilesMatch> " >> ${CONF_PATH}
echo "</Directory> " >> ${CONF_PATH}
}
### main routine
if [ $1 = "--install" ]; then
update || exit 1
update_config || exit 1
exit 0
fi
if [ $1 = "--reconf" ]; then
update_config || exit 1
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment