前段时间,一个朋友需要在一个新服务器centos 6.5 x64安装lnmp,觉得手动安装比较耗时,需要写一个安装脚本。


后来我帮他写了一个lnmp_install.tar.gz,大小有55.3M

解压之后,有一个目录lnmp_install,里面放了几个安装需要的软件包。

nginx是用的tengine,相关介绍大家百度下就知道了。

结构如下:

lnmp_install

├── mysql-5.6.24.tar.gz

├── openssl-1.0.2a.tar.gz

├── pcre-8.36.tar.gz

├── php-5.5.22.tar.gz

├── setup.sh

├── tengine-2.1.0.tar.gz

└── zlib-1.2.8.tar.gz


如果有需要使用的,创建目录lnmp_install,将上面的软件包下载好,然后用tar命令打包下,就可以使用了。


下面贴出setup.sh代码


#!/bin/bash


patha=`pwd`

yum install -y dialog

dialog --title "The LNMP install script" --msgbox "欢迎使用LNMP安装脚本,本脚本只提供简单的安装,没有优化。生产环境请根据实际情况优化相关参数,不要直接使用" 9 30

dialog --menu "请选择安装路径" 15 30 4 1 "/usr/local (default)" 2 "other" 2> /tmp/1.txt

xuan=`cat /tmp/1.txt`

if [ $xuan -eq 1 ];then

        echo "/usr/local" > /tmp/ins.txt

else

        dialog --title "The LNMP install script" --inputbox "请输入安装路径" 9 30 2> /tmp/2.txt

        choice=$(cat /tmp/2.txt)

        if [ ! -d $choice ];then

                echo "安装路径不存在,请检查路径是否正确!"

                exit

        else

                cat /tmp/2.txt > /tmp/ins.txt

        fi

fi


position=`cat /tmp/ins.txt`

echo "本次选择的安装路径是$position"


#检查系统版本

system=`cat /etc/issue | head -1`


if [ "$system"x = "CentOS release 6.5 (Final)"x ];then

        echo  -e "检查系统版本\033[32m [OK] \033[0m"

else

        echo  -e "检查系统版本\033[31m [FAIL] \033[0m"

        echo "本脚本是针对Centos 6.5 x64系统编写的,其他系统运行可能有问题"

        exit

fi


##############MySQL##########################################################

#检查是否安装mysql

rpm -q mysql > /dev/null

a=`echo $?`


if [ $a -eq 0 ];then

        echo "mysql is install"

        yum -y remove mysql

        echo "mysql remove is ok"

fi


if [ -f "/etc/my.cnf" ]; then

        mv /etc/my.cnf /opt

fi


#检测网络联通性#

ping www.baidu.com -c 2 > /dev/null

dbnet=`echo $?`

if [ $dbnet -eq 0 ];then

        echo  -e "检查网络\033[32m [OK] \033[0m"

else

        echo  -e "检查网络\033[31m [FAIL] \033[0m"

        echo "请检查网络,否则无法使用yum"

        exit

fi


#关闭selinux

setenforce 0

sed -i '7s/enforcing/disabled/' /etc/sysconfig/selinux

#安装epel更新源

rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm

sed -i "s/https/http/" /etc/yum.repos.d/epel.repo

yum -y install yum-fastestmirror

rpm --import /etc/pki/rpm-gpg/RPM*

#安装编译环境

yum groupinstall -y "Development tools"

#更新bash和openssl漏洞

yum install -y bash openssl* ntp vim

yum clean all

#更新北京时间

ntpdate ntp.fudan.edu.cn


#安装mysql组件包

yum -y install ncurses-devel openssl* cmake 

#创建用户和组

groupadd mysql

useradd -g mysql mysql

usermod -s /sbin/nologin mysql

mkdir -p /data/3306/data /data/3306/logs



if [ -f "mysql-5.6.24.tar.gz" ]; then

        echo "mysql-5.6.24.tar.gz is found"

else

        echo "mysql-5.6.24.tar.gz不存在,请确保软件包和脚本在同一目录"

exit

fi


#解压安装

tar zxvf mysql-5.6.24.tar.gz -C /usr/src

cd /usr/src/mysql-5.6.24

cmake  -DCMAKE_INSTALL_PREFIX=$position/mysql \

-DMYSQL_DATADIR=/data/3306/data \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_UNIX_ADDR=/data/3306/mysqld.sock \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EXTRA_CHARSETS=all \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DEXTRA_CHARSETS=all \

-DWITH_EMBEDDED_SERVER=1 \

-DWITH_SSL=bundled \

-DWITH_DEBUG=0 \

-DENABLE_DOWNLOADS=1


make

make install


#初始化

$position/mysql/scripts/mysql_install_db \

--basedir=$position/mysql \

--datadir=/data/3306/data \

--user=mysql


sleep 1

cp $position/mysql/my.cnf $position/mysql/my.cnf.bak


#编辑配置文件

cat >  $position/mysql/my.cnf << EOF

[mysqld]

 basedir = $position/mysql

 datadir = /data/3306/data

 port = 3306

 server_id = 2

 socket = /data/3306/mysqld.sock

 pid-file = $position/mysql/mysql.pid

 max_allowed_packet=16M

 log-bin=mysql-bin

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

EOF


#添加启动脚本

chown mysql:mysql -R /data/3306 $position/mysql

cp /usr/src/mysql-5.6.24/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig --level 2345 mysqld on


#添加环境变量

echo 'PATH=$PATH:'"$position/mysql/bin" >> /etc/profile

sleep 1

source /etc/profile

#启动

/etc/init.d/mysqld start

echo  -e "MySQL 5.6.24 已经安装\033[32m [完成] \033[0m"

dialog --title "The LNMP install script" --yesno "是否需要设置MySQL root用户密码?" 9 30

aa=`echo $?`

if [ $aa -eq 0 ];then

        dialog --title "The LNMP install script" --inputbox "请输入密码" 9 30 2> /tmp/3.txt

pass=`cat /tmp/3.txt`

echo "本次输入的密码为$pass,请牢记密码!"

sleep 1

mysqladmin -u root password $pass

rm -rf /tmp/3.txt

fi


##############PHP##########################################################

cd $patha

#检查是否安装php

rpm -q php > /dev/null

a=`echo $?`


if [ $a -eq 0 ];then

        echo "php is installed"

        yum -y remove php

        echo "php remove is ok"

fi


groupadd www

useradd -g www www

usermod -s /sbin/nologin www


yum install -y libxml2 libxml2-devel libxml2-python curl curl-devel  libjpeg libjpeg-devel libpng libpng10 libpng10-devel libpng-devel freetype-devel nss_ldap openldap openldap-clients openldap-devel openldap-servers libmcrypt libmcrypt-devel php-gd gd openssl-devel

yum clean all

\cp -frp /usr/lib64/libldap* /usr/lib


if [ -f "php-5.5.22.tar.gz" ]; then

        echo "php-5.5.22.tar.gz is found"

else

        echo "php-5.5.22.tar.gz不存在,请确保软件包和脚本在同一目录"

        exit

fi


tar zxvf php-5.5.22.tar.gz -C /usr/src/

cd /usr/src/php-5.5.22/

./configure --prefix=$position/php --with-config-file-path=$position/php/etc  \

--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \

--with-libxml-dir=/usr --enable-xml --enable-bcmath \

--enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl \

--enable-mbregex  --enable-fpm  --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 --with-pdo_mysql=$position/mysql \

--with-mysql=$position/mysql --enable-embedded-mysqli \

--with-mysqli=$position/mysql/bin/mysql_config  --with-mysql-sock=/data/3306/mysqld.sock  \

--with-curl=/usr --with-gettext --with-iconv


make

make install


\cp /usr/src/php-5.5.22/php.ini-production $position/php/etc/php.ini

\cp $position/php/etc/php-fpm.conf.default $position/php/etc/php-fpm.conf

\cp /usr/src/php-5.5.22/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm


#添加环境变量

echo 'PATH=$PATH:'"$position/php/bin" >> /etc/profile

echo 'PATH=$PATH:'"$position/php/sbin" >> /etc/profile

sleep 1

source /etc/profile


#修改用户和组为www

sed -i '149s/nobody/www/' $position/php/etc/php-fpm.conf

sed -i '150s/nobody/www/' $position/php/etc/php-fpm.conf

#开启记录pid进程

sed -i '25s/;//' $position/php/etc/php-fpm.conf


#开启代码标志的缩写形式

sed -i '202s/Off/On/' $position/php/etc/php.ini

#CGI设置

sed -i '755s/;//' $position/php/etc/php.ini

sed -i '755s/1/0/' $position/php/etc/php.ini


#设置时区

sed -i '910d' $position/php/etc/php.ini

sed -i "909 a\date.timezone = Asia/Shanghai" $position/php/etc/php.ini


#添加开机自启动

chmod +x /etc/init.d/php-fpm

chkconfig --add php-fpm

chkconfig --level 2345 php-fpm on

#启动php-fpm

/etc/init.d/php-fpm start

echo  -e "PHP 5.5.22 已经安装\033[32m [完成] \033[0m"


##############Tengin##########################################################

cd $patha

if [ -f "openssl-1.0.2a.tar.gz" ]; then

        echo "openssl-1.0.2a.tar.gz is found"

else

        echo "openssl-1.0.2a.tar.gz不存在,请确保软件包和脚本在同一目录"

        exit

fi


if [ -f "pcre-8.36.tar.gz" ]; then

        echo "pcre-8.36.tar.gz is found"

else

        echo "pcre-8.36.tar.gz不存在,请确保软件包和脚本在同一目录"

        exit

fi


if [ -f "zlib-1.2.8.tar.gz" ]; then

        echo "zlib-1.2.8.tar.gz is found"

else

        echo "zlib-1.2.8.tar.gz不存在,请确保软件包和脚本在同一目录"

        exit

fi


if [ -f "tengine-2.1.0.tar.gz" ]; then

        echo "tengine-2.1.0.tar.gz is found"

else

        echo "tengine-2.1.0.tar.gz不存在,请确保软件包和脚本在同一目录"

        exit

fi


tar zxvf openssl-1.0.2a.tar.gz -C /usr/src/

tar zxvf pcre-8.36.tar.gz -C /usr/src/

tar zxvf zlib-1.2.8.tar.gz -C /usr/src/

tar zxvf tengine-2.1.0.tar.gz -C /usr/src/

cd /usr/src/tengine-2.1.0/

./configure --prefix=$position/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/src/openssl-1.0.2a --with-zlib=/usr/src/zlib-1.2.8 --with-pcre=/usr/src/pcre-8.36

make && make install


mkdir $position/nginx/conf/vhosts

cp $position/nginx/conf/nginx.conf $position/nginx/conf/nginx.conf.bak


sed -i '2s/#//' $position/nginx/conf/nginx.conf

sed -i '2s/nobody/www www/' $position/nginx/conf/nginx.conf

sed -i '5s/#//' $position/nginx/conf/nginx.conf

sed -i '9s/#//' $position/nginx/conf/nginx.conf

sed -i '27s/#//' $position/nginx/conf/nginx.conf

sed -i '28s/#//' $position/nginx/conf/nginx.conf

sed -i '29s/#//' $position/nginx/conf/nginx.conf

sed -i '31s/#//' $position/nginx/conf/nginx.conf

sed -i '71s/#//' $position/nginx/conf/nginx.conf

sed -i '72s/#//' $position/nginx/conf/nginx.conf

sed -i '73s/#//' $position/nginx/conf/nginx.conf

sed -i '74s/#//' $position/nginx/conf/nginx.conf

sed -i '75s/#//' $position/nginx/conf/nginx.conf

sed -i '76s/#//' $position/nginx/conf/nginx.conf

sed -i '77s/#//' $position/nginx/conf/nginx.conf

sed -i "121 a\include vhosts/*;" $position/nginx/conf/nginx.conf


echo 'fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> $position/nginx/conf/fastcgi_params


cat > $position/nginx/html/test.php << EOF

<?php

phpinfo( );

?>

EOF


$position/nginx/sbin/nginx

echo  -e "Tengine 2.1.0 已经安装\033[32m [完成] \033[0m"

/etc/init.d/iptables stop

ip=`ifconfig eth0 | grep 'inet addr'| awk '{print $2}' | cut -d ":" -f 2`

echo  -e "请在浏览器地址栏输入\033[32m [http://$ip/test.php] \033[0m访问php网页"


运行效果如下:

wKioL1VLAL3Q2FU1AACH7KoNjec556.jpg

wKiom1VK_2nQlq8jAABfKiIGPF4604.jpg