debian11安装php8

时间:2022-11-20 18:58:00

1、​​https://www.php.net/downloads​​  下载对应的版本

sudo wget ​​​​https://www.php.net/distributions/php-8.1.12.tar.bz2​

2、tar -zxvf php-8.1.12.tar.bz2

3、cd php-8.1.12

4、

sudo ./configure --enable-fpm --with-mysql --enable-pcntl --with-sodium --enable-soap --enable-sockets  --without-pdo-sqlite --with-pdo-mysql --with-mysql-sock --with-gettext --enable-gd-jis-conv --with-freetype --enable-gd --with-external-gd --with-mhash --without-iconv --with-imap --with-avif --with-webp --with-jpeg --with-xpm --with-openssl-dir --with-ffi --enable-exif --enable-bcmath --enable-calendar --with-curl --with-openssl --with-external-pcre --with-zlib --enable-debug --prefix=/usr/local/php8 --with-config-file-scan-dir=/etc/php.d --with-config-file-path=/etc --with-kerberos --with-imap-ssl

5、sudo make && make install

6、sudo cp php.ini-development /usr/local/php8/etc/php.ini

7、sudo cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

8、在php解压文件下下,复制sapi/php-fpm到/usr/local/bin下

sudo cp sapi/fpm/php-fpm /usr/local/bin

9、配置php.ini ,需要注意就是假如访问的文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。

将 php.ini 文件中的配置项 ​​cgi.fix_pathinfo​​​ 设置为 ​​0​​ 。

10、在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。

vim /usr/local/php8/etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
;自己本机添加www用户与www用户组
user = www
group = www

11、启动php-fpm

/usr/local/bin/php-fpm

12、配置 Nginx 使其支持 PHP 应用:

vim /usr/local/nginx/conf/nginx.conf

修改默认的 location 块,使其支持 .php 文件:

location / {
root html;
index index.php index.html index.htm;
}

下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

重启 Nginx。

sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
  1. 创建测试文件。

rm /usr/local/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

打开浏览器,访问 http://localhost,将会显示 phpinfo() 。

通过以上步骤的配置,Nginx 服务器现在可以以 ​​SAPI​​ ​​SAPI​​ 模块的方式支持 PHP 应用了。 当然,对于 Nginx 和 PHP 的配置,还有很多可用的选项, 请在对应的源代码目录执行 ./configure --help 来查阅更多配置选项。