一、编译安装 安装相关依赖 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 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-apt
及checkinstall
1、下载并安装auto-apt
及checkinstall
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 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=nginx
,fpm-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.conf
和www.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-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 su 密码:****** echo "export PATH=/usr/local/php8.1/bin:$PATH " | sudo tee -a /etc/profile
另外也可以只添加到用户环境变量,用户环境变量文件路径为:~/.bashrc
,重新开终端即可生效。 重新加载使配置生效:1 2 3 4 source /etc/profilesource ~/.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 ~/.bashrc
通常二者设置大致相同,所以通常前者会调用后者。
至此,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
网页,输入如下内容: 保存并打开浏览器,输入: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 ); } $PDO = null ; echo "<br/>已成功关闭数据库" ; } catch (PDOException $e ) { echo '连接失败:' . $e ->getMessage (); } ?>
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 ();; } mysqli_query ($conn ,"set names utf8" ); $sql = "select * from usr_table" ;$retval =mysqli_query ($conn ,$sql );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 )); 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
再重启nginx
、php-fpm
就行了。
关于搭建LANMP
请参考 在Debian上安装LANMP及在win下安装MariaDB的zip版本