Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99948
  • 博文数量: 33
  • 博客积分: 101
  • 博客等级: 民兵
  • 技术积分: 185
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-17 22:42
文章分类
文章存档

2013年(1)

2012年(16)

2011年(16)

我的朋友

分类:

2012-01-20 23:58:56

  1. 此脚本在生产服务器上使用了一年多,可以在任何一台有网络的服务器上自动配置LNMP,即Linux+Nginx+MySQL+PHP。本脚本在CentOS 5.6的32位和64位下测试通过,脚本可以自行检查系统的版本和已经安装过的应用。不管安装nginx php和mysql,如果脚本检测系统没有执行脚本的时候,将会自动执行初始化工作。

【51CTO独家特稿】此脚本在生产服务器上使用了一年多,可以在任何一台有网络的服务器上自动配置LNMP。

使用方法:将nginx_php代码保存一个文件,例如nginx_php。上传到服务器上执行。

此脚本在CentOS 5.6 i386/x86_64环境下通过。

bash nginx_php出现提示,如下列图所示。

bash nginx_php提示

根据大家自己的需要选择数字中任意一个,开始全自动安装LNMP的世界中去吧。

如果之前安装过nginx或者 php 的话,脚本会自动检测它们是不是安装过。例如

就会提示nginx或者php 已经安装过了。

  1. #!/bin/bash
  2. # author:coralzd powered by
  3. # written by coralzd 2011.09.30
  4. # version 0.1.5 build 20110930

  5. nginx_dir="/usr/local/nginx"
  6. php52_dir="/usr/local/php52"
  7. mysql_dir="/usr/local/mysql"

  8. function check_init() #检测初始化工作有没有执行
  9. {
  10. if [ `rpm -qa |grep re2c` == "re2c-0.12.1-2.el5.x86_64" ];then
  11. echo "init already exec!"
  12. exit 0
  13. fi
  14. }

  15. function check_nginx() #检测nginx有没有安装
  16. {
  17. if [ -d /usr/local/nginx/ ];then
  18. echo "nginx already installed!"
  19. exit 1
  20. fi
  21. }

  22. function check_php52() #检测php有没有安装
  23. {
  24. if [ -d /usr/local/php52 ];then
  25. echo "php 5.2.17 already installed!"
  26. exit 1
  27. fi
  28. }

  29. function check_mysql() #检测mysql有没有安装
  30. {
  31. if [ -d /usr/local/mysql/ ];then
  32. echo -n "mysql already installed!"
  33. exit 1
  34. fi
  35. }
  36. function init() #系统初始化工作
  37. {
  38. yum -y install wget gcc gcc-c++ autoconf bison flex re2c libmhash libjpeg libjpeg-devel libpng libpng-devel \
  39. freetype freetype-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel \
  40. bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel

  41. echo -n "starting download nginx_php ..."
  42. cat > list << "EOF" &&
  43. nginx-1.0.1.tar.gz
  44. php-5.2.17.tar.gz
  45. php-5.2.17-fpm-0.5.14.diff.gz
  46. libiconv-1.13.1.tar.gz
  47. libmcrypt-2.5.8.tar.gz
  48. mcrypt-2.6.8.tar.gz
  49. memcache-2.2.5.tgz
  50. mhash-0.9.9.9.tar.gz
  51. mysql-5.1.58.tar.gz
  52. suhosin-patch-5.2.17-0.9.7.patch
  53. pcre-8.10.tar.gz
  54. autoconf-2.13.tar.gz
  55. eaccelerator-0.9.5.3.tar.bz2
  56. PDO_MYSQL-1.0.2.tgz
  57. libunwind-0.99.tar.gz
  58. ImageMagick.tar.gz
  59. imagick-2.3.0.tgz
  60. google-perftools-1.6.tar.gz
  61. fcgi.conf
  62. php.ini
  63. php-fpm
  64. nginxd
  65. my.cnf
  66. nginx.conf
  67. php-fpm.conf
  68. EOF
  69. mkdir packages
  70. for i in `cat list`
  71. do
  72. if [ -s packages/$i ]; then
  73. echo "$i [found]"
  74. else
  75. echo "Error: $i not found!!!download now......"
  76. wget http:///linux/nginx-php/$i -P packages/
  77. fi

  78. done
  79. mv packages/nginxd /etc/init.d/
  80. mv packages/php-fpm /etc/init.d/
  81. chmod 755 /etc/init.d/*
  82. chkconfig --add nginxd
  83. chkconfig --add php-fpm
  84. echo "create the eaccelerator directory to /data0/cache"
  85. mkdir /data0/cache
  86. echo "create the mysql data directory to /data0/mysql/data"
  87. mkdir -p /data0/mysql/data
  88. echo "PATH=/usr/local/mysql/bin/:$PATH" >> /etc/profile
  89. source /etc/profile
  90. groupadd -g 80 www && useradd www -s /sbin/nologin -g www -u 80
  91. groupadd -g 3306 mysql && useradd mysql -s /sbin/nologin -g mysql -u 3306
  92. echo "www and mysql user && group create!"
  93. cd packages/
  94. /bin/rm -rf list
  95. echo -e "All of installed sucussful!"

  96. }
  97. function is_version() #检测系统是64位的还是32位的?
  98. {
  99. if [ `uname -m` == "x86_64" ];then
  100. tar zxf libunwind-0.99.tar.gz
  101. tar zxvf libunwind-0.99.tar.gz
  102. cd libunwind-0.99/
  103. CFLAGS=-fPIC ./configure
  104. make CFLAGS=-fPIC
  105. make CFLAGS=-fPIC install
  106. cd ../
  107. else
  108. echo "your system is 32bit ,not install libunwind lib!"
  109. fi

  110. }
  111. function ins_nginx()#安装nginx
  112. {
  113. check_nginx
  114. cd packages/
  115. is_version
  116. tar zxf google-perftools-1.6.tar.gz
  117. cd google-perftools*
  118. ./configure
  119. make
  120. make install
  121. cd ..
  122. tar zxf pcre-8.10.tar.gz
  123. cd pcre-*
  124. ./configure
  125. make
  126. make install
  127. cd ..
  128. tar zxf nginx-1.0.1.tar.gz
  129. cd nginx-1.0.1
  130. ./configure --prefix=${nginx_dir} --user=www --group=www --with-http_stub_status_module
  131. make && make install
  132. cd ..
  133. rm -rf /usr/local/nginx/conf/nginx.conf
  134. echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
  135. cp nginx.conf /usr/local/nginx/conf/
  136. cp fcgi.conf /usr/local/nginx/conf/
  137. echo "nginx installed sucussfully!"
  138. }
  139. function ins_mysqlserver()# 安装mysql服务端
  140. {
  141. cd packages/
  142. tar zxf mysql-5.1.58.tar.gz
  143. cd mysql-5.1.58
  144. CHOST="x86_64-pc-linux-gnu"
  145. CFLAGS="-march=nocona -O2 -pipe"
  146. CXXFLAGS="${CFLAGS}"
  147. ./configure --prefix=/usr/local/mysql --enable-assembler --with-server-suffix=-DZWWW --enable-thread-safe-client --enable-local-infile --enable-thread-safe-client --with-big-tables --with-charset=utf8 --with-client-ldflags=-all-static --with-collation=utf8_general_ci --with-extra-charsets=all --with-mysqld-ldflags=-all-static --with-mysqld-ldflags=-ltcmalloc --with-mysqld-user=mysql --with-plugins=partition,myisammrg --with-pthread --with-unix-socket-path=/tmp/mysql.sock --without-ndb-debug
  148. make && make install
  149. cp support-*/mysql.server /etc/init.d/mysqld
  150. cp my.cnf /etc/
  151. chmod 744 /etc/init.d/mysqld
  152. cd /usr/local/mysql
  153. chown -R mysql:mysql .
  154. rm -rf sql-bench mysql-test
  155. mkdir -p /data0/mysql/relaylog/
  156. mkdir -p /data0/mysql/binlog/
  157. chown -R mysql.mysql /data0/mysql
  158. /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data0/mysql/data
  159. echo "mysql server 5.1.58 installed successfully!"
  160. }
  161. function ins_mysqlclient() #安装mysql客户端
  162. {
  163. cd packages/
  164. tar zxf mysql-5.1.58.tar.gz
  165. cd mysql-5.1.58
  166. CHOST="x86_64-pc-linux-gnu"
  167. CFLAGS="-march=nocona -O2 -pipe"
  168. CXXFLAGS="${CFLAGS}"
  169. ./configure "--prefix=${mysql_dir}" "--with-mysqld-user=mysql" "--without-debug" "--with-charset=utf8" "--with-extra-charsets=all" "--with-pthread" "--with-big-tables" "--enable-thread-safe-client" "--enable-assembler" "--with-readline" "--with-ssl" "--enable-local-infile" "--without-server"
  170. make && make install
  171. cd /usr/local/mysql
  172. chown -R mysql:mysql .
  173. rm -rf sql-bench mysql-test
  174. echo "mysql client 5.1.58 installed successfully!"
  175. }
  176. function ins_php52()#安装php
  177. {
  178. check_php52
  179. cd packages/
  180. tar zxf libiconv-1.13.1.tar.gz
  181. cd libiconv-1.13.1/
  182. ./configure --prefix=/usr/local
  183. make
  184. make install
  185. cd ../
  186. tar zxf autoconf-2.13.tar.gz
  187. cd autoconf-2.13
  188. ./configuire --prefix=/usr
  189. make && make install
  190. cd ..
  191. tar zxf libmcrypt-2.5.8.tar.gz
  192. cd libmcrypt-2.5.8/
  193. ./configure
  194. make
  195. make install
  196. /sbin/ldconfig
  197. cd libltdl/
  198. ./configure --enable-ltdl-install
  199. make
  200. make install
  201. cd ../../
  202. tar zxf mhash-0.9.9.9.tar.gz
  203. cd mhash-0.9.9.9/
  204. ./configure
  205. make
  206. make install
  207. cd ../
  208. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
  209. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
  210. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
  211. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
  212. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
  213. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
  214. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
  215. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
  216. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
  217. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

  218. tar zxf mcrypt-2.6.8.tar.gz
  219. cd mcrypt-2.6.8/
  220. /sbin/ldconfig
  221. ./configure
  222. make
  223. make install
  224. cd ../

  225. tar zxf php-5.2.17.tar.gz
  226. gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
  227. cd php-5.2.17/
  228. patch -p1 -i ../suhosin-patch-5.2.17-0.9.7.patch
  229. ./buildconf --force
  230. ./configure --prefix=${php52_dir} --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=${php52_dir}/etc --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-suhosin --enable-ftp
  231. make ZEND_EXTRA_LIBS='-liconv'
  232. make install
  233. cd ..
  234. cp php.ini /usr/local/php52/etc/
  235. cp php-fpm.conf /usr/local/php52/etc/

  236. echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf.d/mysql_lib.conf
  237. /sbin/ldconfig
  238. echo "php52 installed successfully!"
  239. }

  240. function ins_php52ext() #安装php 扩展
  241. {
  242. cd packages/
  243. tar zxf memcache-2.2.5.tgz
  244. cd memcache-2.2.5/
  245. ${php52_dir}/bin/phpize
  246. ./configure --with-php-config=${php52_dir}/bin/php-config
  247. make
  248. make install
  249. cd ../

  250. tar jxf eaccelerator-0.9.5.3.tar.bz2
  251. cd eaccelerator-0.9.5.3
  252. ${php52_dir}/bin/phpize
  253. ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=${php52_dir}/bin/php-config
  254. make
  255. make install
  256. cd ../

  257. tar zxf ImageMagick.tar.gz
  258. cd ImageMagick-6.5.1-2/
  259. ./configure
  260. make
  261. make install
  262. cd ../
  263. tar zxf imagick-2.3.0.tgz
  264. cd imagick-2.3.0/
  265. ${php52_dir}/bin/phpize
  266. ./configure --with-php-config=${php52_dir}/bin/php-config
  267. make
  268. make install
  269. cd ..

  270. echo "php52 extension installed successfully!"

  271. }
  272. cat << EOF
  273. -* The script is advocating a simple aesthetic, only one file to complete downloading, extract, configure, compile and install Nginx php mysql *-
  274. -* 1 only install nginx *-
  275. -* 2 only install nginx php *-
  276. -* 3 installation of all(include nginx php mysql) *-
  277. -* 4 only download software, no installation other *-
  278. -* 5 only install php 5.2.17 *-
  279. -* 6 only install mysql server 5.1.58 *-
  280. EOF
  281. read -p "Now select the top option to install LNMP...1|2|3|4|5|6:" selectopt
  282. case "$selectopt" in
  283. "1")
  284. init
  285. ins_nginx
  286. ;;
  287. "2")
  288. init
  289. ins_nginx
  290. ins_mysqlclient
  291. ins_php52
  292. ins_php52ext
  293. ;;
  294. "3")
  295. init
  296. ins_nginx
  297. ins_mysqlserver
  298. ins_php52
  299. ins_php52ext
  300. ;;
  301. "4")
  302. init
  303. ;;
  304. "5")
  305. ins_php52
  306. ins_php52ext
  307. ;;
  308. "6")
  309. ins_mysqlserver
  310. ;;
  311. *)
  312. echo "$0 nginx_php"
  313. ;;
  314. esac

nginx_php 各部分功能讲述

不管安装nginx php和mysql,如果脚本检测系统没有执行脚本的时候,将会自动执行初始化工作(包括yum 安装相关组件和下载所有的软件包以及配置nginx php服务)

nginx_php里的函数init 表示初始化,分几步来执行初始化:

1) yum 安装nginx php mysql相关组件

2) 在没有发现当前目录有packages的情况下, wget 下载软件包、配置文档、服务脚本到创建的packages下,之后删除软件列表。

3) 设置nginx和php-fpm服务

4) 创建eaccelerator 缓存目录/data0/cache,创建mysql数据目录/data0/mysql/data

5) 将mysql bin路径加入系统PATH中。

6) 创建myql组和mysql用户,www组和www用户

函数check_init表示,检测初始化工作有没有执行。

函数check_nginx表示,检测nginx有没有安装

函数check_php52表示,检测php有没有安装

函数check_mysql表示,检测mysql有没有安装

函数is_version表示,检测系统是64位的还是32位的,如果是64位的话,就安装libunwind库,因为google-perftools在64位系统中用到了libunwind库,在32位的是不需要安装libunwind库的。

函数ins_nginx表示安装nginx,其中有判断系统是否是64位,以便于顺利安装google-pertfools。

函数ins_mysqlclient表示安装mysql 客户端,这是让php 可以连接mysql server。

函数ins_mysqlserver表示安装mysql 服务端,并设置mysql安装目录为mysql组和mysql用户所有,已经配置mysql系统库。

函数ins_php52表示安装php,包括libiconv、autoconf、libmcrypt、mhash、mcrypt、suhosin等安装。并配置php.ini

函数ins_php52ext表示安装php的扩展,包括mecache、eacceletator、imagick等安装。

【51CTO.com独家特稿,转载请注明原文作者和出处。】

作者简介:崔晓辉(博客),网名coralzd,大众网系统管理员,精通网站系统架构、Unix技术。gtalk:coralzd@gmail.com 。

阅读(1198) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~