Linux環境でlampサーバーをインストールシェルスクリプト

ファイル名:
linux_install.sh
開発元:
https://github.com/latel/EasyLamp

サンプルコード:
#!/bin/sh
BASE_DIR=$PWD/${0%/*}/../
cd $BASE_DIR
BASE_DIR=$PWD
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:$BASE_DIR/install/functions
TTY=`ps|grep $$|awk '{print $2}’`
USER=`who|grep ${TTY}|awk '{print $1}’`

#インストール中に参照されたマニュアルの設定を調整
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#leave values to “do_not_install" on the item you don’t wanted
#apache
apache_version="2.4.7″
#php
php_version="5.5.7″
#mysql
mysql_version="5.6.15″
mysql_password="startnews24″

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#このスクリプトはroot権限で実行必要
get_root(){
if [[ $EUID -ne 0 ]];then
echo “このスクリプトはrootとして実行する必要がある" 1>&2
exit 1
fi
}

#OSの種類を検出
detect_os(){
if uname -a | grep -i “arch" >/dev/null 2>&1;then
echo “arch"
elif uname -a | grep -i “ubuntu|debian">/dev/null 2>&1;then
echo “debian"
elif uname -a | grep -i “centos|redhat">/dev/null 2>&1;then
echo “redhat"
elif cat /proc/version | grep -i “arch">/dev/null 2>&1;then
echo “arch"
elif cat /proc/version | grep -i “ubuntu|debian">/dev/null 2>&1;then
echo “debian"
elif cat /proc/version | grep -i “centos|redhat">/dev/null 2>&1;then
echo “redhat"
else
echo “generic"
fi
}

#システムですでにコマンドを利用するかどうか確認
check_command_exists(){
local command=$1
local stop=$2
if hash $1 2>/dev/null;then
echo “0"
else
echo “1"
if [ $2 == “1" ];then
echo 'building tools $1 not installed. please do it yourself’
exit 1
fi
fi
}

lowcase(){
word=$1
echo $word | tr '[A-Z]’ '[a-z]’
}

#yesまたnoを選択
confirm(){
local prompt=$1
local yaction=$2
local naction=$3
while true; do
read -p “$prompt(y/n)?: " choice
choice=`lowcase $choice`
case $choice in
y ) eval “$yaction";return 1;break;;
n ) eval “$naction";return 0;break;;
* ) echo “wrong input, please type y or n only."
esac
done
}

#detect source code’s compress format
#execute accordingly extract command
extract_source(){
#currently, only tar.gz and tar.bz2 format are supported
local zip_name=`lowcase $1`
local zip_destination=$2
cd $BASE_DIR/install
if [ -f packages/${zip_name}.tar.gz ];then
tar zxvf packages/${zip_name}.tar.gz -C $zip_destination
return 0
elif [ -f packages/${zip_name}.tar.bz2 ];then
tar jxvf packages/${zip_name}.tar.bz2 -C $zip_destination
return 0
else
echo “!!no file is available for source ${zip_name}, installation aborted."
exit 1
fi
}

#構築ツールをインストール
install_tools(){
if [ “`detect_os`" == “arch" ];then
pacman -Syu
pacman -S gcc g++ make cmake wget perl curl openssl build-essential
elif [ “`detect_os`" == “debian" ];then
apt-get update
apt-get install gcc g++ make cmake wget perl curl openssl build-essential
elif [ “`detect_os`" == “redhat" ];then
yum -y gcc g++ make cmake wget perl curl openssl build-essential
fi

check_command_exists “gcc" “1"
check_command_exists “g++" “1"
check_command_exists “make" “1"
check_command_exists “cmake" “1"
check_command_exists “wget" “1"
check_command_exists “perl" “1"
check_command_exists “curl" “1"
}

#ダイアログをインストール
install_dialog(){
#ファイルを準備
tar xvf install/packages/dialog.tar.gz -C install/temp/
#インストール
cd install/temp/dialog-1.2-20130928
./configure
make
make install
cd $BASE_DIR
}

#apacheのインストール
install_apache(){
cd $BASE_DIR
#check previous installation
if [ -d apache* ];then
confirm “You have installed `ls|grep apache` already, uninstall first" “rm -r apache*" “apache_version=`ls|grep apache|grep -oE '((([0-9]){1,}.){2}[0-9]{1,}){1}’` ; return 0"
if [ $? != “1" ];then
return 0
fi
fi
#依存関係をインストール

#ディレクトリを作成
cd $BASE_DIR
[[ -d apache-${apache_version} ]] || mkdir apache-${apache_version}
[[ -d www ]] || mkdir www
[[ -d logs ]] || mkdir logs
[[ -d conf ]] || mkdir conf
#prepare files
if [ -f www/index.php ];then
echo “htdocs files existed, ignore."
else
touch www/index.php
echo “" >> www/index.php
fi
extract_source “httpd-${apache_version}" “${BASE_DIR}/install/temp/"
cd $BASE_DIR
#install
cd install/temp/httpd-${apache_version}
./configure\
–prefix=$BASE_DIR/apache-${apache_version}\
–logfiledir=$BASE_DIR/logs\
–enable-so
#–bindir=/usr/bin\
#–sbindir=/usr/sbin\
#–libdir=/usr/lib/httpd/lib\
#–libexecdir=/usr/lib/httpd/modules\
#–installbuilddir=/usr/lib/httpd/build\
#–htmldir=$BASE_DIR/www\
make
make install

#設定
cd $BASE_DIR
groupadd httpd >/dev/null 2>&1 #create group and user if not exitsts
useradd -s /bin/false -g httpd httpd >/dev/null 2>&1
cd apache-${apache_version}
mv htdocs/index.html $BASE_DIR/www/index.html
cp conf/httpd.conf conf/httpd.conf.bak #backup original conf file
sed -i -e “s/User daemon/User httpd/" -e “s/Group daemon/Group httpd/" conf/httpd.conf
sed -i -e “s/apache-${apache_version}\/htdocs/www/" conf/httpd.conf
sed -i -e “s/#ServerName www.example.com:80/ServerName www.example.com:80/" conf/httpd.conf
sed -i -e “s/ErrorLog \"logs\/error_log\"/ErrorLog \"${BASE_DIR//\//\\/}\/logs\/httpd_error_log\"/" conf/httpd.conf
sed -i -e “s/CustomLog \"logs\/access_log\" common/CustomLog \"${BASE_DIR//\//\\/}\/logs\/httpd_access_log\" common/" conf/httpd.conf
#make files for later-reading
cd $BASE_DIR
touch {logs/httpd_access_log,logs/httpd_error_log}

#cleanup
cp apache-${apache_version}/bin/httpd apache-${apache_version}/bin/httpd.bak
strip apache-${apache_version}/bin/httpd
}

#phpのインストール
install_php(){
cd $BASE_DIR
#以前のインストールを確認
if [ -d php* ];then
confirm “You’v installed `ls|grep php` already, uninstall first" “rm -r php*" “php_version=`ls|grep php|grep -oE '((([0-9]){1,}.){2}[0-9]{1,}){1}’` ; return 0"
if [ $? != “1" ];then
return 0
fi
fi
#install dependencies

#make directory
[[ -d php-${php_version} ]] || mkdir php-${php_version}
[[ -d logs ]] || mkdir logs
[[ -d conf ]] || mkdir conf
#prepare files
extract_source “php-${php_version}" “${BASE_DIR}/install/temp/"
cd $BASE_DIR
#install

cd install/temp/php-${php_version}
./configure\
–prefix=$BASE_DIR/php-${php_version}\
–with-apxs2=$BASE_DIR/apache-${apache_version}/bin/apxs\
–with-config-file-path=$BASE_DIR/conf\
–with-mysql\
–with-pdo-mysql\
–enable-pdo\
–with-openssl\
–with-zlib\
–with-curl\
–with-gd\
–with-mcrypt #phpmyadmin need it
make
make install

#configuration
cd $BASE_DIR
mv php/etc php-${php_version}/
rm -r php
cp install/temp/php-${php_version}/php.ini-development conf/php.ini
sed -i -e “s/;date.timezone =/date.timezone = Asia\/Shanghai/" conf/php.ini
sed -i -e “s/;extension=php_mysql.dll/extension=php_mysql.dll/" conf/php.ini
sed -i -e “s/;extension=php_pdo_mysql.dll/extension=php_pdo_mysql.dll/" conf/php.ini
sed -i -e “s/mysql.default_socket =/mysql.default_socket =${BASE_DIR//\//\\/}\/mysql-${mysql_version}\/mysql.sock/" conf/php.ini
sed -i -e “s/pdo_mysql.default_socket=/pdo_mysql.default_socket=${BASE_DIR//\//\\/}\/mysql-${mysql_version}\/mysql.sock/" conf/php.ini
cp install/resource/php5_module.conf apache-${apache_version}/conf/extra/php5_module.conf
sed -i -e “s/PHPIniDir/PHPIniDir '${BASE_DIR//\//\\/}\/conf\/php.ini’/" apache-${apache_version}/conf/extra/php5_module.conf
echo “Include conf/extra/php5_module.conf" >> apache-${apache_version}/conf/httpd.conf
#do stuff for apache
cp apache-${apache_version}/conf/httpd.conf conf/httpd.conf
}

#mysqlのインストール
install_mysql(){
cd $BASE_DIR
#check previous installation
if [ -d mysql* ];then
confirm “You’v installed `ls|grep mysql` already, uninstall first" “rm -r mysql*" “mysql_version=`ls|grep mysql|grep -oE '((([0-9]){1,}.){2}[0-9]{1,}){1}’` ; return 0"
if [ $? != “1" ];then
return 0
fi
fi
#install dependencies

#make directory
cd $BASE_DIR
[[ -d mysql-${mysql_version}/data ]] || mkdir -p mysql-${mysql_version}/data
[[ -d logs ]] || mkdir logs
[[ -d conf ]] || mkdir conf

#prepare files
extract_source “mysql-${mysql_version}" “${BASE_DIR}/install/temp/"
cd $BASE_DIR
#install
cd install/temp/mysql-${mysql_version}
cmake \
-DCMAKE_INSTALL_PREFIX=$BASE_DIR/mysql-${mysql_version} \
-DMYSQL_DATADIR=$BASE_DIR/mysql-${mysql_version}/data \
-DMYSQL_UNIX_ADDR=$BASE_DIR/mysql-${mysql_version}/mysql.sock \
-DEFAULT_CHARSET=utf8 \
-DEFAULT_COLLATION=utf8_general_ci
#add a user line for coomon user to use mysql
#./configure\
# –prefix=$BASE_DIR/mysql-${mysql_version}
make
make install

#configuration
cd $BASE_DIR
groupadd mysql #create group and user if not exitstsapache-${apache_version}
useradd -s /bin/false -g mysql mysql
mysql-${mysql_version}/scripts/mysql_install_db –basedir=$BASE_DIR/mysql-${mysql_version} –datadir=$BASE_DIR/mysql-${mysql_version}/data –user=mysql
cp install/temp/mysql-${mysql_version}/support-files/mysql.server mysql-${mysql_version}/bin/mysql.server
chmod +x mysql-${mysql_version}/bin/mysql.server
cp install/temp/mysql-${mysql_version}/support-files/my-default.cnf mysql-${mysql_version}/my.cnf
cd mysql-${mysql_version}
echo “basedir = $BASE_DIR/mysql-${mysql_version}/" >> my.cnf
echo “datadir = $BASE_DIR/mysql-${mysql_version}/data" >> my.cnf
echo “port = 3306" >> my.cnf
echo “socket = $BASE_DIR/mysql-${mysql_version}/mysql.sock" >> my.cnf
echo “log-error = $BASE_DIR/logs/mysql_error.log" >> my.cnf
echo “pid-file = $BASE_DIR/mysql-${mysql_version}/mysql.pid" >> my.cnf
echo “user = mysql" >> my.cnf
cd $BASE_DIR
#make files for later-reading
touch logs/mysql_error.log
#adjust permissions
chown -R mysql.mysql mysql-${mysql_version}
}

gen_scripts(){
cd $BASE_DIR
[[ -f ezlamp-st ]] && rm ezlamp-st
[[ -f ezlamp-ki ]] && rm ezlamp-ki
[[ -f ezlamp-ki ]] && rm greenfy
cp install/resource/ezlamp-st ezlamp-st
cp install/resource/ezlamp-ki ezlamp-ki
cp install/resource/greenfy greenfy
chmod +x ezlamp-st ezlamp-ki greenfy
chown $USER ez* green*
}

run(){
cd $BASE_DIR
apache-${apache_version}/bin/apachectl -k start -f ${BASE_DIR}/conf/httpd.conf #start apache
mysql-${mysql_version}/bin/mysql.server start –explicit_defaults_for_timestamp & #start mysql daemon
}

#doing some post configuration work
post_configuration(){
cd $BASE_DIR
mysql-${mysql_version}/bin/mysql_secure_installation
}

eazylamp(){
echo
echo
echo “#############################################################################"
echo
echo “If you have any question, please submit your issues to following address"
echo “https://github.com/latel/EasyLamp"
echo
echo “#############################################################################"
echo

#user confirmation
read -p “Are you sure you want to install lamp in $BASE_DIR(y/n)?" confirm
cd $BASE_DIR
if [ $confirm = y ];then
get_root
#install_tools
touch /tmp/eazylamp_log #create file to log install information
chattr +a /tmp/eazylamp_log #not allowed to delete, only add is alllowed
detect_os
if [ “`check_command_exists “dialog"`" == “1" ];then
read -p “Dialog not detected, which can supply great installation experience, install(y/n)?: " confirm
if [ $confirm == y ];then
install_dialog
[[ $? == 0 ]] && has_dialog = 1
fi
fi
[[ -d install/temp ]] || mkdir install/temp
echo “[1/7]installing apache…"
install_apache
echo “*apache2 installed"
echo “[2/7]installing php…"
install_php
echo “*php5 installed"
echo “[3/7] mysqlインストール中…"
install_mysql
echo “*mysqlインストール完了"
cd $BASE_DIR
echo “[4/7]removing tempotary files"
#rm -r install/temp
echo “[5/7]do some peformance enhancements, please wait…"
chown -R $USER www
echo “[6/7]generating maintain scripts…"
gen_scripts
echo
echo “インストール完了"
echo “#======================================"
echo “# apache: ${apache_version}"
echo “# php : ${php_version}"
echo “# mysql : ${mysql_version}"
echo “#======================================"
echo
echo “[7/7]All done. (。・ω・)ノ"
echo
echo “現在インストールフォルダーを削除 "
run
echo “http://localhost/"
else
echo “手動でフォルダ場所を移動してください"
fi
}
eazylamp

Source

Posted by arkgame