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

相关推荐

  • 关于大型网站技术演进的思考(五):存储的瓶颈(5)

    原文出处: 夏天的森林    上文里我遗留了两个问题,一个问题是数据库做了水平拆分以后,如果我们对主键的设计采取一种均匀分布的策略,那么它对于被水平拆分出的表后续的查询操作将有何种影响,第二个问题就是水平拆分的扩容问题。这两个问题在深入下去,本系列就越来越技术化了,可能最终很多朋友读完后还是没有找到解决实际问题的启迪,而且我觉得…

    Linux干货 2015-03-11
  • N22-第六周作业

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

    Linux干货 2016-09-26
  • 脚本进阶笔记整理

    一、逻辑运算 变量:   本地变量、环境变量、局部变量、位置参数变量、特殊变量   变量赋值:name=value,export name=value,declare -x name=value   变量引用:$name,${name}   注意:有些时候{}不能省略,例如 &n…

    Linux干货 2017-03-26
  • N25第二周作业 文件管理类命令cp mv rm以及 bash特性之文件名通配

    Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。         文件管理类命令主要有cp, mv, rm          cp用于单个文件的复制,例如:  &nb…

    Linux干货 2016-12-13
  • vsftp通过pam_mysql做虚拟用户认证

    vsftpd可以通过三种账户登录系统: 系统账户:系统本地账户登录系统,默认进入的目录是用户家目录,可以在所有有权限的路径切换; 匿名账户:匿名用户登录,然后映射为一个本地用户; 虚拟账户:既不是系统本地用户也不是匿名用户,而是通过pam做第三方认证的方法,支持文件和数据库,ldap的认证,此处通过mysql进行实验; 一、准备实验环境 实验在一台虚拟机上进…

    Linux干货 2017-06-07
  • Homework Week-8 网络及脚本编程

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 设备 功能 使用场景 网桥 用于连接不同网段,将相似的网络连接起来,隔离信息。 连接不同部门间的局域网;连接地理位置分散并且相距较远的局域网,可以增加工作的物理距离;采用由网桥连接的多个局域网调节负载;网桥可以设置在局域网的关键部位,防止单点失常而破坏整个系统等。 集线器 对接收…

    Linux干货 2016-10-09