基于阿里云安装脚本扩展 之 自动安装mongodb及php扩展

    好久没有发布文章了,有点跟不上当初这个博客的初衷。为了使自己的博客更新不半途而废,今天特意再写了一个自动安装脚本,一样是基于阿里云的服务端安装脚本进行的扩展。闲话不说,直接放代码:

  1 #!/bin/bash
  2 #by dxd in 2017-2
  3 #only suit for centos/aliyun os, and based on aliyun install script
  4 
  5 CURR_PATH=$(pwd)
  6 
  7 PHP_EXT_SOURCE_PATH=""
  8 PHP_BIN_PATH=/alidata/server/php/bin
  9 
 10 DOWNLOAD_MONGODB_URL_X64=http://download.verymall.cn/mongodb-2.7.0.tgz
 11 DOWNLOAD_MONGODB_URL_X86=http://download.verymall.cn/mongodb-linux-2.6.7-x86.tgz
 12 DOWNLOAD_MONGODB_CONF_URL=http://download.verymall.cn/mongodb.conf
 13 INSTALL_MONGODB_PATH=/alidata/server/mongodb
 14 MONGODB_PORT=27017
 15 
 16 DOWNLOAD_MONGODB_EXT_URL=http://download.verymall.cn/php-mongo-1.6.14.tgz
 17 PHP_INI_PATH=/alidata/server/php/etc/php.ini
 18 
 19 function check_user()
 20 {
 21     if [ "$(whoami)" != "root" ] 
 22     then
 23         echo "please run as root"
 24         exit 1
 25     fi
 26 }
 27 
 28 function check_os()
 29 {
 30     which yum > /dev/null 2>&1
 31     if [ "$?" != "0" ]
 32     then
 33         echo "linux is not based on centos"
 34         exit 1
 35     fi
 36 
 37     #if [ "$(getconf LONG_BIT)" != "64" ]
 38     #then
 39     #    echo "linux is not x64"
 40     #    exit 1
 41     #fi
 42 }
 43 
 44 function check_curr_path_auth()
 45 {
 46     if [ ! -w $CURR_PATH ]
 47     then
 48         echo "current path has no write auth"
 49         exit 1
 50     fi
 51 }
 52 
 53 function is_curr_path_legal()
 54 {
 55     if [ "$(ls | grep install.sh)" == "" ]
 56     then
 57         echo "path is illegal"
 58         exit 1
 59     fi
 60 }
 61 
 62 function get_php_ext_path()
 63 {
 64     for dir in $(ls)
 65     do
 66         TMP_PATH=$CURR_PATH/$dir/ext
 67         if [ -d $TMP_PATH ]
 68         then
 69             PHP_EXT_SOURCE_PATH=$TMP_PATH
 70             break
 71         fi
 72     done
 73 
 74     if [ -z "$PHP_EXT_SOURCE_PATH" ]
 75     then
 76         echo "php source path not found"
 77         exit 1
 78     fi
 79 
 80     echo -n "php ext source path is $PHP_EXT_SOURCE_PATH , (Y/N): "
 81     read USER_ANSWER
 82     until [ "$USER_ANSWER" = "Y" ] || [ "$USER_ANSWER" = "y" ]
 83     do
 84         if [ "$USER_ANSWER" = "N" ] || [ "$USER_ANSWER" = "n" ]
 85         then
 86             echo "php ext source path is fault, then exit"
 87             exit 1
 88         fi
 89     
 90         echo -n "php ext source path is $PHP_EXT_SOURCE_PATH , (Y/N): "
 91         read USER_ANSWER
 92     done
 93 }
 94 
 95 function check_gcc()
 96 {
 97     which gcc > /dev/null 2>&1
 98     if [ "$?" != "0" ]
 99     then
100         echo "please install gcc first"
101         exit 1
102     fi
103 }
104 
105 function check_mongodb_port()
106 {
107     if [ -n "$(lsof -i:$MONGODB_PORT)" ]
108     then
109         echo "PORT $MONGODB_PORT is occupied"
110         exit 1
111     fi
112 }
113 
114 function check_alidata_path()
115 {
116     if [ ! -d /alidata ]
117     then
118         mkdir /alidata
119     fi
120 
121     if [ ! -d /alidata/lib ]
122     then
123         mkdir /alidata/lib
124     fi
125 
126     if [ ! -d /alidata/server ]
127     then
128         echo "alidata server has not been installed"
129         exit 1
130     fi
131 }
132 
133 function install_dos2unix()
134 {
135     which dos2unix > /dev/null 2>&1
136     if [ "$?" != "0" ]
137     then
138         yum install dos2unix
139     fi
140 }
141 
142 function install_mongodb_x64()
143 {
144     if [ -d $INSTALL_MONGODB_PATH ]
145     then
146         rm -rf $INSTALL_MONGODB_PATH
147     fi
148 
149     if [ -a ./mongodb-2.7.0.tgz ] && [ -n "$(md5sum ./mongodb-2.7.0.tgz | grep 66acfaacb40be88db07065163cc15857)" ]
150     then
151         echo "./mongodb-2.7.0.tgz exist"
152     else
153         rm -f ./mongodb-2.7.0.tgz
154         wget $DOWNLOAD_MONGODB_URL_X64
155     fi
156     
157     rm -rf ./mongodb-linux-x86_64-2.7.0
158     tar -zxvf ./mongodb-2.7.0.tgz > /dev/null
159     if [ ! -d ./mongodb-linux-x86_64-2.7.0 ]
160     then
161         echo "tar fail"
162         exit 1
163     fi
164 
165     mv ./mongodb-linux-x86_64-2.7.0 $INSTALL_MONGODB_PATH
166     mkdir $INSTALL_MONGODB_PATH/data
167     mkdir $INSTALL_MONGODB_PATH/log
168     mkdir $INSTALL_MONGODB_PATH/pid
169     mkdir $INSTALL_MONGODB_PATH/conf
170 
171     wget -O $INSTALL_MONGODB_PATH/conf/mongodb.conf $DOWNLOAD_MONGODB_CONF_URL
172 
173     #run mongodb
174     echo "it will takes a few minutes to fork children process in the first, please be patient!"
175     $INSTALL_MONGODB_PATH/bin/mongod -f $INSTALL_MONGODB_PATH/conf/mongodb.conf
176 }
177 
178 function install_mongodb_x86()
179 {
180     if [ -d $INSTALL_MONGODB_PATH ]
181     then
182         rm -rf $INSTALL_MONGODB_PATH
183     fi
184 
185     if [ -a ./mongodb-linux-2.6.7-x86.tgz ] && [ -n "$(md5sum ./mongodb-linux-2.6.7-x86.tgz | grep 87a57c05e428fe1ced619e63107140ff)" ]
186     then
187         echo "./mongodb-linux-2.6.7-x86.tgz exist"
188     else
189         rm -f ./mongodb-linux-2.6.7-x86.tgz
190         wget $DOWNLOAD_MONGODB_URL_X86
191     fi
192 
193     rm -rf ./mongodb-linux-i686-2.6.7
194     tar -zxvf ./mongodb-linux-2.6.7-x86.tgz > /dev/null
195     if [ ! -d ./mongodb-linux-i686-2.6.7 ]
196     then
197         echo "tar fail"
198         exit 1
199     fi
200 
201     mv ./mongodb-linux-i686-2.6.7 $INSTALL_MONGODB_PATH
202     mkdir $INSTALL_MONGODB_PATH/data
203     mkdir $INSTALL_MONGODB_PATH/log
204     mkdir $INSTALL_MONGODB_PATH/pid
205     mkdir $INSTALL_MONGODB_PATH/conf
206 
207     wget -O $INSTALL_MONGODB_PATH/conf/mongodb.conf $DOWNLOAD_MONGODB_CONF_URL
208 
209     #run mongodb
210     echo "it will takes a few minutes to fork children process in the first, please be patient!"
211     $INSTALL_MONGODB_PATH/bin/mongod -f $INSTALL_MONGODB_PATH/conf/mongodb.conf
212 }
213 
214 function install_mongodb()
215 {
216     if [ "$(getconf LONG_BIT)" == "64" ]
217     then 
218         install_mongodb_x64
219     elif [ "$(getconf LONG_BIT)" == "32" ]
220     then
221         install_mongodb_x86
222     else
223         echo "linux bit error"
224         exit 1
225     fi
226 }
227 
228 function install_mongodb_php_ext()
229 {
230     if [ -d ./mongo-1.6.14 ]
231     then
232         rm -rf ./mongo-1.6.14
233     fi
234 
235     if [ -d $PHP_EXT_SOURCE_PATH/mongo ]
236     then
237         rm -rf $PHP_EXT_SOURCE_PATH/mongo
238     fi
239 
240     if [ -a ./php-mongo-1.6.14.tgz ] && [ -n "$(md5sum ./php-mongo-1.6.14.tgz | grep '19cd8bd94494f924ce8314f304fd83b6')" ]
241     then
242         echo "php mongo exist"
243     else
244         wget $DOWNLOAD_MONGODB_EXT_URL
245     fi
246 
247     tar -zxvf ./php-mongo-1.6.14.tgz
248     chmod -R 0777 ./mongo-1.6.14
249     mv ./mongo-1.6.14 $PHP_EXT_SOURCE_PATH/mongo
250     
251     cd $PHP_EXT_SOURCE_PATH/mongo
252     $PHP_BIN_PATH/phpize
253     ./configure --with-php-config=$PHP_BIN_PATH/php-config 
254     make 
255     make install 
256 
257     write php.ini
258     mongo_exist=$(cat $PHP_INI_PATH | grep 'mongo.so')
259     if [ -z "$mongo_exist" ]
260     then
261         echo "extension=mongo.so" >> $PHP_INI_PATH
262     fi
263 
264     cd $CURR_PATH
265 }
266 
267 function restart_php_fpm()
268 {
269     /etc/init.d/php-fpm restart
270 }
271 
272 check_user
273 check_os
274 check_curr_path_auth
275 is_curr_path_legal
276 get_php_ext_path
277 check_gcc
278 check_mongodb_port
279 check_alidata_path
280 install_dos2unix
281 install_mongodb
282 install_mongodb_php_ext
283 restart_php_fpm
284 
285 echo "mongodb install successfully"
286 exit 0

 

posted on 2017-02-10 01:07  咚..咚  阅读(190)  评论(0编辑  收藏  举报

导航