跳到主要内容
方的代码随记
返回

CentOS 7.9 源码编译 Nginx 与 PHP

更新于:

一、安装编译环境

yum -y install gcc automake autoconf
yum -y install make zlib zlib-devel openssl openssl-devel gcc-c++ libtool

二、安装 pcre 库(8.42 / 8.45)

cd /usr/local/src
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
tar -zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure
make && make install

三、编译安装 Nginx(1.12.2 / 1.25.3)

cd /usr/local/src
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3

# 添加 Nginx 启动用户 www
groupadd -r www
useradd -r -g www www

./configure --prefix=/usr/local/nginx --with-http_stub_status_module \
  --with-http_ssl_module --with-stream --with-http_gzip_static_module \
  --with-pcre=/usr/local/src/pcre-8.45 --user=www --group=www

# 需要更多模块时
./configure --prefix=/usr/local/nginx --with-http_stub_status_module \
  --with-http_ssl_module --with-stream --with-http_gzip_static_module \
  --with-pcre=/usr/local/src/pcre-8.45 --user=www --group=www \
  --with-http_sub_module --with-http_addition_module \
  --with-http_realip_module --with-http_v2_module

make && make install

mkdir -p /var/tmp/nginx/client

echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
source /etc/profile

Nginx 运维命令

nginx -V     # 显示编译模块
nginx -v     # 显示版本
nginx -t     # 检查配置文件是否正确
nginx        # 启动
nginx -s reload   # 重新载入配置
nginx -s reopen   # 重启
nginx -s stop     # 快速关闭
nginx -s quit     # 优雅关闭

四、PHP 依赖

yum -y install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel \
  openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel
yum -y install oniguruma.x86_64
yum -y install oniguruma-devel --enablerepo=epel

yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel \
  libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel \
  glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel \
  curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

yum install -y krb5-devel libxml2-devel sqlite sqlite-devel bzip2 bzip2-devel libcurl libcurl-devel

CentOS 8 以上需自行编译 oniguruma:

wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar xf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4/
./autogen.sh && ./configure --prefix=/usr
make && make install

五、编译安装 PHP(7.3.17 / 8.3.0)

cd /usr/local/src
wget https://www.php.net/distributions/php-8.3.0.tar.gz
tar -zxvf php-8.3.0.tar.gz
cd php-8.3.0

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \
  --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-curl --with-freetype-dir \
  --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 \
  --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql \
  --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl \
  --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml \
  --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache \
  --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem \
  --enable-sysvshm --enable-xml --enable-zip

make && make install

三类常见编译报错

1. Please reinstall the libzip distribution——移除旧版并编译新版本:

yum remove libzip

cd /usr/local/src
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

2. configure: error: off_t undefined; check your library configuration——添加库搜索路径:

echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf

ldconfig -v

3. /usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory:

cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

六、配置 PHP 与 php-fpm

cp /usr/local/src/php-8.3.0/php.ini-production /usr/local/php/etc/php.ini

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

cd /usr/local/php
groupadd www
useradd -g www www

修改 php-fpm.conf(pid 目录不可只读):

pid = /data/logs/php/php-fpm.pid
error_log = /data/logs/php/php-fpm.log

修改 www.conf:

user = www
group = www

启动:

/usr/local/php/sbin/php-fpm

七、Nginx 对接 php-fpm

/usr/local/nginx/conf/nginx.conf:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ {
            include fastcgi_params;                                 # nginx 连接 php-fpm 的常量
            fastcgi_pass 127.0.0.1:9000;                            # php-fpm 地址与端口
            fastcgi_index index.php;                                # 默认页面
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;  # 站点根目录
        }
    }
    include vhosts/*.conf;
}

在 /usr/local/nginx/html 下新建 index.php 验证:

<?php
    phpinfo();
?>

八、把 php-fpm 托管给 systemd

vi /usr/local/src/php-8.3.0/sapi/fpm/php-fpm.service
[Service]
PIDFile=/data/logs/php/php-fpm.pid

注意:若 /usr、/boot、/etc 为只读,需注释掉 ProtectSystem=full。

cp /usr/local/src/php-8.3.0/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload

systemctl start php-fpm
systemctl status php-fpm
systemctl restart php-fpm
systemctl stop php-fpm

分享这篇文章:

上一篇
云主机挂载新数据盘
下一篇
LNMP 一键安装包部署