本文将介绍如何在本地编译 PHP 8.3 并快速搭建一个 ThinkPHP 8.0 框架应用。通过这篇教程,您可以轻松掌握 PHP 8.3 的编译和 ThinkPHP 8.0 的使用,快速构建您的 PHP Web 应用。
安装PHP8.3
下载
wget https://www.php.net/distributions/php-8.3.1.tar.gz
tar -zxvf php-8.3.1.tar.gz
下载地址:https://www.php.net/downloads
安装依赖包
sudo apt-get install libfcgi-dev libfcgi0ldbl libjpeg-turbo8-dev \
libmcrypt-dev libssl-dev libc-client2007e \
libc-client2007e-dev libxml2-dev libbz2-dev \
libcurl4-openssl-dev libjpeg-dev libpng-dev \
libfreetype6-dev libkrb5-dev libpq-dev \
libxml2-dev libxslt1-dev libzip-dev \
libsqlite3-dev libonig-dev pkg-config \
libxml2-dev libkrb5-dev libssl-dev \
libbz2-dev libpng-dev libfreetype6-dev \
libc-client2007e-dev libonig-dev \
libreadline-dev libxslt1-dev libzip-dev
如果安装的依赖包不存在,请通过命令:apt-cache search freetype 查找相应的安装包安装即可
编译
cd php-8.3.1
./configure \
--prefix=/usr/local/php-8.3.1 \
--with-config-file-path=/usr/local/php-8.3.1/etc \
--with-zlib-dir \
--with-freetype \
--enable-mbstring \
--enable-soap \
--enable-calendar \
--with-curl \
--with-zlib \
--enable-gd \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--with-zip \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock=/var/run/mysqld/mysqld.sock \
--with-jpeg \
--with-openssl \
--with-fpm-user=www \
--with-fpm-group=www \
--with-libdir=/lib/x86_64-linux-gnu \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--enable-intl \
--with-pear \
--enable-fpm
安装
make
make install
安装日志
~/build/php-8.3.1$ sudo make install
[sudo] password for www:
Installing shared extensions: /usr/local/php-8.3.1/lib/php/extensions/no-debug-non-zts-20230831/
Installing PHP CLI binary: /usr/local/php-8.3.1/bin/
Installing PHP CLI man page: /usr/local/php-8.3.1/php/man/man1/
Installing PHP FPM binary: /usr/local/php-8.3.1/sbin/
Installing PHP FPM defconfig: /usr/local/php-8.3.1/etc/
Installing PHP FPM man page: /usr/local/php-8.3.1/php/man/man8/
Installing PHP FPM status page: /usr/local/php-8.3.1/php/php/fpm/
Installing phpdbg binary: /usr/local/php-8.3.1/bin/
Installing phpdbg man page: /usr/local/php-8.3.1/php/man/man1/
Installing PHP CGI binary: /usr/local/php-8.3.1/bin/
Installing PHP CGI man page: /usr/local/php-8.3.1/php/man/man1/
Installing build environment: /usr/local/php-8.3.1/lib/php/build/
Installing header files: /usr/local/php-8.3.1/include/php/
Installing helper programs: /usr/local/php-8.3.1/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php-8.3.1/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php-8.3.1/lib/php/
[PEAR] Archive_Tar - installed: 1.4.14
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4
[PEAR] PEAR - installed: 1.10.14
Wrote PEAR system config file at: /usr/local/php-8.3.1/etc/pear.conf
You may want to add: /usr/local/php-8.3.1/lib/php to your php.ini include_path
Installing PDO headers: /usr/local/php-8.3.1/include/php/ext/pdo/
配置文件
1、通过命令php -v 查看PHP版本
/usr/local/php-8.3.1/bin/php -v
PHP 8.3.1 (cli) (built: Dec 25 2023 21:14:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
2、核心 php.ini 配置文件
cd /home/www/build/php-8.3.1
sudo cp php.ini-production /usr/local/php-8.3.1/etc/php.ini
注意: 如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。定位到 cgi.fix_pathinfo 并将其修改为如下所示:
cgi.fix_pathinfo=0
3、PHP-FPM 进程服务的配置文件 php-fpm.conf
sudo mv /usr/local/php-8.3.1/etc/php-fpm.conf.default /usr/local/php-8.3.1/etc/php-fpm.conf
4、PHP-FPM 扩展配置文件 www.conf
sudo mv /usr/local/php-8.3.1/etc/php-fpm.d/www.conf.default /usr/local/php-8.3.1/etc/php-fpm.d/www.conf
用户和用户组配置
配置后
user = www
group = www
listen.owner = www
listen.group = www
listen.mode = 0660
给www用户组分配权限(非必须)
chown www:www /opt/php-8.3.1/etc/php-fpm.d/www.conf
修改fpm监听的端口
; listen = 127.0.0.1:9000 -- 默认是打开的,在前面加; 注释掉
listen = /var/run/php8.3.1-fpm.sock -- 添加这一行
创建系统单元文件
接下来,我们将创建系统单元文件,该文件用于启动和停止PHP-FPM守护程序。
1、添加服务
vim /lib/systemd/system/php-8.3.1-fpm.service
具有以下内容:
[Unit]
Description=The PHP 8.3.1 FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php-8.3.1/var/run/php-fpm.pid
ExecStart=/usr/local/php-8.3.1/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php-8.3.1/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
2、启用服务并重新加载systemd
sudo systemctl enable php-8.3.1-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-8.3.1-fpm.service → /lib/systemd/system/php-8.3.1-fpm.service.
sudo systemctl daemon-reload
3、启动PHP-FPM 服务
sudo systemctl start php-8.3.1-fpm.service
4、查看PHP-FPM 服务状态
sudo systemctl status php-8.3.1-fpm.service
● php-8.3.1-fpm.service - The PHP 8.3.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php-8.3.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-12-25 21:27:37 CST; 45s ago
Main PID: 22472 (php-fpm)
Tasks: 3 (limit: 4915)
CGroup: /system.slice/php-8.3.1-fpm.service
├─22472 php-fpm: master process (/usr/local/php-8.3.1/etc/php-fpm.conf)
├─22483 php-fpm: pool www
└─22485 php-fpm: pool www
Dec 25 21:27:37 wt-test-develop systemd[1]: Started The PHP 8.3.1 FastCGI Process Manager.
5、开机自启动
sudo systemctl enable php-8.3.1-fpm.service
安装ThinkPHP8.0
官方安装文档:https://doc.thinkphp.cn/v8_0/setup.html
注意:这里由于有多套环境,所以安装需要指定PHP绝对路径
安装composer
wget https://getcomposer.org/download/latest-stable/composer.phar
查看composer -v 版本,出现以下界面表示安装成功
/usr/local/php-8.1/bin/php composer.phar -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.6.6 2023-12-08 18:32:26
使用阿里云镜像源
/usr/local/php-8.1/bin/php composer.phar config -g repo.packagist composer https://mirrors.aliyun.com/composer/
安装ThinkPHP8.0
/usr/local/php-8.1/bin/php composer.phar create-project topthink/think tp8.0
Creating a "topthink/think" project at "./tp8.0"
Installing topthink/think (v8.0.0)
- Downloading topthink/think (v8.0.0)
- Installing topthink/think (v8.0.0): Extracting archive
Created project in /home/www/build/tp8.0
Loading composer repositories with package information
Updating dependencies
Lock file operations: 14 installs, 0 updates, 0 removals
- Locking league/flysystem (2.5.0)
- Locking league/mime-type-detection (1.13.0)
- Locking psr/container (2.0.2)
- Locking psr/http-message (1.1)
- Locking psr/log (3.0.0)
- Locking psr/simple-cache (3.0.0)
- Locking symfony/deprecation-contracts (v3.3.0)
- Locking symfony/polyfill-mbstring (v1.28.0)
- Locking symfony/var-dumper (v6.4.0)
- Locking topthink/framework (v8.0.3)
- Locking topthink/think-filesystem (v2.0.2)
- Locking topthink/think-helper (v3.1.6)
- Locking topthink/think-orm (v3.0.14)
- Locking topthink/think-trace (v1.6)
查看安装目录结构
cd tp8.0
tree -L 1
.
├── app
├── composer.json
├── composer.lock
├── config
├── extend
├── LICENSE.txt
├── public
├── README.md
├── route
├── runtime
├── think
├── vendor
└── view
Nginx 主机配置
server {
listen 80;
server_name 121.128.128.128;
set $root_path /home/www/build/tp8.0/public;
root $root_path;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
location ~ \.php$ {
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php8.3.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_connect_timeout 10000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
}
}
访问该服务器公网IP,如:http://121.128.128.128 ,看到以下界面表示安装成功啦!
查看PHP安装信息,在public目录新建文件tinywan.php
vim public/tinywan.php
添加以下内容,从 PHP 获取系统信息