1. 下载
> 以5.4和7.2为例
wget -c http://museum.php.net/php5/php-5.4.16.tar.gz
wget -c http://cn.php.net/distributions/php-7.2.6.tar.gz
2. 解压
tar -zxvf php-5.4.16.tar.gz
tar -zxvf php-5.4.16.tar.gz
3. 依赖安装
yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib
libxml2-devel ncurses-devel bison \ libtool-ltdl-devel libiconv epel-release libmcrypt libmcrypt-devel mhash mcrypt pcre-devel openssl-dev
el freetype-devel libcurl-devel bzip2 bzip2-devel readline-devel
4. 安装
> 进入安装目录
cd php-5.4.16
> 使用./configure --help查看编译支持的选项。
./configure --hlep
> 编译参数
./configure \
--prefix=/usr/local/php/php54 \
--with-config-file-path=/usr/local/php/php54/etc/ \
--with-config-file-scan-dir=/usr/local/php/php54/etc/ \
--enable-inline-optimization \
--enable-opcache \
--enable-session \
--enable-fpm \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-sqlite3 \
--with-gettext \
--enable-mbregex \
--enable-mbstring \
--enable-xml \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-xmlrpc \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-curlwrappers \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-gd \
--enable-gd-native-ttf \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-readline
> 编译安装
make && make install
注意事项:
如果写了不支持的选项,如php7里不支持--with-mysql=mysqlnd会提示:
configure: WARNING: unrecognized options: --with-mysql
参数
--prefix 为安装路径
--with-config-file-path 为php.ini路径
--with-config-file-scan-dir 为php.ini 的扩展目录
--prefix=/usr/local/php
--with-config-file-scan-dir=/usr/local/php/etc/
一定要设置正确,多个PHP版本须编译安装在不同路径!!!
可选项:--with-fpm-user=www --with-fpm-group=www
这里面开启了很多扩展。如果这时候忘了开启,以后还能加上吗?答案是可以的。以后只需要进入源码的ext目录,例如忘了pdo_mysql,进入ext/pdo_mysql,使用phpize工具,像安装普通扩展一样即可生成pdo_mysql.so。
关于:--enable-safe-mode
开启的话php可以执行一下系统函数,建议关闭(可搜索受此函数影响的php函数)。
如果只需要配置某一个目录可以执行则 设置为on并指定 safe_mode_exec_dir=string 目录来执行系统函数。本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。
safe_mode = off
php7编译不用加这个配置。编译比较耗内存和CPU,等待半小时左右编译完成。
5. 配置文件
> 复制安装包里的php.ini到安装目录
cp php.ini-development /usr/local/php/php54/etc/php.ini
cp php-fpm.conf.default php-fpm.conf
> 配置php.ini
cd /usr/local/php/php54/etc
vim php.ini
# 不显示错误,默认
display_errors = Off
# 在关闭display_errors后开启PHP错误日志(路径在php-fpm.conf中配置),默认
log_errors = On
# 字符集,默认
default_charset = "UTF-8"
# 文件上传大小,默认值太小,建议修改10M
upload_max_filesize = 2M
# Maximum size of POST data that PHP will accept. 表单最大值,默认是8M,如果表单含有多图上传,大小可能不够。超过该大小后台收不到 表单数据
post_max_size = 8M
# 设置PHP的扩展库路径,,默认被注释了。
extension_dir = "./ext/"
# 如果不设置extension_dir,也可以直接写绝对位置:
# extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/redis.so
# 设置PHP的时区
date.timezone = PRC
# 开启opcache,默认是0
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
#函数禁用
disable_functions =exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,chmod,popen
#将php.ini里的cgi.fix_pathinfo设置为0,不然会有漏洞。
cgi.fix_pathinfo=0
#更多扩展库根据需要自行添加
> 配置php-fpm.conf,如果不用FastCGI进程管理器进行多版本共存,次步骤可以省略,安装也可以省略
vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/php/php54/var
; Default Value: none
; pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
pid = run/php-fpm.pid
; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /usr/local/php/php54/var
; Default Value: log/php-fpm.log
; 设置错误日志的路径,可以默认值
error_log = /var/log/php-fpm/error_54.log
; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
; Log等级,可以默认值
log_level = notice
; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
; 后台运行,默认yes,可以默认值
daemonize = yes
> 创建日志文件夹及文件
mkdir /var/log/php-fpm
touch /var/log/php-fpm/error_54.log
> 检查配置 如果出现诸如 test is successful 字样,说明配置没有问题
/usr/local/php/php54/sbin/php-fpm -t
6. 添加环境变量
echo -e '\nexport PATH=/usr/local/php/php54/bin:/usr/local/php/php54/sbin:$PATH\n' >> /etc/profile
source /etc/profile
7. 其他版本PHP安装
> 7.2版本编译参数
./configure \
--prefix=/usr/local/php/php72 \
--with-config-file-path=/usr/local/php/php72/etc/ \
--with-config-file-scan-dir=/usr/local/php/php72/etc/ \
--enable-inline-optimization \
--enable-opcache \
--enable-session \
--enable-fpm \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-sqlite3 \
--with-gettext \
--enable-mbregex \
--enable-mbstring \
--enable-xml \
--with-iconv \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-xmlrpc \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-readline
8. 编译安装mod_fcgid.so模块,多版本共存依赖此模块
> 下载mod_fcgid.so
wget -c http://mirrors.hust.edu.cn/apache//httpd/mod_fcgid/mod_fcgid-2.3.9.tar.gz
> 解压
tar -zxvf mod_fcgid-2.3.9.tar.gz
> 编译安装,利用APXS安装,如果找不到APXS,则安装httpd-devel
> APXS="赋值的路径为你的httpd目录下apxs文件位置"
> 编译安装完成之后会自动将其编入httpd目录下的modules里面
yum install -y httpd-devel
cd mod_fcgid-2.3.9
APXS=/usr/bin/apxs ./configure.apxs
make && make install
9. 配置http.conf
> 加载fcgid模块
LoadModule fcgid_module /usr/lib64/httpd/modules/mod_fcgid.so
> 添加ExecCGI,允许使用mod_cgi模块执行CGI脚本。不添加会出现403错误
<Directory "/var/www">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
> 设置fcgid相关配置
> AddHandler fcgid-script .fcgi .php #映射fcgi执行脚本
> 设置PHP_FCGI_MAX_REQUESTS大于或等于FcgidMaxRequestsPerProcess,防止php-cgi进程在处理完所有请求前退出
> php-cgi每个进程的最大请求数
> php-cgi最大的进程数
AddHandler fcgid-script .fcgi .php
FcgidInitialEnv PHP_MAX_REQUESTS 1000
FcgidMaxRequestsPerProcess 1000
FcgidMaxProcesses 3
FcgidIOTimeout 120
FcgidIdleTimeout 120
FcgidMaxRequestLen 2097152
AddType application/x-httpd-php .php
10. 配置虚拟目录
<VirtualHost *:8080>
ServerName localhost
DocumentRoot /var/www/demo
FcgidInitialEnv PHPRC "/usr/local/php/php54/etc"
FcgidWrapper "/usr/local/php/php54/bin/php-cgi" .php
CustomLog "/var/log/httpd/demo_access.log" common
ErrorLog "/var/log/httpd/demo_error.log"
</VirtualHost>
11. 关闭selinux,否则会出现403错误
vim /etc/selinux/config
> 将SELINUX=enforcing改为SELINUX=disabled
reboot
Comments | NOTHING