1 Debian 安装 php7.x 和 php8.x

1.1 Debian11 编译安装 PHP7.2

1.11 更新系统安装包列表

root@debian:~# cat /etc/apt/sources.list
deb http://ftp.de.debian.org/debian bullseye main
deb http://mirrors.ustc.edu.cn/debian buster main contrib non-free
deb http://mirrors.ustc.edu.cn/debian buster-updates main contrib non-free
deb http://mirrors.ustc.edu.cn/debian buster-backports main contrib non-free
deb http://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian buster main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian buster-updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian buster-backports main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free
# deb http://deb.debian.org/debian buster main contrib non-free
# deb http://deb.debian.org/debian buster-updates main contrib non-free
# deb http://deb.debian.org/debian-security/ buster/updates main contrib non-free
# deb-src http://deb.debian.org/debian buster main contrib non-free
# deb-src http://deb.debian.org/debian buster-updates main contrib non-free
# deb-src http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb http://mirrors.163.com/debian/ buster main non-free contrib
deb http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

deb-src http://mirrors.163.com/debian/ buster main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib


deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main

deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security buster/updates main

root@debian:~# apt update

1.12 安装依赖包

root@debian:~# apt install gcc make libxml2 libxml2-dev   <-- xml库

1.13 安装 php7

官方网站: http://php.net/

#创建目录source和web,分别用来放源码和编译后的文件。

root@debian:~# mkdir /source/
root@debian:~# mkdir /web/

root@debian:~# cd /source/
root@debian:~# wget http://www.php.net/distributions/php-7.1.10.tar.gz
root@debian:~# tar -zxf php-7.1.10.tar.gz
root@debian:~# cd php-7.1.10
root@debian:~# ./configure --prefix=/web/php --with-config-file-path=/web/php/etc --enable-fpm --with-mysqli --with-pdo-mysql

root@debian:~# make && make install

#配置文件所在目录
--with-config-file-path 

#启用php-fpm模块(推荐)
--enable-fpm   

#启用mbstring模块(推荐)。多字节字符串,让php支持支持ISO-8859-*, EUC-JP, UTF-8之外编码的语言
--enable-mbstring

#打开mysqli模块
--with-mysqli

#pdo_mysql模块
--with-pdo-mysql

1.14 安装php扩展(可选)

#上面的方法是将所以扩展都编译在一起。这里是单独编译出来,然后通过php.ini文件,自由度更高

#/source/php-7.1.10/ext 你会看到很多的文件夹。需要什么扩展就进入哪个文件夹,运行如下命令

# /web/php/bin/phpize
# ./configure --with-php-config=/web/php/bin/php-config
# make && make install

#安装必须

# apt install autoconf              //php扩展编译需要

-------------------------------------------
phpredis扩展

[官方网站] http://pecl.php.net/package/redis

#命令流程:

# cd /source/
# wget http://pecl.php.net/get/redis-3.1.4.tgz
# tar -zxf redis-3.1.4.tgz
# cd redis-3.1.4
# /web/php/bin/phpize
# ./configure --with-php-config=/web/php/bin/php-config
# make && make install

-------------------------------------------
openssl扩展

#命令流程:

# cd /source/php-7.1.10/ext/openssl
# cp config0.m4 config.m4
# ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
# /web/php/bin/phpize
# ./configure --with-php-config=/web/php/bin/php-config
# make && make install

-------------------------------------------
gd扩展

# apt install libpng-dev
# apt install libfreetype6 libfreetype6-dev
# cd /source/php-7.1.10/ext/gd
# /web/php/bin/phpize
# ./configure --with-php-config=/web/php/bin/php-config --with-freetype-dir
# make && make install

--with-freetype-dir      #打开gd库对freetype字体库的支持

1.15 配置php-fpm

#拷贝默认配置文件

# cd /source/php-7.1.10
# cp php.ini-development /web/php/etc/php.ini

# cd /web/php/etc
# cp php-fpm.conf.default php-fpm.conf

# cd /web/php/etc/php-fpm.d/
# cp [www.conf.default](http://www.conf.default/) [www.conf](http://www.conf/)

#修改php-fpm.conf

#说明:为了可以使用信号命令。

找到以下字段
;pid = run/php-fpm.pid

修改成
pid = run/php-fpm.pid


# 修改[www.conf](http://www.conf/)

# groupadd web
# useradd -g web nginx -M -s /sbin/nologin

#找到以下字段
user = nobody
group = nobody

#修改成
user = nginx
group = www

#php-fpm常用命令

# /web/php/sbin/php-fpm -c /web/php/etc/php.ini  //启动php-fpm
# kill -SIGUSR2 `cat /web/php/var/run/php-fpm.pid` //重启php-fpm
# kill -SIGINT `cat /web/php/var/run/php-fpm.pid`  //关闭php-fpm

1.16 信号解释

SIGINT, SIGTERM 立刻终止
SIGQUIT 平滑终止
SIGUSR1 重新打开日志文件
SIGUSR2 平滑重载所有worker进程并重新载入配置和二进制模块

1.2 Debian11 仓库源安装 PHP8

1.21 更新系统仓库

# sudo apt update

1.22 添加仓库和密钥

# sudo apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2

# echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list

# wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -

# sudo apt update

1.23 安装php8及扩展

# sudo apt install php8.0

# php -v
PHP 8.0.9 (cli) (built: Jul 30 2021 13:09:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.9, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.9, Copyright (c), by Zend Technologies

# apt install php8.0-<extension>
# sudo apt install php8.0-

image-20220611152724927

# 例子
# sudo apt install php8.0-{mysql,cli,common,imap,ldap,xml,fpm,curl,mbstring,zip}

# 检查加载模块
# php -m

1.3 查看系统中php版本信息

root@debian:~# php7.0 -v
PHP 7.0.33-57+0~20211119.61+debian11~1.gbp5d8ba5 (cli) (built: Nov 19 2021 06:42:48) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-57+0~20211119.61+debian11~1.gbp5d8ba5, Copyright (c) 1999-2017, by Zend Technologies
  
root@debian:~# php8.0 -v
PHP 8.0.19 (cli) (built: May 17 2022 18:49:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.19, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.19, Copyright (c), by Zend Technologies

image-20220611152945238

1.31 启动 apache2 服务

root@debian:~# cat /var/www/html/index.php 
<?php
    //php软件信息
    echo phpinfo(0b01)
?>

root@debian:~# systemctl start  apache2.service 
root@debian:~# systemctl status  apache2.service

image-20220611153132348

1.32 浏览器访问

http://192.168.3.32/index.php

image-20220611153206977

1.33 切换php版本

root@debian:~# a2dismod php8.0 
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = "zh_CN:zh",
	LC_ALL = (unset),
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Module php8.0 disabled.
To activate the new configuration, you need to run:
  systemctl restart apache2
  
root@debian:~# a2enmod php7.0 
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = "zh_CN:zh",
	LC_ALL = (unset),
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Considering dependency mpm_prefork for php7.0:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
  systemctl restart apache2
  
root@debian:~# systemctl restart apache2.service

浏览器访问验证

image-20220611153323118