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.www58058.com/61689

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

相關推薦

  • N21—-第一周課程練習題

    1、描述計算機的組成及其功能。 我們現在通常理解為計算機是由硬件系統+軟件系統組成的,根據馮·諾依曼體系結構。計算機主要由五大部件組成:      運算器(Datapath): 計算機中執行各種算術和邏輯運算操作的部件      控制器(Control):是整個計算機的中樞神經,…

    Linux干貨 2016-07-12
  • 點名腳本

    腳本要求:1、隨機抽點80以內的隨機證書;                2、可以一次抽取多個隨機數;同時間抽取的隨機數要唯一;                3、被抽取之后的隨機數,之后不會再抽?。弧?/p>

    2017-05-08
  • 查找與壓縮

    文件查找 在文件系統上查找符合條件的文件; ? 文件查找:locate, find locate :非實時查找(數據庫查找) find :實時查找 locate 1.查詢系統上預建的文件索引數據庫 /var/lib/mlocate/mlocate.db ? 2.依賴于事先構建的索引,索引的構建是在系統較為空閑時自動進行(周期性任務);管理需要員手動更新數據庫…

    Linux干貨 2016-08-22
  • N22-第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 ~]# cp -r /etc/skel /home/tuser1 ~]# chmod -R go= /home/tuser1/ 2、編輯/etc…

    Linux干貨 2016-09-06
  • 排錯

    把/etc/inittab  模式改為6模式 怎么修復 1 先把 vim /etc/inittab 打開 2 把/etc/inittab 模式改為6 3 reboot 4 在倒計時完之前按任意鍵 5按A進入 6 在quiet  命令后面寫入 3  模式 重啟 7把 vim /etc/inittab 打開 8 把/etc…

    Linux干貨 2017-05-15
  • linux 軟件包管理

    1、Linux軟件包管理 Redhat, SUSE: RPM Redhat Package Manager PRM is Package Manager 前端工具:yum, apt-get 后端工具:RPM, dpt   2、rpm: 數據庫:/var/lib/rpm  rpm包: 軟件包作者下載源程序,編譯配置完成后,制作成rpm包 格…

    Linux干貨 2017-08-06
欧美性久久久久