003-LNMP之PHP环境搭建

Posted charseki.chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了003-LNMP之PHP环境搭建相关的知识,希望对你有一定的参考价值。

正文:

php官网:https://www.php.net/

搜狐镜像源:http://mirrors.sohu.com/ (推荐,国内源下载速度嗖嗖的)

1.php编译安装前解决依赖包

yum -y install epel-release
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel sqlite sqlite-devel oniguruma-devel oniguruma

2.源码编译安装php

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm
make && make install
# 在/usr/local/php/etc目录下,默认是没有php.ini这个配置文件的,需要拷贝一个过去。
# 拷贝的文件路径/usr/local/src/php-7.4.7
cp /usr/local/src/php-7.4.7/php.ini-production /usr/local/php/etc/php.ini

3.php编译安装说明

--prefix 指定php的安装目录
--with-config-file-path 指定php的配置文件位置
--with-mysql 让php可以操作mysql
--enable-fpm 主要是nginx要来调用php语言使得php-fpm

4.启动php-fpm

# 环境变量
export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/
# 检查配置文件
php-fpm -t
# 使用默认配置文件
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# 查看php-fpm的listen配置

5.验证php-fpm的启动

1.进程

 

 

2.端口

 

 

3.日志

 

 

6.使用systemctl管理php-fpm

# 创建配置文件的路径
vi /usr/lib/systemd/system/php-fpm.service
# 配置内容:
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target

7.nginx默认配置无法处理php程序,只会下载,需要改配置文件nginx.conf

# 写一个test.php测试
<?php
 echo "hello world";
?>

8.nginx+php-fpm结合的配置

location ~ \\.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            # 修改默认的/scripts为$document_root,去root->html下面找页面
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

9.页面test.php访问测试

 

 

常见问题:

1、运行systemctl start php-fpm报错

 

[30-Jun-2020 18:34:23] WARNING: Nothing matches the include pattern \'/usr/local/php/etc/php-fpm.d/*.conf\' from /usr/local/php/etc/php-fpm.conf at line 125.
[30-Jun-2020 18:34:23] ERROR: No pool defined. at least one pool section must be specified in config file
[30-Jun-2020 18:34:23] ERROR: failed to post process the configuration
[30-Jun-2020 18:34:23] ERROR: FPM initialization failed

 

解决方法:

# cd /usr/local/php/etc/php-fpm.d/
# cp www.conf.default www.conf
# /usr/local/php/sbin/php-fpm -t
[30-Jun-2020 18:35:29] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
[root@localhost php-fpm.d]# systemctl start php-fpm
Starting php-fpm  done

 

以上是关于003-LNMP之PHP环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

PHP环境搭建之单独安装

PHP环境搭建之单独安装

PHP之开发环境搭建

信步漫谈之PHP—开发环境搭建

LAMP环境搭建之php安装

lamp环境搭建之编译安装php