您好,登录后才能下订单哦!
# nginx+php手动编译的详细过程
## 前言
在Linux服务器环境中,Nginx+PHP的组合因其高性能和低资源消耗而广受欢迎。虽然大多数Linux发行版提供预编译的软件包,但手动编译能带来以下优势:
- 获得最新版本功能
- 深度定制模块和功能
- 优化特定硬件环境
- 更好的安全性控制
本文将详细讲解从源码手动编译Nginx和PHP(以PHP-FPM模式运行)的全过程,环境基于CentOS 7/8,但原理适用于大多数Linux发行版。
---
## 一、准备工作
### 1.1 系统环境要求
- 操作系统:CentOS 7/8(或其他Linux发行版)
- 用户权限:root或具备sudo权限的用户
- 磁盘空间:至少1GB可用空间
- 内存:建议1GB以上
### 1.2 安装基础依赖
```bash
# CentOS/RHEL
yum groupinstall "Development Tools" -y
yum install epel-release -y
yum install -y wget git gcc make cmake autoconf automake \
libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel \
libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel \
freetype freetype-devel curl curl-devel libxslt libxslt-devel \
sqlite sqlite-devel oniguruma oniguruma-devel
mkdir -p /usr/local/src/nginx_php
cd /usr/local/src/nginx_php
推荐使用最新稳定版(以1.25.3为例):
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module
关键参数说明:
- --prefix
:指定安装目录
- --user/--group
:设置运行用户
- --with-http_ssl_module
:启用HTTPS支持
- --with-http_v2_module
:支持HTTP/2
make -j $(nproc) # 使用所有CPU核心加速编译
make install
创建systemd服务文件/lib/systemd/system/nginx.service
:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MNPID
ExecStop=/bin/kill -s QUIT $MNPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启用服务:
systemctl daemon-reload
systemctl enable nginx
以PHP 8.2.10为例:
cd /usr/local/src/nginx_php
wget https://www.php.net/distributions/php-8.2.10.tar.gz
tar zxvf php-8.2.10.tar.gz
cd php-8.2.10
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mbstring \
--enable-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--enable-sockets \
--enable-opcache \
--with-openssl \
--with-zlib \
--with-curl \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gd \
--with-jpeg \
--with-freetype \
--with-gettext \
--with-xmlrpc \
--with-iconv \
--with-pear
重要参数说明:
- --enable-fpm
:启用PHP-FPM支持
- --with-fpm-user/group
:设置PHP-FPM运行用户
- --with-openssl
:SSL支持
- --with-gd
:图形处理支持
make -j $(nproc)
make install
复制配置文件:
cp 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
修改www.conf
:
[www]
user = nginx
group = nginx
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
创建/lib/systemd/system/php-fpm.service
:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MNPID
[Install]
WantedBy=multi-user.target
启用服务:
systemctl daemon-reload
systemctl enable php-fpm
编辑/usr/local/nginx/conf/nginx.conf
,在server块中添加:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 测试Nginx配置
/usr/local/nginx/sbin/nginx -t
# 启动服务
systemctl start php-fpm
systemctl start nginx
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php
浏览器访问http://服务器IP/info.php
,应显示PHP信息页面。
systemctl status nginx
systemctl status php-fpm
ss -lnp | grep -E 'nginx|php-fpm'
chown -R nginx:nginx /usr/local/nginx/html
find /usr/local/nginx/html -type d -exec chmod 755 {} \;
find /usr/local/nginx/html -type f -exec chmod 644 {} \;
编辑/usr/local/php/etc/php.ini
:
expose_php = Off
display_errors = Off
log_errors = On
disable_functions = exec,passthru,shell_exec,system
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
可能原因: 1. PHP-FPM未运行 2. Socket文件权限问题 3. Nginx与PHP-FPM用户不一致
解决方案:
systemctl restart php-fpm
chown nginx:nginx /var/run/php-fpm.sock
通过本文的详细步骤,您已成功完成Nginx+PHP的手动编译安装。手动编译虽然过程复杂,但能带来更好的性能和灵活性。建议定期检查官方更新,及时升级以获得新特性和安全补丁。
注意:生产环境建议使用版本控制工具记录所有配置变更,并做好备份工作。 “`
这篇文章共计约3800字,包含了从环境准备到编译安装、配置优化的完整流程,采用Markdown格式编写,结构清晰,适合作为技术文档使用。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。