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)
上一篇 2016-12-05 17:04
下一篇 2016-12-05 17:05

相关推荐

  • 程序包编译安装

    一、几个概念     1、开放源码         程序代码,人类可能读懂的程序语言,但是计算机不能识别和执行;     2、编译程序      &n…

    Linux干货 2015-05-11
  • Linux基础知识(三)

     本文的主要内容是:  1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。  2、取出最后登录到当前系统的用户的相关信息。  3、取出当前系统上被用户当作其默认shell的最多的那个shell。  4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全…

    Linux干货 2016-10-03
  • 【N25第六周作业】VIM、crontab、简单脚本

    请详细总结vim编辑器的使用并完成以下练习题 第二周有写过vim的用法,请查看连接: 周期性任务计划:cron 服务程序: cronie:主程序包,提供了crond守护进程及相关辅助工具; 确保crond守护进程(daemon)处于运行状态: CentOS 7: systemctl  status  cron…

    Linux干货 2016-12-27
  • heartbeatV1+nfs实现高可用httpd

        随着互联网技术的不断发展,Web应用也越来越普遍,Web服务器的无故障工作时间就显得尤重要,但由于各种各样的原因,一台服务器并不能保证永远不出问题的运行,此时就需要一种机制来实现多台服务器共同为相同的来务功能提供服务,以确保任意一台服务器宕机后,不会影响其所承载的业务的访问。   &nbsp…

    Linux干货 2015-06-26
  • 学习试题演练

    1.写一个脚本实现:提示用户给出自己的选择随后显示对应的信息。 区分大小写: if [[ "$option" =~ [Dd][Ii][Ss][Kk] ]];then 2.根据id号来判断用户类型,若没有则提示没有此用户 0:管理员 1-999:系统用户 1000+:登陆用户 不得不提下: bash -n usertype.sh 前期擅用是…

    Linux干货 2016-08-21
  • Linux上文件管理命令、元素据及时间戳、bash的工作特性回显和命令展开、定义别名,命令引用。

    文件管理命令: mkdir 创建空目录 语法: mkdir [选项]…目录… 选项: -P:逐层创建目录。 -v:显示过程。 -m:直接给定权限。 注意:路径的基名为命令作用对象。基名路径必须存在。 示例 创建/tmp/x1/a/a1和/tmp/x1/b rmdir 移除空目录 语法 rmdir [选项]…目录&#8230…

    Linux干货 2017-12-10