Lnmp安装脚本

1、源码编译安装LNMP架构环境;

此题略

此链接为安装nginx时,编译参数和各个模块和第三方模块的介绍,十分丰富,安装前可以参考学习

https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/

2、编写一个脚本完成以下功能:

   (1)、一键搭建LNMP源码编译环境;

   (2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。

脚本思路:

该脚本使用了脚本嵌套来达到安装目的,需要运行该脚本主机ssh无需密码登录其他主机,
更详细安装需要丰富或更改各个子脚本内容,建议安装后自己手动更改配置文件

脚本运行:

[root@centos ~]# ls /test
fastcgi_params  lnmp.sh  mysql-5.5.53-linux2.6-x86_64.tar.gz  mysql.sh 
nginx-1.6.3.tar.gz  nginx.conf  nginx.sh  php-5.6.28.tar.gz  php.sh  readme.txt
[root@centos ~]# /test/lnmp.sh 
Before we get started,you must ensure that all servers 
have epel repository and distribution repository;
localhost can login other servers by ssh without password 
including itself;
pathname cannot suffix with /;
======================
quit) exit script
yes) continue script
=======================
Enter a option:yes
Plz input nginx SerIp localhostIp or remoteIp:192.168.40.137 
Plz input nginx install path:/usr/local
Plz input mysql SerIp localhostIp or remoteIp:192.168.40.137
Plz input mysql data path:/data
Plz input php-fpm SerIp localhostIp or remoteIp:192.168.40.141
Plz input php install path:/usr/local
 nginx installing......
root@192.168.40.137's password: 
root@192.168.40.137's password: 
root@192.168.40.137's password: 
"nginx install completed!"
mysql installing......
root@192.168.40.137's password: 
root@192.168.40.137's password: 
root@192.168.40.137's password: 
“mysql install completed!”
php installing.....
“php install completed!”
Lnmp install completed, U can test html and php,manual modifiy specific configuration or
improve this script. 
[root@centos ~]# ss -antl | grep -E "(:80|:3306)"
LISTEN     0      50                        *:3306                     *:*     
LISTEN     0      128                       *:80                       *:*     
[root@centos ~]# ssh root@192.168.40.141 "ss -atnl | grep  :9000"
LISTEN     0      128          192.168.40.141:9000
此处我故意将lnmp分离 192.168.40.137上安装了nginx和mysql
192.168.40.141为php服务器,安装后各个服务正常,当然可以装在同一主机上

 脚本剖析:

上述的/test 为该脚本解压后的路径
[root@centos ~]# cat /test/lnmp.sh 主程序
#!/bin/bash
cat << EOF
Before we get started,you must ensure that all servers 
have epel repository and distribution repository;
localhost can login other servers by ssh without password 
including itself;
pathname cannot suffix with /;
======================
quit) exit script
yes) continue script
=======================
EOF
read -p "Enter a option:" option
if [ $option == "quit" ];then
echo "exit..."
exit 14
else
function pathformat(){    判断函数
if ! echo "$1" | grep   "^[/].*[^/]$" &>/dev/null;then
echo "illegal path format!"
exit 13
fi
}
function ipformat(){
if ! echo $1 | egrep -o "[1-2][0-9]?[0-9]?.[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9].[0-2]?[0-9]?[0-9]"&>/dev/null;then
echo "illegal ip format!"
exit 12
fi
}
read -p "Plz input nginx SerIp localhostIp or remoteIp:" nginxIp
ipformat $nginxIp
read -p "Plz input nginx install path:" nginxPath
pathformat $nginxPath
read -p "Plz input mysql SerIp localhostIp or remoteIp:" mysqlIp
ipformat $mysqlIp
read -p "Plz input mysql data path:" dataPath
pathformat $dataPath
read -p "Plz input php-fpm SerIp localhostIp or remoteIp:" phpIp
ipformat $phpIp
read -p "Plz input php install path:" phpPath
pathformat $phpPath
echo "$nginxIp $nginxPath $phpIp" >/tmp/nginx.vars 取出需要的变量
echo "$mysqlIp $dataPath" >/tmp/mysql.vars
echo "$phpIp $phpPath" >/tmp/php.vars
echo  " nginx installing......"开始安装nginx
dir=$0
basedir=$(dirname $dir)
scp /tmp/nginx.vars $basedir/nginx-1.6* $basedir/nginx.conf 
$basedir/fastcgi_params $basedir/nginx.sh root@$nginxIp:/tmp/ &>/dev/null 
ssh root@$nginxIp "/tmp/nginx.sh" &>/dev/null&&
sleep 10
ssh root@$nginxIp "rm -rf /tmp/nginx.vars /tmp/nginx-1.6* /tmp/nginx.conf
 /tmp/fastcgi_params /tmp/nginx.sh" &>/dev/null 
echo "nginx install completed!"
echo mysql installing......开始安装mysql
scp -p  $basedir/mysql.sh $basedir/mysql-5.*  /tmp/mysql.vars root@$mysqlIp:/tmp/ &>/dev/null
ssh root@$mysqlIp "/bin/bash /tmp/mysql.sh" &>/dev/null&&
sleep 10
ssh root@$mysqlIp “rm -rf /tmp/mysql.vars  /mysql/mysql.sh /tmp/mysql-5.* ” &>/dev/null
echo “mysql install completed!”
echo php installing..... 开始安装php
scp $basedir/php.sh $basedir/php-5.6*  /tmp/php.vars root@$phpIp:/tmp/ &>/dev/null
ssh root@$phpIp "/tmp/php.sh" &>/dev/null&&
sleep 10
ssh root@$phpIp “rm -rf /tmp/php.vars  /tmp/php.sh /tmp/php-5.* ” &>/dev/null
echo “php install completed!”
ssh root@$nginxIp"$nginxPath/nginx/sbin/nginx -s reload " &>/dev/null
echo "Lnmp install completed, U can test html and php,manual modifiy specific
 configuration or improve this script. "
fi
[root@centos ~]# cat /test/nginx.sh 具体怎样安装nginx
#!/bin/bash
nginxip=$(awk '{print $1}' /tmp/nginx.vars)
phpip=$(awk '{print $3}' /tmp/nginx.vars)
nginxpath=$(awk '{print $2}' /tmp/nginx.vars)
[ ! -e $nginxpath ]&& mkdir -p  $nginxpath &>/dev/null
yum -y install pcre-devel &>/dev/null
yum -y groupinstall "Development Tools" &>/dev/null
! groups nginx &>/dev/null && groupadd -r nginx 
! id nginx &>/dev/null && useradd -r -g nginx nginx 
mkdir -p /var/tmp/nginx/{client,proxy,fcgi,uwsgi,scgi} &>/dev/null
tar zxf /tmp/nginx-1.6* -C /usr/local/ &>/dev/null
cd /usr/local/nginx-1.6*/
./configure \
  --prefix=$nginxpath \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre&&make&&make install &>/dev/null

\cp -f /tmp/nginx.conf /etc/nginx/nginx.conf
\cp -f /tmp/fastcgi_params  /etc/nginx/fastcgi_params
echo "$nginxip"
sed -i "s@localhost@$nginxip@" /etc/nginx/nginx.conf 模版替换
sed -i "s@~.~.~.~@$phpip@" /etc/nginx/nginx.conf 
/usr/local/nginx/sbin/nginx
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@centos ~]# cat /test/mysql.sh  具体怎样安装mysql
#!/bin/bash
datapath=$(awk '{print $2}' /tmp/mysql.vars)
[ ! -d $datapath ]&& mkdir -p $datapath &>/dev/null
! id mysql &>/dev/null  && groupadd -r mysql
! groups mysql &>/dev/null && useradd -g mysql -r -s /sbin/nologin  mysql
chown -R mysql:mysql $datapath
tar zxf /tmp/mysql-5.5.53-linux2.6-x86_64.tar.gz -C  /usr/local/ &>/dev/null
cd
ln -sv /usr/local/mysql-5.5.53-linux2.6-x86_64 /usr/local/mysql &>/dev/null
cd /usr/local/mysql
chown -R root:mysql ./
cd scripts
./mysql_install_db --basedir=/usr/local/mysql --datadir=$datapath &>/dev/null
\cp ../support-files/my-large.cnf  /etc/my.cnf
\cp ../support-files/mysql.server  /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
sed -i "/\[mysqld\]/a datadir=$datapath" /etc/my.cnf
sed  -i "s@/tmp/mysql.sock@/var/lib/mysql/mysql.sock@g" /etc/my.cnf 
cd $datapath
chown -R mysql:mysql ./*
service mysqld start &>/dev/null
chkconfig mysqld on
cd /usr/local/mysql/bin
./mysql -uroot  -h127.0.0.1 -e"grant all privileges on *.* to 
'root'@'192.168.40.%' identified by 'centos';flush privileges;"
[root@centos ~]# cat /test/php.sh  具体怎样安装php
#!/bin/bash
phpip=$(awk '{print $1}' /tmp/php.vars)
phppath=$(awk '{print $2}' /tmp/php.vars)
[ ! -e $phppath ]&& mkdir -p  $phppath &>/dev/null
yum -y groupinstall "Development Tools" &>/dev/null
yum install -y libmcrypt* &>/dev/null
yum install -y mhash-*  &>/dev/null
yum install -y mcrypt*  &>/dev/null
yum -y groupinstall "Desktop Platform Development" &>/dev/null
yum -y install bzip2-devel libmcrypt-devel libxml2-devel &>/dev/null
tar zxf /tmp/php-5* -C /usr/local/ &>/dev/null
cd /usr/local/php-5*
./configure --prefix=$phppath/php5 --with-openssl --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr \
--enable-xml  --enable-sockets --enable-fpm --with-mcrypt  \
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 && make && make install
\cp -f php.ini-production /etc/php.ini &>/dev/null
\cp -f ./sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
\cp -f $phppath/php5/etc/php-fpm.conf.default $phppath/php5/etc/php-fpm.conf
sed -i "s@127.0.0.1@$phpip@" $phppath/php5/etc/php-fpm.conf 
sed -i "1a pid = $phppath/php5/var/run/php-fpm.pid"  $phppath/php5/etc/php-fpm.conf
service php-fpm start

安装后测试:

[root@centos ~]# curl -I http://192.168.40.137
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Wed, 30 Nov 2016 07:52:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 30 Nov 2016 07:04:39 GMT
Connection: keep-alive
ETag: "583e7a07-264"
Accept-Ranges: bytes
[root@centos html]# curl -I http://192.168.40.137/index.php
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Wed, 30 Nov 2016 10:58:14 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.28
[root@centos html]# curl -I http://192.168.40.137/test.php
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Wed, 30 Nov 2016 10:58:21 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.28
[root@centos html]# ssh root@192.168.40.141
"cat/usr/html/{index.php,test.php}"
<?php 
$link=mysql_connect("192.168.40.137","root","centos"); 
if(!$link) echo "FAILD!"; 
else echo "OK!"; 
?> 
<?php
phpinfo();
?>
此处所有的php文件都需要放在php服务器上,或者共享存储上
[root@centos html]# /usr/local/nginx/sbin/nginx -s stop
[root@centos ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [error] invalid PID number "" in "/var/run/nginx/nginx.pid"
[root@centos ~]# /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
[root@centos ~]# /usr/local/nginx/sbin/nginx -s reload
此处为能正常启动,重启nginx服务的方法

原创文章,作者:Snoo,如若转载,请注明出处:http://www.178linux.com/61689

(0)
SnooSnoo
上一篇 2016-12-05 17:05
下一篇 2016-12-05 17:05

相关推荐

  • RAID,pv,vg,lv的实验

    首先在虚拟机中添加5块硬盘,以备实验使用,此实验以sdc,sdd,sde,sdf,sdg硬盘为例:     添加硬盘:(不用重新启动虚拟机读取新添加磁盘)             ~]# echo “- – -” > /sys/class…

    Linux干货 2017-03-19
  • 马哥教育网络21期+第十周练习博客

    马哥教育网络21期+第十周练习博客 1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) CentOS启动流程:1,加电自检->Boot Sequence–>加载内核文件 BOOT Sequence中包含了MBR和GRUB     MBR:记录磁盘扇区,共512字…

    Linux干货 2016-09-19
  • Linux运维学习历程-第二天-虚拟机的配置

    学习Linux我自己的感觉是可以按Linus的哲学思想来学习 比如一切皆文件,那我们首先可以记住一些重要的常见的路径和文件,并知道有什么作用,这样在初期学习时,我们要干什么时,知道在哪里找;   而命令我们可以每天记忆并练习一些,本身Linux的基本命令都是一下短小精悍的而且有些命令名本身就是英文单词,像date命令就是和系统时间有关的命令用来显示…

    Linux干货 2016-08-03
  • find命令详解

    find命令详解 实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找; 一、工作特性: 查找速度略慢; 精确查找,只查找文件路径的基名而非整个路径; 实时查找; 可能只搜索用户具备读取和执行权限的目录 二、 用法: find [查找起始路径] [OPTIONS] [查找条件] [处理动作] 查找起始路径:指定具体搜索目标起始路径;默认为当前目录…

    Linux干货 2017-03-19
  • vim的使用说明

    vim的使用说明 vim是一款功能丰富而强大的文本编辑器,vim是从 vi 发展出来的一个文本编辑器。其代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛的使用。 vim的使用非常方便,使用方法为:vim [options] [file ..] 1、vim可以对指定文件进行编辑,也可以直接启动vim编辑器进行编辑,对于打开指定文件编辑时可以加…

    Linux干货 2016-08-12
  • 马哥教育21期网络班—第11周课程+练习—-成长进行时–不退步–上

    1、详细描述一次加密通讯的过程,结合图示最佳。 对称加密: 加密和解密使用同一个密钥; 缺点:如何通信方多的话,需要保存多组密钥 公钥加密:密钥是成对儿出现 公钥:公开给所有人;pubkey 私钥:自己留存,必须保证其私密性;secret key 特点:用公钥加密的数据,只能使用与之配对儿的私钥解密;反之亦然; 数字签名:主要在于让接收方确认发送方…

    Linux干货 2016-09-19