Ubuntu 21下编译安装php8.0

文章是记录自己安装的一个过程,其中的配置完全是个人的需求。

你可能对如下文章也感兴趣~ [Ubuntu 16.04下编译安装Nginx] [Ubuntu 16.04下编译安装PostgreSQL 10.3]

php

前期准备

  • 安装一大堆东西
1
sudo apt-get install gcc autoconf curl libxml2 libxml2-dev libssl-dev bzip2 libbz2-dev libjpeg-dev  libpng12-dev libfreetype6-dev libgmp-dev libmcrypt-dev libreadline6-dev libsnmp-dev libxslt1-dev libcurl4-openssl-dev libonig-dev libzip-dev 

这是必须的,因为编译前的配置需要它们的支持。

  • 新建php用户组和php用户
1
2
sudo groupadd -r php
sudo useradd -r -g php -s /bin/false -d /usr/local/php7 -M php

下载并解压PHP源码

截至到目前,php的最新稳定版本为php-8.0.8,那么就用它了。

选择一个合适的节点源码文件下载,并进行解压操作

wget http://am1.php.net/distributions/php-7.2.0.tar.xz xz -d php-7.2.0.tar.xz tar -xvf php-7.2.0.tar

1
2
3
wget https://www.php.net/distributions/php-8.0.8.tar.xz
tar -Jxvf php-8.0.8.tar.xz
cd php-8.0.8

配置并编译&安装PHP

根据自己的实际需求进行配置

 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
./configure \
--prefix=/usr/local/php8 \
--exec-prefix=/usr/local/php8 \
--bindir=/usr/local/php8/bin \
--sbindir=/usr/local/php8/sbin \
--includedir=/usr/local/php8/include \
--libdir=/usr/local/php8/lib/php \
--mandir=/usr/local/php8/php/man \
--with-config-file-path=/usr/local/php8/etc \
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-pgsql=/usr/local/pgsql \
--with-pdo-pgsql=/usr/local/pgsql \
--with-iconv \
--with-zlib \
--with-zip \
--enable-gd \
--enable-dba \
--enable-pdo \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-simplexml \
--enable-bcmath \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-pcntl \
--enable-sockets \
--enable-calendar \
--enable-soap \
--enable-exif \
--enable-intl \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg \
--with-freetype \
--enable-opcache \
--enable-dom \
--enable-fpm \
--with-bz2 \
--with-cdb \
--with-fpm-user=php \
--with-fpm-group=php \
--without-gdbm \
--disable-fileinfo

输出以下信息时就表示配置完成了!

 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
Generating files
configure: patching main/php_config.h.in
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| 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
2
3
make
make test
make install

安装完成后应该是有类似以下的输出信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Installing shared extensions:     /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/
Installing PHP CLI binary:        /usr/local/php8/bin/
Installing PHP CLI man page:      /usr/local/php8/php/man/man1/
Installing PHP FPM binary:        /usr/local/php8/sbin/
Installing PHP FPM defconfig:     /usr/local/php8/etc/
Installing PHP FPM man page:      /usr/local/php8/php/man/man8/
Installing PHP FPM status page:   /usr/local/php8/php/php/fpm/
Installing phpdbg binary:         /usr/local/php8/bin/
Installing phpdbg man page:       /usr/local/php8/php/man/man1/
Installing PHP CGI binary:        /usr/local/php8/bin/
Installing PHP CGI man page:      /usr/local/php8/php/man/man1/
Installing build environment:     /usr/local/php8/lib/php/build/
Installing header files:          /usr/local/php8/include/php/
Installing helper programs:       /usr/local/php8/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php8/php/man/man1/
  page: phpize.1
  page: php-config.1
/usr/local/src/php/php-8.0.8/build/shtool install -c ext/phar/phar.phar /usr/local/php8/bin/phar.phar
ln -s -f phar.phar /usr/local/php8/bin/phar
Installing PDO headers:           /usr/local/php8/include/php/ext/pdo/

排错

由于环境变量问题,配置过程会出现如下报错信息

configure: error: Cannot find OpenSSL's libraries

openssl version 可以看到 OpenSSL 1.0.2g 1 Mar 2016 解决方法,找到libssl.so软链接到/usr/lib下:

1
2
sudo find / -name libssl.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib

当然我的排错过程远不止这么简单,如果你按照我的第一个步骤做了,那么仅仅只要做这个操作。

配置PHP

  • 配置环境变量

配置PHP_HOME

1
vim /etc/profile

在最底下一行的上面添加如下内容:

1
2
3
#set for PHP
export PHP_HOME=/usr/local/php7
export PATH=$PHP_HOME/bin:$PATH

保存并退出,编译/etc/profile 使配置生效

1
source /etc/profile
  • 配置 PHP-FPM

增加 php-fpm 命令:

1
2
cp sapi/fpm/init.d.php-fpm /usr/local/bin/php-fpm
chmod +x /usr/local/bin/php-fpm
  • 初始化 PHP 和 PHP-FPM 的配置
1
2
cp php.ini-production /usr/local/php7/etc/php.ini
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

当然了,为了更精简以及后续的修改方便php-fpm.conf也可以直接使用下面的方式配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
cat <<EOF >/usr/local/php7/etc/php-fpm.conf
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
; Pid file
; Note: the default prefix is /usr/local/php7/var
pid = run/php-fpm.pid
; Error log file
; Note: the default prefix is /usr/local/php7/var
error_log = log/php-fpm.log

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
include=/usr/local/php7/etc/php-fpm.d/*.conf

EOF
  • 新建 php-fpm 的 pool 进程池配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cat <<EOF >/usr/local/php7/etc/php-fpm.d/www.conf
; Start a new pool named 'www'
[www]
listen = /usr/local/php7/var/run/$pool-php-fpm.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
user = www-data
group = www-data
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.status_path = /status
ping.path = /ping
ping.response = pong
request_terminate_timeout = 100
request_slowlog_timeout = 10s
slowlog = /usr/local/php7/var/log/$pool.log.slow
EOF
  • 让NGINX用PHP7.2.0来解析PHP

找到Nginx配置文件的 fastcgi_passunix:/run/php/php7.0-fpm.sock; 改为 php-fpm进程池的监听地址unix:/usr/local/php7/var/run/www-php-fpm.sock;

以下是一个例子:

站点配置基本例子

 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
server {
	listen 80 default_server;
	listen [::]:80 default_server;


	root /srv/www/html;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		include /usr/local/nginx/conf/fastcgi_params;
	#
	#	# With php7.0-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php7.0-fpm:
		fastcgi_pass unix:/usr/local/php7/var/run/wwww-php-fpm.sock;
		fastcgi_param  SCRIPT_FILENAME  /srv/www/html$fastcgi_script_name;

	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

设置开机自动启动 PHP-FPM

1
2
cp sapi/fpm/php-fpm.service   /etc/systemd/system/
chmod +x /etc/systemd/system/php-fpm.service

保存以后,设置开机自启以及开启服务:

1
2
systemctl enable php-fpm.service
systemctl start php-fpm.service

好了!终于填坑完成了!

点击刷新🚌