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 --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php
else
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php
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 --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php
else
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql--prefix=/usr/local/php
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来进行相对应的安装。
最后感谢各位的阅读,希望各位有可以优化的地方提出来,互相学习。