介绍
php7.2是第一个在标准库中采用现代密码学模块的编程语言。比起php7.1性能上略有提升。
如果系统是REHL7默认是没有yum源的
国内源文档:https://www.cnblogs.com/imweihao/p/7357484.html
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装“源”
yum install epel-release -y
EPEL
的全称叫 Extra Packages for Enterprise Linux
。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。装上了 EPEL之后,就相当于添加了一个第三方源。yum update
安装软件依赖
yum -y install ImageMagick wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
如果安装现代安全加密库、执行下面命令
yum install -y libsodium libsodium-devel
如果安装了第三方源执行上面libsodium还是没有找到包、那么可能就需要手动下载编译安装
官方包地址:https://download.libsodium.org/libsodium/releases/
文档地址:https://download.libsodium.org/doc/installation
wget下载后
./configure
make && make check
sudo make install
如果用之前的加密库执行、PHP7官方就不推荐使用mcrypt原作者早就不维护了
yum install libmcrypt libmcrypt-devel -y
mariadb
如果用的是mariadb、安装源文档:https://downloads.mariadb.org/mariadb/repositories/
vim /etc/yum.repos.d/mariadb.repo
添加内容
# MariaDB 10.3 CentOS repository list - created 2018-07-28 14:05 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
mariadb官方源速度特别慢、可以用国内的、如果其他版本更换数字10.3即可
# MariaDB 10.3 CentOS repository list - created 2018-07-28 14:05 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
以上是中国科学技术大学的 mariadb yum源,下载起来非常快,如果直接官网下载,非常的慢,非常的慢,因为这软件加依赖包,都有167M !!!
参考链接:http://mirrors.ustc.edu.cn/help/mariadb.html
yum clean all
rm -rf /var/cache/yum
yum makecache
安装数据库
yum install MariaDB-server MariaDB-client MariaDB-devel
MariaDb的简单配置:https://www.linuxidc.com/Linux/2016-03/128880.htm
安装php
cd /tmp
groupadd www
useradd -g www www
wget http://am1.php.net/distributions/php-7.2.8.tar.gz
tar xvf php-7.2.8.tar.gz
cd php-7.2.8
源码编译
cp -frp /usr/lib64/libldap* /usr/lib/
注意:如果要开启多进程和多线程的话加上下面参数
--enable-pcntl 多进程功能
--enable-maintainer-zts 线程安全
--with-mcrypt 老版本加密、不建议在用了
7.2.8的从来原来的–enable-zip
改为–with-libzip
测试环境开启 --enable-fileinfo
生产环境关闭 --disable-fileinfo
php7.1默认是不带sodium加密扩展的。
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-sodium \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--enable-calendar \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-libmbfl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-xmlrpc \
--with-libzip \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--with-pear \
--with-ldap=shared \
--without-gdbm \
--enable-maintainer-zts \
--enable-pcntl
–enable-gd-jis-conv
此参数会导致Zabbix中文字符乱码,建议取消(用的较少)。–with-ladp
中是小写L
若无报错执行下一步安装,如果编译过程中出现错误,根据报错安装依赖包,通常不会出现这种问题。
开始安装
make -j 4 && make install
完成安装后配置php.ini文件
cp php.ini-development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
修改 php.ini 相关参数:
vim /usr/local/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
extension = "ldap.so"
注解
php.ini | 默认值 | 描述 |
---|---|---|
expose_php = Off | 默认值:On | 禁用将expose_php,防止Web服务器签名,防止在http头里暴露X-Powered-By:PHP/7.2这样的敏感信息。 |
short_open_tag = Off | 默认值:On | 启用php短标签, 这是正常的写法,为短标签。 |
max_execution_time = 120 | 默认值:30 | PHP程序处理请求的最大时间,单位“秒” 。 |
max_input_time = 60 | 默认值:60 | 此变量可以以“秒”为单位对通过POST、GET以及PUT方式接收数据时间进行限制。 |
memory_limit = 128M | 默认值:128M | 允许使用内存的最大值限制。设置脚本可以分配的最大内存量,防止失控的脚本独占服务器内存。 |
file_uploads = On | 默认值:On | 确定服务器上的PHP脚本是否可以接受HTTP文件上传 |
post_max_size = 8M | 默认值:8M | 限制通过POST方法可以接受的信息最大量 |
upload_max_filesize = 8M | 默认值:2M | 限制PHP处理上传文件的最大值,此值必须小于post_max_size值 |
date.timezone = Asia/Shanghai | 默认值: | 设置PHP执行的默认时间的时区规则。 |
mbstring.func_overload=2 | 默认值:0 | 常用的substr()会被自动替换为 mb_substr()。<br/>0:代表不重载任何函数(默认值);<br/>1:代表重载mail()函数;<br/>2:代表重载str系列字符串处理函数;<br/>4:代表重载ereg系列正则处理函数;<br/>7:代表重载所有以上提及的函数。 |
设置OPcache缓存
OPcache使用总结:https://blog.csdn.net/why_2012_gogo/article/details/51134674
[opcache]
zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20160303/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
设置php安全函数
vim /usr/local/php/etc/php.ini
默认值:disable_functions =
根据业务实际情况该或者不改:
在文件313行左右
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru
配置www.conf
取消以下注释并修改优化其参数:
此参数可以改或者不改 listen = /var/run/www/php-cgi.sock
默认是监听端口、据说用sock文件效率比较高
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
listen.backlog = -1
pm.max_children = 180
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 180
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log
注解:
; fpm监听端口
; 即nginx中php处理的地址,一般默认值即可。
; 可用格式为: ‘ip:port’, ‘port’, ‘/path/to/unix/socket’. 每个进程池都需要设置.
listen = 127.0.0.1:9000
; backlog数,-1表示无限制,由操作系统决定。
; 此行注释掉就行。
;listen.backlog = -1
; 允许访问FastCGI进程的IP,设置any为不限制IP(默认值是any)。
; 如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。
; 每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接。
listen.allowed_clients = 127.0.0.1
; 启动进程的帐户和组
user = nginx
group = nginx
; unix socket设置选项,如果使用tcp方式访问,这里注释即可。
;listen.owner = www
;listen.group = www
;listen.mode = 0666
; 允许访问FastCGI进程的IP,设置any为不限制IP(默认值是any)。
; 如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。
; 每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接。
pm = dynamic
; 子进程最大数
pm.max_children = 8
; 启动时的进程数(Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2)
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
pm.start_servers = 4
; 前提是pm = dynamic
; 保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程。
pm.min_spare_servers = 4
; 保证空闲进程数最大值,如果空闲进程大于此值,此进行清理。
; 前提是pm = dynamic
pm.max_spare_servers = 4
; 设置每个子进程重生之前服务的请求数.
; 对于可能存在内存泄漏的第三方模块来说是非常有用的.
; 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
pm.max_requests = 500
; FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到
; Default Value: not set
;pm.status_path = /status
; FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。
; Default Value: not set
;ping.path = /ping
; 用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.
; Default Value: pong
;ping.response = pong
; 设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用.
; 设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。
; Default Value: 0
request_terminate_timeout = 180
; 当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中.
; 设置为 '0' 表示 'Off'
; Default Value: 0
;request_slowlog_timeout = 10
; 慢请求的记录日志,配合request_slowlog_timeout使用
;slowlog = log/$pool.log.slow
; 设置文件打开描述符的rlimit限制.
; 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。
; Default Value: system defined value
;rlimit_files = 1024
; 设置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整数. 默认值: 系统定义值.
; Default Value: system defined value
;rlimit_core = 0
; 启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.
; Default Value: not set
;chroot =
; 设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)
;chdir = /var/www
; 重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.
; Default Value: no
;catch_workers_output = yes
如果用sock文件的形式
创建php-cgi.sock存放目录
mkdir /var/run/www/
chown -R www:www /var/run/www
配置php-fpm.conf
取以下注释并填写完整路径:pid = /var/run/php-fpm.pid
注解:
; 错误日志,默认在安装目录中的var/log/php-fpm.log
error_log = log/php-fpm.log
; 错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
log_level = notice
; 表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
emergency_restart_threshold = 60
emergency_restart_interval = 60s
; 后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。
daemonize = yes
至此php7已经安装完成。
nginx配置
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_param HTTPS on;
}
如果出现:FastCGI sent in stderr: “Primary script unknown” while reading response head
- 文件不存在
- php-fpm 与nginx不能通信
- root位置错误
完整的主机配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_param HTTPS on;
}
}
启动脚本
创建system系统单元文件php-fpm启动脚本:vim /usr/lib/systemd/system/php-fpm.service
添加如下变量内容:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
开机自启fpm
systemctl daemon-reload
systemctl enable php-fpm.service
systemctl restart php-fpm.service
php7.3编译参数
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=webadmin --with-fpm-group=webadmin --with-sodium --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --enable-calendar --with-iconv --with-freetype-dir=/usr/include/freetype2 --with-jpeg-dir=/usr/include --with-webp-dir=/usr/include/webp --enable-gd-jis-conv --with-zlib --with-libxml-dir=/usr/include/libxml2 --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-exif --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --with-gd --with-openssl --with-mhash --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fileinfo --enable-opcache --with-pear --with-ldap=shared --without-gdbm --enable-maintainer-zts --enable-pcntl
解决
//解决该问题:php7.3 configure: error: off_t undefined; check your library configuration
添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
更新配置
ldconfig -v