Linux下PHP7.2的安装与配置

Linux下PHP7.2的安装与配置

前置条件

首先要准备一台linux服务器,系统为CentOS 7.4 64位,本次以安装php-7.2.23为例进行介绍。

php下载

# wget https://www.php.net/distributions/php-7.2.23.tar.gz

php安装

1、先安装依赖包

yum install gcc libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel  libjpeg libjpeg-devel libpng libpng-devel  freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel

2、解压并安装

# tar -zxvf php-7.2.23.tar.gz
# cd php-7.2.23
# ./configure   --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --with-png-dir=/usr/local/libpng   --with-jpeg-dir=/usr/local/jpeg   --with-freetype-dir=/usr/local/freetype   --with-zlib-dir=/usr/local/zlib   --with-libxml-dir=/usr/local/libxml2/   --with-iconv-dir=/usr/local/libiconv   --enable-libxml   --enable-xml   --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization   --enable-opcache   --enable-mbregex   --enable-fpm   --enable-mbstring   --with-openssl   --enable-pcntl   --enable-sockets   --with-xmlrpc   --enable-zip   --enable-soap   --without-pear   --with-gettext   --enable-session   --with-curl   --enable-ctype   --enable-shared   --with-gd  --with-mysqli  --with-pdo-mysql  --enable-mysqlnd
# make && make install
# php -v   //查看php版本信息

执行上面操作时可能会报一些错误,解决方案如下:

1、执行报错:
configure: error: in `/root/php/php-7.3.4':
configure: error: no acceptable C compiler found in $PATH
解决方案:
yum install -y gcc

2、执行报错:
checking whether to use system default cipher list instead of hardcoded value... no
checking for RAND_egd... no
checking for pkg-config... /usr/bin/pkg-config
configure: error: Cannot find OpenSSL's <evp.h>
解决方案:
yum install openssl openssl-devel
ln -s /usr/lib64/libssl.so /usr/lib/

3、执行报错:
checking pcre install prefix... no
checking libzip... yes
checking for the location of zlib... /usr
checking for pkg-config... (cached) /usr/bin/pkg-config
checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
解决方案:
yum remove libzip -y
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

4、执行报错:
configure: error: libxml2 not found. Please check your libxml2 installation.
解决方案:
yum install -y libxml2-devel

5、执行报错:
checking for cURL 7.10.5 or greater... configure: error: cURL version 7.10.5 or later is required to compile php with cURL support
解决方案:
yum -y install curl-devel

6、执行报错:
If configure fails try --with-webp-dir=<DIR> 
configure: error: jpeglib.h not found.
解决方案:
yum install -y libjpeg libjpeg-devel

7、执行报错:
If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
configure: error: png.h not found.
解决方案:
yum install libpng libpng-devel

8、执行报错:
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.
解决方案:
yum install -y freetype freetype-devel

php配置

1、配置php

# cp php.ini-production /usr/local/php/etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# cp /usr/local/php/bin/php  /usr/local/bin/php (解决方案:https://blog.csdn.net/sinat_14826983/article/details/84654805)
# chmod +x /etc/init.d/php-fpm

2、配置php-fpm.conf与www.conf文件

# 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 

# vi /usr/local/php/etc/php-fpm.conf,取消daemonize前的;注释,并设置daemonize = no,默认是yes(注:使用supervisor管理php-fpm时需进行该设置)
配置www.conf文件
# groupadd www  //新建www组
# useradd -g www www     //将新建用户www添加到www组
vi /usr/local/php/etc/php-fpm.d/www.conf

修改www.conf如下内容:

List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
user = www       //将nobody改为www
group = www      //将nobody改为www  


; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000               //注释php-ftpm的端口监听
;listen = /var/run/php/php-fpm.sock    //启用php-fpm.sock监听方式,二者取其一即可。

; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 511

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = www      //去掉注释,将listen.owner = nobody改为listen.ouner = www
listen.group = www      //去掉注释,将listen.owner = nobody改为listen.ouner = www
listen.mode = 0666      //去掉注释,将listen.mode =0660 改为listen.mode =0666
; When POSIX Access Control Lists are supported you can set them using
; these options, value is a comma separated list of user/group names.
; When set, listen.owner and listen.group are ignored

3、添加环境变量

# vi /etc/profile
export PATH=$PATH:/usr/local/php/bin/:/usr/local/php/sbin/

# source /etc/profile

4、启动php

# /usr/local/php/sbin/php-fpm   或
# service php-fpm start

检查启动进程与侦听端口号

# ps -ef | grep php-fpm
root      6310 19790  0 Aug16 ?        00:02:35 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www       6405  6310  0 Aug16 ?        00:00:01 php-fpm: pool www
www       6406  6310  0 Aug16 ?        00:00:01 php-fpm: pool www
root     23465 23443  0 16:27 pts/0    00:00:00 grep --color=auto php-fpm

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×