溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Centos安裝PHP7及配置php-fpm開機(jī)啟動(dòng)的示例

發(fā)布時(shí)間:2021-01-08 14:24:15 來(lái)源:億速云 閱讀:166 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹Centos安裝PHP7及配置php-fpm開機(jī)啟動(dòng)的示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

cd /usr/local/src
wget -c http://cn2.php.net/distributions/php-7.2.11.tar.gz

下載好后解壓 php-7.2.11.tar.gz 壓縮包,并進(jìn)入解壓后的目錄

tar -xzvf php-7.2.11.tar.gz
cd php-7.2.11

安裝php7需要的一些依賴庫(kù)包 libxml2和一些其他依賴的擴(kuò)展庫(kù)

yum -y install libxml2 
yum -y install libxml2-devel 
yum -y install openssl 
yum -y install openssl-devel 
yum -y install curl-devel 
yum -y install libjpeg-devel 
yum -y install libpng-devel 
yum -y install freetype-devel
yum -y install bzip2-devel
yum -y install libmcrypt libmcrypt-devel
yum -y install postgresql-devel
yum -y install aspell-devel
yum -y install readline-devel
yum -y install libxslt-devel
yum -y install net-snmp-devel
yum -y install unixODBC-devel
yum -y install libicu-devel
yum -y install libc-client-devel
yum -y install libXpm-devel
yum -y install libvpx-devel
yum -y install enchant-devel
yum -y install openldap
yum -y install openldap-devel
yum -y install db4-devel
yum -y install gmp-devel
yum -y install sqlite-devel
yum -y install mysql-devel
yum install oniguruma-devel -y

安裝前的環(huán)境配置檢查,php7的一些依賴包的檢查和php擴(kuò)展的啟動(dòng),這個(gè)過(guò)程如果缺少php依賴的庫(kù)包會(huì)有報(bào)錯(cuò)提示。

添加用戶和組: (此處用戶和組為 nginx, 我 nginx 用的用戶名 ) groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin

php 安裝配置  

請(qǐng)根據(jù)以下 A B C 選擇合適的一項(xiàng)進(jìn)行安裝配置

A. PHP 7.2.10 單選模式

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-xmlrpc --with-openssl --with-mcrypt --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache

B. PHP 7.2.10 多行模式

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \ 
--with-fpm-user=nginx \
--with-fpm-group=nginx \ 
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

C. PHP 7.4.10

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-kerberos \
--with-libdir=lib64 \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--enable-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--with-zip \
--enable-xml \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--enable-pcntl

從 PHP 7.4.0 開始,必須在編譯 PHP 時(shí)用 --with-zip 配置選項(xiàng)來(lái)提供 zip 支持。之前的 PHP 版本,需要使用 --enable-zip 選項(xiàng)。

對(duì)php7進(jìn)行編譯和安裝的操作

make && make install

( 如果出現(xiàn) make: *** [sapi/cli/php] Error 1 的錯(cuò)誤,通過(guò)繼續(xù)執(zhí)行 make ZEND_EXTRA_LIBS='-liconv' 命令解決

編譯成功,查看php版本

/usr/local/php/bin/php -v

運(yùn)行php-fpm

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

啟動(dòng)php-fpm可能會(huì)遇到的錯(cuò)誤,基本都是配置文件配置和重命名的問(wèn)題:

錯(cuò)誤一:缺少www.conf配置文件(WARNING: Nothing  matches the include pattern /usr/local/php/etc/php-fpm.d/*.conf),需要把/usr/local/php/etc/php-fpm.d/*.conf目錄中的www.conf.default重命名為www.conf。

錯(cuò)誤二:缺少php-fpm.conf配置文件 需要在/usr/local/php/etc/php-fpm.conf 把默認(rèn)的配置文件php-fpm.conf.default改成php-fpm.conf

錯(cuò)誤三:缺少php.ini配置文件 php7編譯安裝好后會(huì)發(fā)現(xiàn)沒(méi)有php.ini配置文件,需要在解壓包的根目錄中復(fù)制php.ini-production或php.ini-development并重命名為php.ini到php編譯好的安裝目錄中的/usr/local/php/etc文件夾即可。


其它錯(cuò)誤的解決:
error: Package requirements (sqlite3 > 3.7.4) were not met

yum install libsqlite3x-devel -y

error: Package requirements (oniguruma) were not met

yum install oniguruma-devel -y

當(dāng)yum install 提示 "沒(méi)可用軟件包"

yum install -y epel-release

off_t undefined 報(bào)錯(cuò)

configure: error: off_t undefined; check your library configuration

configure: WARNING: unrecognized options: --with-mcrypt, --enable-gd-native-ttf

php7.2以后不支持–with-mcrypt, --enable-gd-native-ttf 這兩個(gè)參數(shù),需要去掉


在centos7中編譯安裝好php后,系統(tǒng)重啟后并不會(huì)對(duì)php進(jìn)行開機(jī)啟動(dòng),所以需要對(duì)centos7添加開機(jī)啟動(dòng)腳本對(duì)php進(jìn)行開機(jī)啟動(dòng)服務(wù)。

php7編譯安裝方法:http://zixuephp.net/article-207.html

手動(dòng)啟動(dòng):

  1. /usr/local/php/sbin/php-fpm

開機(jī)自動(dòng)啟動(dòng):

開啟配置php-fpm pid

  1. #找到php-fpm.conf配置文件

  2. /usr/local/php/etc/php-fpm.conf

  3. #開啟pid ,去掉分號(hào)注釋

  4. pid = run/php-fpm.pid


安裝后要進(jìn)行的操作:

1. 把安裝目錄下的 php.ini-development 復(fù)制到 /etc/ 目錄下
2. 修改 /usr/local/php/etc/php-fpm.conf.default 名為 php-fpm.conf 并進(jìn)入修改
3. 修改 /usr/local/php/etc/php-fpm.d/www.conf.default 為 www.conf 并進(jìn)入修改

PHP 7.4.10 安裝 ZIP

1 擴(kuò)展包    wget https://pecl.php.net/get/zip-1.19.0.tgz 
2 解壓 并進(jìn)入    tar -zxvf zip-1.19.0.tgz      2.2   cd  zip-1.19.0
3 /usr/local/php/bin/phpize  
4 ./configure --with-php-config=/usr/local/php/bin/php-config   自己的php目錄
5 make && make install

---------------------
然后下載最新的,解壓

wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxvf libzip-1.5.2.tar.gz

這時(shí)發(fā)現(xiàn)還要安裝cmake,再去下載cmake:
wget https://github.com/Kitware/CMake/releases/download/v3.15.4/cmake-3.15.4.tar.gz

解壓:
tar -zxvf cmake-3.15.4.tar.gz

注意:cmake要按照文檔安裝
./bootstrap && make && sudo make install

漫長(zhǎng)的等待,安裝完成后,執(zhí)行
cmake -version
出現(xiàn)版本號(hào)即表示cmake安裝成功。

接下來(lái), 再進(jìn)入 cd libzip-1.5.2
mkdir build && cd build/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libzip ..
make
make install

//檢測(cè)libzip是否安裝成功
whereis libzip 
libzip: /usr/local/libzip

創(chuàng)建開機(jī)啟動(dòng)腳本

在Linux中將php-fpm配置成服務(wù)的方法

PHP 源文件路徑:   /usr/local/src/php-7.2.11/
PHP 安裝后路徑:   /usr/local/php/

1. 配置 php-fpm.conf

vim /usr/local/php/etc/php-fpm.conf

php-fpm.pid 目錄必須指向:/usr/local/php/var/run/php-fpm.pid

2. 拷貝php-fpm腳本至/etc/init.d目錄

cp /usr/local/src/php-7.2.11/sapi/fpm/init.d.php-fpm /etc/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=/usr/local/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

3.設(shè)置權(quán)限并啟動(dòng)php-fpm:

chmod 755 /etc/init.d/php-fpm

/etc/init.d/php-fpm start

chkconfig –add php-fpm

最后,給出php-fpm以服務(wù)的方式啟動(dòng)、停止和重啟:

service php-fpm start

service php-fpm stop

service php-fpm reload


PHP-FPM 自啟動(dòng)配置服務(wù):  
vim /etc/rc.d/rc.local

Centos安裝PHP7及配置php-fpm開機(jī)啟動(dòng)的示例


查看開機(jī)啟動(dòng)的所有服務(wù)

chkconfig --list


nginx demo 配置:(不要忘記chown www-data:www-data 網(wǎng)站目錄 不然沒(méi)權(quán)限)

Centos安裝PHP7及配置php-fpm開機(jī)啟動(dòng)的示例

server {
	    listen      80;
	    server_name {{ server_name }};
	    access_log  /data/logs/nginx/{{ server_name }}/access.log;
	    error_log   /data/logs/nginx/{{ server_name }}/error.log;
    location / {
        index           index.php index.html index.htm;
        root            /data/www/wwwroot/{{ server_name }}/;
 
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php;
        }
    }
 
    location ~ \.php$ {
        include        fastcgi_params;
        fastcgi_pass   unix:/tmp/php-fcgi.sock;
        fastcgi_index  index.php;
    }
}

php-fpm配置 (重命名兩個(gè)文件 其他看實(shí)際情況配置(略。。。))
www.conf.default -> www.conf
php-fpm.conf.default -> php-fpm.conf
啟動(dòng): php-fpm -D
nginx -s reload

以上是“Centos安裝PHP7及配置php-fpm開機(jī)啟動(dòng)的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI