CentOS 8 源码编译安装 PHP 和设置 php-fpm

源安装和编译安装的区别

yum 或 dnf 安装:

从yum安装来说吧,yum相当于是自动化帮你安装,你不用管软件的依赖关系,在yum安装过程是帮你把软件的全部依赖关系帮你傻瓜式的解决了。而且现在Centos7的服务启动已经换成systemctl命令来控制了。通过yum安装会帮你自动注册服务,你可以通过systemctl start xxx.service启动服务,方便快捷。但是缺点是yum安装你没办法干预,安装的目录也是分散的。你可能要执行whereis或者find命令去找yum安装的路径。有时候yum安装的软件版本比较低,你不得不去找其他的yum源,或者rpm包。


源码编译安装:

源码编译在安装过程中可能要解决很多的依赖问题,才能装好一个软件。装好的软件你还不能通过systemctl来启动服务,因为在/usr/lib/systemd/system/路径下并没有你的服务的配置文件,你要自己手写一个。但是好处在于你能选择软件的版本,自定义安装目录,安装的模块。更加灵活方便。


安装编译工具

yum install -y gcc gcc-c++
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel libmcrypt-devel libzip-devel pcre-devel openldap openldap-devel


源码编译安装 PHP 

在 https://www.php.net/downloads.php 找到需要下载的版本。

cd /software
wget https://www.php.net/distributions/php-7.3.23.tar.gz
tar -xzf php-7.3.23.tar.gz
cd php-7.3.23


配置编译参数

#命令列出所有用户
cat /etc/passwd |cut -f 1 -d:
#创建用户组和用户
groupadd www
useradd -g www www


配置fpm的用户组和用户,以及安装其他扩展

### 因为富文本编辑器的问题,请把下面所有“- -”换成“--”

./configure - -prefix=/software/php73 \
- -with-config-file-path=/software/php73/etc \
- -with-config-file-scan-dir=/software/php73/etc/php.d \
- -enable-mysqlnd \
- -with-mysqli \
- -with-pdo-mysql \
- -enable-fpm \
- -with-fpm-user=www \
- -with-fpm-group=www \
- -with-gd \
- -with-iconv \
- -with-zlib \
- -enable-xml \
- -enable-shmop \
- -enable-sysvsem \
- -enable-inline-optimization \
- -enable-mbregex \
- -enable-mbstring \
- -enable-ftp \
- -with-openssl \
- -enable-pcntl \
- -enable-sockets \
- -with-xmlrpc \
- -enable-zip \
- -enable-soap \
- -without-pear \
- -with-gettext \
- -enable-session \
- -with-curl \
- -with-jpeg-dir \
- -with-freetype-dir \
- -disable-fileinfo \
- -enable-opcache

./configure --prefix=/software/php73 是安装目录


如果配置通过,会提示如下:

+--------------------------------------------------------------------+
| License:                              |
| This software is subject to the PHP License, available in this   |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement.   |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.              |
+--------------------------------------------------------------------+

Thank you for using PHP.


编译过程报错处理

报错1:configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:No package 'libxml-2.0' found

yum install -y libxml2-devel


报错2:configure: error: Package requirements (sqlite3 > 3.7.4) were not met:No package 'sqlite3' found

yum install -y libcurl-devel
yum -y install sqlite-devel


报错3:configure: error: Package requirements (oniguruma) were not met:No package 'oniguruma' found

执行以下命令

yum config-manager --set-enabled PowerTools
yum -y install oniguruma oniguruma-devel


报错4:configure: ERROR: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:

yum install libicu-devel


报错5:configure: error: Cannot find ldap libraries in /usr/lib

cp -frp /usr/lib64/libldap* /usr/lib/


报错6: -c /software/php74src/Zend/zend_execute.c -o Zend/zend_execute.lo Cmake: *** [Makefile:2283:Zend/zend_execute.lo] 错误 1

卡在这里好久,我换了一台内存大于2GB的服务器,才不报这个错误。



设置 php.ini 配置文件

将官方提供的源代码里的模板拷贝到配置参数中所指定的目录中

cp /software/php73src/php.ini-production /software/php73/etc/php.ini

然后可以根据自己服务器的需求,对php.ini文件的内容进行修改


设置php-fpm配置文件

cd /software/php73/etc
cp php-fpm.conf.default php-fpm.conf
cd /software/php73/etc/php-fpm.d
cp www.conf.default www.conf


修改 www.conf

user = www
group = www
listen = 127.0.0.1:9000
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

其他配置可根据自己的情况进行相应修改


加入系统环境变量

使用vim命令打开/etc/profile文件,在文件最末尾加上如下代码

export PHP_HOME=/software/php73
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin


保存修改后,使用source命令重新加载配置文件,命令如下

source /etc/profile
echo $PATH

命令查看环境变量中是否已经加入了相关的路径


加入系统服务

如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令systemcel enable nginx.service设置开机启动即可。源码安装的手动建立nginx.service服务文件。


在 /usr/lib/systemd/system 中创建 nginx.service 和 php-fpm.service


nginx.service 文件

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/software/nginx/sbin/nginx -c /software/nginx/conf/nginx.conf
ExecReload=/software/nginx/sbin/nginx -s reload
ExecStop=/software/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target


php-fpm.service 文件

[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/software/php73/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target


将服务加入开机自启

systemctl enable nginx.service
systemctl enable php-fpm.service

pkill php-fpm 可以直接关闭 php-fpm 进程


命令查看进程,若能够看到相关进程,则证明启动成功。查询进程时,可以看到进程是以www用户身份执行的

ps aux | grep php-fpm


配置 Nginx + PHP

本文中对Nginx进行了多域名配置,每个域名只需要在/software/nginx/conf/vhosts目录下有一个自己的配置文件即可。

server {
  listen    80;
  server_name localhost;
  index index.php index.htm index.html;
  root /software/www;
  location ~ .*\.(php|php5)?$
  {
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  }
  location ~ .*\.(js|css)?$
  {
    expires 1h;
  }
  # 配置页面静态化
  include /software/nginx/conf/rewrite/default.conf;
  access_log /software/nginx/access/default.log;
  error_log /software/nginx/error/default.log;
}


可以看到,与之前的配置文件相比,主要是index配置项后,多了关于php的配置内容,以及location相关配置中,多了对于php的内容,保存以上配置文件,使用

systemctl reload nginx


命令,重新加载配置文件后,通过浏览器访问http://服务器的IP/info.php,即可看到上文中php代码的运行效果,具体如下图所示


PHP 配置信息

至此,即完成了Nginx和PHP的配置,这里需要说明,上述的配置只是最简单的配置,若项目不是运行在web根目录下的,那么配置文件也需要对应的进行修改,若项目是采用CodeIgniter或Laravel等其他框架技术开发的,那么也需要对配置文件进行相应修改,这部分内容大家可自行百度,有很多相关的博客。



备注

(1)Primaryscriptunknown 

https://www.cnblogs.com/panblack/p/php_primary_script_unknown_solution.html


(2)通过phpInfo()查看 gd库已经开启,但是里边没有freeType 和jpeg的支持 但有png的支持 ,如果是 freetype 跟 jpeg没有安装,则安装这两软件

yum install freetype*
yum install libjpeg*

安装之后寻找安装路径find / -name freetype

结果在 /usr/include/freetype2/ 文件夹中

./configure --with-freetype-dir=/usr/include/freetype2 --with-jpeg-dir=/usr/include --with-php-config=/usr/local/php/bin/php-config


(3)开启 opcache,需要 添加 zend_extension="opcache.so"

https://www.jianshu.com/p/582b683a26a2




参考:

https://www.cnblogs.com/lichina/p/10556221.html【棒棒的】

https://blog.csdn.net/stacy06/article/details/84032985【freetype】

https://www.php.net/manual/zh/install.general.php

https://www.php.net/downloads.php

https://www.cnblogs.com/werben/p/11833903.html

https://www.cnblogs.com/alliancehacker/p/12255445.html

https://www.cnblogs.com/chxmtl/p/13847236.html

https://blog.csdn.net/qq_32828933/article/details/103756240

https://www.jianshu.com/p/582b683a26a2

https://www.pieruo.com/887.html


声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
Nginx 使用 Njs 授权访问文件
wp_signon() 自定义用户登录函数
FFmpeg 修改默认音轨
WordPress 数据库操作
Node.js 控制台进度条实现原理
WordPress 侧边栏小工具
MySQL 数据库中货币单位如何存储
如何使主题支持 Woocommerce