运维辞典@欢迎访问小站,如有疑问,可通过右侧QQ或底部邮箱联系,非常感谢@

多版本php编译安装,适合nginx服务使用(centos6,centos7)

PHP Administrator 532℃

多版本php编译安装,配置nginx服务使用.
1.提供php5.3,php5.4,php5.5,php5.6,php7.0,php7.1安装
2.缺少php5.2,后期更新
3.所有php监听采用sock文件,而非端口

#!/bin/bash
# PHP多版本编译安装
# 提供php5.2,php5.3,php5.4,php5.5,php5.6,php7.0,php7.1安装
# 测试centos6,centos7系统可编译成功.
# 多核心快速编译
# site:www.724001.com
# 

PHPUSER=www
RUNDIR=/usr/local/src
DOWNADDR=http://192.168.6.188/down/all
TMPDOWN=/tmp/downloads
CPUNUM=`cat /proc/cpuinfo | grep processor | wc -l`
 
# 自定义颜色美化提示
function EchoRed(){
	echo -e "\033[31m $1 \033[0m"
}
function EchoYellow(){
	echo -e "\033[33m $1 \033[0m"
}
function EchoGreen(){
	echo -e "\033[32m $1 \033[0m"
}
 
# 脚本需要root用户权限来运行,因此,对当前用户进行判断,对不符合要求的用户给出友好提示,并终止程序运行
if [ "$UID" -ne 0 ];  then 
	EchoRed "Must be root to run this script."
	exit 1
fi
 
# 自定义yum方法,若组件已经存在,则跳过安装
# MYum lsof
function MYum(){
	if [[ `rpm -qa $1` == 0 ]]; then
		yum -y install $1
	fi
}

# 端口占用检查
# CheckPort 3306
function CheckPort(){
	MYum lsof
	lsof -i :$1
	if [ $? == 0 ]; then
		EchoRed "服务端口$1被占用,程序退出!"
		exit 1
	fi
}
 
# 检查上一步操作是否成功,若错误,程序将退出
# 使用方法:checkOK $1
function checkOK(){
	if [[ $? -ne 0 ]]; then
		EchoRed "出错了,程序退出.出错位置:$1"
		exit 1
	fi
}

# 检查文件是否存在,不存在则下载
# FileNotFoundWget xxx.rar
function FileNotFoundWget(){
	mkdir -p $TMPDOWN
	cd $TMPDOWN
	if [ ! -f $1 ];then
		wget $DOWNADDR/$1 -O $TMPDOWN/$1
		checkOK "下载$1 失败."
	fi
}

# 安装php需要的组件支持
function PHPInit(){
	# 安装必要组件支持
	yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel gcc openssl openssl-devel
	FileNotFoundWget libiconv-1.15.tar.gz
	tar -zxf libiconv-1.15.tar.gz
	cd libiconv-1.15
	mkdir -p /usr/local/libiconv
	./configure --prefix=/usr/local/libiconv
	checkOK "配置libiconv组件"
	make -j$CPUNUM && make install
	checkOK "编译及安装libiconv组件"
	#配置阿里云源再通过yum安装
	yum -y install libmcrypt-devel mhash mcrypt
}

# 设置php-fpm配置文件
# PHPConf PHPVER
function PHPConf(){
cat <<EOF >$RUNDIR/php-$1/etc/php-fpm.conf
[global]
pid = $RUNDIR/php-$1/var/run/php-fpm.pid
error_log = $RUNDIR/php-$1/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-$1-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = $PHPUSER
listen.group = $PHPUSER
listen.mode = 0666
user = $PHPUSER
group = $PHPUSER
pm = dynamic
pm.max_children = 12
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 2000
request_terminate_timeout = 60
EOF
}
 
function PHPInstall(){
	# php5.2.17编译需要的环境组件有所区别,单独安装
	if [[ $1 == 5.2.17 ]]; then
		ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
		ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
		yum install mhash.x86_64 mhash-devel.x86_64
	fi	
	# 创建PHP运行用户
	id $PHPUSER || useradd $PHPUSER -s /sbin/nologin -M
	# 创建PHP运行目录
	mkdir -p $RUNDIR/php-$1
	# 下载php编译安装包
	FileNotFoundWget php-$1.tar.gz
	tar -zxf php-$1.tar.gz && cd php-$1
	# 配置php
	./configure --prefix=$RUNDIR/php-$1 --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=$PHPUser --with-fpm-group=$PHPUser --enable-ftp --enable-opcache=no --with-gettext
	checkOK "php配置,版本$1"
	# 编译及安装php
	make -j$CPUNUM && make install
	checkOK "php编译及安装,版本$1"
 
	# 设置配置文件
	# /bin/cp $RUNDIR/php-$1/etc/php-fpm.conf.default $RUNDIR/php-$1/etc/php-fpm.conf
	# sed -i s#"^user = nobody"#"user = $PHPUser"# $RUNDIR/php-$1/etc/php-fpm.conf
	# sed -i s#"^group = nobody"#"group = $PHPUser"# $RUNDIR/php-$1/etc/php-fpm.conf
	PHPConf $1
	/bin/cp php.ini-production $RUNDIR/php-$1/lib/php.ini
	# 启动php-fpm服务
	$RUNDIR/php-$1/sbin/php-fpm
 	checkOK "启动php-fpm服务"

	if [[ `ps -ef | grep php-fpm | grep php-$1 | wc -l` == 0 ]]; then
		EchoYellow "PHP-$1 的php-fpm没有运行,请检查."
		exit 1
	fi
 
	EchoGreen "PHP-$1 配置并启动成功."
}

PHPInit
#PHPInstall 5.2.17
PHPInstall 5.3.29
PHPInstall 5.4.28
PHPInstall 5.5.38
PHPInstall 5.6.32
PHPInstall 7.0.25
PHPInstall 7.1.11

码字很辛苦,转载请注明来自:《运维辞典 » 多版本php编译安装,适合nginx服务使用(centos6,centos7)

喜欢 (5)