centos6.9编译安装php-7.2.0

切换到root用户

sudo -i

查看有没有www账号

cat /etc/passwd

没有就创建用户组的先创建个用户组,有就忽略

# 创建一个组
groupadd www
# 创建一个用户,不允许登陆和不创主目录 
useradd -s /sbin/nologin -g www -M www

创建下载文件放置目录

mkdir -p /www/local/src

创建php文件安装目录,我这是放在/www/server/php

mkdir -p /www/server/php

进入下载文件的目录

cd /www/local/src
# 下载文件
wget http://cn.php.net/distributions/php-7.2.0.tar.gz
# 解压压缩文件
tar zxvf php-7.2.0.tar.gz

安装编译时需要的相关依赖

yum -y install autoconf libjpeg libjpeg-devel libwebp-devel libevent libevent-devel libXpm-devel libtiff-devel libpng-devel libcurl-devel fontconfig-devel libzip-devel freetype-devel libxml2-devel zlib-devel glibc-devel bzip2-devel gmp-devel ncurses-devel curl-devel readline-devel recode-devel GeoIP-devel bison re2c e2fsprogs-devel krb5-devel openssl-devel libXpm* gcc gcc-c++ perl mhash-devel gd-devel file libtool libtool-libs icu libicu libicu-devel libedit-devel libtidy-devel php-devel* libmcrypt-devel libxslt-devel net-snmp-devel aspell-devel unixODBC-devel libc-client-devel libvpx-devel enchant-devel openldap openldap-devel sqlite-devel postgresql-devel pcre-devel

安装libiconv

#加强系统对支持字符编码转换的功能
cd /www/local/src
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar zxvf libiconv-1.15.tar.gz    
cd libiconv-1.15
./configure \
--prefix=/usr/local

# 检查完毕没报错就再执行下面命令
make && make install  

会有这个警告提示libtool: warning: remember to run 'libtool --finish /usr/local/lib'
然后就执行

libtool --finish /usr/local/lib

添加环境变量

echo -e "/usr/local/lib\n`cat /etc/ld.so.conf`" > /etc/ld.so.conf
/sbin/ldconfig

创建libiconv软连接

ln -s /usr/local/lib/libiconv* /usr/lib
/sbin/ldconfig

进入php解压的目录

cd /www/local/src/php-7.2.0

添加phar.phar

cd ext/phar/phar
cp ./phar.php ../phar.phar

否则编译的时候会出现下面这样的问题

Generating phar.phar
chmod: cannot access `ext/phar/phar.phar': No such file or directory
make: [ext/phar/phar.phar] Error 1 (ignored)
Build complete.
Don't forget to run 'make test'.

进入php解压的目录

cd /www/local/src/php-7.2.0

临时使用文件作为Swap分区,来解决由于内存不足导致编译被停止的问题,这是下面编译时开启fileinfo所需要的(主要原因还是本机内存不足)

# 创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)
dd if=/dev/zero of=/tmp/swapfile bs=1M count=1024

# 格式化为交换分区文件,建立swap的文件系统
mkswap /tmp/swapfile

# 启用交换分区文件,启用swap文件
swapon /tmp/swapfile

接下来执行预编译脚本,如下:

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

检查完毕没报错就再执行下面命令

make ZEND_EXTRA_LIBS='-liconv'

make之后显示如下(这是提示信息)

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
pharcommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
directorytreeiterator.inc
clicommand.inc
phar.inc
Build complete.
Don't forget to run 'make test'.

先忽略,继续执行安装

make install

安装成功之后显示如下(这是提示信息)

Wrote PEAR system config file at: /www/server/php/72/etc/pear.conf
You may want to add: /www/server/php/72/lib/php to your php.ini include_path
/www/local/src/php-7.2.0/build/shtool install -c ext/phar/phar.phar /www/server/php/72/bin
ln -s -f phar.phar /www/server/php/72/bin/phar
Installing PDO headers: /www/server/php/72/include/php/ext/pdo/

再执行

/www/local/src/php-7.2.0/build/shtool install -c ext/phar/phar.phar /www/server/php/72/bin
ln -s -f phar.phar /www/server/php/72/bin/phar

编译完成之后,删掉Swap文件

swapoff /tmp/swapfile
rm /tmp/swapfile

设置配置文件

cd /www/local/src/php-7.2.0
cp php.ini-production /www/server/php/72/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp -v /www/server/php/72/etc/{php-fpm.conf.default,php-fpm.conf}
cp -v /www/server/php/72/etc/php-fpm.d/{www.conf.default,www.conf}

更改用户组和用户名(上面配置已经设置了,所以这里不用再设置了)

vim /www/server/php/72/etc/php-fpm.d/www.conf

# 修改user和group
user = nobody
group = nobody

编辑php.ini文件,修改时区 ,在这个地方----(大概在全篇48%左右的地方)

vim /www/server/php/72/etc/php.ini

如下:

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
# 主要改这里
date.timezone = Asia/Shanghai

添加php服务和自启动

chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on 
service php-fpm start
service php-fpm restart

添加php环境变量

# 编辑/etc/profile文件
vim /etc/profile

# 在文件末尾加上如下两行代码 
PATH=$PATH:/www/server/php/72/bin
export PATH

# 更新配置文件
source /etc/profile

# 查看环境变量是否添加
echo $PATH

配置解析php,/etc/nginx/conf.d/default.conf这是yum安装的默认路径,如果是编译安装一般在/usr/local/nginx/html/,具体看你安装路径

/usr/share/nginx/html是yum安装的站点默认根目录,编译安装在/usr/local/nginx/html,具体还要看你安装路径

vim /etc/nginx/conf.d/default.conf
# 找到
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}
# 改成
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm index.php;
}

# 找到
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# 改成
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

重新加载nginx服务

service nginx reload

测试解析php

vim /usr/share/nginx/html/index.php

写入

<?php
    echo phpinfo();
?>

最后在浏览器打开,http://你的服务器地址/index.php

其他问题解决

编译安装PHP时,make后出现:PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

解决办法:

先不理它,make install之后,运行以下命令:

/www/server/php/72/bin/pear channel-update pear.php.net

/www/server/php/72/bin/pear install channel://pear.php.net/PHP_Archive-0.12.0

出现下面提示

downloading PHP_Archive-0.12.0.tgz ...
Starting to download PHP_Archive-0.12.0.tgz (86,179 bytes)
.....................done: 86,179 bytes
install ok: channel://pear.php.net/PHP_Archive-0.12.0

这样就可以了

php版本隐藏

PHP容易暴露的版本号在什么地方呢?其实也是在HTTP头,以类似X-Powered-By: PHP/5.2.11这种形式存在

打开php.ini,找到下面叙述:

;;;;;;;;;;;;;;;;;
; Miscellaneous ;
;;;;;;;;;;;;;;;;;
 
; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
expose_php = On

expose_php = On改为expose_php = Off就搞定了

END

推荐阅读更多精彩内容