无论是自己的兴趣还是与工作需求,经常需要在Ubuntu Server上安装LAMPP,于是乎自己编写了自己需要的安装脚本,主要功能如下:
1)安装到指定目录,默认安装到/opt/lampp
2)支持多个版本,使用符号链接
3)添加自启动脚本
代码如下:
1 #Install lampp 2 # parameters: 3 # $1: mandatory, install package 4 # $2: mandatory, lampp version, ex. 1.8.1 5 # $3: optional, target folder, default is /opt/lampp 6 7 # date 8 year=`date +%Y` 9 month=`date +%m` 10 day=`date +%d` 11 today=$year-$month-$day 12 13 ############################### 14 # checking linux distribution 15 ############################### 16 linux_distribution=`head -n 1 /etc/issue | awk -F ' ' '{ 17 printf("%s", $1); 18 }'` 19 20 case $linux_distribution in 21 "CentOS") 22 ;; 23 "Ubuntu") 24 ;; 25 *) 26 echo "[INFO]: Unsupported Linux Distribution $linux_distribution." 27 exit 1 28 ;; 29 esac 30 31 # include functions 32 . ../lib.$linux_distribution/yesno.func 33 . ../lib.$linux_distribution/issue.func 34 . ../lib.$linux_distribution/rc.func 35 36 #linux_ver=$(GetIssueVer) 37 38 echo "[INFO]: Installing Lampp on $linux_distribution $linux_ver..." 39 40 41 # Define variables 42 LAMPP_INSTALL_PATH=/opt 43 LAMPP_LINK="lampp" 44 #LAMPP_PATH=$LAMPP_INSTALL_PATH/lampp 45 LAMPP_PKG="" 46 LAMPP_VER="" 47 CUR_PATH=`pwd` 48 49 # check input parameters 50 case $# in 51 "2") 52 LAMPP_PKG=$CUR_PATH/$1 53 LAMPP_VER=$2 54 ;; 55 "3") 56 LAMPP_PKG=$CUR_PATH/$1 57 LAMPP_VER=$2 58 LAMPP_INSTALL_PATH=$3 59 ;; 60 *) 61 echo "usage: $0 filename version [target_folder]" 62 echo "filename: xampp installation downloaded from xampp in .tar.gz" 63 echo "target_folder: where you want to install, default is /opt" 64 exit 1 65 esac 66 67 LAMPP_NAME="lampp-$LAMPP_VER" 68 LAMPP_PATH=$LAMPP_INSTALL_PATH/$LAMPP_NAME 69 70 # debug 71 #echo "LAMPP_INSTALL_PATH="$LAMPP_INSTALL_PATH 72 73 ################################################ 74 # checking environment and preparation 75 ################################################ 76 if [ ! -d $LAMPP_INSTALL_PATH ] 77 then 78 echo "[INFO]: $LAMPP_INSTALL_PATH does not exist!" 79 exit 1 80 fi 81 82 if [ ! -w $LAMPP_INSTALL_PATH ] 83 then 84 echo "[INFO]: Cannot write $LAMPP_INSTALL_PATH ! Try sudo." 85 exit 1 86 fi 87 88 if [ ! -f $LAMPP_PKG ] 89 then 90 echo "[INFO]: Install file $LAMPP_PKG does not exist!" 91 exit 1 92 fi 93 94 if [ ! -r $LAMPP_PKG ] 95 then 96 echo "[INFO]: Cannot read install file $LAMPP_PKG." 97 exit 1 98 fi 99 100 n=`file $LAMPP_PKG|grep "gzip compressed data"|wc -l` 101 #echo $n 102 if [ $n -ne 1 ] 103 then 104 echo "[INFO]: $LAMPP_PKG is not in .tar.gz format." 105 exit 1 106 fi 107 108 ################################################ 109 # unzip file and correct permission 110 ################################################ 111 if [ -d $LAMPP_PATH ] 112 then 113 echo "[WARNING]: $LAMPP_PATH exists" 114 # stop apache 115 apache_running=`ps aux|grep "$LAMPP_PATH/bin/httpd"|grep -v grep|wc -l` 116 if [ $apache_running -gt 0 ] 117 then 118 YesNo "Apache is running, stop?" "y" 119 if [ $answer = 'Y' -o $answer = 'y' ] 120 then 121 $LAMPP_PATH/lampp stopapache 122 else 123 echo "[ALARM]: You have to stop apache before continue." 124 exit 3 125 fi 126 fi 127 128 # stop mysql 129 mysql_running=`ps aux|grep "$LAMPP_PATH/sbin/mysqld"|grep -v grep|wc -l` 130 if [ $mysql_running -gt 0 ] 131 then 132 YesNo "Mysql is running, stop?" "y" 133 if [ $answer = 'Y' -o $answer = 'y' ] 134 then 135 YesNo "!!!MAKE SURE YOU HAVE MADE NECESSARY BACKUP!!! continue?" "n" 136 if [ $answer = 'Y' -o $answer = 'y' ] 137 then 138 $LAMPP_PATH/lampp stopmysql 139 else 140 echo "[INFO]: Please backup database in advance." 141 exit 3 142 fi 143 else 144 echo "[ALARM]: You have to stop mysql before continue." 145 exit 3 146 fi 147 fi 148 149 YesNo "$LAMPP_PATH exists, remove ?" "n" 150 if [ $answer = 'Y' -o $answer = 'y' ] 151 then 152 echo "[INFO]: Removing ..." 153 if [ ! -w $LAMPP_PATH ] 154 then 155 echo "Cannot remove $LAMPP_PATH, try sudo" 156 exit 1 157 fi 158 cd $LAMPP_INSTALL_PATH 159 rm -rf $LAMPP_PATH 160 echo "Done!" 161 162 cd $LAMPP_INSTALL_PATH 163 164 if [ -s $LAMPP_LINK -o -d $LAMPP_LINK ] 165 then 166 rm $LAMPP_LINK 167 fi 168 169 tar xzvf $LAMPP_PKG|while read ff 170 do 171 echo -n "." 172 done 173 echo "" 174 echo "[INFO]: Unzip done!" 175 mv $LAMPP_LINK $LAMPP_NAME 176 177 # backup http.conf 178 cd $LAMPP_PATH/etc/ 179 cp httpd.conf httpd.conf.$today 180 181 # copy configuration 182 cd $CUR_PATH 183 if [ -d $LAMPP_PATH/etc/extra ] 184 then 185 cp conf/* $LAMPP_PATH/etc/extra 186 else 187 echo "[WARNING]: No directory $LAMPP_PATH/etc/extra." 188 fi 189 else 190 echo "[WARNING]: Keep existing lampp, exit..." 191 fi 192 else 193 cd $LAMPP_INSTALL_PATH 194 195 if [ -s $LAMPP_LINK -o -d $LAMPP_LINK ] 196 then 197 rm $LAMPP_LINK 198 fi 199 200 tar xzvf $LAMPP_PKG|while read ff 201 do 202 echo -n "." 203 done 204 echo "" 205 echo "[INFO]: Unzip done!" 206 mv $LAMPP_LINK $LAMPP_NAME 207 208 # backup http.conf 209 cd $LAMPP_PATH/etc/ 210 cp httpd.conf httpd.conf.$today 211 212 # copy configuration 213 cd $CUR_PATH 214 if [ -d $LAMPP_PATH/etc/extra ] 215 then 216 cp conf/* $LAMPP_PATH/etc/extra 217 else 218 echo "[WARNING]: No directory $LAMPP_PATH/etc/extra." 219 fi 220 221 #echo "[ERROR]: There is no $LAMPP_PATH, exit!" 222 #exit 1 223 fi 224 225 # create symbol link 226 cd $LAMPP_INSTALL_PATH 227 if [ -s $LAMPP_LINK ] 228 then 229 rm $LAMPP_LINK 230 fi 231 ln -s $LAMPP_PATH $LAMPP_LINK 232 233 ############################## 234 # install auto-start script 235 ############################## 236 cd $CUR_PATH 237 238 INITD_PATH="/etc/init.d" 239 240 echo "[INFO] Installing scripts..." 241 if [ -f $INITD_PATH/apache ] 242 then 243 echo "[WARNING]: $INITD_PATH/apache exists, skip." 244 else 245 echo "[WARNING]: $INITD_PATH/apache does not exist,creating... " 246 if [ -f $linux_distribution/apache ] 247 then 248 cp $linux_distribution/apache /etc/init.d/ 249 else 250 echo "WARNING: File $linux_distribution/apache does not exist, continue." 251 fi 252 fi 253 254 if [ -f $INITD_PATH/mysql ] 255 then 256 echo "[WARNING]: $INITD_PATH/mysql exists, skip." 257 else 258 if [ -f $linux_distribution/mysql ] 259 then 260 cp $linux_distribution/mysql /etc/init.d/ 261 else 262 echo "WARNING: File $linux_distribution/mysql does not exist, continue." 263 fi 264 fi 265 266 lvl=`runlevel|awk -F ' ' '{print $2;}'` 267 #echo $lvl 268 269 #echo $rc_folder 270 #RC_PATH="/etc/rc.d/rc$lvl.d" 271 RC_PATH=`get_rcd $lvl` 272 273 if [ -d $RC_PATH -a -w $RC_PATH ] 274 then 275 cd $RC_PATH 276 if [ -s S99apache ] 277 then 278 echo "[INFO]: S99apache exist! Skip." 279 else 280 ln -s ../init.d/apache S99apache 281 fi 282 283 if [ -s S99mysql ] 284 then 285 echo "[INFO]: S99mysql exist! Skip." 286 else 287 ln -s ../init.d/mysql S99mysql 288 fi 289 else 290 echo "[WARNING]: cannot make auto-start script in $RC_PATH" 291 fi 292 293 #RC_PATH="/etc/rc.d/rc0.d" 294 RC_PATH=`get_rcd 0` 295 296 if [ -d $RC_PATH -a -w $RC_PATH ] 297 then 298 cd $RC_PATH 299 if [ -s K10apache ] 300 then 301 echo "[INFO]: K10apache exist! Skip." 302 else 303 ln -s ../init.d/apache K10apache 304 fi 305 if [ -s K10mysql ] 306 then 307 echo "[INFO]: K10mysql exist! Skip." 308 else 309 ln -s ../init.d/mysql K10mysql 310 fi 311 else 312 echo "[WARNING]: cannot make auto-stop script in $RC_PATH" 313 fi 314 315 echo "Done!"
其中用到了几个文件内容如下:
yesno.func:
1 # YesNo 2 # arg1 : questoin 3 # arg2 : default answer "y" or "n", if $# == 1, default is 'n' 4 YesNo() 5 { 6 default_answer='n' 7 8 if [ $# -ne 2 ] 9 then 10 if [ $# -ne 1 ] 11 then 12 yesno="(Y/N? Default is $default_answer)" 13 else 14 yesno="$1(Y/N Default is $default_answer)" 15 fi 16 else 17 default_answer=$2 18 yesno="$1(Y/N Default is $default_answer)" 19 fi 20 echo "[QUES] : $yesno" 21 read answer 22 if [ -z "$answer" ] 23 then 24 answer=$default_answer 25 fi 26 }
issue.func:
1 # return system distribution name from /etc/issue 2 GetIssue() 3 { 4 ret="" 5 if [ -f /etc/issue -a -r /etc/issue ] 6 then 7 ret=`head -n 1 /etc/issue | awk -F ' ' '{ 8 printf("%s", $1); 9 }'` 10 else 11 echo "cannot read /etc/issue." 12 fi 13 14 echo $ret 15 } 16 17 GetIssueVer() 18 { 19 ret="" 20 if [ -f /etc/issue -a -r /etc/issue ] 21 then 22 name=`head -n 1 /etc/issue | awk -F ' ' '{ 23 printf("%s", $1); 24 }'` 25 case $name in 26 "CentOS") 27 ret=`head -n 1 /etc/issue | awk -F ' ' '{ 28 printf("%s", $3); 29 }'` 30 ;; 31 "Ubuntu") 32 ret=`head -n 1 /etc/issue | awk -F ' ' '{ 33 printf("%s", $2); 34 }'` 35 ;; 36 *) 37 echo "Unknown." 38 esac 39 else 40 echo "cannot read /etc/issue." 41 fi 42 43 echo $ret 44 }
rc.func:
1 # 2 get_rcd() 3 { 4 lvl=$1 5 f="/etc/rc$lvl.d" 6 echo $f 7 return 0 8 }
apache的自启动脚本如下:
1 #! /bin/sh 2 3 ### BEGIN INIT INFO 4 # Provides: apache 5 # Required-Start: service apache start 6 # Required-Stop: service apache stop 7 # Default-Start: 2 3 4 5 8 # Default-Stop: 9 # Short-Description: XAMPP httpd service 10 ### END INIT INFO 11 12 set -e 13 14 # /etc/init.d/ssh: start and stop the Ubuntu "secure shell(tm)" daemon 15 16 test -x /opt/lampp/lampp || exit 0 17 18 umask 022 19 20 . /lib/lsb/init-functions 21 22 # Are we running from init? 23 run_by_init() { 24 ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ] 25 } 26 27 flag1=0 28 flag2=0 29 30 case "$1" in 31 start) 32 log_daemon_msg "Starting XAMPP httpd server" "httpd" 33 /opt/lampp/lampp startapache 34 flag1=$? 35 36 if [ $flag1 -eq 0 ] 37 then 38 log_end_msg 0 39 else 40 log_end_msg 1 41 fi 42 ;; 43 stop) 44 log_daemon_msg "Stopping XAMPP httpd server" "httpd" 45 /opt/lampp/lampp stopapache 46 flag1=$? 47 48 if [ $flag1 -eq 0 ] 49 then 50 log_end_msg 0 51 else 52 log_end_msg 1 53 fi 54 ;; 55 56 restart) 57 log_daemon_msg "Restarting XAMPP apache" "httpd" 58 /opt/lampp/lampp stopapache 59 flag1=$? 60 61 /opt/lampp/lampp startapache 62 flag2=$? 63 64 if [ $flag1 -eq 0 -a $flag2 -eq 0 ] 65 then 66 log_end_msg 0 67 else 68 log_end_msg 1 69 fi 70 ;; 71 72 status) 73 ;; 74 75 *) 76 log_action_msg "Usage: /etc/init.d/apache {start|stop|restart|status}" 77 exit 1 78 esac 79 80 exit 0
mysql的自启动脚本如下:
1 #! /bin/sh 2 3 ### BEGIN INIT INFO 4 # Provides: mysql 5 # Required-Start: service mysql start 6 # Required-Stop: service mysql stop 7 # Default-Start: 2 3 4 5 8 # Default-Stop: 9 # Short-Description: XAMPP mysqld service 10 ### END INIT INFO 11 12 set -e 13 14 # /etc/init.d/ssh: start and stop the Ubuntu "secure shell(tm)" daemon 15 16 test -x /opt/lampp/lampp || exit 0 17 18 umask 022 19 20 . /lib/lsb/init-functions 21 22 # Are we running from init? 23 run_by_init() { 24 ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ] 25 } 26 27 flag1=0 28 flag2=0 29 30 case "$1" in 31 start) 32 log_daemon_msg "Starting XAMPP mysqld server" "mysqld" 33 /opt/lampp/lampp startmysql 34 flag1=$? 35 36 if [ $flag1 -eq 0 ] 37 then 38 log_end_msg 0 39 else 40 log_end_msg 1 41 fi 42 ;; 43 stop) 44 log_daemon_msg "Stopping XAMPP mysqld server" "mysqld" 45 /opt/lampp/lampp stopmysql 46 flag1=$? 47 48 if [ $flag1 -eq 0 ] 49 then 50 log_end_msg 0 51 else 52 log_end_msg 1 53 fi 54 ;; 55 56 restart) 57 log_daemon_msg "Restarting XAMPP mysqld" "mysqld" 58 /opt/lampp/lampp stopmysql 59 flag1=$? 60 61 /opt/lampp/lampp startmysql 62 flag2=$? 63 64 if [ [ $flag1 -eq 0 ] -a [ $flag2 -eq 0 ] ] 65 then 66 log_end_msg 0 67 else 68 log_end_msg 1 69 fi 70 ;; 71 72 status) 73 ;; 74 75 *) 76 log_action_msg "Usage: /etc/init.d/mysql {start|stop|restart|status}" 77 exit 1 78 esac 79 80 exit 0