最後更新: 2018-08-07
目錄
PHP 5.6.15
Get Source
http://php.net/downloads.php
Source:
wget -O php-5.6.15.tar.gz http://hk1.php.net/get/php-5.6.15.tar.gz/from/this/mirror
tar -zxvf php-5.6.15.tar.gz
cd php-5.6.15
準備
yum groupinstall "Development tools"
# Library 有關
yum install bzip2-devel curl-devel libxml2-devel openssl-devel libtidy-devel readline-devel libmcrypt-devel
# 圖有關
yum install libjpeg-devel libpng-devel freetype-devel gd-devel libjpeg-turbo-devel
# Centos 6
yum install httpd24u-devel httpd24u httpd24u-tools httpd24u-filesystem
# Centos 7
yum install httpd httpd-tools httpd-devel
build-script.sh
#!/bin/bash
./configure \
--prefix=/usr \
--with-config-file-path=/etc \
-with-apxs2=/usr/bin/apxs \
--disable-cgi \
--disable-ipv6 \
--enable-static \
--enable-mbstring \
--enable-zip \
--with-bz2 \
--with-mysql \
--with-mysqli \
--enable-mysqlnd \
--with-pdo-mysql \
--with-mysql-sock \
--enable-soap \
--enable-exif \
--with-bz2 \
--with-zlib \
--with-mhash \
--with-mcrypt \
--with-openssl \
--with-pcre-regex \
--enable-memcached \
--disable-memcached-sasl \
--enable-memcached-json \
--with-gd \
--with-jpeg-dir \
--with-imagick \
--with-curl \
--enable-ftp \
--with-readline \
--enable-sockets \
--with-tidy \
--enable-gd-native-ttf \
--with-freetype-dir=/usr/include/freetype2/freetype \
--without-sqlite3 \
--without-pdo-sqlite \
--enable-opcache
Cli
#!/bin/bash cd php-5.6.31 ./configure \ --disable-ipv6 \ --disable-cgi \ --enable-mbstring \ --enable-zip \ --with-bz2 \ --with-mysql \ --with-mysqli \ --enable-mysqlnd \ --with-pdo-mysql \ --with-mysql-sock \ --enable-soap \ --enable-exif \ --with-bz2 \ --with-zlib \ --with-mhash \ --with-mcrypt \ --with-openssl \ --with-pcre-regex \ --with-curl \ --enable-ftp \ --with-readline \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --without-sqlite3 \ --without-pdo-sqlite
php configure
--with-config-file-path=/etc
如果遇到以以情況
Configuration File (php.ini) Path /etc
但係
Loaded Configuration File (none)
那 "make clean" 就可以解決
module requirement
readline - yum install readline-devel
curl - yum install libcurl-devel
tidy - libtidy-devel
mysql
mysql, mysqli, mysqlnd 與 pdo_mysql 的分別
mysqli difference mysql: The "i" stands for "improved"
mysqlnd (pdo_mysql)
* written in C as a PHP extension
mysqli
MySQLi is the OOP version of MySQL extension. In the end, the accomplish the same thing: extension for interacting with MySQL from PHP. A lot of people still use the original MySQL extension vs the new MySQLi extension because MySQLi requires MySQL 4.1.13+ and PHP 5.0.7+ .
mysqlnd
MySQL Native Driver is a replacement for the MySQL Client Library (libmysqlclient).
MySQL Native Driver is part of the official PHP sources as of PHP 5.3.0.
The MySQL database extensions MySQL extension, mysqli and PDO MYSQL all communicate with the MySQL server. In the past, this was done by the extension using the services provided by the MySQL Client Library. The extensions were compiled against the MySQL Client Library in order to use its client-server protocol.
opcache
OPcache can only be compiled as a shared extension.
/etc/php.ini
zend_extension=/full/path/to/opcache.so
i.e.
zend_extension=/usr/local/lib/php/extensions/no-debug-zts-20151012/opcache.so
gd
# To enable support for FreeType 2
yum install freetype-devel
add '--with-freetype-dir=DIR'
# 找出 Path
rpm -ql freetype-devel | grep include
--with-freetype-dir=/usr/include/freetype2/freetype
# Enable TrueType string function
yum install libjpeg-turbo-devel
--enable-gd-native-ttf
memcached
Homepage: https://github.com/php-memcached-dev/php-memcached
Step 0 - Download Souce
cd /usr/src
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0...
wget https://github.com/php-memcached-dev/php-memcached/archive/2.2.0.zip
tar -zxf memcached-2.2.0.tgz
mv memcached-2.2.0 php-5.5.38/ext/memcached
Step 1 - Install SASL:
yum install cyrus-sasl-devel
Step 2 - Compile libmemcached with SASL installed:
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure
make && make install
Step 3 - Install PHP Memcached:
pecl install memcached
Remark
If libmemcached is installed in a non-standard location, use --with-libmemcached-dir=DIR switch,
where DIR is the libmemcached install prefix. This directory has to contain the include/libmemcached/memcached.h file.
Zlib is required for compression support. To specify non-standard installation of Zlib, use --with-zlib-dir=DIR switch,
where DIR is the Zlib install prefix.
This extension requires libmemcached client library (version 1.0.0 or greater).
For SASL authentication support, libmemcached must be built with SASL enabled.
Since version 0.2.0, this extension requires PHP version 5.2.0 or higher.
CLI mode php
#!/bin/bash ./configure \ --with-config-file-path=/etc \ --disable-cgi \ --disable-ipv6 \ --enable-static \ --enable-mbstring \ --enable-zip \ --with-bz2 \ --with-mysql \ --with-mysqli \ --enable-mysqlnd \ --with-pdo-mysql \ --with-mysql-sock \ --enable-soap \ --enable-exif \ --with-bz2 \ --with-zlib \ --with-mhash \ --with-mcrypt \ --with-openssl \ --with-pcre-regex \ --with-gd \ --with-jpeg-dir \ --with-imagick \ --with-curl \ --enable-ftp \ --with-readline \ --enable-sockets \ --with-tidy \ --enable-static
Setup your php.ini
cd /usr/src/php-5.5.38
cp php.ini-development /etc/php.ini
Apache Configure
php.conf
LoadModule php5_module modules/libphp5.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> AddType text/html .php DirectoryIndex index.php index.html index.htm # php session php_value session.save_handler "files" # mkdir -p /var/lib/php/session # chown apache. /var/lib/php/session # chmod 770 /var/lib/php/session php_value session.save_path "/var/lib/php/session"
Compiling PECL extensions statically into PHP
流程
$ cd /your/phpsrcdir/ext
$ pecl download extname
$ gzip -d < extname.tgz | tar -xvf -
$ mv extname-x.x.x extname
# force PHP to rebuild the configure script
cd /your/phpsrcdir # cd /usr/src/php-5.6.15
rm -f configure
./buildconf --force # need autoconf 2.13 and automake 1.4
# checking
./configure --help | grep extname # ./configure --help | grep imagick
Whether --enable-extname or --with-extname is used depends on the extension.
Typically an extension that does not require external libraries uses --enable.
i.e.
你會發現多了 "--with-imagick=DIR"
Modules
imagick
[0] 安裝 ImageMagick-devel
yum install ImageMagick-devel
[1] download imagick
DL Page: https://pecl.php.net/package/imagick
# 獲得: imagick-3.1.2.tgz
cd /usr/src
wget https://pecl.php.net/get/imagick-3.1.2.tgz
tar -zxf imagick-3.1.2.tgz
[2] 放入相應 Folder
mv imagick-3.1.2 /usr/src/php-5.x.x/ext/imagick
Troubleshoot
checking ImageMagick MagickWand API configuration program... configure: error: not found.
Please provide a path to MagickWand-config or Wand-config program. (yum install ImageMagick-devel)
memcached
php 5.2~5.6 -> 2.2.0
php 7.X -> 3.0.3
# Download pecl memcached & prepare
# 查看 version https://pecl.php.net/package/memcached
mkdir /usr/src/php7/memcached
cd /usr/src/php7/memcached
wget https://pecl.php.net/get/memcached-2.2.0.tgz
tar -zxf memcached-2.2.0.tgz
mv memcached-2.2.0 ../php-src/ext/memcached
yum install libmemcached-devel memcached-devel
# Centos 7
yum install -y libevent-devel
# build
rm -f configure
./buildconf --force
./configure --help | grep memcached # 測試有沒有 "--enable-memcached"
./configure \ --enable-memcached \ --disable-memcached-sasl \ # 不用安裝 cyrus-sasl-devel --enable-memcached-json \ --enable-memcached-protocol \ # https://github.com/memcached/memcached/blob/master/doc/protocol.txt .........
# pecl install memcache
Install PHP 7.0
php7 介紹
https://wiki.php.net/phpng
# 準備
# Ubuntu 14
apt-get install apache2-dev
# bison - GNU Project parser generator (yacc replacement)
apt-get install build-essential autoconf bison
apt-get install libfcgi-dev libfcgi0ldbl \
libbz2-dev \
libc-client2007e libc-client2007e-dev \
libssl-dev libcurl4-openssl-dev libmcrypt-dev \
libjpeg-dev libpng12-dev libgd-dev \
libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-dev \
libreadline-dev libghc-readline-dev
ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a
# Centos 7
yum groupinstall -y "Development Tools"
yum install httpd-devel
yum install libxml2-devel \
bzip2-devel curl-devel \
gd-devel libpng-devel libjpeg-devel freetype-devel \
libmcrypt-devel openssl-devel \
readline-devel \
mariadb-devel
# DL Source
# 方法一
Current Stable PHP 7.0.8 (找 Last Version)
DL:
wget http://hk1.php.net/get/php-7.0.8.tar.bz2/from/this/mirror -O php-7.0.8.tar.bz2
# 方法二
apt-get install git
mkdir /usr/src/php7
cd /usr/src/php7
git clone https://git.php.net/repository/php-src.git
git branch -a | grep PHP-7.0
git branch
# Support IMAP
# 1
configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing.
# UW C-client mail library
libc-client
# Contains the header files and libraries for developing programs
# which will use the UW C-client common API.
uw-imap-devel
# Contains static libraries for developing programs
# which will use the UW C-client common API.
uw-imap-static
yum install uw-imap-static uw-imap-devel libc-client
# 2
configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.
ln -s /usr/lib64/libc-client.a /usr/lib/libc-client.a
# 3
configure: error: This c-client library is built with Kerberos support.
Add --with-kerberos to your configure line. Check config.log for details.
# Compile
# 進入 git 的 source tree
cd php-src
cd php-*
# 建立 "./configure" file
./buildconf --force
# 看一看 Help 先
./configure --help | less
compile.sh
#!/bin/bash ./configure \ --with-config-file-path=/etc \ --prefix=/opt/php70 \ --enable-opcache \ --with-apxs2 \ --disable-cgi \ --enable-exif \ --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ # other setting --enable-static \ --disable-ipv6 \ # net --with-curl \ --enable-ftp \ --enable-sockets \ # zip --enable-zip \ --with-zlib \ --with-bz2 \ # other module --enable-bcmath \ --enable-calendar \ # encrytion --with-mcrypt \ --with-openssl \ # string --enable-mbstring \ --with-iconv \ --with-gettext \ --enable-soap \ # gd --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ # sql --with-mysqli \ --enable-mysqlnd \ --with-pdo-mysql \ # memcached --enable-memcached \ --disable-memcached-sasl \ --enable-memcached-json \ --enable-memcached-protocol \ # imap --with-imap \ --with-kerberos \ --with-imap-ssl \ # sqlite --without-sqlite3 \ --without-pdo-sqlite
Remark
--enable-maintainer-zts
U14.04 的 apache 係 threaded MPM, 所以要加 "--enable-maintainer-zts"去 Build
--with-apxs2=FILE
Build shared Apache 2.0 Handler module.
FILE is the optional pathname to the Apache apxs tool apxs
--prefix
bash compile.sh
make
make install
Installing PHP SAPI module: apache2handler /usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' libphp7.la /usr/lib/apache2/modules /usr/share/apr-1.0/build/libtool --mode=install install libphp7.la /usr/lib/apache2/modules/ libtool: install: install .libs/libphp7.so /usr/lib/apache2/modules/libphp7.so libtool: install: install .libs/libphp7.lai /usr/lib/apache2/modules/libphp7.la libtool: install: warning: remember to run `libtool --finish /usr/src/php7/php-src/libs' chmod 644 /usr/lib/apache2/modules/libphp7.so [preparing module `php7' in /etc/apache2/mods-available/php7.load] Enabling module php7. To activate the new configuration, you need to run: service apache2 restart Installing shared extensions: /usr/local/lib/php/extensions/no-debug-zts-20160303/ Installing PHP CLI binary: /usr/local/bin/ Installing PHP CLI man page: /usr/local/php/man/man1/ Installing phpdbg binary: /usr/local/bin/ Installing phpdbg man page: /usr/local/php/man/man1/ Installing build environment: /usr/local/lib/php/build/ Installing header files: /usr/local/include/php/ Installing helper programs: /usr/local/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/lib/php/ --2016-06-29 15:44:34-- https://pear.php.net/install-pear-nozlib.phar Resolving pear.php.net (pear.php.net)... 109.203.101.62 Connecting to pear.php.net (pear.php.net)|109.203.101.62|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 3579275 (3.4M) [text/plain] Saving to: ‘pear/install-pear-nozlib.phar’ 100%[============================================================>] 3,579,275 1.24MB/s in 2.8s 2016-06-29 15:44:38 (1.24 MB/s) - ‘pear/install-pear-nozlib.phar’ saved [3579275/3579275] [PEAR] Archive_Tar - installed: 1.4.0 [PEAR] Console_Getopt - installed: 1.4.1 [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.3.0 [PEAR] PEAR - installed: 1.10.1 Wrote PEAR system config file at: /usr/local/etc/pear.conf You may want to add: /usr/local/lib/php to your php.ini include_path /usr/src/php7/php-src/build/shtool install -c ext/phar/phar.phar /usr/local/bin ln -s -f phar.phar /usr/local/bin/phar Installing PDO headers: /usr/local/include/php/ext/pdo/
Apache Setting
a2dismod php5
# 自行建立設定檔
/etc/apache2/mods-enabled/php7.load
LoadModule php7_module /usr/lib/apache2/modules/libphp7.so
php7.conf
<FilesMatch ".+\.ph(p[345]?|t|tml)$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch ".+\.phps$"> SetHandler application/x-httpd-php-source # Deny access to raw php sources by default # To re-enable it's recommended to enable access to the files # only in specific virtual host or directory Order Deny,Allow Deny from all </FilesMatch> # Deny access to files without filename (e.g. '.php') <FilesMatch "^\.ph(p[345]?|t|tml|ps)$"> Order Deny,Allow Deny from all </FilesMatch> # Running PHP scripts in user directories is disabled by default # To re-enable PHP in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. <IfModule mod_userdir.c> <Directory /home/*/public_html> php_admin_flag engine Off </Directory> </IfModule>
Centos7
LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so DirectoryIndex index.php index.htm index.html <FilesMatch ".+\.ph(p[345]?|t|tml)$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch ".+\.phps$"> SetHandler application/x-httpd-php-source Order Deny,Allow Deny from all </FilesMatch> <FilesMatch "^\.ph(p[345]?|t|tml|ps)$"> Order Deny,Allow Deny from all </FilesMatch> <IfModule mod_userdir.c> <Directory /home/*/public_html> php_admin_flag engine Off </Directory> </IfModule>
compile.sh
#!/bin/bash cd php-7.0.27 ./configure \ --prefix=/opt/php70 \ --enable-fpm --with-fpm-user=apache --with-fpm-group=apache \ --disable-opcache --disable-ipv6 --disable-cgi \ --enable-exif \ --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --with-curl \ --enable-ftp \ --enable-sockets \ --enable-zip \ --with-zlib \ --with-bz2 \ --enable-bcmath \ --enable-calendar \ --with-mcrypt \ --with-openssl \ --enable-mbstring \ --with-iconv \ --with-gettext \ --enable-soap \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ --with-mysqli \ --enable-mysqlnd \ --with-pdo-mysql \ --without-sqlite3 \ --without-pdo-sqlite
Config switch
--enable-fpm - This provides us with a new binary, called php-fpm, and a default configuration file called php-fpm.conf is installed in /etc
--with-fpm-user - set FPM user (default - nobody)
--with-fpm-group - set FPM group (default - nobody)
--with-fpm-systemd - Activate systemd integration (default - no)
--with-fpm-acl - Use POSIX Access Control Lists (default - no)
php-5.3
http://php.net/releases/
./configure \ --prefix=/opt/php-5.3 \ --disable-ipv6 \ --enable-cli \ --with-pdo-mysql --with-mysql --with-mysqli \ --with-bz2 --with-zlib --enable-zip \ --with-mhash --with-openssl \ --enable-mbstring --with-tidy --with-mcrypt --with-gettext --with-pcre-regex \ --enable-bcmath --enable-calendar --enable-intl \ --with-gd --enable-inline-optimization \ --enable-sysvmsg --enable-sysvsem --enable-sysvshm \ --enable-soap \ --enable-pcntl --enable-exif \ --enable-ftp --with-curl --with-xmlrpc --enable-sockets \ --with-imap --with-kerberos --with-imap-ssl
依賴的 Package
yum install openssl-devel libcurl-devel libmcrypt-devel \
libxslt-devel libxml2-devel openssl-devel bzip2-devel \
libtidy-devel
--with-imap
要加埋 --with-kerberos --with-imap-ssl
yum install -y libc-client-devel
--with-pdo-mysql --with-mysql --with-mysqli
yum install mysql-devel
--with-gd
yum install libjpeg-turbo-devel libpng-devel freetype-devel
--enable-intl
yum install libicu-devel
make 時見遇到以下 Error
/usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status make: *** [sapi/cgi/php-cgi] Error 1
yum install libtool-ltdl-devel
pear
error
PHP Fatal error: require_once(): Failed opening required 'Date.php'
php56u-pear-1.10.5-1.ius.el6.noarch # /usr/share/pear
include_path: .:/usr/share/pear:/usr/share/php
# Code
//pear //chdir("system/lib/pear"); require_once "Date.php"; //chdir("../../..");
解決方案:
自行安裝 php-pear-Date.noarch
Compile module - apcu
# Get apcu source code
[A]
wget https://pecl.php.net/get/apcu-5.1.12.tgz
tar -zxf apcu-5.1.12.tgz
cd apcu-5.1.12
OR
[B]
git clone https://github.com/krakjoe/apcu
cd apcu
# which phpize ( Result: /usr/local/bin/phpize )
phpize
Configuring for: PHP Api Version: 20151012 Zend Module Api No: 20151012 Zend Extension Api No: 320151012
./configure
make -j 2
make install
... Installing shared extensions: /usr/local/lib/php/extensions/no-debug-zts-20151012/ Installing header files: /usr/local/include/php/
# /etc/php.ini
extension = /usr/local/lib/php/extensions/no-debug-zts-20151012/apcu.so
# Checking
php -m | grep apcu
# Config
# This is primarily useful when APC is statically compiled into PHP, # since there is no other way to disable it apc.enabled "1" apc.enable_cli "0" # The number of shared memory segments to allocate for the compiler cache. apc.shm_segments "1" # The size of each shared memory segment apc.shm_size "32M" apc.entries_hint "4096" apc.ttl "0" apc.gc_ttl "3600" apc.mmap_file_mask NULL # This option sets the percentage of processes that will skip trying to cache an uncached file. # 0 disables this feature. apc.slam_defense "1" apc.use_request_time "1" apc.serializer "default" apc.rfc1867 "0" apc.rfc1867_prefix "upload_" apc.rfc1867_name "APC_UPLOAD_PROGRESS" apc.rfc1867_freq "0" apc.rfc1867_ttl "3600" apc.coredump_unmap "0" apc.preload_path NULL