之前有记录一个安装php 5.5以及php 5.6的文章,没有安装php 7的版本,这里简单加了一个php7 的进去,可以有三个php版本进行选择安装了。
不同的是php 7.x的启动配置文件和之前的有不同即www.conf,这里多加了一个nginx的判断以及sed修改,防止无法启动。
简单的脚本具体如下:
#!/bin/bash
#auto install php 5.5 or 5.6 for centos 6.x
#author 21yunwei
downloaddir="/usr/local/src"
cpuinfo=`cat /proc/cpuinfo | grep -c processor`
function checkroot(){
if [ $UID -ne 0 ]
then
echo "Please login as root"
exit;
fi
}
function create_user_nginx(){
gflag=`cat /etc/group |awk -F':' '{print $1}' | grep nginx`
[[ $gflag != "" ]] && echo "group 'nginx' already exists" || groupadd nginx
uflag=`cat /etc/passwd |awk -F':' '{print $1}' | grep nginx`
[[ $uflag != "" ]] && echo "user 'nginx' already exists" ||useradd -r nginx -g nginx -s /sbin/nologin
}
function install_required_libs(){
echo "Install required libs,please wait:"
yum -y install gcc automake autoconf libtool make >/dev/null
yum -y install gcc gcc-c++ glibc >/dev/null
yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libicu-devel libc-client-devel bzip2-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-devel > /dev/null
wget -O ${downloaddir}/libiconv-1.13.1.tar.gz http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz >/dev/null
tar -zxf ${downloaddir}/libiconv-1.13.1.tar.gz -C ${downloaddir}
cd ${downloaddir}/libiconv-1.13.1 && ./configure --prefix=/usr/local/libiconv >/dev/null && make -j $cpuinfo >/dev/null 2>&1 && make install >/dev/null 2>&1
[ $? -eq 0 ] && echo "Required libs install complete " || echo "Required libs installed failed ,please check"
}
function menu(){
echo "###############################################################"
cat < <EOF
Please choose php version which you want to install:
1,PHP 5.5.x
2,PHP 5.6.x
3,PHP 7.0.x
EOF
echo "###############################################################"
}
function php_version(){
read -p "please choose php version that you want to install:" flag
}
function php_download(){
case $flag in
1)
version="php-5.5.29"
echo "${version} will be installed" && wget -O ${downloaddir}/${version}.tar.gz http://cn2.php.net/distributions/${version}.tar.gz
;;
2)
version="php-5.6.31"
echo "${version} will be installed" && wget -O ${downloaddir}/${version}.tar.gz http://cn.php.net/distributions/${version}.tar.gz
;;
3)
version="php-7.1.7"
echo "${version} will be installed" && wget -O ${downloaddir}/${version}.tar.gz http://cn.php.net/distributions/${version}.tar.gz
;;
*)
echo "Please input number 1,2 or 3,other novalid"
php_version
php_download
esac
}
function php_install(){
case $version in
php-5.5.29)
cd ${downloaddir} && tar zxf ${version}.tar.gz && cd ${version} && echo "Pre_configure PHP now,please wait:"
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mcrypt=/usr/include --with-openssl --with-mhash --with-zlib --with-mysql --enable-mysqlnd --with-mysqli --with-pdo-mysql --with-gd --with-iconv --enable-inline-optimization --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache=no --enable-fpm --enable-fastcgi --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-maintainer-zts --enable-intl >/dev/null 2>&1
echo "Compiling php now, please wait:"&& make ZEND_EXTRA_LIBS='-liconv' -j $cpuinfo >/dev/null 2>&1 && make install >/dev/null
#set php config
cp php.ini-development /usr/local/php/etc/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && sed -i "s/nobody/nginx/g" /usr/local/php/etc/php-fpm.conf
#set php-fpm start with system start
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig php-fpm on
#set php env
echo "export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH">> /etc/profile && source /etc/profile
;;
php-5.6.31)
cd ${downloaddir} && tar zxf ${version}.tar.gz && cd ${version} && echo "Pre_configure PHP now,please wait:"
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mcrypt=/usr/include --with-openssl --with-mhash --with-zlib --with-mysql --enable-mysqlnd --with-mysqli --with-pdo-mysql --with-gd --with-iconv --enable-inline-optimization --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache=no --enable-fpm --enable-fastcgi --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-maintainer-zts --enable-intl >/dev/null 2>&1
echo "Compiling php now, please wait:"&& make ZEND_EXTRA_LIBS='-liconv' -j $cpuinfo >/dev/null 2>&1 && make install >/dev/null
#set php config
cp php.ini-development /usr/local/php/etc/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && sed -i "s/nobody/nginx/g" /usr/local/php/etc/php-fpm.conf
#set php-fpm start with system start
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig php-fpm on
#set php env
echo "export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH">> /etc/profile && source /etc/profile
;;
php-7.1.7)
cd ${downloaddir} && tar zxf ${version}.tar.gz && cd ${version} && echo "Pre_configure PHP now,please wait:"
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mcrypt=/usr/include --with-openssl --with-mhash --with-zlib --with-mysql --enable-mysqlnd --with-mysqli --with-pdo-mysql --with-gd --with-iconv --enable-inline-optimization --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache=no --enable-fpm --enable-fastcgi --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-maintainer-zts --enable-intl >/dev/null 2>&1
echo "Compiling php now, please wait:"&& make ZEND_EXTRA_LIBS='-liconv' -j $cpuinfo >/dev/null 2>&1 && make install >/dev/null
#set php config
cp php.ini-development /usr/local/php/etc/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && sed -i "s/nobody/nginx/g" /usr/local/php/etc/php-fpm.d/www.conf
#set php-fpm start with system start
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig php-fpm on
#set php env
echo "export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH">> /etc/profile && source /etc/profile
;;
*)
echo "php version eror,please check." && exit;
;;
esac
}
function php_check_status(){
#start php
/etc/init.d/php-fpm restart && netstat -tunlp | grep php-fpm
if [ $? -eq 0 ];then
phpversion=`php -v | awk 'NR==1{print $1 $2}'`
echo "PHP start sucess,and php version is :" ${phpversion}
else
echo "PHP start failed ,please check"
exit;
fi
}
function main(){
checkroot
create_user_nginx
install_required_libs
menu
php_version
php_download
php_install
php_check_status
}
main
转载请注明:西数超哥博客www.ysidc.top» [原创]linux环境下安装php5.5.29、php5.6.31以及php7.*简易脚本
https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top











