centos7安装php的方法:1、安装wget命令 ;2、在“/usr/local/src”目录下载php源码包;3、安装编译php需要的依赖包即可。
本文操作环境:centos7系统、PHP7.2版,DELL G3电脑
centos7安装php详解
Centos7 编译安装PHP7.2
1、安装wget命令
yum install wget
2、在 /usr/local/src 目录下载php源码包
wget http://cn2.php.net/distributions/php-7.2.4.tar.gz
解压源码包
tar zxvf php-7.2.1.tar.gz
3、安装编译php需要的依赖包:
yum install gcc autoconf gcc-c++yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-develyum install systemd-develyum install openjpeg-devel
添加php-fpm用户
创建群组
groupadd php-fpm
创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g php-fpm -M php-fpm
编译参数:
开发环境: --enable-phpdbg\ --enable-dtrace\生产环境: --disable-phpdbg\ --disable-dtrace\./configure \--prefix=/usr/local/php \--with-config-file-path=/usr/local/php/etc \--with-zlib-dir \--with-freetype-dir \--enable-mbstring \--with-libxml-dir=/usr \--enable-xmlreader \--enable-xmlwriter \--enable-soap \--enable-calendar \--with-curl \--with-zlib \--with-gd \--with-pdo-sqlite \--with-pdo-mysql \--with-mysqli \--with-mysql-sock \--enable-mysqlnd \--disable-rpath \--enable-inline-optimization \--with-bz2 \--with-zlib \--enable-sockets \--enable-sysvsem \--enable-sysvshm \--enable-pcntl \--enable-mbregex \--enable-exif \--enable-bcmath \--with-mhash \--enable-zip \--with-pcre-regex \--with-jpeg-dir=/usr \--with-png-dir=/usr \--with-openssl \--enable-ftp \--with-kerberos \--with-gettext \--with-xmlrpc \--with-xsl \--enable-fpm \--with-fpm-user=php-fpm \--with-fpm-group=php-fpm \--with-fpm-systemd \--disable-fileinfo
执行编译:
make && make installphp-ini:
源码包里面有配置文件:
php.ini-development 测试开发环境 php.ini-production 生产环境
复制一份到指定的目录下(根据自己的情况选用,自己可以对比下这两个文件的差异):
cp php.ini-production /usr/local/php/etc/php.ini
php-fpm复制一份新的php-fpm配置文件:
cd /usr/local/php/etc cp php-fpm.conf.default php-fpm.conf vim php-fpm.conf
配置错误日志:
error_log = /usr/local/php/var/php-fpm.log
配置pid文件:
pid = /usr/local/php/var/run/php-fpm.pid
保存退出
cd /usr/local/php/etc/php-fpm.d cp www.conf.default www.conf
管理php-fpm配置:
cd /usr/local/src/php-7.2.4
cp ./sapi/fpm/php-fpm.service 到 /usr/lib/systemd/system/下
配置开机启动php-fpm:
systemctl enable php-fpm
启动php-fpm:
systemctl start php-fpm
查看状态:
systemctl status php-fpm
添加环境变量:
vim /etc/profile
在末尾追加:
export PATH=$PATH:'/usr/local/php/bin/'
保存退出。
source /etc/profile
测试:
php -v
看到下面这个就表示已经成功了。
如果需要区分web和cli环境,可以将 /usr/local/php/etc/php.ini 复制一份,重命名为php-cli.ini
cp /usr/local/php/etc/php.ini /usr/local/php/etc/php-cli.ini
需要调整配置,就在各自的配置文件中进行调整即可。
推荐学习:《PHP视频教程》