sys-init.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. #! /bin/bash -e
  2. #
  3. platform=`uname -i`
  4. if [ $platform != "x86_64" ];then
  5. echo "\033[31mthis script is only for 64bit Operating System !\033[0m"
  6. exit 1
  7. fi
  8. system=`cat /etc/redhat-release |awk '{print $1}'`
  9. if [ $system != "CentOS" ];then
  10. echo "\033[31mthis script is only for CentOS 6 !\033[0m"
  11. exit 1
  12. fi
  13. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  14. wget -c http://mirrors.163.com/.help/CentOS6-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo
  15. DATE=`date +"%Y-%m-%d %H:%M:%S"`
  16. CPU_PROCESSOR=`grep 'processor' /proc/cpuinfo | sort -u | wc -l`
  17. HOSTNAME=`hostname -s`
  18. USER=`whoami`
  19. IPADDR=`ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g' |sed 's/Bcast:.*$//g'`
  20. CPU_AVERAGE=`cat /proc/loadavg | cut -c1-14`
  21. MemTotal=$[`grep MemTotal /proc/meminfo |awk '{print $2}'`/1024/1024+1]
  22. BASEPATH=$(cd `dirname $0`; pwd)
  23. DOWNURL="http://cdn.iyueni.com/download/package/"
  24. function system {
  25. echo -e "\033[41;33m|-----------System Infomation-----------\033[0m"
  26. echo -e "| DATE :$DATE"
  27. echo -e "| HOSTNAME :$HOSTNAME"
  28. echo -e "| USER :$USER"
  29. echo -e "| Root dir :$BASEPATH"
  30. echo -e "| IP :$IPADDR"
  31. echo -e "| CPU_AVERAGE :$CPU_AVERAGE"
  32. echo -e "| CPU_PROCESSOR:$CPU_PROCESSOR"
  33. echo -e "| MemTotal :${MemTotal}G"
  34. }
  35. function process {
  36. echo -e "\033[41;33m|-----------Current Process-----------\033[0m"
  37. for service in nginx php mysql
  38. do
  39. if [ `ps aux|grep $service |grep -v grep|wc -l` -gt 0 ];then
  40. echo -e "\033[32m$service.......................[RUNNING]\033[0m"
  41. else
  42. echo -e "\033[31m$service.......................[NOT RUN]\033[0m"
  43. fi
  44. done
  45. }
  46. function configSysctl {
  47. if [ -z "`grep '^net.ipv4.tcp_max_tw_buckets' /etc/sysctl.conf`" ];then
  48. echo "net.ipv4.tcp_max_tw_buckets = 10000" >> /etc/sysctl.conf
  49. else
  50. sed -i "s/^net.ipv4.tcp_max_tw_buckets *=.*/net.ipv4.tcp_max_tw_buckets = 10000/g" /etc/sysctl.conf
  51. fi
  52. if [ -z "`grep '^net.ipv4.ip_local_port_range' /etc/sysctl.conf`" ];then
  53. echo "net.ipv4.ip_local_port_range = 1024 65000" >> /etc/sysctl.conf
  54. else
  55. sed -i "s/^net.ipv4.ip_local_port_range *=.*/net.ipv4.ip_local_port_range = 1024 65000/g" /etc/sysctl.conf
  56. fi
  57. if [ -z "`grep '^net.ipv4.tcp_tw_recycle' /etc/sysctl.conf`" ];then
  58. echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf
  59. else
  60. sed -i "s/^net.ipv4.tcp_tw_recycle *=.*/net.ipv4.tcp_tw_recycle = 1/g" /etc/sysctl.conf
  61. fi
  62. if [ -z "`grep '^net.ipv4.tcp_tw_reuse' /etc/sysctl.conf`" ];then
  63. echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
  64. else
  65. sed -i "s/^net.ipv4.tcp_tw_reuse *=.*/net.ipv4.tcp_tw_reuse = 1/g" /etc/sysctl.conf
  66. fi
  67. if [ -z "`grep '^net.ipv4.tcp_syncookies' /etc/sysctl.conf`" ];then
  68. echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
  69. else
  70. sed -i "s/^net.ipv4.tcp_syncookies *=.*/net.ipv4.tcp_syncookies = 1/g" /etc/sysctl.conf
  71. fi
  72. if [ -z "`grep '^net.core.somaxconn' /etc/sysctl.conf`" ];then
  73. echo "net.core.somaxconn = 262144" >> /etc/sysctl.conf
  74. else
  75. sed -i "s/^net.core.somaxconn *=.*/net.core.somaxconn = 262144/g" /etc/sysctl.conf
  76. fi
  77. if [ -z "`grep '^net.core.netdev_max_backlog' /etc/sysctl.conf`" ];then
  78. echo "net.core.netdev_max_backlog = 262144" >> /etc/sysctl.conf
  79. else
  80. sed -i "s/^net.core.netdev_max_backlog *=.*/net.core.netdev_max_backlog = 262144/g" /etc/sysctl.conf
  81. fi
  82. if [ -z "`grep '^net.ipv4.tcp_max_orphans' /etc/sysctl.conf`" ];then
  83. echo "net.ipv4.tcp_max_orphans = 2621444" >> /etc/sysctl.conf
  84. else
  85. sed -i "s/^net.ipv4.tcp_max_orphans *=.*/net.ipv4.tcp_max_orphans = 2621444/g" /etc/sysctl.conf
  86. fi
  87. if [ -z "`grep '^net.ipv4.tcp_max_syn_backlog' /etc/sysctl.conf`" ];then
  88. echo "net.ipv4.tcp_max_syn_backlog = 262144" >> /etc/sysctl.conf
  89. else
  90. sed -i "s/^net.ipv4.tcp_max_syn_backlog *=.*/net.ipv4.tcp_max_syn_backlog = 262144/g" /etc/sysctl.conf
  91. fi
  92. if [ -z "`grep '^net.ipv4.tcp_timestamps' /etc/sysctl.conf`" ];then
  93. echo "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf
  94. else
  95. sed -i "s/^net.ipv4.tcp_timestamps *=.*/net.ipv4.tcp_timestamps = 0/g" /etc/sysctl.conf
  96. fi
  97. if [ -z "`grep '^net.ipv4.tcp_synack_retries' /etc/sysctl.conf`" ];then
  98. echo "net.ipv4.tcp_synack_retries = 1" >> /etc/sysctl.conf
  99. else
  100. sed -i "s/^net.ipv4.tcp_synack_retries *=.*/net.ipv4.tcp_synack_retries = 1/g" /etc/sysctl.conf
  101. fi
  102. if [ -z "`grep '^net.ipv4.tcp_syn_retries' /etc/sysctl.conf`" ];then
  103. echo "net.ipv4.tcp_syn_retries = 1" >> /etc/sysctl.conf
  104. else
  105. sed -i "s/^net.ipv4.tcp_syn_retries *=.*/net.ipv4.tcp_syn_retries = 1/g" /etc/sysctl.conf
  106. fi
  107. if [ -z "`grep '^net.ipv4.tcp_fin_timeout' /etc/sysctl.conf`" ];then
  108. echo "net.ipv4.tcp_fin_timeout = 1" >> /etc/sysctl.conf
  109. else
  110. sed -i "s/^net.ipv4.tcp_fin_timeout *=.*/net.ipv4.tcp_fin_timeout = 1/g" /etc/sysctl.conf
  111. fi
  112. if [ -z "`grep '^net.ipv4.tcp_keepalive_time' /etc/sysctl.conf`" ];then
  113. echo "net.ipv4.tcp_keepalive_time = 1200" >> /etc/sysctl.conf
  114. else
  115. sed -i "s/^net.ipv4.tcp_keepalive_time *=.*/net.ipv4.tcp_keepalive_time = 1200/g" /etc/sysctl.conf
  116. fi
  117. sysctl -p
  118. echo -e "\033[31msysctl.conf to complete the optimization!\033[0m"
  119. }
  120. function configUlimit {
  121. if [ -z "`grep '^ulimit -SHn' /etc/profile`" ];then
  122. echo "ulimit -SHn 65535" >> /etc/profile
  123. else
  124. sed -i "s/^ulimit -SHn.*/ulimit -SHn 65535/g" /etc/profile
  125. fi
  126. if [ -z "`grep '^ulimit -SHu' /etc/profile`" ];then
  127. echo "ulimit -SHu 256716" >> /etc/profile
  128. else
  129. sed -i "s/^ulimit -SHu.*/ulimit -SHu 256716/g" /etc/profile
  130. fi
  131. echo -e "\033[31m\"source /etc/profile\" command to take effect!\033[0m"
  132. }
  133. function installNginx {
  134. echo -e "============================Install Nginx================================="
  135. if [ ! -e "/usr/local/nginx" ];then
  136. yum -y update && yum -y install gcc-c++ zlib-devel openssl--devel pcre-devel
  137. groupadd www && /usr/sbin/useradd -g www www
  138. if [ ! -s "nginx-1.7.9.tar.gz" ];then
  139. #http://nginx.org/download/nginx-1.7.9.tar.gz
  140. wget -c ${DOWNURL}nginx-1.7.9.tar.gz
  141. fi
  142. tar zxvf nginx-1.7.9.tar.gz
  143. cd nginx-1.7.9
  144. ./configure
  145. make && make install
  146. if [ $CPU_PROCESSOR -ge 8 ];then
  147. workerCA="00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000"
  148. workerP=8
  149. elif [ $CPU_PROCESSOR -ge 4 ] && [ $CPU_PROCESSOR -lt 8 ];then
  150. workerCA="0001 0010 0100 1000"
  151. workerP=4
  152. else
  153. workerCA="1"
  154. workerP=1
  155. fi
  156. sed -i '1,$d' /usr/local/nginx/conf/nginx.conf
  157. echo -e "user www;\nworker_processes ${workerP};\nworker_cpu_affinity ${workerCA};\nerror_log logs/error.log;\npid logs/nginx.pid;\nworker_rlimit_nofile 102400;\nevents {\n\tuse epoll;\n\tworker_connections 10240;\n}\nhttp {\n\tinclude mime.types;\n\tdefault_type application/octet-stream;\n\tclient_header_buffer_size 64k;\n\tlarge_client_header_buffers 4 64k;\n\tclient_max_body_size 300m;\n\tserver_tokens off;\n\tsendfile on;\n\ttcp_nopush on;\n\tgzip on;\n\tgzip_disable 'MSIE [1-6].';\n\tkeepalive_timeout 60;\n\topen_file_cache max=102400 inactive=20s;\n\topen_file_cache_valid 30s;\n\topen_file_cache_min_uses 1;\n\tinclude vhost/*.conf;\n}" > /usr/local/nginx/conf/nginx.conf
  158. mkdir -p /usr/local/nginx/conf/vhost
  159. /usr/local/nginx/sbin/nginx &
  160. echo -e "\033[31mNginx complete installation!\033[0m"
  161. cleanFiles nginx-1.7.9
  162. else
  163. echo -e "\033[31mNginx is already installed!\033[0m"
  164. fi
  165. }
  166. function updateNginx {
  167. if [ ! -s "nginx-1.9.0.tar.gz" ];then
  168. #http://nginx.org/download/nginx-1.9.0.tar.gz
  169. wget -c ${DOWNURL}nginx-1.9.0.tar.gz
  170. fi
  171. tar zxvf nginx-1.9.0.tar.gz
  172. cd nginx-1.9.0
  173. ./configure
  174. make
  175. mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
  176. cp -f objs/nginx /usr/local/nginx/sbin/
  177. /usr/local/nginx/sbin/nginx -s reload
  178. echo -e "\033[31mNginx complete upgrade!\033[0m"
  179. cleanFiles nginx-1.9.0
  180. }
  181. function configVhost {
  182. while :
  183. do
  184. read -p "Please input domain(example: www.test.com): " domain
  185. if [ -z "`echo $domain | grep -Pix '^([a-z0-9]+[a-z0-9_]*[a-z0-9]+(\.)?)[a-z0-9]+[a-z0-9_]*(\.org\.cn|\.net|\.com|\.com\.cn)$'`" ]; then
  186. echo -e "\033[31minput error! \033[0m"
  187. else
  188. if [ ! -f "/usr/local/nginx/conf/vhost/${domain}.conf" ]; then
  189. echo -e "\033[31mdomain=${domain}\033[0m"
  190. else
  191. echo -e "\033[31m${domain} is exist!\033[0m"
  192. fi
  193. break
  194. fi
  195. done
  196. while :
  197. do
  198. echo "Please input the directory for the domain:$domain :"
  199. read -p "(Default directory: /data/$domain): " vhostdir
  200. if [ "$vhostdir" = "" ]; then
  201. vhostdir="/data/$domain"
  202. fi
  203. if [ -z "`echo $vhostdir | grep -Pix '(\/[\w\.]+)+'`" ]; then
  204. echo -e "\033[31minput error! \033[0m"
  205. else
  206. if [ ! -e "$vhostdir" ]; then
  207. mkdir -p $vhostdir
  208. echo -e "\033[31mCreate Virtul Host directory ${vhostdir}\033[0m"
  209. else
  210. echo -e "\033[31m${vhostdir} is exist!\033[0m"
  211. fi
  212. chown -R $USER.$USER $vhostdir
  213. break
  214. fi
  215. done
  216. echo -e "server {\n\tlisten 80 default_server;\n\tlocation / {\n\t\treturn 403;\n\t}\n}\nserver\n{\n\tlisten 80;\n\tserver_name ${domain};\n\tindex index.html index.php;\n\troot ${vhostdir};\n\tlocation / {\n\t}\n\tlocation ~ .*\.(php|php5)?$\n\t{\n\t\tfastcgi_pass 127.0.0.1:9000;\n\t\tfastcgi_send_timeout 3600s;\n\t\tfastcgi_connect_timeout 3600s;\n\t\tfastcgi_read_timeout 3600s;\n\t\tfastcgi_index index.php;\n\t\tfastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\t\tinclude fastcgi_params;\n\t}\n\tlocation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$\n\t{\n\t\texpires 30d;\n\t\taccess_log off;\n\t}\n\tlocation ~ .*\.(js|css)?$\n\t{\n\t\texpires 12h;\n\t\taccess_log off;\n\t}\n}" > /usr/local/nginx/conf/vhost/$domain.conf
  217. kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
  218. }
  219. function installPHP {
  220. if [ ! -e "/usr/local/php/bin/php" ];then
  221. yum -y install gcc gcc-c++ libxml2-devel.x86_64 autoconf libjpeg-devel freetype-devel.x86_64 zlib-devel.x86_64 glibc-devel.x86_64 glib2-devel.x86_64 libpng-devel.x86_64 libcurl-devel.x86_64
  222. if [ ! -s "php-5.5.24.tar.gz" ];then
  223. #http://cn2.php.net/distributions/php-5.5.24.tar.gz
  224. wget -c ${DOWNURL}php-5.5.24.tar.gz
  225. fi
  226. tar zxvf php-5.5.24.tar.gz
  227. cd php-5.5.24
  228. ./configure --prefix=/usr/local/php --with-curl --enable-mbstring --with-mysql=mysqlnd --with-mysqli --enable-opcache --with-pdo-mysql --with-iconv --with-gd --enable-fpm --with-jpeg-dir --with-png-dir --enable-zip --with-freetype-dir --with-gettext --enable-gd-native-ttf --without-pdo-sqlite --without-sqlite3
  229. make && make install
  230. cp -f php.ini-production /usr/local/php/lib/php.ini
  231. sed -i '{
  232. s/;date.timezone *=.*/date.timezone = PRC/g
  233. s/upload_max_filesize *=.*/upload_max_filesize = 5M/g
  234. s/memory_limit *=.*/memory_limit = 5120M/g
  235. s/post_max_size *=.*/post_max_size = 100M/g
  236. s/expose_php *=.*/expose_php = Off/g
  237. s/; extension_dir = ".\/"/extension_dir = "\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20121212\/"/g
  238. }' /usr/local/php/lib/php.ini
  239. cp -f /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  240. /bin/mkdir -p /usr/local/php/log/
  241. sed -i '1,$d' /usr/local/php/etc/php-fpm.conf
  242. echo -e "[global]\nerror_log = /usr/local/php/log/error.log\nlog_level = warning\n[www]\nuser = www\ngroup = www\nlisten = 127.0.0.1:9000\npm = dynamic\npm.max_children = 2000\npm.start_servers = 10\npm.min_spare_servers = 5\npm.max_spare_servers = 200\npm.max_requests = 12000\npm.process_idle_timeout = 10s\nrequest_terminate_timeout = 300s\nrequest_slowlog_timeout = 10s\nslowlog = /usr/local/php/log/slow.log" > /usr/local/php/etc/php-fpm.conf
  243. cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  244. chmod 700 /etc/init.d/php-fpm
  245. chkconfig --add php-fpm
  246. chkconfig php-fpm on
  247. php_version=`/usr/local/php/bin/php -v |awk 'NR==1 {print $2}'`
  248. echo -e "\033[31mPHP${php_version} complete installation!\033[0m"
  249. cleanFiles php-5.5.24
  250. else
  251. php_version=`/usr/local/php/bin/php -v |awk 'NR==1 {print $2}'`
  252. echo -e "\033[31mPHP${php_version} is already installed!\033[0m"
  253. fi
  254. }
  255. function updatePHP {
  256. php_version=`/usr/local/php/bin/php -v |awk 'NR==1 {print $2}'`
  257. if [ $php_version == "5.5.24" ];then
  258. echo -e "\033[31mPHP${php_version} is the latest version!\033[0m"
  259. return
  260. fi
  261. if [ -e "/usr/local/php/bin/php" ];then
  262. /bin/rm -rf /usr/local/php/
  263. yum -y install gcc gcc-c++ libxml2-devel.x86_64 autoconf libjpeg-devel freetype-devel.x86_64 zlib-devel.x86_64 glibc-devel.x86_64 glib2-devel.x86_64 libpng-devel.x86_64 libcurl-devel.x86_64
  264. if [ ! -s "php-5.5.24.tar.gz" ];then
  265. #http://cn2.php.net/distributions/php-5.5.24.tar.gz
  266. wget -c ${DOWNURL}php-5.5.24.tar.gz
  267. fi
  268. tar zxvf php-5.5.24.tar.gz
  269. cd php-5.5.24
  270. ./configure --prefix=/usr/local/php --with-curl --enable-mbstring --with-mysql=mysqlnd --with-mysqli --enable-opcache --with-pdo-mysql --with-iconv --with-gd --enable-fpm --with-jpeg-dir --with-png-dir --enable-zip --with-freetype-dir --with-gettext --enable-gd-native-ttf --without-pdo-sqlite --without-sqlite3
  271. make && make install
  272. cp -f php.ini-production /usr/local/php/lib/php.ini
  273. sed -i '{
  274. s/;date.timezone *=.*/date.timezone = PRC/g
  275. s/upload_max_filesize *=.*/upload_max_filesize = 5M/g
  276. s/memory_limit *=.*/memory_limit = 5120M/g
  277. s/post_max_size *=.*/post_max_size = 100M/g
  278. s/expose_php *=.*/expose_php = Off/g
  279. s/; extension_dir = ".\/"/extension_dir = "\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20121212\/"/g
  280. }' /usr/local/php/lib/php.ini
  281. cp -f /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  282. /bin/mkdir -p /usr/local/php/log/
  283. sed -i '1,$d' /usr/local/php/etc/php-fpm.conf
  284. echo -e "[global]\nerror_log = /usr/local/php/log/error.log\nlog_level = warning\n[www]\nuser = www\ngroup = www\nlisten = 127.0.0.1:9000\npm = dynamic\npm.max_children = 2000\npm.start_servers = 10\npm.min_spare_servers = 5\npm.max_spare_servers = 200\npm.max_requests = 12000\npm.process_idle_timeout = 10s\nrequest_terminate_timeout = 300s\nrequest_slowlog_timeout = 10s\nslowlog = /usr/local/php/log/slow.log" > /usr/local/php/etc/php-fpm.conf
  285. cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  286. chmod 700 /etc/init.d/php-fpm
  287. chkconfig --add php-fpm
  288. chkconfig php-fpm on
  289. php_version=`/usr/local/php/bin/php -v |awk 'NR==1 {print $2}'`
  290. echo -e "\033[31mPHP${php_version} complete installation!\033[0m"
  291. cleanFiles php-5.5.24
  292. fi
  293. }
  294. function installPHPRedis {
  295. if [ ! -s "redis-2.2.7.tgz" ];then
  296. #http://pecl.php.net/get/redis-2.2.7.tgz
  297. wget -c ${DOWNURL}redis-2.2.7.tgz
  298. fi
  299. tar zxvf redis-2.2.7.tgz
  300. cd redis-2.2.7
  301. /usr/local/php/bin/phpize
  302. ./configure -with-php-config=/usr/local/php/bin/php-config
  303. make && make install
  304. if [ -z "`grep '^extension=redis.so' /usr/local/php/lib/php.ini`" ];then
  305. sed -i '/;extension=php_xsl.dll/a\extension=redis.so' /usr/local/php/lib/php.ini
  306. fi
  307. /sbin/service php-fpm restart
  308. echo -e "\033[31mRedis extension complete installation!\033[0m"
  309. cleanFiles redis-2.2.7
  310. }
  311. function installPHPMongo {
  312. if [ ! -s "mongo-1.6.8.tgz" ];then
  313. #http://pecl.php.net/get/mongo-1.6.8.tgz
  314. wget -c ${DOWNURL}mongo-1.6.8.tgz
  315. fi
  316. tar zxvf mongo-1.6.8.tgz
  317. cd mongo-1.6.8
  318. /usr/local/php/bin/phpize
  319. ./configure -with-php-config=/usr/local/php/bin/php-config
  320. make && make install
  321. if [ -z "`grep '^extension=mongo.so' /usr/local/php/lib/php.ini`" ];then
  322. sed -i '/;extension=php_xsl.dll/a\extension=mongo.so' /usr/local/php/lib/php.ini
  323. fi
  324. /sbin/service php-fpm restart
  325. echo -e "\033[31mMongo extension complete installation!\033[0m"
  326. cleanFiles mongo-1.6.8
  327. }
  328. function installPHPSphinx {
  329. yum -y update && yum -y install libtool autoconf automake mysql-devel libxml2-devel expat-devel
  330. if [ ! -s "coreseek-4.1-beta.tar.gz" ];then
  331. #http://www.coreseek.cn/uploads/csft/4.0/coreseek-4.1-beta.tar.gz
  332. wget -c ${DOWNURL}coreseek-4.1-beta.tar.gz
  333. fi
  334. tar zxvf coreseek-4.1-beta.tar.gz
  335. cd coreseek-4.1-beta/csft-4.1/api/libsphinxclient
  336. aclocal
  337. libtoolize --force
  338. automake --add-missing && autoconf && autoheader
  339. ./configure
  340. make && make install
  341. cleanFiles coreseek-4.1-beta
  342. wget -c ${DOWNURL}sphinx-1.3.2.tgz
  343. tar zxvf sphinx-1.3.2.tgz
  344. cd sphinx-1.3.2
  345. /usr/local/php/bin/phpize
  346. ./configure -with-php-config=/usr/local/php/bin/php-config
  347. make && make install
  348. if [ -z "`grep '^extension=sphinx.so' /usr/local/php/lib/php.ini`" ];then
  349. sed -i '/;extension=php_xsl.dll/a\extension=sphinx.so' /usr/local/php/lib/php.ini
  350. fi
  351. /sbin/service php-fpm restart
  352. echo -e "\033[31mSphinx extension complete installation!\033[0m"
  353. cleanFiles sphinx-1.3.2
  354. }
  355. function installPHPXsplit {
  356. if [ ! -s "xsplit-0.0.8.zip" ];then
  357. #wget -O xsplit-0.0.8.zip -c https://github.com/chopins/php-xsplit/archive/master.zip
  358. wget -c ${DOWNURL}xsplit-0.0.8.zip
  359. fi
  360. unzip xsplit-0.0.8.zip
  361. cd php-xsplit-master
  362. /usr/local/php/bin/phpize
  363. ./configure -with-php-config=/usr/local/php/bin/php-config
  364. make && make install
  365. if [ -z "`grep '^extension=xsplit.so' /usr/local/php/lib/php.ini`" ];then
  366. sed -i '/;extension=php_xsl.dll/a\extension=xsplit.so' /usr/local/php/lib/php.ini
  367. fi
  368. /sbin/service php-fpm restart
  369. echo -e "\033[31mXsplit extension complete installation!\033[0m"
  370. cleanFiles php-xsplit-master
  371. }
  372. function installPHPPhalcon {
  373. if [ ! -s "phalcon-2.0.1.zip" ];then
  374. #wget -O phalcon-2.0.1.zip -c https://github.com/phalcon/cphalcon/archive/master.zip
  375. wget -c ${DOWNURL}phalcon-2.0.1.zip
  376. fi
  377. unzip phalcon-2.0.1.zip
  378. cd cphalcon-master/build/64bits
  379. /usr/local/php/bin/phpize
  380. ./configure -with-php-config=/usr/local/php/bin/php-config
  381. make && make install
  382. if [ -z "`grep '^extension=phalcon.so' /usr/local/php/lib/php.ini`" ];then
  383. sed -i '/;extension=php_xsl.dll/a\extension=phalcon.so' /usr/local/php/lib/php.ini
  384. fi
  385. /sbin/service php-fpm restart
  386. echo -e "\033[31mPhalcon extension complete installation!\033[0m"
  387. cleanFiles cphalcon-master
  388. }
  389. function installPHPGmagick {
  390. if [ ! -s "giflib-5.1.0.tar.gz" ];then
  391. #http://cznic.dl.sourceforge.net/project/giflib/giflib-5.1.0.tar.gz
  392. wget -c ${DOWNURL}giflib-5.1.0.tar.gz
  393. fi
  394. tar zxvf giflib-5.1.0.tar.gz
  395. cd giflib-5.1.0
  396. ./configure --prefix=/usr/local/giflib
  397. make && make install
  398. cleanFiles giflib-5.1.0
  399. if [ ! -s "libwebp-0.4.0.tar.gz" ];then
  400. #http://webp.googlecode.com/files/libwebp-0.4.0.tar.gz
  401. wget -c ${DOWNURL}libwebp-0.4.0.tar.gz
  402. fi
  403. tar zxvf libwebp-0.4.0.tar.gz
  404. cd libwebp-0.4.0
  405. ./configure --prefix=/usr/local/libwep --with-gifincludedir=/usr/local/giflib/include/ --with-giflibdir=/usr/local/giflib/lib/
  406. make && make install
  407. cleanFiles libwebp-0.4.0
  408. if [ ! -s "GraphicsMagick-1.3.21.tar.gz" ];then
  409. wget -c http://jaist.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.21/GraphicsMagick-1.3.21.tar.gz
  410. fi
  411. tar zxvf GraphicsMagick-1.3.21.tar.gz
  412. cd GraphicsMagick-1.3.21/
  413. ./configure --prefix=/usr/local/gmagick --enable-shared CPPFLAGS='-I/usr/local/libwep/include' LDFLAGS='-L/usr/local/libwep/lib'
  414. make && make install
  415. cp -f /usr/local/gmagick/bin/gm /usr/bin/
  416. cleanFiles GraphicsMagick-1.3.21
  417. if [ ! -s "gmagick-1.1.7RC3.tgz" ];then
  418. wget -c http://pecl.php.net/get/gmagick-1.1.7RC3.tgz
  419. fi
  420. tar zxvf gmagick-1.1.7RC3.tgz
  421. cd gmagick-1.1.7RC3
  422. /usr/local/php/bin/phpize
  423. ./configure --with-php-config=/usr/local/php/bin/php-config --with-gmagick=/usr/local/gmagick
  424. make && make install
  425. if [ -z "`grep '^extension=gmagick.so' /usr/local/php/lib/php.ini`" ];then
  426. sed -i '/;extension=php_xsl.dll/a\extension=gmagick.so' /usr/local/php/lib/php.ini
  427. fi
  428. /sbin/service php-fpm restart
  429. echo -e "\033[31mGmagick extension complete installation!\033[0m"
  430. cleanFiles gmagick-1.1.7RC3
  431. }
  432. function installPHPSSH2 {
  433. yum -y install openssl openssl-devel
  434. if [ ! -s "libssh2-1.4.3.tar.gz" ];then
  435. wget -c http://7xorsj.dl1.z0.glb.clouddn.com/libssh2-1.4.3.tar.gz
  436. fi
  437. tar zxvf libssh2-1.4.3.tar.gz
  438. cd libssh2-1.4.3
  439. ./configure --prefix=/usr/local/libssh2
  440. make && make install
  441. cleanFiles libssh2-1.4.3
  442. if [ ! -s "ssh2-0.12.tgz" ];then
  443. wget -c http://pecl.php.net/get/ssh2-0.12.tgz
  444. fi
  445. tar zxvf ssh2-0.12.tgz
  446. cd ssh2-0.12
  447. /usr/local/php/bin/phpize
  448. ./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2 --with-php-config=/usr/local/php/bin/php-config
  449. make && make install
  450. if [ -z "`grep '^extension=ssh2.so' /usr/local/php/lib/php.ini`" ];then
  451. sed -i '/;extension=php_xsl.dll/a\extension=ssh2.so' /usr/local/php/lib/php.ini
  452. fi
  453. /sbin/service php-fpm restart
  454. echo -e "\033[31mSSH2 extension complete installation!\033[0m"
  455. cleanFiles ssh2-0.12
  456. }
  457. function installMysql {
  458. yum -y update && yum -y downgrade ncurses* && yum -y install make gcc-c++ cmake bison-devel ncurses-devel
  459. if [ ! -e "/usr/local/mysql" ];then
  460. if [ ! -s "mysql-5.6.20.tar.gz" ];then
  461. wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.20.tar.gz
  462. fi
  463. tar zxvf mysql-5.6.20.tar.gz
  464. cd mysql-5.6.20
  465. read -p "Please input the root password of mysql:" mysqlrootpwd
  466. if [ "$mysqlrootpwd" = "" ]; then
  467. mysqlrootpwd="root"
  468. fi
  469. while :
  470. do
  471. read -p "Please input the directory for the mysql data(example: /data/mysql): " mysqldatadir
  472. if [ "$mysqldatadir" = "" ]; then
  473. mysqldatadir="/data/mysql"
  474. fi
  475. if [ -z "`echo $mysqldatadir | grep -Pix '(\/[\w\.]+)+'`" ]; then
  476. echo -e "\033[31minput error! \033[0m"
  477. else
  478. if [ ! -e "$mysqldatadir" ]; then
  479. mkdir -p $mysqldatadir
  480. echo -e "\033[31mCreate directory ${mysqldatadir}\033[0m"
  481. else
  482. echo -e "\033[31m${mysqldatadir} is exist!\033[0m"
  483. fi
  484. break
  485. fi
  486. done
  487. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=$mysqldatadir -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
  488. make && make install
  489. groupadd mysql && useradd -g mysql mysql
  490. chown -R mysql:mysql /usr/local/mysql
  491. cd /usr/local/mysql
  492. scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$mysqldatadir --user=mysql
  493. rm -rf /etc/my.cnf
  494. mv /usr/local/mysql/my.cnf /etc/
  495. sed -i '1,$d' /etc/my.cnf
  496. ibps=`echo "$mem * 0.8"|bc`
  497. ibps=${ibps%.*}
  498. echo -e "[client]\nport=3306\nsocket=/tmp/mysql.sock\n[mysqld]datadir=${mysqldatadir}\nsocket=/tmp/mysql.sock\nport=3306\nuser=mysql\nsql_mode=\"NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION\"\nlong_query_time=1\nslow_query_log=1\nslow_query_log_file=slow.log\nkey_buffer_size=1024M\nmax_allowed_packet=512M\ntable_open_cache=2048\nsort_buffer_size=64M\nmax_length_for_sort_data=8096\nread_buffer_size=64M\nread_rnd_buffer_size=64M\nmyisam_sort_buffer_size=512M\nthread_cache_size=256\nquery_cache_size=512M\nquery_cache_type=1\nquery_cache_limit=2M\ntmp_table_size=4096M\nthread_concurrency=16\nmyisam-recover=BACKUP,FORCE\nmax_connections=3000\nskip-name-resolve\nback_log=384\nmyisam_max_sort_file_size=10G\nmax_allowed_packet=256M\nwait_timeout=3600\nlog-bin=mysql-bin\nbinlog_format=mixed\nserver-id=811\nexpire_logs_days=0\ninnodb_buffer_pool_size=${ibps}G\ninnodb_flush_log_at_trx_commit=0\n[mysqld_safe]\nlog-error=mysql.log" > /etc/my.cnf
  499. cp -f support-files/mysql.server /etc/init.d/mysql
  500. chkconfig mysql on
  501. service mysql start
  502. /usr/local/mysql/bin/mysql -uroot -e "SET PASSWORD = PASSWORD(\"${mysqlrootpwd}\");GRANT ALL PRIVILEGES ON *.* TO root@\"%\" IDENTIFIED BY \"${mysqlrootpwd}\" WITH GRANT OPTION;"
  503. echo -e "\033[31mMysql complete installation!\033[0m"
  504. cleanFiles mysql-5.6.20
  505. else
  506. echo -e "\033[31mMysql is already installed!\033[0m"
  507. fi
  508. }
  509. function toContinue {
  510. echo -e "\033[41;33mPress any key to continue...\033[0m"
  511. read -n1
  512. clear
  513. }
  514. function cleanFiles {
  515. if [ $# -eq 1 ] && [ -e "${BASEPATH}/$1" ];then
  516. /bin/rm -rf ${BASEPATH}/$1 &
  517. fi
  518. cd ${BASEPATH}
  519. }
  520. function startService {
  521. /usr/local/nginx/sbin/nginx &
  522. /sbin/service php-fpm start
  523. /sbin/service mysql start
  524. }
  525. function stopService {
  526. kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
  527. /sbin/service php-fpm stop
  528. /sbin/service mysql stop
  529. }
  530. function restartService {
  531. kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
  532. /sbin/service php-fpm restart
  533. /sbin/service mysql restart
  534. }
  535. function menu {
  536. while true
  537. do
  538. system
  539. process
  540. echo -e "\033[41;33m|-----------Menu-----------\033[0m"
  541. echo -e "(1) Configure \033[1mSystem Kernel\033[0m"
  542. echo -e "(2) Configure \033[1mNGINX\033[0m Service"
  543. echo -e "(3) Configure \033[1mPHP\033[0m Service"
  544. echo -e "(4) Configure \033[1mMYSQL\033[0m Service"
  545. echo -e "(0) Quit"
  546. read -p "Please enter your choice[0-4]: " input
  547. case $input in
  548. 1)
  549. clear
  550. while true
  551. do
  552. system
  553. echo -e "\033[41;33m|-----------Configure System Kernel-----------\033[0m"
  554. echo -e "(1) Configure /etc/sysctl.conf"
  555. echo -e "(2) Configure ulimit"
  556. echo -e "(0) Back"
  557. read -p "Please enter your choice[0-2]: " input1
  558. case $input1 in
  559. 1)
  560. configSysctl
  561. toContinue
  562. ;;
  563. 2)
  564. configUlimit
  565. exit 0
  566. ;;
  567. 0)
  568. clear
  569. break
  570. ;;
  571. *)
  572. echo -e "\033[31mPlease Enter Right Choice!\033[0m"
  573. toContinue
  574. ;;
  575. esac
  576. done
  577. ;;
  578. 2)
  579. clear
  580. while true
  581. do
  582. system
  583. echo -e "\033[41;33m|-----------Configure NGINX Service-----------\033[0m"
  584. echo -e "(1) Install the latest version nginx"
  585. echo -e "(2) Seamless upgrade nginx"
  586. echo -e "(3) Configure the virtual host"
  587. echo -e "(0) Back"
  588. read -p "Please enter your choice[0-3]: " input2
  589. case $input2 in
  590. 1)
  591. installNginx
  592. toContinue
  593. ;;
  594. 2)
  595. updateNginx
  596. toContinue
  597. ;;
  598. 3)
  599. configVhost
  600. toContinue
  601. ;;
  602. 0)
  603. clear
  604. break
  605. ;;
  606. *)
  607. echo -e "\033[31mPlease Enter Right Choice!\033[0m"
  608. toContinue
  609. ;;
  610. esac
  611. done
  612. ;;
  613. 3)
  614. clear
  615. while true
  616. do
  617. system
  618. echo -e "\033[41;33m|-----------Configure PHP Service-----------\033[0m"
  619. echo -e "(1) Install the Old Stable \033[1mPHP 5.5.24\033[0m"
  620. echo -e "(2) Upgrade to Old Stable \033[1mPHP 5.5.24\033[0m"
  621. echo -e "(3) Install the latest \033[1mredis\033[0m extension of PHP"
  622. echo -e "(4) Install the latest \033[1mmongoDB\033[0m extension of PHP"
  623. echo -e "(5) Install the latest \033[1msphinx\033[0m extension of PHP"
  624. echo -e "(6) Install the latest \033[1mxsplit\033[0m extension of PHP"
  625. echo -e "(7) Install the latest \033[1mphalcon\033[0m extension of PHP"
  626. echo -e "(8) Install the latest \033[1mgmagick\033[0m extension of PHP"
  627. echo -e "(9) Install the latest \033[1mssh2\033[0m extension of PHP"
  628. echo -e "(0) Back"
  629. read -p "Please enter your choice[0-9]: " input3
  630. case $input3 in
  631. 1)
  632. installPHP
  633. toContinue
  634. ;;
  635. 2)
  636. updatePHP
  637. toContinue
  638. ;;
  639. 3)
  640. installPHPRedis
  641. toContinue
  642. ;;
  643. 4)
  644. installPHPMongo
  645. toContinue
  646. ;;
  647. 5)
  648. installPHPSphinx
  649. toContinue
  650. ;;
  651. 6)
  652. installPHPXsplit
  653. toContinue
  654. ;;
  655. 7)
  656. installPHPPhalcon
  657. toContinue
  658. ;;
  659. 8)
  660. installPHPGmagick
  661. toContinue
  662. ;;
  663. 9)
  664. installPHPSSH2
  665. toContinue
  666. ;;
  667. 0)
  668. clear
  669. break
  670. ;;
  671. *)
  672. echo -e "\033[31mPlease Enter Right Choice!\033[0m"
  673. toContinue
  674. ;;
  675. esac
  676. done
  677. ;;
  678. 4)
  679. clear
  680. while true
  681. do
  682. system
  683. echo -e "\033[41;33m|-----------Configure MYSQL Service-----------\033[0m"
  684. echo -e "(1) Install the latest version MYSQL"
  685. echo -e "(0) Back"
  686. read -p "Please enter your choice[0-1]: " input4
  687. case $input4 in
  688. 1)
  689. installMysql
  690. toContinue
  691. ;;
  692. 0)
  693. clear
  694. break
  695. ;;
  696. *)
  697. echo -e "\033[31mPlease Enter Right Choice!\033[0m"
  698. toContinue
  699. ;;
  700. esac
  701. done
  702. ;;
  703. 0)
  704. clear
  705. break
  706. ;;
  707. *)
  708. echo -e "\033[31mPlease Enter Right Choice!\033[0m"
  709. toContinue
  710. ;;
  711. esac
  712. done
  713. }
  714. #start Judge script parameters
  715. if [ "$1" != "" ];then
  716. arr=($1)
  717. for i in ${arr[@]}
  718. do
  719. $i >> install.log
  720. done
  721. else
  722. clear
  723. menu
  724. fi