php笔记
# php5.6源码安装
yum install -y epel-release
yum install -y bzip2 bzip2-devel libpng libpng-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt-devel libxml2-devel openssl openssl-devel curl-devel openldap openldap-devel libmcrypt-devel libmcrypt
./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql --with-mysql-sock=/var/lib/mysql/mysql.sock --with-php-config=/www/php/bin/php-config --with-mysqli --with-libxml-dir --with-bz2 --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-curl --enable-bcmath --enable-sockets --with-iconv-dir=/usr/local --enable-xml --enable-dom --enable-calendar --with-gettext --with-pdo-mysql=mysqlnd --disable-fileinfo --with-ldap=shared --with-apxs2=/usr/bin/apxs
./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-php-config=/opt/php/bin/php-config --with-mysqli --with-libxml-dir --with-bz2 --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-curl --enable-bcmath --enable-sockets --with-iconv-dir=/usr/local --enable-xml --enable-dom --enable-calendar --with-gettext --with-pdo-mysql=mysqlnd --disable-fileinfo --with-ldap=shared
这里会报一个错,需要拷贝libldap到/usr/lib下 cp -frp /usr/lib64/libldap* /usr/lib/
make && make install
拷贝启动脚本:
cp php.ini-production /opt/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
chmod 755 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
service php-fpm start
如果报错,需要创建用户php-fpm和组(或者更改启动php的用户)
groupadd php-fpm
useradd -g php-fpm -s /sbin/nologin php-fpm
修改php.ini参数:(zabbix环境需要修改的参数) max_execution_time = 300 memory_limit = 128M post_max_size = 16M upload_max_filesize = 2M max_input_time = 300 date.timezone = Asia/Shanghai always_populate_raw_post_data = -1
yum install httpd-devel.x86_64 -y apache 加个参数 --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql
添加到httpd.conf AddType application/x-httpd-php-source .phps AddType application/x-httpd-php .php LoadModule php5_module modules/libphp5.so
LoadModule php5_module modules/libphp5.so
cp php.ini-production /usr/local/php/lib/php.ini 正式配置文件一般关闭显示log
make ZEND_EXTRA_LIBS='-liconv'
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
查看安装参数 php -i|grep confi
php编译安装参数详解
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql --with-mysql
#mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。 make ZEND_EXTRA_LIBS='-liconv' make install
--prefix=/usr/local/php
指定 php 安装目录
--with-apxs2=/usr/local/apache/bin/apxs
整合 apache,apxs功能是使用mod_so中的LoadModule指令,加载指定模块到 apache,要求 apache 要打开SO模块
--with-config-file-path=/usr/local/php/etc
指定php.ini位置
--with-MySQL=/usr/local/mysql
mysql安装目录,对mysql的支持
--with-mysqli=/usr/local/mysql/bin/mysql_config
mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。
--enable-safe-mode 打开安全模式
--enable-ftp 打开ftp的支持
--enable-zip 打开对zip的支持
--with-bz2 打开对bz2文件的支持
--with-jpeg-dir 打开对jpeg图片的支持
--with-png-dir 打开对png图片的支持
--with-freetype-dir 打开对freetype字体库的支持
--without-iconv 关闭iconv函数,种字符集间的转换
--with-libXML-dir 打开libxml2库的支持
--with-XMLrpc 打开xml-rpc的c语言
--with-zlib-dir 打开zlib库的支持
--with-gd 打开gd库的支持
--enable-gd-native-ttf 支持TrueType字符串函数库
--with-curl 打开curl浏览工具的支持
--with-curlwrappers 运用curl工具打开url流
--with-ttf 打开freetype1.*的支持,可以不加了
--with-xsl 打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件
--with-gettext 打开gnu 的gettext 支持,编码库用到
--with-pear 打开pear命令的支持,PHP扩展用的
--enable-calendar 打开日历扩展功能
--enable-mbstring 多字节,字符串的支持
--enable-bcmath 打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-sockets 打开 sockets 支持
--enable-exif 图片的元数据支持
--enable-magic-quotes 魔术引用的支持
--disable-rpath 关闭额外的运行库文件
--disable-debug 关闭调试模式
--with-mime-magic=/usr/share/file/magic.mime 魔术头文件位置
CGI方式安装才用的参数
--enable-fpm
打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序
--enable-fastCGI
支持fastcgi方式启动PHP
--enable-force-CGI-redirect
重定向方式启动PHP
--with-ncurses
支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库
--enable-pcntl freeTDS需要用到的,可能是链接mssql 才用到
mhash和mcrypt算法的扩展
--with-mcrypt 算法
--with-mhash 算法
以上函数库需要安装
--with-gmp 应该是支持一种规范
--enable-inline-optimization 优化线程
--with-openssl openssl的支持,加密传输时用到的
--enable-dbase 建立DBA 作为共享模块
--with-pcre-dir=/usr/local/bin/pcre-config perl的正则库案安装位置
--disable-dmalloc
--with-gdbm dba的gdbm支持
--enable-sigchild
--enable-sysvsem
--enable-sysvshm
--enable-zend-multibyte 支持zend的多字节
--enable-mbregex
--enable-wddx
--enable-shmop
--enable-soap
# php7.2源码安装
wget http://103.40.19.56/lnmp/libzip-1.3.2.tar.gz
yum -y install curl-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel libzip
./configure --prefix=/usr/local/php71 --exec-prefix=/usr/local/php71 --bindir=/usr/local/php71/bin --sbindir=/usr/local/php71/sbin --includedir=/usr/local/php71/include --libdir=/usr/local/php71/lib/php --mandir=/usr/local/php71/php/man --with-config-file-path=/usr/local/php71/etc --with-mcrypt=/usr/local/libmcrypt --with-mhash --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-gd --with-iconv --with-zlib --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-fpm --without-gdbm --disable-fileinfo --enable-opcache=no
./configure --prefix=/www/php --with-config-file-path=/www/php/etc --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring=all --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-pdo-mysql --with-mysql-sock=/var/lib/mysql/mysql.sock
cp php.ini-development /www/php/etc/php.ini
cd /www/php/etc
cp php-fpm.conf.default php-fpm.conf
vim /www/php/etc/php.ini
cgi.fix_pathinfo=0
cd /www/php/etc/php-fpm.d
cp www.conf.default www.conf
vim www.conf
/www/php/sbin/php-fpm
cp /www/php/bin/php /usr/local/bin/php
php -m
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# php7.3源码安装
./configure --prefix=/www/php73 --with-config-file-path=/www/php73 --enable-fpm --with-mysqli --with-pdo-mysql --enable-mbstring --with-gd --enable-soap --enable-cli --with-curl --with-openssl-dir --with-zlib-dir --disable-fileinfo --with-ldap=shared --enable-xml --enable-dom --enable-calendar --with-gettext --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-curl --enable-bcmath --enable-sockets
make && make install
cp php.ini-development /www/php73/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php73
cp /www/php73/etc/php-fpm.conf.default /www/php73/etc/php-fpm.conf
chmod 755 /etc/init.d/php73
chkconfig --add php73
chkconfig php73 on
service php73 start
修改一下date.timezone="Asia/Shanghai"
在进入安装后php路径etc下将 cp php-fpm.conf.default php-fpm.conf
在进入cd php-fpm.d/
cp www.conf.default www.conf
修改www.conf 的启动用户和端口
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# php7.4源码安装
yum install -y openssl-devel libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel recode-devel libicu-devel libzip-devel sqlite-devel oniguruma-devel gmp-devel readline-devel gflags-devel
cp -frp /usr/lib64/libldap* /usr/lib/
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
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
或者
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum —enablerepo=remi install libzip5-devel
./configure --prefix=/opt/php74 --with-mysqli=/opt/mysql/bin/mysql_config --with-pdo-mysql=/opt/mysql --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-fpm --without-gdbm --enable-fileinfo --enable-opcache --enable-ctype --enable-dom --with-mhash --with-iconv --enable-json --enable-mbstring --with-openssl --enable-posix --enable-session --enable-simplexml --enable-xmlreader --enable-xmlwriter --with-zlib --enable-gd --with-zip --enable-bz2 --enable-intl --with-ldap --with-smbclient --enable-bcmath --with-gmp --enable-exif --enable-pcntl --with-libxml
/lib64/liblber-2.4.so.2: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
./configure后
编辑MakeFile
找到 开头是 'EXTRA_LIBS' 这一行 在结尾加上 '-llber' 然后执行 make && make install
PHP模块imagick
avconv或ffmpeg
OpenOffice或LibreOffice
imagick
memcached
Zend OPcache
yum install -y libwebp libwebp-devel
./configure --with-php-config=/opt/php74/bin/php-config --with-jpeg-dir=/usr/local/libjpeg --with-png-dir=/usr/local/libpng --with-freetype-dir=/usr/local/freetype --with-zlib-dir=/usr/local/zlib
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# yum安装php7
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
现在使用yum命令按以下方式安装所需的全部扩展程序PHP和PHP-FPM。
sudo yum -y install php70w-fpm php70w-mcrypt php70w-curl php70w-cli php70w-mysql php70w-gd php70w-xsl php70w-json php70w-intl php70w-pear php70w-devel php70w-common php70w-mbstring php70w-tidy php70w-zip php70w-soap curl
安装完成后,我们需要编辑'php.ini'配置文件并编辑php-fpm池配置'www.conf'。
使用vim编辑器编辑'php.ini'配置文件。
vim /etc/php.ini
取消注释'cgi.fix_pathinfo'行并将该值更改为'0'。
cgi.fix_pathinfo=0
保存并退出。
接下来,编辑池配置文件'www.conf'。
vim /etc/php-fpm.d/www.conf
将运行PHP-FPM服务的默认用户更改为'nginx'用户和组。
user = nginx
group = nginx
在'listen'行中,将值更改为sock文件,如下所示。 我们将在sock文件下运行PHP-fpm。
listen = /var/run/php-fpm/php-fpm.sock
现在为套接字权限和所有者配置。 取消注释这些行并按如下所示更改值。
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
最后,取消注释PHP-FPM环境。
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
在CentOS 7上安装PHP Composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer
composer -v
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# php.ini配置文件参数优化
用于生产环境中的PHP需要对其进行优化,让PHP自身发挥更好的性能,除了写好PHP代码,还要配置好php-fpm以及php.ini调优。本文从内存、OPcache、上传、会话以及安全等方面讲解php.ini的配置调优。
# 内存优化
运行 PHP 时需要关心每个 PHP 进程要使用多少内存,php.ini 中的memory_limit 设置用于设定单个 PHP 进程可以使用的系统内存最大值。 这个设置的默认值是 128M,这对于大多数中小型 PHP 应用来说或许合适,不过,如果运行的是微型 PHP 应用,可以降低这个值,以便节省系统资源,反之,如果运行的是内存集中型 PHP 应用,可以增加这个值。这个值的大小由可用的系统内存决定,确定给 PHP 分配多少值是一门艺术,决定给 PHP 分配多少内存,以及能负担起多少个 PHP-FPM 进程时,可以根据以下维度信息进行判断:
\1. 一共可以分配给 PHP 多少内存?以一个 2G 内存的 VPS 为例,这台设备中可能还运行了其他进程,如 MySQL、Nginx 等,那么留 512M 给 PHP 是合适的。
\2. 每个 PHP 进程平均耗费多少内存?这个要监控进程的内存使用量,可以使用命令行命令top,也可以在 PHP 脚本中调用 memory_get_peak_usage() 函数,不管使用哪种方式,都要多次运行同一个脚本,然后取内存消耗的平均值。
\3. 能负担起多少个 PHP-FPM 进程?假设我给 PHP 分配了 512M 内存,每个 PHP 进程平均耗费 15M 内存,那么可以负担起 34 个 PHP-FPM 进程。 有足够的系统资源吗?最后还需要确认有足够的系统资源运行 PHP 应用并处理预期的流量。
# 开启Zend OPcache性能加速
PHP属于解释型语言,在执行代码过程中,翻译器并不产生目标机器代码,而是产生易于执行的中间代码,这种中间代码每执行一次就翻译一次,通常会导致执行效率较低,而在PHP7中可以直接开启Opcache来进行性能优化和加速。 PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的),PHP5.5之后opcache可以直接--enable-opcache。对于PHP 5.5以下版本的,可以使用APC来进行缓存。
# 配置Opcache
zend_extension=opcache.so
;(网上很多资料使用engine = On来,是window下的php)
opcache.enable=1
;设置多少内存缓存opcode,单位M。如果内存不够用,就会出现一些php文件缓存不到的情况。解决办法是设置缓存到文件中去
opcache.memory_consumption=128
;最大允许缓存多少个php文件,需要根据项目的文件数来定。这个值一定要比 PHP 应用中的文件数大。最大支持100万个文件
opcache.max_accelerated_files=4000
;这个设置的值为1时,经过一段时间后 PHP 会检查 PHP 脚本的内容是否有变化,检查的时间间隔由opcache.revalidate_freq设置指定。如果这个设置的值为0,PHP 不会检查 PHP 脚本的内容是否有变化,我们必须自己动手清除缓存的操作码。建议在开发环境中设置为1,生产环境中设置为0。
opcache.validate_timestamps=0
;验证时间戳的频率。单位是秒。;此选型依赖于opcache.vilidate_timestamps=1(开启检查机制),才生效
opcache.revalidate_freq = 60
;字符串驻留技术使用多少内存,设置为8M,这是默认值。
opcache.interned_strings_buffer = 16
;快速释放内存,推荐开启,节省资源。php-7.2.0开始,不需要配置,已集成在php引擎中
opcache.fast_shutdown=1
;设置:是不是只使用文件来缓存opcode,不使用内存缓存。建议:关掉。最好内存和文件都同时使用
opcache.file_cache_only=false
;将内存中缓存的opcdoe,备份到磁盘文件中。这样好处,重启服务器时,可以避免重新生成了。注意目录的权限要设对,设置php引擎所属linux用户能够写入。实测,若权限不够,并不会报错,只是缓存不进去
opcache.file_cache=/apps/php-7.1.7/opcode_file_cache
# 文件上传
如果你的应用允许上传文件,最好设置最大能上传的文件大小。除此之外,最好还要设置最多能同时上传多少个文件
file_uploads = 1
upload_max_filesize = 10M
max_file_uploads = 3
2
3
默认情况下,PHP 允许在单次请求中上传 20 个文件,上传的文件最大为 2MB,这里我设置为单次请求最多只能上传 3 个文件,每个文件最大为 10MB,这个值不要设置太大,否则会出现超时。 注:如果非要上传大文件,Web 服务器的配置也要做相应调整。除了在 php.ini 中设置之外,还要调整 Nginx 虚拟主机配置中的 client_max_body_size 设置。 此外,如果是上传特大文件,我建议使用Webuploader专门的上传组件,前端对大文件进行切片,后端php对分片数据进行合并还原文件。有关WebUploader应用请参考本站文章:功能强大的文件上传组件-WebUploader。
# 执行时间
max_execution_time 用于设置单个 PHP 进程在终止之前最长可运行时间。这个设置默认是 30 秒,建议将其设置为 5 秒:
max_execution_time = 5
注:在 PHP 脚本中可以调用*set_limit_time()*函数覆盖这个设置。 假设我们想要生成报告,并把结果制作成 PDF 文件,这个任务可能要花 10 分钟才能完成,而我们肯定不想让 PHP 请求等待 10 分钟,我们应该单独编写一个 PHP 文件,让其在单独的后台进程中执行,Web 应用只需几毫秒就可以派生一个单独的后台进程,然后返回 HTTP 响应。 实际上,我们在跑需要消耗大量时间来完成的任务,一般采用后台进程方式,比如我们可以使用PHP的swoole扩展来生成报表、批量发送邮件耗时长的任务。
# 处理会话
PHP 默认的会话处理程序会拖慢大型应用,因为这个处理程序会把会话数据存储在硬盘中,需要创建不必要的磁盘 I/O,浪费时间。我们应该把会话数据保存在内存中,例如可以使用 Memcached 或 Redis。这么做还有个额外好处 —— 以后便于伸缩。如果会话数据存储在硬盘中,不便于增加额外的服务器,如果把会话数据存放在 Memcached 或 Redis 里,任何一台分布式 PHP-FPM 服务器都能访问会话数据。 如果想把会话数据保存在 Memcached 中,需要做如下配置:
session.save_handler = 'memcached'
session.save_path = '127.0.0.1:11211'
2
# 缓冲输出
如果是在较少的块中发送更多数据,而不是在较多的块中发送较少的数据,那么网络的效率会更高,也就是说,在较少的片段中把内容传递给访问者的浏览器,能减少 HTTP 请求总数。 因此,我们要让 PHP 缓冲输出,默认情况下,PHP 已经启用了输出缓冲功能,PHP 缓冲 4096 字节的输出之后才会把内容发送给 Web 服务器,推荐配置如下:
output_buffering = 4096implicit_flush = false
注:如果想要修改输出缓冲区的大小,确保使用的值是4(32位系统)或8(64位系统)的倍数。
# 安全
open_basedir:使用open_basedir选项能够控制PHP脚本只能访问指定的目录,这样能够避免PHP脚本访问不应该访问的文件,一定程度上限制了phpshell的危害。我们一般可以设置为只能访问网站目录:
open_basedir = /data/www
一般我们要禁止系统函数和禁止任何文件和目录的操作,如:
disable_functions = passthru,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
expose_php = Off:将此项设置为false即不会再header头输出PHP版本信息。 display_errors = Off:生产环境中,我们应该禁止错误提示,如果是本地开发环境,可以设置为On。 log_errors = On:建议在关闭display_errors后能够把错误信息记录下来,便于查找服务器运行的原因。 error_log:设置PHP错误日志存放的目录。