1.介绍:
久闻 php7 的速度以及性能那可是比 php5 系列的任何一版本都要快,具体性能有多好,建议还是先尝试下再说。如果你是升级或新安装,那你首先需要考虑 php7 和程序是否存在兼容性,如果程序是基于 php5 开发的,那么就需要考虑 php7 是否适合你当前的生产环境,今天我就实操并安装用于生产中。
2.安装扩展包并更新系统内核:
yum install epel-release -y
yum update
3.安装 php 依赖组件(包含 Nginx 依赖):
yum -y install 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 libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
先安装 php 依赖包,否则在编译安装 php7 的过程当中会出现各种报错,完成后即可进入下一个环节。
4.创建用户和组,并下载 php 安装包解压:
假设安装包存放目录是 ~/soft/
cd ~/soft/
groupadd www
useradd -g www www
wget http://am1.php.net/distributions/php-7.2.11.tar.gz
tar xvf php-7.2.11.tar.gz
cd php-7.2.11
5.设置变量并开始源码编译:
cp -frp /usr/lib64/libldap* /usr/lib/
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--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-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-jis-conv \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm \
注意:
1.若无报错执行下一步安装,如果编译过程中出现错误,根据报错安装依赖包,通常不会出现这种问题。
2. –enable-gd-jis-conv 此参数会导致 Zabbix 中文字符乱码,建议取消.
6.开始安装:
$ make -j 4 && make install
7.完成安装后配置 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
8.修改 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
mbstring.func_overload=2
extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20160303/ldap.so"
9.设置 OPcache 缓存:
[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
10.设置 php 安全函数:
vim /usr/local/php/etc/php.ini
默认值:
disable_functions =
修改为:
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
或通配:
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
11.配置 www.conf
取消以下注释并修改优化其参数:
listen = /var/run/www/php-cgi.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
12.创建 php-cgi.sock 存放目录
mkdir /var/run/www/
chown -R www:www /var/run/www
13.配置 php-fpm.conf
取下以下注释并填写完整路径:
pid = /usr/local/php/var/run/php-fpm.pid
至此 php7 已经安装完成。
说明:禁用 php 函数,如果程序需要这些函数,可以取消禁止,新手建议忽略此步骤。
14.创建 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=/usr/local/php/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
15.启动 php-fpm 服务并加入开机自启动:
systemctl enable php-fpm.service
systemctl restart php-fpm.service
PHP 整个安装过程已经完成。如果你是按照本文安装未成功的,希望你能留言说明错误原因,我将无偿协助你配置。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于