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
下一篇 2016-12-05

相关推荐

  • 马哥教育网络班21期+第五周课程练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; ~]# egrep ^[[:space:]] /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; ~]# egrep …

    Linux干货 2016-08-08
  • 在Centos系统上安装EPEL扩展源以及安装htop工具

        Htop是一个强大的进程管理前端工具,但这是一个扩展工具,一般在Centos系统源中并没有,所有我们需要到fedora-epel源中下载。         EPEL即Extra Packages for Enterprise Lin…

    Linux干货 2016-02-14
  • bash编程初体验(一)

    bash编程初体验(一) 认识bash编程 变量与赋值 算术与逻辑运算 条件测试与退出状态 认识bash编程 Bash(GNU Bourne-Again Shell)是许多Linux发行版的默认Shell,我们要认识的bash中,就是在bash的环境下的一种编程。 众所周知,程序=指令+数据,由此也决定了两种不同的编程风格,过程过与对象式; 过程式:以指令为…

    Linux干货 2016-08-15
  • 第六周

    1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; :%s/^[[:space:]]\+/#&/ 2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符; :%s/^[[:space:]]\…

    Linux干货 2016-09-19
  • N25期—第一周作业

    计算机的组成及其功能 计算机主要由硬件和软件两部分组成, 硬件目前由5大部件组成 控制器:类似人类的大脑!控制整个计算机的运作 运算器:对数据进行逻辑运算处理 存储器:存放数据部件 输出设备:比如显示器,打印机 输入设备:比如键盘,鼠标 Linux发行版本 主要三大分之:Debian系,Reb Hat系,Slackware系。 1.Debian GNU / …

    Linux干货 2016-12-01
  • 如何在微软Azure云机上添加新磁盘

    大家好: 最近在项目实践中,分享下如何在微软Azure云机上添加新磁盘。 首先需要查看下是否有未用上的磁盘,先fdisk -l查(看下图)并和Azure技术确认该磁盘是否可永久保存数据: 然后找到未分区的磁盘号,如上图中的/dev/sdc,再 fdisk /dev/sdc后开始在该新磁盘上进行分区创建: 为方便管理,我们一般就创建一个分区,然后进行格式化: …

    Linux干货 2016-11-27