ubuntu22 编译安装PHP并配置Nginx

in 默认分类 / 0 评论 / 649阅读

PHP8.2

# 根据自己需要安装对应的依赖
apt install build-essential libxml2-dev libssl-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev libonig-dev pkg-config libsqlite3-dev libpq-dev libmysqlclient-dev

#下载并解压PHP
wget https://www.php.net/distributions/php-8.2.11.tar.gz
tar -zxvf php-8.2.11.tar.gz
cd php-8.2.11

# 根据自己需求配置并编译
./configure --program-suffix=82 --prefix=/usr/local/php82 --enable-fpm --with-config-file-path=/usr/local/php82/etc --with-curl --with-openssl --with-mysqli --with-pdo-mysql --with-sqlite3 --with-zip --enable-mbstring
make -j$(nproc) && make install

#--program-suffix 添加后缀,编译后文件命令为:php82
#--prefix 安装目录
# --with-config-file-path php.ini配置文件目录

最后添加到环境变量,查看PHP版本是否安装成功,安装成功按下面配置Nginx教程配置相应的网站配置文件。

echo PATH="/usr/local/php82/bin:$PATH" > /etc/environment
source /etc/environment
php82 -v

PHP7.4

根据PHP文档,PHP版本不同支持OpenSSL版本也不同。Ubuntu22自带的OpenSSL版本是3.0,安装PHP7.4需要自己编译OpenSSL1.1.1版本。

PHP版本OpenSSL版本
7.0OpenSSL >= 0.9.8
7.1-8.0OpenSSL >= 1.0.1,< 3.0
8.1+OpenSSL >= 1.0.2,< 4.0

编译OpenSSL,打开OpenSSL官网找到OpenSSL1.1.1最新版本地址。

wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w/

./config --prefix=/usr/local/openssl1.1.1 --openssldir=/usr/local/openssl1.1.1
make
make install

编译PHP7.4,配置环境

wget https://www.php.net/distributions/php-7.4.33.tar.gz
tar -zxvf php-7.4.33.tar.gz

#配置PKG_CONFIG_PATH为你OpenSSL的路径
./configure --program-suffix=74 --prefix=/usr/local/php74 --enable-fpm --with-config-file-path=/usr/local/php74/etc PKG_CONFIG_PATH=/usr/local/openssl1.1.1/lib/pkgconfig --with-curl --with-openssl --with-mysqli --with-pdo-mysql --with-sqlite3 --with-zip --enable-mbstring
make
make install

#添加环境变量
echo PATH="/usr/local/php74/bin:$PATH" > /etc/environment
source /etc/environment
php74 -v

配置Nginx

先配置PHP-FPM,复制配置文件并配置

cp /usr/local/php74/etc/php-fpm.conf.default /usr/local/php74/etc/php-fpm.conf
cp /usr/local/php74/etc/php-fpm.d/www.conf.default /usr/local/php74/etc/php-fpm.d/www.conf

vi /usr/local/php74/etc/php-fpm.d/www.conf

# 修改配置
user = www-data
group = www-data
listen = /run/php/php74-fpm.sock;
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

添加到systemctl。

vi /etc/systemd/system/php74-fpm.service


[Unit]
Description=PHP 7.4 FastCGI Process Manager

[Service]
ExecStart=/usr/local/php74/sbin/php-fpm74 --nodaemonize --fpm-config /usr/local/php74/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
Type=simple
Restart=always

[Install]
WantedBy=multi-user.target

重载 Systemd 配置,然后启动 PHP-FPM 服务并将其设置为开机启动

systemctl daemon-reload
systemctl start php74-fpm
systemctl enable php74-fpm

配置Nginx配置文件

vi /etc/nginx/conf.d/test.conf

找到类似以下的配置取消注释或修改,如果没有就添加

    location ~ \.php$ {
        fastcgi_pass   unix:/run/php/php74-fpm.sock;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }

配置完成,重载Nginx

nginx -s reload
回复