1. 程式人生 > 其它 >ubuntu21.10:編譯安裝php8.1.1

ubuntu21.10:編譯安裝php8.1.1

一,下載php8的安裝包:

1,建立目錄:
root@lhdpc:~# mkdir /usr/local/source
root@lhdpc:~# mkdir /usr/local/soft
root@lhdpc:~# chmod 777 /usr/local/source/
root@lhdpc:~# chmod 777 /usr/local/soft
說明:習慣上用source目錄儲存原始碼或安裝包 soft目錄是安裝後的可執行程式 2,官網下載地址:
https://www.php.net/downloads.php
下載:
root@lhdpc:~# cd /usr/local/source/
root@lhdpc:
/usr/local/source# mkdir php8 root@lhdpc:/usr/local/source# cd php8 root@lhdpc:/usr/local/source/php8# wget https://www.php.net/distributions/php-8.1.1.tar.gz
3,解壓:
root@lhdpc:/usr/local/source/php8# tar -zxvf php-8.1.1.tar.gz
root@lhdpc:/usr/local/source/php8# ls
php-8.1.1  php-8.1.1.tar.gz

說明:劉巨集締的架構森林是一個專注架構的部落格,地址:

https://www.cnblogs.com/architectforest

對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/
或:https://gitee.com/liuhongdi

說明:作者:劉巨集締 郵箱: [email protected]

二,配置php安裝:configure

1,配置:
root@lhdpc:/usr/local/source/php8# cd php-8.1.1/
root@lhdpc:/usr/local/source/php8/php-8.1.1# ./configure --prefix=/usr/local/soft/php8 --with-config-file
-path=/usr/local/soft/php8/etc
--with-curl --with-freetype --enable-gd --with-jpeg --with-gettext --with-kerberos --with-libdir=lib64 --with-libxml
--with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --enable-sockets --with-mhash
--with-ldap-sasl --with-xsl --with-zlib --with-zip -with-bz2 --with-iconv
--enable-fpm --enable-pdo --enable-ftp --enable-bcmath --enable-mbregex --enable-mbstring
--enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem
--enable-xml --enable-sysvsem --enable-cli --enable-opcache --enable-intl
--enable-calendar --enable-static --enable-mysqlnd
2, 配置成功時的提示資訊:
+--------------------------------------------------------------------+
| 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: in `/usr/local/source/php8/php-8.1.1':
configure: error: no acceptable C compiler found in $PATH
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install gcc
2,
checking for libxml-2.0 >= 2.9.0... no
configure: error: in `/usr/local/source/php8/php-8.1.1':
configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libxml2-dev
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install pkg-config
3,
configure: error: Package requirements (krb5-gssapi krb5) were not met:
 
No package 'krb5-gssapi' found
No package 'krb5’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libkrb5-dev
4,
configure: error: Package requirements (openssl >= 1.0.2) were not met:
 
No package 'openssl' found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libssl-dev
5,
configure: error: Package requirements (sqlite3 >= 3.7.7) were not met:
 
No package 'sqlite3’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libsqlite3-dev
6,
configure: error: Package requirements (zlib >= 1.2.0.4) were not met:
 
No package 'zlib’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install zlib1g-dev
7,
checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libbz2-dev
8,
checking for libcurl >= 7.29.0... no
configure: error: Package requirements (libcurl >= 7.29.0) were not met:
 
No package 'libcurl’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libcurl4-openssl-dev
9,
checking for libpng... no
configure: error: Package requirements (libpng) were not met:
 
No package 'libpng’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libpng-dev
10,
checking for libjpeg... no
configure: error: Package requirements (libjpeg) were not met:
 
No package 'libjpeg' found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libjpeg-dev
11,
checking for freetype2... no
configure: error: Package requirements (freetype2) were not met:
 
No package 'freetype2’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install libfreetype-dev
12,
checking whether the compiler supports GNU C++... no
checking whether g++ accepts -g... no
checking for g++ option to enable C++11 features... none needed
checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/usr/local/source/php8/php-8.1.1':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt-get install g++
13,
checking for oniguruma... no
configure: error: Package requirements (oniguruma) were not met:
 
No package 'oniguruma' found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt install libonig-dev
14,
checking for libxslt >= 1.1.0... no
configure: error: Package requirements (libxslt >= 1.1.0) were not met:
 
No package 'libxslt’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt install libxslt-dev
15,
checking for libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0... no
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
 
No package 'libzip’ found
解決:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt install libzip-dev

四,編譯php8

1,編譯
root@lhdpc:/usr/local/source/php8/php-8.1.1# make
說明:事先應該安裝有make命令: 可以用下面的命令安裝:
root@lhdpc:/usr/local/source/php8/php-8.1.1# apt install make
2,看到如下內容時,表示編譯完成:
Build complete.
Don't forget to run 'make test'.

五,安裝php8

1,安裝
root@lhdpc:/usr/local/source/php8/php-8.1.1# make install
2,測試效果:
root@lhdpc:/usr/local/source/php8/php-8.1.1# /usr/local/soft/php8/bin/php --version
PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
能看到版本表示已安裝成功

六,生成配置檔案:

1,生成php.ini
root@lhdpc:/usr/local/source/php8/php-8.1.1# cp php.ini-production /usr/local/soft/php8/etc/php.ini
2,生成www.conf
root@lhdpc:/usr/local/source/php8/php-8.1.1# cd /usr/local/soft/php8/etc/php-fpm.d/
root@lhdpc:/usr/local/soft/php8/etc/php-fpm.d# ls
www.conf.default
root@lhdpc:/usr/local/soft/php8/etc/php-fpm.d# cp www.conf.default www.conf
3,生成php-fpm.conf
root@lhdpc:/usr/local/soft/php8/etc/php-fpm.d# cd /usr/local/soft/php8/etc
root@lhdpc:/usr/local/soft/php8/etc# ls
pear.conf  php-fpm.conf.default  php-fpm.d  php.ini
root@lhdpc:/usr/local/soft/php8/etc# cp php-fpm.conf.default php-fpm.conf

七,測試php-fpm

1,配置使用者:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# vim /usr/local/soft/php8/etc/php-fpm.d/www.conf
說明:指定使用者為 www-data 如下:
;user = nobody
;group = nobody
user = www-data
group = www-data
說明:如果不設定會因為找不到nobody這個group而報錯:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# systemctl status php-fpm
× php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/etc/systemd/system/php-fpm.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2021-12-20 16:48:53 CST; 4s ago
    Process: 253676 ExecStart=/usr/local/soft/php8/sbin/php-fpm --nodaemonize --fpm-config /usr/local/soft/php8/etc/php-fpm.conf (code=exited, status=78)
   Main PID: 253676 (code=exited, status=78)
        CPU: 304ms
 
12月 20 16:48:53 lhdpc systemd[1]: Started The PHP FastCGI Process Manager.
12月 20 16:48:53 lhdpc php-fpm[253676]: [20-Dec-2021 16:48:53] ERROR: [pool www] cannot get gid for group 'nobody'
12月 20 16:48:53 lhdpc php-fpm[253676]: [20-Dec-2021 16:48:53] ERROR: FPM initialization failed
12月 20 16:48:53 lhdpc systemd[1]: php-fpm.service: Main process exited, code=exited, status=78/CONFIG
12月 20 16:48:53 lhdpc systemd[1]: php-fpm.service: Failed with result 'exit-code'.
2,生成啟動檔案:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# cp php-fpm.service /etc/systemd/system/
然後做一下修改:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# vim /etc/systemd/system/php-fpm.service
修改內容:
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit.
#ProtectSystem=full
ProtectSystem=false
把 ProtectSystem=full修改為: ProtectSystem=false 說明:如果不做修改會報錯如下 檢視報錯:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# systemctl status php-fpm
× php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/etc/systemd/system/php-fpm.service; disabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2021-12-20 16:44:02 CST; 3s ago
    Process: 252867 ExecStart=/usr/local/soft/php8/sbin/php-fpm --nodaemonize --fpm-config /usr/local/soft/php8/etc/php-fpm.conf (code=exited, status=78)
   Main PID: 252867 (code=exited, status=78)
        CPU: 317ms
 
12月 20 16:44:02 lhdpc systemd[1]: Started The PHP FastCGI Process Manager.
12月 20 16:44:02 lhdpc php-fpm[252867]: [20-Dec-2021 16:44:02] ERROR: failed to open error_log (/usr/local/soft/php8/var/log/php-fpm.log): Read-only file system (30)
12月 20 16:44:02 lhdpc php-fpm[252867]: [20-Dec-2021 16:44:02] ERROR: failed to post process the configuration
12月 20 16:44:02 lhdpc php-fpm[252867]: [20-Dec-2021 16:44:02] ERROR: FPM initialization failed
12月 20 16:44:02 lhdpc systemd[1]: php-fpm.service: Main process exited, code=exited, status=78/CONFIG
12月 20 16:44:02 lhdpc systemd[1]: php-fpm.service: Failed with result 'exit-code’.
3,啟動 因為service檔案做過修改,做一次載入:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# systemctl daemon-reload
啟動:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# systemctl start php-fpm
4,檢視php-fpm的程序:
root@lhdpc:/usr/local/source/php8/php-8.1.1/sapi/fpm# ps auxfww | grep php
root      253913  0.0  0.0  12248  2384 pts/1    S+   16:57   0:00  |   |           \_ grep --color=auto php
root      253906  2.8  0.6  82380 25064 ?        Ss   16:57   0:00 php-fpm: master process (/usr/local/soft/php8/etc/php-fpm.conf)
www-data  253908  0.0  0.2  82864  9752 ?        S    16:57   0:00  \_ php-fpm: pool www
www-data  253909  0.0  0.2  82864  9752 ?        S    16:57   0:00  \_ php-fpm: pool www

八,安裝nginx,配置nginx可訪問php-fpm

1,安裝nginx
root@lhdpc:~# apt-get install nginx
2, 建立目錄和一個php程式:
root@lhdpc:/etc/nginx/sites-enabled# mkdir -p /data/web/html
root@lhdpc:/etc/nginx/sites-enabled# cd /data/web/html
root@lhdpc:/data/web/html# vi index.php
root@lhdpc:/data/web/html# more index.php
<?php
  phpinfo();
?>
3,配置一個站點,供測試php使用:
root@lhdpc:/data/web/html# cd /etc/nginx/sites-enabled/
root@lhdpc:/etc/nginx/sites-enabled# vim php.conf
root@lhdpc:/etc/nginx/sites-enabled# more php.conf
server {
        listen       8003;
        root   /data/web/html;
        location ~ \.php {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
    }
說明:建立了一個埠是8003的站點 4,測試訪問:
http://127.0.0.1:8003/index.php
如圖:

九,啟動jit

預設安裝完成後,phpinfo的返回內容中沒有一段專門的opcache配置段, 表示opcache還未開啟 1,
root@lhdpc:/usr/local/soft/php8/etc# vim php.ini
在[opcache]段編輯如下:
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=128M
opcache.jit=tracing
zend_extension=opcache
2,重啟:
root@lhdpc:/usr/local/soft/php8/etc# systemctl reload php-fpm.service
3,檢視效果: 可以看到新增了Zend OPcache段 說明: 關於tracing和function 請參考這篇:https://stitcher.io/blog/php-8-jit-setup

十,檢視linux的版本:

root@lhdpc:/usr/local/soft/php8/etc# cat /etc/os-release
PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish