安装PHP7

一、下载源码PHP7

wget http://cn2.php.net/distributions/php-7.1.8.tar.gz

二、安装php

安装依赖

yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel gcc gcc-c++  autoconf   zlib zlib-devel glibc glibc-devel glib2 glib2-devel  curl curl-devel   libmcrypt libmcrypt-devel
tar zxvf php-7.1.8.tar.gz
cd php-7.1.8

./configure --prefix=/usr/local/php  --with-zlib  --with-pdo-mysql --with-pdo-mysql=shared,mysqlnd  --enable-mbstring  --with-gd  --with-png-dir=/usr/lib64 --with-jpeg-dir=/usr/lib64  --with-freetype-dir=/usr/lib64 --with-mhash --with-openssl --with-mysqli=shared,mysqlnd  --with-pdo-mysql --with-iconv --enable-zip --enable-inline-optimization  --disable-fileinfo --without-gdbm  --enable-session --with-curl --with-gettext --enable-fpm --enable-ftp --enable-bcmath --enable-xml --enable-shmop --enable-soap --enable-mbregex --enable-sysvsem --enable-shared --disable-debug --enable-embedded-mysqli --enable-pcntl --with-xmlrpc --enable-sockets --enable-gd-native-ttf  --enable-mysqlnd --with-mysqli --with-mysqli=shared,mysqlnd   --with-pcre-regex --with-pear --with-mcrypt --with-kerberos --with-pdo-sqlite  --enable-libxml --disable-rpath  --enable-opcache --with-fpm-user=nginx --with-fpm-group=nginx  --with-mysql-sock=/var/lib/mysql/mysql.sock

make && make install

三、配置文件
执行命令cp php.ini-production /usr/local/php/lib/php.ini

安装后的PHP7配置文件所在路径是/usr/local/php/etc。该目录下会有两个配置文件:php-fpm.conf和php.ini,以及一个文件夹php-fpm.d(包含www.conf文件)。
1 配置php.ini

######避免PHP信息暴露在http头中
expose_php = Off

######避免暴露php调用mysql的错误信息
display_errors = Off

######在关闭display_errors后开启PHP错误日志(路径在php-fpm.conf中配置)
log_errors = On

######设置PHP的扩展库路径
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20141001/"

######设置PHP的opcache和mysql动态库
zend_extension=opcache.so
extension=mysqli.so
extension=pdo_mysql.so

######设置PHP的时区
date.timezone = PRC

######开启opcache
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

######设置PHP脚本允许访问的目录(需要根据实际情况配置)
;open_basedir = /usr/share/nginx/html;

2 配置php-fpm.conf

php-fpm.conf是php-fpm进程服务的配置文件:

将;pid = run/php-fpm.pid前的;去掉并修改为
将daemonize = yes开启守护进程
######设置错误日志的路径自定义
error_log = /var/log/php-fpm/error.log
######引入www.conf文件中的配置
include=/usr/local/php/etc/php-fpm.d/*.conf

3 配置www.conf(在php-fpm.d目录下)

www.conf这是php-fpm进程服务的扩展配置文件:

######设置用户和用户组
user = nginx
group = nginx

######根据nginx.conf中的配置fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;设置PHP监听
; listen = 127.0.0.1:9000   #####不建议使用
listen = /var/run/php-fpm/php-fpm.sock

######开启慢日志
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10s

######设置php的session目录(所属用户和用户组都是nginx)
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
优化:
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20

四、开机启动
在系统服务目录里创建php-fpm.service文件

vi /lib/systemd/system/php-fpm.service

写入以下内容(路径改成自己的)

[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm  -y /usr/local/php/etc/php-fpm.conf 

PrivateTmp=true
[Install]
WantedBy=multi-user.target

启动

systemctl start php-fpm.service

开启成功,将服务加入开机自启

systemctl enable php-fpm.service

你可能感兴趣的:(安装PHP7)