dl528888
作者dl528888·2015-03-24 14:11
系统运维工程师·游戏公司

模块化的安装lnmp脚本

字数 34501阅读 1345评论 0赞 0

最近公司的项目很多,研发那里需要的测试环境很多,而且基本都是lnmp的测试环境(也有apache与tomcat,但非常少),测试没有问题之后还需要上线,所以最近我很忙,而且都是重复性的工作,本来我用虚拟机安装一个lnmp的环境,但研发说必须用真实机器进行测试,所以为了偷懒,我只能用lnmp的自动安装脚本了,刚开始使用还可以,但很多的脚本里都不能设置安装路径、软件也是老版本的,所以我又根据我自己的实际需要编写了一份模块化的安装lnmp脚本。

此脚本可以需要单独的安装mysql、nginx、php,还可以选择自动的安装lnmp,并且安装的目录都可以自己设定,很简单与智能化。

本脚本我已经在rhel 5.4 32与64位系统都进行了测试,没有发现问题,并且我在生产环境里也使用了这个脚本,也没有发现问题。

一、准备工作

脚本最新的下载地址为http://pan.baidu.com/share/link?shareid=97808&uk=3892479934

1、把install_lnmp.tar.gz上传的到服务器(我传输的目录是tmp)

解压

  1. [root@localhost tmp]# tar zxf install_lnmp.tar.gz 

查看install_lnmp.sh与soft是否解压

  1. [root@localhost tmp]# ll  
  2. total 64480  
  3. -rwxr-xr-x 1 root root    13726 Mar 25 02:17 install_lnmp.sh  
  4. -rw-r--r-- 1 root root 65911213 Mar 25 02:17 install_lnmp.tar.gz  
  5. srwxr-xr-x 1 root root        0 Mar 23 14:19 mapping-root  
  6. drwxr-xr-x 5 root root     4096 Mar 23 11:54 soft  
  7. drwx------ 2 root root     4096 Mar 23 14:19 ssh-IMPTGZ3620  

运行install_lnmp.sh

  1. [root@localhost tmp]# sh install_lnmp.sh   
  2. Usage:install_lnmp.sh {install_yum|init|install_mysql|install_nginx|install_php|install_lnmp|install_check}  

从输出可以看出,可以使用install_yum、init、install_mysql等命令进行,下面介绍这些命令的含义

install_yum                      如果本机的yum不能使用的时候,可以使用此命令

init                             进行安装所需的库

install_mysql                    进行mysql的安装

install_nginx                    进行nginx的安装

install_php                      进行php的安装

install_lnmp                     进行nginx、mysql、php与所需库文件的安装

install_check                    进行检查是否安装nginx、mysql、php,并输出安装目录

现在我们先进行检查本机是否安装了nginx、mysql、php,这里使用install_check

  1. [root@localhost tmp]# sh install_lnmp.sh install_check  
  2. Sun Mar 25 02:26:32 EDT 2012 Start install!  
  3. ========================== Check install ================================  
  4. Error: /usr/local/nginx not found!!!  
  5. Error: /usr/local/php not found!!!  
  6. Error: /usr/local/mysql not found!!!  
  7. ========================== Check install ================================  
  8. Sorry,Failed to install LNMP!  
  9. Please check errors and logs.  
  10. Sun Mar 25 02:26:32 EDT 2012 Finish install!  
  11. Total runtime: 0 Seconds  

从输出可以看出,nginx、mysql、php都没有安装

在进行安装的时候,可以使用nohup来进行后台的安装,并且还有nohup.out目录可以查看安装的情况

二、脚本介绍

 1、下载方法

 

  1. wget http://202.96.42.117/soft/install_lnmp.tar.gz  
  2. tar zxf install_lnmp.tar.gz  

如果上面的下载地址不好使,可以使用以下的地址:http://pan.baidu.com/share/link?shareid=97808&uk=3892479934

2、脚本中软件的版本信息

  1. cmake-2.8.4.tar.gz   
  2. mysql-5.5.10.tar.gz   
  3. libiconv-1.13.1.tar.gz   
  4. libmcrypt-2.5.8.tar.gz   
  5. mhash-0.9.9.9.tar.gz   
  6. mcrypt-2.6.8.tar.gz   
  7. php-5.3.10.tar.gz   
  8. memcache-2.2.5.tgz   
  9. eaccelerator-0.9.6.1.tar.bz2   
  10. PDO_MYSQL-1.0.2.tgz   
  11. ImageMagick-6.6.7-10.tar.gz   
  12. imagick-2.3.0.tgz   
  13. pcre-8.12.tar.gz   
  14. nginx-1.0.12.tar.gz   
  15. ngx_cache_purge-1.3.tar.gz   

3、脚本介绍

  1. #!/bin/bash  
  2. #author dl528888
  3. #blog http://dl528888.blog.51cto.com
  4. LANG=C 
  5. installhere="/data/software"  #脚本与软件包存放的地方
  6. nginx_dir="/usr/local/nginx"  #nginx的安装目录
  7. php_dir="/usr/local/php"  #php的安装目录
  8. mysql_dir="/usr/local/mysql"  #mysql的安装目录
  9. mysql_datadir="/data/mysql/data" #mysql的数据存放目录
  10. mysql_logdir="/data/mysql"  #mysql的日志目录
  11. mysql_passwd="admin"  #mysql的登陆密码
  12. # Check if user is root   #脚本需要在root用户下运行,所以先进行用户监测
  13. if [ $(id -u) != "0" ]; then  
  14.     echo "Error: You must be root to run this script, please use root to install soft"  
  15.     exit 1  
  16. fi  
  17. #Disable SeLinux   #关闭selinux
  18. if [ -s /etc/selinux/config ]; then  
  19. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config  
  20. fi  
  21. if [ ! -d "$installhere" ];then  #如果脚本存放的目录不存在,就自动的创建
  22.     mkdir -p $installhere  
  23. fi  
  24. if [ ! -d "$installhere/soft" ];then  #如果脚本不在那个存放的目录里,则复制过去
  25.     cp -a soft $installhere  
  26. fi  
  27. #set up runtime   #进行运行时间的统计
  28. function start_time()  
  29. {  
  30. start_time="$(date +%s)" 
  31. echo "$(date) Start install!"  
  32. echo "$start_time" > /tmp/Install_lnmp_runtime  
  33. }  
  34. function end_time()  
  35. {  
  36. end_time="$(date +%s)" 
  37. total_s=$(($end_time - $start_time))  
  38. total_m=$(($total_s / 60))  
  39. if [ $total_s -lt 60 ]; then  
  40.     time_en="${total_s} Seconds" 
  41. else  
  42.     time_en="${total_m} Minutes" 
  43. fi  
  44. echo "$(date) Finish install!"  
  45. echo "Install_lnmp.sh runtime: ${time_en} "> /tmp/Install_lnmp_runtime  
  46. echo "Total runtime: ${time_en}"  
  47. }  
  48. #if yum fail,please use install_yum to solve.  如果yum不可用,可以使用此模块来进行安装yum
  49. function install_yum()  
  50. {  
  51.  wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm  
  52.  wget http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt  
  53. rpm -Uvh rpmforge-release-0.5.1-1.el5.rf.i386.rpm  
  54. rpm --import RPM-GPG-KEY.dag.txt  
  55. yum -y install yum-fastestmirror yum-presto  
  56. }  
  57. #init set up Library 安装lnmp需要的库
  58. function init()  
  59. {  
  60. yum -y install yum-fastestmirror yum-presto  
  61. yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel  openldap-clients openldap-servers libxslt-devel libevent-devel ntp  libtool-ltdl bison libtool vim-enhanced  
  62. }  
  63. #install mysql  安装mysql的模块
  64. function install_mysql()  
  65. {  
  66. cd $installhere/soft/mysql/  
  67. useradd -M -s /sbin/nologin mysql  
  68. mkdir -p $mysql_datadir;  
  69. chown mysql.mysql -R $mysql_datadir  
  70. tar xzf cmake-2.8.4.tar.gz  
  71. cd cmake-2.8.4  
  72. ./configure  
  73. make &&  make install  
  74. cd ..  
  75. tar zxf mysql-5.5.10.tar.gz  
  76. cd mysql-5.5.10  
  77. cmake . -DCMAKE_INSTALL_PREFIX=$mysql_dir/   
  78. -DMYSQL_DATADIR=$mysql_datadir   
  79. -DMYSQL_UNIX_ADDR=$mysql_logdir/mysqld.sock   
  80. -DWITH_INNOBASE_STORAGE_ENGINE=1   
  81. -DENABLED_LOCAL_INFILE=1   
  82. -DMYSQL_TCP_PORT=3306   
  83. -DCMAKE_THREAD_PREFER_PTHREAD=1   
  84. -DEXTRA_CHARSETS=all   
  85. -DDEFAULT_CHARSET=utf8   
  86. -DDEFAULT_COLLATION=utf8_general_ci   
  87. -DMYSQL_UNIX_ADDR=$mysql_logdir/mysql.sock   
  88. -DWITH_DEBUG=0 
  89. make && make install  
  90. rm -rf /etc/my.cnf  
  91. rm -rf /etc/init.d/mysqld  
  92. mkdir $mysql_logdir/relaylog  
  93. mkdir $mysql_logdir/binlog  
  94. cp $installhere/soft/mysql/my.cnf /etc/my.cnf  
  95. cp support-files/mysql.server /etc/init.d/mysqld  
  96. chmod 755 /etc/init.d/mysqld  
  97. chkconfig --add mysqld  
  98. chkconfig mysqld on  
  99. chown mysql.mysql -R $mysql_logdir  
  100. chown mysql.mysql -R $mysql_datadir  
  101. $mysql_dir/scripts/mysql_install_db --user=mysql --basedir=$mysql_dir --datadir=$mysql_datadir  
  102. /sbin/service mysqld start  
  103. echo 'export PATH=$PATH:'$mysql_dir'/bin' >> /etc/profile  
  104.  
  105. $mysql_dir/bin/mysql -e "grant all privileges on *.* to root@'%' identified by '$mysql_passwd' with grant option;"  
  106. $mysql_dir/bin/mysql -e "flush privileges;"  
  107. $mysql_dir/bin/mysql -e "delete from mysql.user where password='';"  
  108. source /etc/profile  
  109. /sbin/service mysqld restart  
  110. echo "mysql install success!"  
  111. }  
  112. #install php  安装php的模块
  113. function install_php()  
  114. {  
  115. cd $installhere/soft/php  
  116. tar xzf libiconv-1.13.1.tar.gz  
  117. cd libiconv-1.13.1  
  118. ./configure --prefix=/usr/local  
  119. make && make install  
  120. cd ../  
  121. tar xzf libmcrypt-2.5.8.tar.gz  
  122. cd libmcrypt-2.5.8  
  123. ./configure  
  124. make && make install  
  125. /sbin/ldconfig  
  126. cd libltdl/  
  127. ./configure --enable-ltdl-install  
  128. make && make install  
  129. cd ../../  
  130. tar xzf mhash-0.9.9.9.tar.gz  
  131. cd mhash-0.9.9.9  
  132. ./configure  
  133. make && make install  
  134. cd ../  
  135. if [ -e "/lib64" ];then  
  136.     ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la  
  137.     ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so  
  138.     ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4  
  139.     ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8  
  140.     ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a  
  141.     ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la  
  142.     ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so  
  143.     ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2  
  144.     ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1  
  145.     ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config  
  146.     ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /lib64/libmysqlclient.so.18  
  147. else  
  148.     ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la  
  149.     ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so  
  150.     ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4  
  151.     ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8  
  152.     ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a  
  153.     ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la  
  154.     ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so  
  155.     ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2  
  156.     ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1  
  157.     ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config  
  158.     ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /lib/libmysqlclient.so.18  
  159. fi  
  160.  
  161. tar xzf mcrypt-2.6.8.tar.gz  
  162. cd mcrypt-2.6.8  
  163. /sbin/ldconfig  
  164. ./configure  
  165. make && make install  
  166. cd ../  
  167. ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib  
  168. if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then  
  169.         ln -s /usr/lib64/libpng.* /usr/lib/  
  170.         ln -s /usr/lib64/libjpeg.* /usr/lib/  
  171. fi  
  172. if [ ! `grep -l "/lib"    '/etc/ld.so.conf'` ]; then  
  173.     echo "/lib" >> /etc/ld.so.conf  
  174. fi  
  175.  
  176. if [ ! `grep -l '/usr/lib'    '/etc/ld.so.conf'` ]; then  
  177.     echo "/usr/lib" >> /etc/ld.so.conf  
  178. fi  
  179.  
  180. if [ -d "/usr/lib64" ] && [ ! `grep -l '/usr/lib64'    '/etc/ld.so.conf'` ]; then  
  181.     echo "/usr/lib64" >> /etc/ld.so.conf  
  182. fi  
  183.  
  184. if [ ! `grep -l '/usr/local/lib'    '/etc/ld.so.conf'` ]; then  
  185.     echo "/usr/local/lib" >> /etc/ld.so.conf  
  186. fi  
  187. /sbin/ldconfig  
  188. tar xzf php-5.3.10.tar.gz  
  189. useradd -M -s /sbin/nologin www  
  190. cd php-5.3.10  
  191. ./configure  --prefix=$php_dir --with-mysql=$mysql_dir  --with-mysqli=$mysql_dir/bin/mysql_config --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-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --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-ftp --enable-zip --enable-soap --disable-debug  
  192. make ZEND_EXTRA_LIBS='-liconv' 
  193. make install  
  194. cp php.ini-production $php_dir/lib/php.ini  
  195. cd ../  
  196. tar xzf memcache-2.2.5.tgz  
  197. cd memcache-2.2.5  
  198. $php_dir/bin/phpize  
  199. ./configure --with-php-config=$php_dir/bin/php-config  
  200. make && make install  
  201. cd ../  
  202. tar xjf eaccelerator-0.9.6.1.tar.bz2  
  203. cd eaccelerator-0.9.6.1  
  204. /usr/local/php/bin/phpize  
  205. ./configure --enable-eaccelerator=shared --with-php-config=$php_dir/bin/php-config  
  206. make && make install  
  207. cd ../  
  208. tar xzf PDO_MYSQL-1.0.2.tgz  
  209. cd PDO_MYSQL-1.0.2  
  210. $php_dir/bin/phpize  
  211. ./configure --with-php-config=$php_dir/bin/php-config --with-pdo-mysql=$mysql_dir  
  212. make && make install  
  213. cd ../  
  214. tar xzf ImageMagick-6.6.7-10.tar.gz  
  215. cd ImageMagick-6.6.7-10  
  216. ./configure  
  217. make && make install  
  218. cd ../  
  219. tar xzf imagick-2.3.0.tgz  
  220. cd imagick-2.3.0  
  221. /usr/local/php/bin/phpize  
  222. ./configure --with-php-config=$php_dir/bin/php-config  
  223. make && make install  
  224. cd ../  
  225. #Modiry php.ini  
  226. mkdir /tmp/eaccelerator  
  227. /bin/chown -R www.www /tmp/eaccelerator/  
  228. sed -i '808a extension_dir = "'$php_dir'/lib/php/extensions/no-debug-non-zts-20090626/"' $php_dir/lib/php.ini  
  229. sed -i '809a extension = "memcache.so"' $php_dir/lib/php.ini  
  230. sed -i '810a extension = "pdo_mysql.so"' $php_dir/lib/php.ini  
  231. sed -i '811a extension = "imagick.so"' $php_dir/lib/php.ini  
  232. sed -i '134a output_buffering = On' $php_dir/lib/php.ini  
  233. sed -i '847a cgi.fix_pathinfo=0' $php_dir/lib/php.ini  
  234. sed -i 's@;date.timezone =@date.timezone = Asia/Shanghai@g' $php_dir/lib/php.ini  
  235. echo '[eaccelerator]  
  236. zend_extension="'$php_dir'/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so" 
  237. eaccelerator.shm_size="64" 
  238. eaccelerator.cache_dir="/tmp/eaccelerator" 
  239. eaccelerator.enable="1" 
  240. eaccelerator.optimizer="1" 
  241. eaccelerator.check_mtime="1" 
  242. eaccelerator.debug="0" 
  243. eaccelerator.filter="" 
  244. eaccelerator.shm_max="0" 
  245. eaccelerator.shm_ttl="0" 
  246. eaccelerator.shm_prune_period="0" 
  247. eaccelerator.shm_only="0" 
  248. eaccelerator.compress="0" 
  249. eaccelerator.compress_level="9" 
  250. eaccelerator.keys = "disk_only" 
  251. eaccelerator.sessions = "disk_only" 
  252. eaccelerator.content = "disk_only">> $php_dir/lib/php.ini  
  253.  
  254. echo ';;;;;;;;;;;;;;;;;;;;;  
  255. ; FPM Configuration ;  
  256. ;;;;;;;;;;;;;;;;;;;;;  
  257.  
  258. ;;;;;;;;;;;;;;;;;;  
  259. ; Global Options ;  
  260. ;;;;;;;;;;;;;;;;;;  
  261.  
  262. [global]  
  263. pid = run/php-fpm.pid  
  264. error_log = log/php-fpm.log  
  265. log_level = notice 
  266.  
  267. emergency_restart_threshold = 30 
  268. emergency_restart_interval = 1m 
  269. process_control_timeout = 5s 
  270. daemonize = yes 
  271.  
  272. ;;;;;;;;;;;;;;;;;;;;  
  273. ; Pool Definitions ;  
  274. ;;;;;;;;;;;;;;;;;;;;  
  275.  
  276. [www]  
  277.  
  278. listen = 127.0.0.1:9000  
  279. listen.backlog = -1  
  280. listen.allowed_clients = 127.0.0.1  
  281. listen.owner = www 
  282. listen.group = www 
  283. listen.mode = 0666 
  284. user = www 
  285. group = www 
  286.  
  287. pm = dynamic 
  288. pm.max_children = 32 
  289. pm.start_servers = 4 
  290. pm.min_spare_servers = 4 
  291. pm.max_spare_servers = 16 
  292. pm.max_requests = 512 
  293.  
  294. request_terminate_timeout = 0 
  295. request_slowlog_timeout = 0 
  296. slowlog = log/$pool.log.slow  
  297. rlimit_files = 51200 
  298. rlimit_core = 0 
  299.  
  300. catch_workers_output = yes 
  301. env[HOSTNAME] = $HOSTNAME  
  302. env[PATH] = /usr/local/bin:/usr/bin:/bin  
  303. env[TMP] = /tmp  
  304. env[TMPDIR] = /tmp  
  305. env[TEMP] = /tmp ' >> $php_dir/etc/php-fpm.conf  
  306. echo "$php_dir/sbin/php-fpm" >> /etc/rc.local  
  307. $php_dir/sbin/php-fpm  
  308. echo '<? 
  309. phpinfo();  
  310. ?>>$nginx_dir/html/phpinfo.php  
  311. echo "php install success!"  
  312. }  
  313. #install nginx  安装nginx的模块
  314. function install_nginx()  
  315. {  
  316. cd $installhere/soft/nginx  
  317. tar xzf pcre-8.12.tar.gz  
  318. cd pcre-8.12  
  319. ./configure  
  320. make && make install  
  321. cd ../  
  322. tar xzf ngx_cache_purge-1.3.tar.gz  
  323. tar xzf nginx-1.0.12.tar.gz  
  324. cd nginx-1.0.12  
  325. #Modify nginx Edition information  
  326. sed -i 's@#define NGINX_VERSION.*$@#define NGINX_VERSION      "1.0"@g' src/core/nginx.h  
  327. sed -i 's@#define NGINX_VER.*NGINX_VERSION$@#define NGINX_VER          "YWS/" NGINX_VERSION@g' src/core/nginx.h  
  328. ./configure --prefix=$nginx_dir --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_cache_purge-1.3  
  329. make && make install  
  330. cd $installhere/soft/nginx/  
  331. cp nginx.sh /etc/init.d/nginx  
  332. chmod 755 /etc/init.d/nginx  
  333. chkconfig --add nginx  
  334. chkconfig nginx on  
  335. rm -rf $nginx_dir/conf/nginx.conf  
  336. cp nginx.conf $nginx_dir/conf/nginx.conf  
  337. echo "ulimit -SHn 65535" >>/etc/rc.local  
  338. echo "$nginx_dir/sbin/nginx" >> /etc/rc.local  
  339. echo '#ADD  
  340. net.ipv4.tcp_max_syn_backlog = 65536 
  341. net.core.netdev_max_backlog =  32768 
  342. net.core.somaxconn = 32768 
  343.  
  344. net.core.wmem_default = 8388608 
  345. net.core.rmem_default = 8388608 
  346. net.core.rmem_max = 16777216 
  347. net.core.wmem_max = 16777216 
  348.  
  349. net.ipv4.tcp_timestamps = 0 
  350. net.ipv4.tcp_synack_retries = 2 
  351. net.ipv4.tcp_syn_retries = 2 
  352.  
  353. net.ipv4.tcp_tw_recycle = 1 
  354. #net.ipv4.tcp_tw_len = 1 
  355. net.ipv4.tcp_tw_reuse = 1 
  356.  
  357. net.ipv4.tcp_mem = 94500000 915000000 927000000  
  358. net.ipv4.tcp_max_orphans = 3276800 
  359.  
  360. #net.ipv4.tcp_fin_timeout = 30 
  361. #net.ipv4.tcp_keepalive_time = 120 
  362. net.ipv4.ip_local_port_range = 1024  65535' >>/etc/sysctl.conf  
  363. /sbin/sysctl -p  
  364. echo '#!/bin/bash  
  365. # This script run at 00:00  
  366.  
  367. # The Nginx logs path  
  368. logs_path="'$nginx_dir'/logs/" 
  369. mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/  
  370. mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log  
  371. kill -USR1 `cat '$nginx_dir'/nginx.pid` '>>$nginx_dir/sbin/cut_nginx_log.sh  
  372. chmod 755 $nginx_dir/sbin/cut_nginx_log.sh  
  373. echo "00 00 * * * /bin/bash  $nginx_dir/sbin/cut_nginx_log.sh" >> /var/spool/cron/root  
  374. $nginx_dir/sbin/nginx  
  375. echo "nginx install success!"  
  376. }  
  377. #check install  检测模块
  378. function install_check()  
  379. {  
  380. echo "========================== Check install ================================"  
  381. clear  
  382. if [ -s $nginx_dir ]; then  
  383.   echo "$nginx_dir [found]"  
  384.   else  
  385.   echo "Error: $nginx_dir not found!!!"  
  386. fi  
  387.  
  388. if [ -s $php_dir ]; then  
  389.   echo "$php_dir   [found]"  
  390.   else  
  391.   echo "Error: $php_dir not found!!!"  
  392. fi  
  393.  
  394. if [ -s $mysql_dir ]; then  
  395.   echo "$mysql_dir [found]"  
  396.   else  
  397.   echo "Error: $mysql_dir not found!!!"  
  398. fi  
  399.  
  400. echo "========================== Check install ================================"  
  401. if [ -s $nginx_dir ] && [ -s $php_dir ] && [ -s $mysql_dir ]; then  
  402.  
  403. echo "LNMP  is completed! "  
  404. echo "default mysql root password:$mysql_passwd"  
  405. echo "The path of some dirs:"  
  406. echo "mysql dir:      $mysql_dir"  
  407. echo "php dir:        $php_dir"  
  408. echo "php info:         $nginx_dir/html/phpinfo.php"  
  409. echo "nginx dir:      $nginx_dir"  
  410. echo "web dir :       $nginx_dir/html"  
  411. echo "=========================================================================="  
  412.  
  413. else  
  414.   echo "Sorry,Failed to install LNMP!"  
  415.   echo "Please check errors and logs."  
  416. fi  
  417. }  
  418. case $1 in  
  419. install_yum)  
  420. install_yum  
  421. ;;  
  422. init)  
  423. start_time  
  424. init  
  425. end_time  
  426. ;;  
  427. install_mysql)  
  428. start_time  
  429. install_mysql  
  430. end_time  
  431. ;;  
  432. install_nginx)  
  433. start_time  
  434. install_nginx  
  435. end_time  
  436. ;;  
  437. install_php)  
  438. start_time  
  439. install_php  
  440. end_time  
  441. ;;  
  442. install_lnmp)  
  443. start_time  
  444. init  
  445. install_mysql  
  446. install_nginx  
  447. install_php  
  448. end_time  
  449. ;;  
  450. install_check)  
  451. start_time  
  452. install_check  
  453. end_time  
  454. ;;  
  455. *)  
  456. echo "Usage:`basename $0` {install_yum|init|install_mysql|install_nginx|install_php|install_lnmp|install_check}"  
  457. ;;  
  458. esac  

 

三、安装

现在进行lnmp的安装(使用nohup)

  1. [root@localhost tmp]# nohup sh install_lnmp.sh install_lnmp &  
  2. [1] 6861  
  3. [root@localhost tmp]# nohup: appending output to `nohup.out'  

可以看到安装已经在后台进行,并且安装情况都输出到nohup.out里了

现在就是漫长的等待了......

  1. [root@localhost tmp]#   
  2. [1]+  Done                    nohup sh install_lnmp.sh install_lnmp  

结果可以看到脚本运行完成

现在可以看出脚本运行完成,我们查看一下日志

  1. [root@localhost tmp]# tail -f nohup.out   
  2. net.ipv4.tcp_synack_retries = 2 
  3. net.ipv4.tcp_syn_retries = 2 
  4. net.ipv4.tcp_tw_recycle = 1 
  5. net.ipv4.tcp_tw_reuse = 1 
  6. net.ipv4.tcp_mem = 94500000 915000000 927000000  
  7. net.ipv4.tcp_max_orphans = 3276800 
  8. net.ipv4.ip_local_port_range = 1024  65535  
  9. nginx install success!  
  10. Sun Mar 25 03:47:26 EDT 2012 Finish install!  
  11. Total runtime: 68 Minutes  

可以看到安装运行了68分钟(我在脚本里设置了运行时间,所以可以帮助我们观察脚本运行的时间)。

四、检测

分别查看msyql、 nginx、php是否启动

  1. [root@localhost tmp]# ps -ef|grep mysql  
  2. root      9337  3848  0 03:49 pts/2    00:00:00 grep mysql  
  3. root     25402     1  0 03:09 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/mysql.pid  
  4. mysql    26280 25402  0 03:09 pts/2    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql_error.log --open-files-limit=10240 --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306 
  5. [root@localhost tmp]# ps -ef|grep nginx  
  6. root      9321     1  0 03:47 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx  
  7. www       9322  9321  0 03:47 ?        00:00:00 nginx: worker process        
  8. www       9325  9321  0 03:47 ?        00:00:00 nginx: worker process        
  9. www       9326  9321  0 03:47 ?        00:00:00 nginx: worker process        
  10. www       9327  9321  0 03:47 ?        00:00:00 nginx: worker process        
  11. www       9328  9321  0 03:47 ?        00:00:00 nginx: worker process        
  12. www       9329  9321  0 03:47 ?        00:00:00 nginx: worker process        
  13. www       9330  9321  0 03:47 ?        00:00:00 nginx: worker process        
  14. www       9331  9321  0 03:47 ?        00:00:00 nginx: worker process        
  15. root      9339  3848  0 03:49 pts/2    00:00:00 grep nginx  
  16. [root@localhost tmp]# ps -ef|grep php  
  17. root      3431     1  0 03:45 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)  
  18. www       3432  3431  0 03:45 ?        00:00:00 php-fpm: pool www            
  19. www       3433  3431  0 03:45 ?        00:00:00 php-fpm: pool www            
  20. www       3434  3431  0 03:45 ?        00:00:00 php-fpm: pool www            
  21. www       3435  3431  0 03:45 ?        00:00:00 php-fpm: pool www            
  22. root      9341  3848  0 03:49 pts/2    00:00:00 grep php  

从输出可以看到,mysql、php、nginx都已经启动了,我们在网页里查看一下nginx与phpinfo.php

网页能打开,证明nginx安装成功

 

可以看到php也已经安装完成

下面我们在来通过install_check来检查lnmp是否安装完成

  1. [root@localhost tmp]# sh install_lnmp.sh install_check  
  2. Sun Mar 25 04:04:24 EDT 2012 Start install!  
  3. ========================== Check install ================================  
  4. /usr/local/nginx [found]  
  5. /usr/local/php   [found]  
  6. /usr/local/mysql [found]  
  7. ========================== Check install ================================  
  8. LNMP  is completed!   
  9. default mysql root password:admin  
  10. The path of some dirs:  
  11. mysql dir:        /usr/local/mysql  
  12. php dir:          /usr/local/php  
  13. php info:         /usr/local/nginx/html/phpinfo.php  
  14. nginx dir:        /usr/local/nginx  
  15. web dir :         /usr/local/nginx/html 
  16. ==========================================================================  
  17. Sun Mar 25 04:04:24 EDT 2012 Finish install!  
  18. Total runtime: 0 Seconds  

可以看到,lnmp已经安装完成。

希望大家能在使用本脚本过程中帮我进行纠错与建议,谢谢!

BTW:感谢小愚的建议,我经过测试发现是有他所说的问题出现,现在我已经把脚本修改了一下,修改内容为cp php.ini到$php_dir/lib目录下。

希望能与小愚及各位同好一起交流、学习!

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

X社区推广