lnmp shell安装脚本

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。LNMP是现在大多数大型公司在用的web环境,nginx小巧高效的web服务器和反向代理服务器,相对于apach占用资源更少,抗并发力更强,而且处理静态文件速度非常快。Mysql是一个小型关系型数据库管理系统。PHP是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言。这四种软件均为免费软件,组合到一起,成为一个免费、高效的网站服务系统。接近过年,笔者在家闲着没事写了个LNMP的Shell安装脚本,笔者现在还是个在学校学习的菜鸟,有可以优化的地方,希望大家能提出来,感谢。

#!/bin/bash

clear
cat << EOF
                Welcome to Ike LNMP install system!!!(^*^)

                    1) Install nginx
                    2) Install php
                    3) Install mysql
                    4) Install nginx and php
                    5) Install lnmp(linux nginx,mysql,php)

EOF
echo -n "Please you enter (1-5):"
read  enter
clear

#install pcre
downloadpcre=‘wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz‘
tarpcre1=‘tar -zvxf pcre-8.41.tar.gz‘
cdpcredir=‘cd pcre-8.41‘
install2=‘./configure‘
install3=‘make‘
install4=‘make install‘
cplibpcre=‘cp /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1‘
addgroup=‘groupadd www‘
adduser=‘useradd -s /bin/false www‘
adddir=‘mkdir /usr/local/nginx‘
httpdstop=‘service httpd stop‘
addlogdir=‘mkdir /data0/logs/nginx‘
dirmod=‘chown www:www /data0/logs/nginx -R‘

#install nginx

downloadnginx=‘wget http://nginx.org/download/nginx-1.8.1.tar.gz‘
sec=‘tar -zvxf nginx-1.8.1.tar.gz‘
then0=‘cd nginx-1.8.1‘
then1=‘./configure --user=www --group=www --prefix=/usr/local/nginx‘
nginxinstall=‘make‘
nginxinstall2=‘make install‘
setnginx1=‘sed -i "s/\/scripts/$document_root/g" /usr/local/nginx/conf/nginx.conf‘
startnginx1=‘/usr/local/nginx/sbin/nginx‘

installnginx(){
$zlib
$downloadpcre
$tarpcre1
$cdpcredir
$install2
$install3
$install4
$cplibpcre
$addgroup
$adduser
$adddir
$addlogdir
$httpdstop
$dirmod
$downloadnginx
$sec
$then0
$then1
$nginxinstall
$nginxinstall2
$setnginx1
$startnginx1
}

installmysql()
{
rm -rf /etc/my.cnf
touch /etc/my.cnf
cat >> /etc/my.cnf << EOF
[mysqld]

character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
explicit_defaults_for_timestamp=true

[mysqld_safe]

pid-file=/usr/local/mysql/data/fzjh.pid

[log]
log-error=/usr/local/mysql/log/error.log

EOF

    if [ $(uname -p) == x86_64 ]
        then
        wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
        tar -zvxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
        mv  mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
        rm -rf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
    else
        wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-i686.tar.gz
        tar -zvxf mysql-5.7.20-linux-glibc2.12-i686.tar.gz
        mv  mysql-5.7.20-linux-glibc2.12-i686 /usr/local/mysql
        rm -rf mysql-5.7.20-linux-glibc2.12-i686.tar.gz
    fi

addmysqluser=‘useradd mysql -s /sbin/nologin‘
createdatadir=‘mkdir /usr/local/mysql/data‘
installmysql=‘/usr/local/mysql/bin/mysql_install_db  --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data‘
startmysql=‘/usr/local/mysql/support-files/mysql.server restart‘

$addmysqluser
$createdatadir
$installmysql
$startmysql

dbpasswd=$(cat /root/.mysql_secret | grep -v ‘#‘)
/usr/local/mysql/bin/mysql -uroot -p$dbpasswd << EOF

 alter user ‘root‘@‘localhost‘ identified by ‘123456‘

EOF
}

#installphp

downloadphp=‘wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror -O php-7.1.10.tar.gz‘
tarphp=‘tar -zvxf php-7.1.10.tar.gz‘
gophpdir=‘cd php-7.1.10‘

maketest=‘make‘
makeinstall=‘make install‘
set1=‘mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf  ‘
set2=‘mv /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf‘
set3=‘mv php.ini-development /usr/local/php/bin/php.ini‘

chajian(){
if [ $(rpm -qa | grep libxml2-devel | wc -l) != 0 ];then
echo libxml2 libxml2-devel is installed
else
yum install libxml2 libxml2-devel -y
fi
if [ $(rpm -qa | grep curl-devel | wc -l) != 0 ];then
 echo curl-devel is installed
else
yum install -y curl-devel
fi

if [ $(rpm -qa | grep wget | wc -l) != 0 ]
then
 if [ $(rpm -qa | grep gcc-c++ | wc -l) != 0 ];then
echo "wget and gcc gcc-c++ is installed"
else
yum install -y gcc gcc-c++
fi
else
yum install -y wget
fi

if [ $(rpm -qa | grep zlib-devel | wc -l) != 0 ];then
  echo  "zlib-devel is install"
else
  yum install -y zlib-devel

fi
}
installphp(){
$downloadphp
$tarphp
$gophpdir
if [ enter == 5 ]
    then
./configure --prefix=/usr/local/php --enable-fpm --with-curl --with-mysql=/usr/local/mysql --with-pdo-mysql
    else
./configure --prefix=/usr/local/php --enable-fpm --with-curl
fi
$maketest
$makeinstall
$set1
$set2
$set3
}

case $enter in
1)
chajian
installnginx
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
;;
2)
chajian
installphp
clear
echo ‘php file:/usr/local/php‘
;;
3)
chajian
installmysql
clear
echo ‘mysql file:=/usr/local/mysql‘
echo ‘you mysql_user:root‘
echo  you mysql_password:$dbpasswd
;;
4)
chajian
installnginx
installphp
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
echo ‘php file:/usr/local/php‘
;;
5)
chajian
installnginx
installmysql
installphp
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
echo ‘php file:/usr/local/php‘
echo ‘mysql file:=/usr/local/mysql‘
echo ‘you mysql_user:root‘
echo  you mysql_password:$dbpasswd
esac

接下来来解释下脚本代码

clear
cat << EOF
                Welcome to Ahui install system!!!(^*^)

                    1) Install nginx
                    2) Install php
                    3) Install mysql
                    4) Install nginx and php
                    5) Install lnmp(linux nginx,mysql,php)

EOF
echo -n "Please you enter (1-5):"
read  enter

首先用eof将我们想要显示的界面给cat打印出来,然后用read读取一个输入的变量给enter这个变量。

#install pcre
downloadpcre=‘wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz‘
tarpcre1=‘tar -zvxf pcre-8.41.tar.gz‘
cdpcredir=‘cd pcre-8.41‘
install2=‘./configure‘
install3=‘make‘
install4=‘make install‘
cplibpcre=‘cp /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1‘
addgroup=‘groupadd www‘
adduser=‘useradd -s /bin/false www‘
adddir=‘mkdir /usr/local/nginx‘
httpdstop=‘service httpd stop‘
addlogdir=‘mkdir /data0/logs/nginx‘
dirmod=‘chown www:www /data0/logs/nginx -R‘

#install nginx

downloadnginx=‘wget http://nginx.org/download/nginx-1.8.1.tar.gz‘
sec=‘tar -zvxf nginx-1.8.1.tar.gz‘
then0=‘cd nginx-1.8.1‘
then1=‘./configure --user=www --group=www --prefix=/usr/local/nginx‘
nginxinstall=‘make‘
nginxinstall2=‘make install‘
setnginx1=‘sed -i "s/\/scripts/$document_root/g" /usr/local/nginx/conf/nginx.conf‘
startnginx1=‘/usr/local/nginx/sbin/nginx‘

installnginx(){
$zlib
$downloadpcre
$tarpcre1
$cdpcredir
$install2
$install3
$install4
$cplibpcre
$addgroup
$adduser
$adddir
$addlogdir
$httpdstop
$dirmod
$downloadnginx
$sec
$then0
$then1
$nginxinstall
$nginxinstall2
$setnginx1
$startnginx1
}

我们将安装nginx的需要的pcre先编译安装了,定义好变量名,直接用$加变量名,使用这些命令就行了,然后再安装nginx。

installmysql()
{
rm -rf /etc/my.cnf
touch /etc/my.cnf
cat >> /etc/my.cnf << EOF
[mysqld]

character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
explicit_defaults_for_timestamp=true

[mysqld_safe]

pid-file=/usr/local/mysql/data/fzjh.pid

[log]
log-error=/usr/local/mysql/log/error.log

EOF

    if [ $(uname -p) == x86_64 ]
        then
        wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
        tar -zvxf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
        mv  mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
        rm -rf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
    else
        wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-i686.tar.gz
        tar -zvxf mysql-5.7.20-linux-glibc2.12-i686.tar.gz
        mv  mysql-5.7.20-linux-glibc2.12-i686 /usr/local/mysql
        rm -rf mysql-5.7.20-linux-glibc2.12-i686.tar.gz
    fi

addmysqluser=‘useradd mysql -s /sbin/nologin‘
createdatadir=‘mkdir /usr/local/mysql/data‘
installmysql=‘/usr/local/mysql/bin/mysql_install_db  --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data‘
startmysql=‘/usr/local/mysql/support-files/mysql.server restart‘

$addmysqluser
$createdatadir
$installmysql
$startmysql

dbpasswd=$(cat /root/.mysql_secret | grep -v ‘#‘)
}

然后是mysql,首先删除掉系统内的my.cnf,用touch加EOF将我们自己的my.cnf创建好,然后使用uname -p 来检测我们的系统是多少位的,在下载相应的mysql安装包。

chajian(){
if [ $(rpm -qa | grep libxml2-devel | wc -l) != 0 ];then
echo libxml2 libxml2-devel is installed
else
yum install libxml2 libxml2-devel -y
fi
if [ $(rpm -qa | grep curl-devel | wc -l) != 0 ];then
 echo curl-devel is installed
else
yum install -y curl-devel
fi

if [ $(rpm -qa | grep wget | wc -l) != 0 ]
then
 if [ $(rpm -qa | grep gcc-c++ | wc -l) != 0 ];then
echo "wget and gcc gcc-c++ is installed"
else
yum install -y gcc gcc-c++
fi
else
yum install -y wget
fi

if [ $(rpm -qa | grep zlib-devel | wc -l) != 0 ];then
  echo  "zlib-devel is install"
else
  yum install -y zlib-devel

fi

PS:这些是用if语句检测一些要依赖的包

#installphp

downloadphp=‘wget http://au1.php.net/get/php-7.1.10.tar.gz/from/this/mirror -O php-7.1.10.tar.gz‘
tarphp=‘tar -zvxf php-7.1.10.tar.gz‘
gophpdir=‘cd php-7.1.10‘

maketest=‘make‘
makeinstall=‘make install‘
set1=‘mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf  ‘
set2=‘mv /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf‘
set3=‘mv php.ini-development /usr/local/php/bin/php.ini‘

installphp(){
$downloadphp
$tarphp
$gophpdir
if [ enter == 5 ]
    then
./configure --prefix=/usr/local/php --enable-fpm --with-curl --with-mysql=/usr/local/mysql --with-pdo-mysql
    else
./configure --prefix=/usr/local/php --enable-fpm --with-curl
fi
$maketest
$makeinstall
$set1
$set2
$set3
}

此为php的安装方法,和上面几个差不多一个样。笔者觉得入股安装lnmp必须使用pod-mysql来使mysql和php可以链接在一起。

case $enter in
1)
chajian
installnginx
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
;;
2)
chajian
installphp
clear
echo ‘php file:/usr/local/php‘
;;
3)
chajian
installmysql
clear
echo ‘mysql file:=/usr/local/mysql‘
echo ‘you mysql_user:root‘
echo  you mysql_password:$dbpasswd
;;
4)
chajian
installnginx
installphp
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
echo ‘php file:/usr/local/php‘
;;
5)
chajian
installnginx
installmysql
installphp
clear
echo ‘nginx file:/usr/local/nginx‘
echo ‘html file:/usr/local/nginx/html‘
echo ‘php file:/usr/local/php‘
echo ‘mysql file:=/usr/local/mysql‘
echo ‘you mysql_user:root‘
echo  you mysql_password:$dbpasswd
esac

最后使用case多选来通过enter变量输入的值来,根据case来进行相对应的安装。
最后感谢各位的阅读,希望各位有可以优化的地方提出来,互相学习。

原文地址:http://blog.51cto.com/12598157/2070982

时间: 2024-10-10 05:14:30

lnmp shell安装脚本的相关文章

lnmp一键安装脚本(含有np与mysql分离)

基于lanny一键安装包:(含lnmp所需软件及配置文件) 安装nginx: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum clean all yum makecache yum install pcre p

centos6—lnmp一键安装脚本

把需要的脚本下载放到/root 目录下,然后运行本脚本即可自动化安装lnmp环境: #!/bin/bash #lnmp环境搭建自动化脚本  #date 2017/8/22 #author vaedit yum install epel-release -y yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ cmake ncurses-devel libtool zlib-devel libxml2-devel lib

LAMP or LNMP 一键安装脚本

#!/bin/bash echo "It will install lamp or lnmp." sleep 1 ##check last command is OK or not. check_ok() { if [ $? != 0 ] then     echo "Error, Check the error log."     exit 1 fi } ##get the archive of the system,i686 or x86_64. ar=`arc

源码包Apache的shell安装脚本

#!/bin/bash#安装apacherpm -e httpd --nodepsyum -y install apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcretar zxf /httpd- -C /usr/src#必须将httpd- 放在/下cd /usr/src/httpd-./configure --prefix=/usr

LNMP一键安装脚本

#!/bin/bash # INSTALL Nginx+PHP5 yum -y install libxml2* libtool* pcre* gettext* DIR=`pwd` mkdir ${DIR}/Tmp #VERSION=`cat /etc/redhat-release | awk '{print $7}' | cut -c1` VERSION=`lsb_release -r |awk '{print $2}' |cut -c1` cp -r /usr/local/src/insta

LAMP or LNMP 自动安装脚本(函数思想编写)

#!/bin/bash # date: 2016-03-12 # desc: LAN/MP auto install script  # Author: Djoker #function area #<---------------------start----------------------------------------> #function: check error runcheck(){ if [ $1 != 0 ];then   echo "------------

linux LNMP自动安装脚本

#!/bin/bashsoft_dir="/home/soft"config_dir="/home/config"httpd="httpd-2.0.52"mysql="mysql-5.1.56"php="php-5.2.17"phpmyadmin="phpMyAdmin-2.9.0.2"zend="ZendOptimizer-3.2.6-linux-glibc21-i386&q

tomcat的shell安装脚本

1 #!/bin/bash 2 #by zhangjia 3 #2019年2月18日20:39:23 4 #shell_name:tomcat_auto_install.sh 5 ###################################3333 6 set -e 7 yum install -y vim wget java-1.8.0-openjdk* 8 #tomcat版本 9 TOMCAT_PACK="apache-tomcat-8.5.38" 10 #tomcat运

python3自动安装脚本,python3.x与python2.x共存

1.前言: python3过程中,通过搜索一些文章参考安装过程发现比较麻烦,而且还出现一些不可预期的报错.python3环境需要升级openssl,所以为了部署到其他环境更方便,写自动安装脚本方式,且安装模式为python3.x与python2.x共存,防止可能发生原有环境异常的情况. 现在我把脚本分享出来,希望可以帮助到有需要的人,业务采集监控python脚本文件不提供,因为那些跟实际业务开发关联比较紧,比较没有参考意义也不方便公开. shell自动安装脚本,只需按说明简单修改配置,和调用集成