一、安装PHP
更新yum源(此步可以跳过)
yum -y update
1.首先安装依赖包
yum install libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel
安装依赖包,不然编译会报sqlite3错误,生成不了Makefile文件
yum install sqlite-devel
sqlite3报错:configure: error: Package requirements (sqlite3 > 3.7.4) were not met: No package ‘sqlite3‘ found
安装依赖包
sudo yum install oniguruma-devel
2.下载PHP压缩包并解压
[root@try ~]#cd /usr/local/src/
[[email protected] src]#wget https://www.php.net/distributions/php-7.2.31.tar.gz
[[email protected] src]#tar -zxvf php-7.2.31.tar.gz
[[email protected] src]#cd php-7.2.31
3.编译安装,(--prefix=/usr/local/php 指定php安装目录;--with-config-file-path指定配置文件放置位置) 编译的时候可能会出现各种报错情况
./configure --disable-fileinfo--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--enable-fpm--enable-inline-optimization--disable-debug--disable-rpath--enable-shared--enable-soap--with-libxml-dir--with-xmlrpc--with-openssl--with-mcrypt--with-mhash--with-pcre-regex--with-sqlite3--with-zlib--enable-bcmath--with-iconv--with-bz2--enable-calendar--with-curl--with-cdb--enable-dom--enable-exif--enable-fileinfo--enable-filter--with-pcre-dir--enable-ftp--with-gd--with-openssl-dir--with-jpeg-dir--with-png-dir--with-zlib-dir--with-freetype-dir--enable-gd-native-ttf--enable-gd-jis-conv--with-gettext--with-gmp--with-mhash--enable-json--enable-mbstring--enable-mbregex--enable-mbregex-backtrack--with-libmbfl--with-onig--enable-pdo--with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd--with-zlib-dir--with-pdo-sqlite--with-readline--enable-session--enable-shmop--enable-simplexml--enable-sockets--enable-sysvmsg--enable-sysvsem--enable-sysvshm--enable-wddx--with-libxml-dir--with-xsl--enable-zip--enable-mysqlnd-compression-support--with-pear--enable-opcache
4.编译成功后安装
make && make install
5.安装成功后配置环境变量
5.1为了使用方便,可以在 /etc/profile文件末尾添加php环境变量
[[email protected] php-7.2.31]#vim /etc/profi
PATH=$PATH:/usr/local/php/bin
export PATH
5.2保存后刷新环境变量
[[email protected] php-7.2.31]#source /etc/profile
5.3查看环境变量
[[email protected] php-7.2.31]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/php/bin
5.4看到php环境变量的路径后,以后就可以直接使用php命令了,查看php版本
[[email protected] php-7.2.31]#php -v
PHP 7.2.31 (cli) (built: Jun 26 2020 13:58:52) ( NTS )
Copyright (c)1997-2018The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
二、nginx配置PHP-FPM
现在的PHP没有.ini文件和配置文件,还需要配置PHP-FPM
1.先创建用户和组,后面会用到
[[email protected] ~]#groupadd www-data
[[email protected] ~]#useradd -g www-data www-data
前面编译安装的时候(--with-config-file-path=/usr/local/php/etc)指定了配置文件存放目录/usr/local/php/etc;如果不加这个参数默认位置就是php安装目录下的lib目录
2.先进入配置文件目录,配置php.ini
[[email protected] ~]#cd /usr/local/php/etc
[[email protected] etc]#find / -name php.ini-production
/usr/local/src/php-7.2.31/php.ini-#cp /usr/local/src/php-7.2.31/php.ini-production /usr/local/php/etc/php.ini
设置php.ini,打开php配置文件找到cgi.fix_pathinfo配置项,这一项默认被注释并且值为1,
根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0
3.配置php-fpm.conf
[[email protected] etc]#find / -name php-fpm.conf.default
/usr/local/php/etc/php-fpm.conf.default#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm.conf
A: 把php-fpm改成后台运行模式
4.配置www.conf
[[email protected] etc]#find / -name www.conf.default
/usr/local/php/etc/php-fpm.d/www.conf.default#cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
把刚才创建的 www-data(组) www-data(用户)加入到这个配置文件
把user和group 改成刚刚创建的 www-data
5.将php-fpm作为系统服务(复制到/etc/init.d目录下)
[[email protected] etc]#find / -name init.d.php-fpm
/usr/local/src/php-7.2.31/sapi/fpm/init.d.php-#cp /usr/local/src/php-7.2.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
开启权限
[[email protected] etc]#chmod +x /etc/init.d/php-fpm
6.重启php服务
[[email protected] etc]#service php-fpm restart
7.查看PHP进程,php已经运行
[[email protected] etc]#ps -ef|grep php
root 15475 1 0 14:41 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nobody15476 15475 0 14:41 ? 00:00:00 php-fpm:pool www
nobody15477 15475 0 14:41 ? 00:00:00 php-fpm:pool www
root15495 1130 0 14:42 pts/0 00:00:00 grep --color=auto php
8.编辑nginx.conf
然后执行 vim /usr/local/nginx/nginx.conf 编辑nginx配置文件,具体路径根据实际的nginx.conf配置文件位置编辑,
下面主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:
然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root
修改完上面的,回到nginx.conf第一行,默认是#user nobody;这里要去掉注释改为user www-data;或者user www-data www-data;表示nginx服务器的权限为www-data
修改完这些保存并退出,然后重启nginx:
[[email protected] php-fpm.d]#/usr/local/nginx/sbin/nginx -s stop
[[email protected] php-fpm.d]#/usr/local/nginx/sbin/nginx
接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:
然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功了:
其它命令:
php-fpm的启动、停止和重启:service php-fpm start #启动
service php-fpm stop #停止
service php-fpm restart #重启
直接访问路径启动php-fpm服务:/etc/init.d/php-fpm
通过netstat查看9000端口
[[email protected] etc]#netstat -anpo | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16079/php-fpm: mast off (0.00/0/0)
原文:https://www.cnblogs.com/clubs/p/13195618.html