Centos8安装php7.2

安装必要的依赖软件

如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作。

yum -y install bzip2-devel curl-devel freetype-devel gcc libjpeg-devel libpng-devel libxslt-devel libxml2-devel openssl-devel pcre-devel pcre-devel zlib-devel

   1、下载php

下载地址:http://php.net/get/php-7.0.11.tar.gz/from/a/mirror

下载“php-7.0.11.tar.gz”后移动到linux服务器的任意目录下,只在编译时指定安装到的目录。

    2、解压安装包

tar -zxvf php-7.0.11.tar.gz
cd php-7.0.11

    3、配置安装变量

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip




“/usr/local/php”是安装路径,可以改成自己喜欢的安装路径。

    4、编译源码

# 在解压目录执行编译命令:make
cd php-7.0.11 make

编译会花费一些时间,耐心等待即可。编译完成的最后会提示你执行make test命令。就是对上一步 make 的检查,

要确保 make 是没有错误的,也就是这一步的 test、check要全部是 OK 的,error 为0。

    5、安装php

make install

    6、配置PHP

将PHP源码包(/usr/local/php-7.0.11)中的php.ini-development文件复制到/usr/local/php/下,更名为php.ini。

cp /usr/local/php-7.0.11/php.ini-development /usr/local/php/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

配置php.ini “cgi.fix_pathinfo=0”

启动php-fpm服务:

/usr/local/php/sbin/php-fpm

    7、配置nginx

编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:

然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

location ~* \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root

修改完这些保存并退出,然后重启nginx:/usr/local/nginx/sbin/nginx -s reload

接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:

<?php
    phpinfo();
?>

  然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功了:

设置开机自启动

设置软连接
ln -sf /www/server/php/bin/php /usr/bin/php
ln -sf /www/server/php/bin/phpize /usr/bin/phpize
ln -sf /www/server/php/bin/pear /usr/bin/pear
ln -sf /www/server/php/bin/pecl /usr/bin/pecl
ln -sf /www/server/php/sbin/php-fpm /usr/bin/php-fpm

/etc/systemd/system/添加文件php-fpm.service

内容如下

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/www/server/php/var/run/php-fpm.pid
ExecStart=/www/server/php/sbin/php-fpm --nodaemonize --fpm-config /www/server/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置启动脚本

在目录/etc/rc.d/init.d/添加文件php-fpm

内容如下

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/www/server/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
	try=0

	while test $try -lt 35 ; do

		case "$1" in
			'created')
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;

			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN --daemonize $php_opts

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		fi

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if [ ! -r $php_fpm_PID ] ; then
			echo "php-fpm is stopped"
			exit 0
		fi

		PID=`cat $php_fpm_PID`
		if ps -p $PID | grep -q $PID; then
			echo "php-fpm (pid $PID) is running..."
		else
			echo "php-fpm dead but pid file exists"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	configtest)
		$php_fpm_BIN -t
	;;

	*)
		echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
		exit 1
	;;

esac

查看当前状态
systemctl start php-fpm.service

设置开启其他

systemctl enable php-fpm.service

刷新配置文件

systemctl daemon-reload

systemctl list-unit-files|grep php-fpm

出现php-fpm.service enabled 表示成功开启

未经允许不得转载:紫竹林-程序员中文网 » Centos8安装php7.2
关于我们 免责申明 意见反馈 隐私政策
程序员中文网:公益在线网站,帮助学习者快速成长!
关注微信 技术交流
推荐文章
每天精选资源文章推送
推荐文章
随时随地碎片化学习
推荐文章
发现有趣的