Debian11编译安装PHP8.1.0版

一、编译安装

安装相关依赖

1
sudo apt install gcc make zlib1g* libssl-dev libpcre+* libcurl-ocaml libcurl-ocaml-dev libcurl3-nss libcurlpp-dev libcurlpp0 libonig5 libreadline-dev libtidy-dev slapd ldap-utils libxml2-dev libpcre2 libjpeg-dev libxslt1-dev libzip-dev

下载并解压php8.1.0压缩包

1
2
wget https://www.php.net/distributions/php-8.1.0.tar.gz
tar -zxvf php-8.1.0.tar.gz

关于php的安装,提供两个方案,第二个方案针对Debian系统。

(一) 一般编译安装

(1)编译

1
2
cd php-8.1.0
./configure --prefix=/usr/local/php8.1 --with-config-file-path=/usr/local/php8.1/etc --sysconfdir=/etc/php8.1 --disable-fileinfo --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-config-file-scan-dir=/etc/php.d --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --disable-rpath --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mhash --with-openssl --with-zlib --with-curl --enable-ftp --with-bz2 --with-libxml --enable-gd --with-jpeg --with-freetype --enable-sockets --without-iconv --with-xsl --with-zip --with-external-pcre --with-pear --enable-session --enable-xml --enable-gd-jis-conv --enable-shared --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-gettext --with-readline --with-tidy --enable-mbstring --with-pear --enable-bcmath --enable-calendar --enable-exif --enable-shmop --with-curl --enable-intl --enable-soap --enable-opcache --without-gdbm

(2)安装

1
2
make > make.log 2>&1 # 将输出重定向到log.txt文件中,同时将标准错误也重定向到make.log
sudo make install

(3)一些问题:

1>若提示缺依赖就安装相应依赖
2>可以用./configure –help查看配置选项
3>No package 'oniguruma' found错误解决:
oniguruma是处理正则表达式的库
新开一个终端,然后输入:

1
2
3
4
5
wget https://github.com/kkos/oniguruma/releases/download/v6.9.7.1/onig-6.9.7.1.tar.gz
tar zxvf onig-6.9.7.1.tar.gz
cd onig-6.9.7
./configure
sudo make && sudo make install

(二) Debian系统用auto-aptcheckinstall

1、下载并安装auto-aptcheckinstall

1
2
3
4
wget https://mirrors.edge.kernel.org/debian/pool/main/a/auto-apt/auto-apt_0.3.24_amd64.deb
sudo dpkg -i auto-apt_0.3.24_amd64.deb
sudo apt install -f # 安装auto-apt依赖
sudo apt install checkinstall

2、用auto-apt进行编译

编译命令:

1
auto-apt run ./configure  --prefix=/usr/local/php8.1 --with-config-file-path=/usr/local/php8.1/etc --sysconfdir=/etc/php8.1 --disable-fileinfo --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-config-file-scan-dir=/etc/php.d  --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --disable-rpath --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mhash --with-openssl --with-zlib --with-curl --enable-ftp --with-bz2 --with-libxml --enable-gd --with-jpeg --with-freetype --enable-sockets --without-iconv --with-xsl --with-zip --with-external-pcre --with-pear --enable-session --enable-xml --enable-gd-jis-conv --enable-shared --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-gettext --with-readline --with-tidy --enable-mbstring --with-pear --enable-bcmath --enable-calendar --enable-exif --enable-shmop --with-curl --enable-intl --enable-soap --enable-opcache --without-gdbm

注意:--with-fpm-user=nginx --with-fpm-group=nginxfpm-php将属于nginx用户组;
编译完成会提示是否更改安装包信息。

3、checkinstall安装

1
make && sudo checkinstall

其他更多命令:

1
2
3
4
5
6
7
8
dpkg -i packagename.deb  //下次直接用生成的`deb包`安装
dpkg -r packagename.deb // 卸载
# 列出包中安装的文件位置
dpkg -L packagename
# 检查是否安装成功
dpkg -l | grep packagename
# 同上
apt list --installed | grep packagename

二、配置php8.1.0相关文件

1、复制php-fpm.confwww.conf配置文件

1
2
sudo cp /etc/php8.1/php-fpm.conf.default /etc/php8.1/php-fpm.conf && sudo cp /etc/php8.1/php-fpm.d/www.conf.default /etc/php8.1/php-fpm.d/www.conf
sudo vim /etc/php8.1/php-fpm.conf

将以下两个配置项目打开,即去掉前面的;

1
2
3
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice

2、创建以下文件夹和文件【根据编译时设置的软件路径】

1
2
sudo mkdir -p /usr/local/php8.1/var/log && sudo touch /usr/local/php8.1/var/log/php-fpm.log
sudo mkdir /usr/local/php8.1/etc # 放置php.ini配置文件

从源码文件夹拷贝php.ini-development或者php.ini-production/usr/local/php8.1/etc并命名为php.ini

比如:

1
sudo cp ~/下载/软件/deb包/php-8.1.0/php.ini-production /usr/local/php8.1/etc/php.ini

3、启动php-fpm命令

1
sudo /usr/local/php8.1/sbin/php-fpm

4、设置php-fpm开机启动及管理服务

1
sudo vim /usr/lib/systemd/system/php-fpm.service

输入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/php8.1/var/run/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

5、重新加载systemctl配置

1
sudo systemctl daemon-reload

6、开机自动启动php-fpm服务

1
2
sudo systemctl enable  php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /lib/systemd/system/php-fpm.service.

7、为PHP8.1.0设置软连接或者环境变量

1
sudo ln -s /usr/local/php8.1/bin/php /usr/local/bin/php

或者直接将/usr/local/php8.1/bin添加到系统环境变量[推荐]

1
2
3
4
5
6
7
8
9
echo "export PATH=/usr/local/php8.1/bin/:$PATH" | sudo tee -a ~/.bashrc  # $PATH 保留原来变量,添加后面的新变量; tee -a 在后面追加

# 添加到系统环境
su
密码:******
echo "export PATH=/usr/local/php8.1/bin:$PATH" | sudo tee -a /etc/profile
# 需要注意的是:
# 对/etc/profile有修改的话必须得重启你的修改才会生效
# 在/etc/profile里设置系统环境变量时,路径末尾不能以"/"结尾,否则将导致整个PATH变量出错。

另外也可以只添加到用户环境变量,用户环境变量文件路径为:~/.bashrc,重新开终端即可生效。
重新加载使配置生效:
1
2
3
4
# 如果是 /etc/profile,重启生效
source /etc/profile
# 如果是添加到用户环境变量,重启生效
source ~/.bash_profile

查看安装的php版本命令:
1
2
3
4
5
php -v
# 输出
PHP 8.1.0 (cli) (built: Dec 16 2021 20:25:06) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies

附:.bash_profile.bashrc的区别

1
2
~/.bash_profile # 是交互式、login 方式进入bash 运行的;
~/.bashrc # 是交互式 non-login 方式进入bash 运行的;

通常二者设置大致相同,所以通常前者会调用后者。

至此,php8.1.0编译安装全部完成。

三、nginx配置支持php

修改nginx.conf配置

1
sudo vim /etc/nginx/conf/nginx.conf

参照下面内容修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /你的站点路径/;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
location ~ \.php$ {
root /你的站点路径/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 注意修改为`$document_root$`
include fastcgi_params;
}
}

测试PHP网页

设置的php站点路径下新建info.php网页,输入如下内容:

1
2
3
<?php
phpinfo();
?>

保存并打开浏览器,输入:http://localhost:8091/info.php.
出现php信息则安装完成

四、附:PHP连接mariadb数据库

1、PDO方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
$dbms = "mysql";
$host = "localhost:3306";
$mysql_username = "数据库用户名";
$mysql_psw = "数据库用户密码";
$dbname = "所要使用的数据库名";
$dsn = "$dbms:host=$host;dbname=$dbname";
try {
$PDO = new PDO($dsn,$username,$password);
echo 'PDO连接方式连接成功!';
echo "<br/>";
foreach ($PDO->query('SELECT * from usr_table') as $row) {
print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
}
$PDO = null;
echo "<br/>已成功关闭数据库";
} catch(PDOException $e)
{
echo '连接失败:'. $e->getMessage();
}


// $dbms='mysql'; //数据库类型
// $host='localhost:3306'; //数据库主机名
// $dbname='数据库'; //使用的数据库
// $user='数据库用户名'; //数据库连接用户名
// $pass='数据库用户密码'; //对应的密码
// $dsn="$dbms:host=$host;dbname=$dbname";
// try {
// $PDO = new PDO($dsn,$username,$password);//初始化一个PDO对象,就是创建了数据库连接对象$PDO
// echo 'PDO连接方式连接成功!';
// echo "<br/>";
// // // 执行查询
// $sql = "select * from usr_table";//需要执行的sql语句
// $retval=$PDO->prepare($sql);//准备查询语句
// $retval->execute();//执行查询语句,并返回结果集
// // $code=$retval->errorCode();
// // echo $code.'<br>';
// //mysqli_query($conn,"INSERT INTO usr_table (usr_id, usr_name, submit_time, is_vip)
// //VALUES (10,'TEN',NOW(),0)");

// // if($code != 00000){
// // die("无法读取数据:".$retval->errorInfo());
// // }
// echo "<table border='2' width='300'>
// <tr>
// <th>usr_id</th>
// <th>usr_name</th>
// <th>submit_time
// </th><th>isVip</th>
// </tr>";
// do {
// echo "<tr>";
// echo "<th>".$row['usr_id']."</th>";
// echo "<th>".$row['usr_name']."</th>";
// echo "<th>".$row['submit_time']."</th>";
// echo "<th>".$row['is_vip']."</th>";
// echo "</tr>";
// } while($row=$retval->fetch(PDO::FETCH_ASSOC));// 循环输出查询结果集,并且设置结果集为关联数据形式。
// echo "</table>";
// $PDO = null;
// echo "<br/>已成功关闭数据库";
// } catch (PDOException $e) {
// die ("Error!: " . $e->getMessage() . "<br/>");
// }
//数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
// $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
?>

2、mysqli方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php 
$mysql_server_name = "localhost:3306";
$mysql_username = "数据库用户名";
$mysql_psw = "数据库用户密码";
$mysql_database = "要使用的数据库";
try {
$conn = mysqli_connect($mysql_server_name,$mysql_username,$mysql_psw,$mysql_database);
echo "mysqli连接方式连接成功!";
} catch (Exception $e)
{
echo $e->getMessage();;
}
// if (!$conn)
// {
// echo "连接 MySQL 失败: " .mysqli_connect_error();
// } else {
// echo "mysqli连接方式连接成功!";
// }
mysqli_query($conn,"set names utf8"); // 调整数据库编码

// // 执行查询
$sql = "select * from usr_table";
$retval=mysqli_query($conn,$sql);
//mysqli_query($conn,"INSERT INTO usr_table (usr_id, usr_name, submit_time, is_vip)
//VALUES (10,'TEN',NOW(),0)");

if(!$retval){
die("无法读取数据:". mysqli_error($conn));
}
echo "<table border='2' width='300'>
<tr>
<th>usr_id</th>
<th>usr_name</th>
<th>submit_time
</th><th>isVip</th>
</tr>";
do {
echo "<tr>";
echo "<th>".$row['usr_id']."</th>";
echo "<th>".$row['usr_name']."</th>";
echo "<th>".$row['submit_time']."</th>";
echo "<th>".$row['is_vip']."</th>";
echo "</tr>";
} while($row=mysqli_fetch_array($retval));#光标移动到下一个结果集

// while($row=mysqli_fetch_array($retval))
// {
// echo "<tr>";
// echo "<th>".$row['usr_id']."</th>";
// echo "<th>".$row['usr_name']."</th>";
// echo "<th>".$row['submit_time']."</th>";
// echo "<th>".$row['is_vip']."</th>";
// echo "</tr>";
// }#光标移动到下一个结果集

echo "</table>";
mysqli_free_result($retval);
mysqli_close($conn);
?>

3、一些问题

pdo连接提示Error!: SQLSTATE[HY000] [2002] No such file or directory,解决办法:

1
vim /usr/local/php8.1/etc/php.ini

修改pdo_mysql.default_socket=pdo_mysql.default_socket=/tmp/mysql.sock,重启Nginx即可。

若提示Warning: Undefined variable $row in #####,解决办法:

php.ini这个配置文件修改一行代码,解决方法如下:
php.ini
error_reporting = E_ALL
修改成:
error_reporting = E_ALL & ~E_NOTICE
再重启nginxphp-fpm就行了。

关于搭建LANMP请参考
在Debian上安装LANMP及在win下安装MariaDB的zip版本