PHP编译安装详细教程

如果PHP编译参数有--with-mcrypt必须手动安装libmcrypt,因为版权问题CentOS的yum源没有libmcrypt包。
 
libmcrypt下载地址:点此下载

建立服务管理组和用户
[root@localhost ~]# groupadd php && useradd -s /sbin/nologin -g php -M php
[root@localhost ~]# groupadd www && useradd -s /sbin/nologin -g www -M www

开始安装libmcrypt
[root@localhost ~]# cd /src/
[root@localhost src]# rz -b
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring libmcrypt-2.5.8.tar.gz...
  100%    1303 KB    1303 KB/sec    00:00:01       0 Errors
[root@localhost src]# tar -xf libmcrypt-2.5.8.tar.gz
[root@localhost src]# cd libmcrypt-2.5.8
[root@localhost libmcrypt-2.5.8]# ./configure --prefix=/program/libmcrypt && make && make install
[root@localhost libmcrypt-2.5.8]# chown -R php:php /program/libmcrypt/
 
libmcrypt安装好了,在编译PHP时由于要用到libmcrypt的lib目录,但是在编译PHP过程中使用的是{LD_LIBRARY_PATH}环境变量来引入libmcrypt的lib目录,所以要先把libmcrypt的lib目录路径加入{LD_LIBRARY_PATH}环境变量。
[root@localhost libmcrypt-2.5.8]# vim /etc/profile
……………此处省略代码若干……………
export LD_LIBRARY_PATH=/program/libmcrypt/lib:$LD_LIBRARY_PATH
[root@localhost libmcrypt-2.5.8]# shutdown -r now
[root@localhost ~]# echo $LD_LIBRARY_PATH
/program/libmcrypt/lib:
[root@localhost ~]# 
 
接下来就是编译安装PHP
[root@localhost ~]# cd /src/
[root@localhost src]# rz -b
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring php-5.6.40.tar.gz...
  100%   18853 KB    18853 KB/sec    00:00:01       0 Errors
[root@localhost src]# tar -xf php-5.6.40.tar.gz
[root@localhost src]# cd php-5.6.40
[root@localhost php-5.6.40]# ./configure \
--prefix=/program/php \ # 指定安装目录
--with-config-file-path=/program/php \ # 配置文件php.ini存放目录
--with-fpm-group=www \ # PHP-FPM进程所属组
--with-fpm-user=www \ # PHP-FPM进程所属用户
--with-mcrypt=/program/libmcrypt \ # 指定libmcrypt 目录
--with-gd \ # 如果GD是RPM安装的则不用指定路径,否则要用--with-gd=<GD路径>
--enable-opcache \ # 该参数编译如果没办法通过可设为[--enable-opcache=no]或去掉该参数
--disable-debug \
--disable-rpath \
--enable-bcmath \
--enable-calendar \
--enable-ctype \
--enable-exif \
--enable-fpm \
--enable-ftp \
--enable-gd-native-ttf \
--enable-inline-optimization \
--enable-maintainer-zts \ # 如果需要使用多线程扩展pthreads则必须加上该参数
--enable-mbregex \
--enable-mbstring \
--enable-pcntl \
--enable-session \
--enable-shared \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--with-bz2 \
--with-curl \
--with-freetype-dir \
--with-gettext \
--with-iconv \
--with-jpeg-dir \
--with-kerberos \
--with-ldap-sasl \
--with-libxml-dir \
--with-mhash \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-png-dir \
--with-readline \
--with-xmlrpc \
--with-xsl \
--with-zlib
[root@localhost php-5.6.40]# make
[root@localhost php-5.6.40]# make install

建立配置文件和服务管理文件
[root@localhost ~]# cp /src/php-5.6.40/php.ini-production /program/php/php.ini && \ # 建立PHP配置文件php.ini文件
cp /program/php/etc/php-fpm.conf.default /program/php/etc/php-fpm.conf && \ # 建立PHP-FPM配置文件php-fpm.conf文件
chown -R php:php /program/php/ && \
cp /src/php-5.6.40/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \ # 建立PHP-FPM服务管理文件
chmod +x /etc/init.d/php-fpm && \
chkconfig php-fpm on # 设置开启自动启动PHP-FPM
[root@localhost ~]# 
备注:PHP源码目录下有php.ini-production和php.ini-development两份php.ini文件,分别用于生产环境和开发环境,可根据实际情况选择使用其中一份。

修改PHP-FPM服务管理文件
[root@localhost ~]# vim /etc/init.d/php-fpm
……………此处省略代码若干……………
prefix=/program/php
exec_prefix=${prefix}
 
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
# 更改PHP-FPM的pid文件保存路径
# php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_fpm_PID=/dev/shm/php-fpm.pid
 
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
……………此处省略代码若干……………
[root@localhost ~]# 

修改PHP-FPM配置文件
[root@localhost ~]# vim /program/php/etc/php-fpm.conf
……………此处省略代码若干……………
; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]
 
; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or /program/php) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool
 
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = www
group = 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 IPv4 addresses 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.
; 监听地址端口或socket,可根据需求决定监听哪一个,前者可做负载均衡,后者性能更好
; listen = 127.0.0.1:9000
listen = /dev/shm/php-fpm.sock
 
; Set listen(2) backlog.
; Default Value: 65535 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 65535
 
; 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.group = www
listen.mode = 0660
; 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
……………此处省略代码若干……………
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
; 指定慢执行日志文件路径
;slowlog = log/$pool.log.slow
slowlog = log/$pool.slow.log
 
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
; 慢执行时间阈值(即PHP脚本执行超过多长时间就写慢执行日志),其中0s表示关闭慢执行日志记录
;request_slowlog_timeout = 0
request_slowlog_timeout = 3s
 
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
……………此处省略代码若干……………
[root@localhost ~]# 

配置php.ini文件
[root@localhost ~]# vim /program/php/php.ini
 
[PHP]
; 隐藏PHP版本号
expose_php = Off
 
[Date]
; 设置时区
date.timezone = Asia/Shanghai
 
[Pdo_mysql]
pdo_mysql.default_socket = /dev/shm/mysql.sock
 
[MySQL]
mysql.default_socket = /dev/shm/mysql.sock
 
[MySQLi]
mysqli.default_socket = /dev/shm/mysql.sock
 
[root@localhost ~]# 

Copyright © 2024 码农人生. All Rights Reserved