在开始之前,请确保你的系统已经安装了必要的开发工具和依赖库:
# 对于基于Debian/Ubuntu的系统
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# 对于基于RHEL/CentOS的系统
sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel zlib-devel openssl-devel
# 创建工作目录
mkdir ~/nginx-build && cd ~/nginx-build
# 下载NGINX (替换为最新版本号)
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -xzvf nginx-1.25.3.tar.gz
# 下载PHP (替换为最新版本号)
wget https://www.php.net/distributions/php-8.2.10.tar.gz
tar -xzvf 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-openssl \
--with-zlib \
--with-curl \
--enable-mbstring \
--with-mysqli \
--with-pdo-mysql
make -j$(nproc)
sudo make install
安装完成后,复制配置文件:
sudo cp php.ini-production /usr/local/php/etc/php.ini
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cd ../nginx-1.25.3
./configure --prefix=/usr/local/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
make -j$(nproc)
sudo make install
编辑PHP-FPM配置文件:
sudo nano /usr/local/php/etc/php-fpm.d/www.conf
确保以下设置:
user = www-data
group = www-data
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
编辑NGINX配置文件:
sudo nano /usr/local/nginx/conf/nginx.conf
在server
块中添加PHP支持:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sudo sh -c 'echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php'
sudo /usr/local/php/sbin/php-fpm
sudo /usr/local/nginx/sbin/nginx
在浏览器中访问 http://your-server-ip/info.php
,你应该能看到PHP信息页面。
对于systemd系统,创建服务文件:
sudo nano /etc/systemd/system/php-fpm.service
内容如下:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/usr/local/php/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 $MAINPID
[Install]
WantedBy=multi-user.target
sudo nano /etc/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 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启用服务:
sudo systemctl daemon-reload
sudo systemctl enable php-fpm
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl start nginx
权限问题:确保NGINX工作进程用户(通常是www-data
或nginx
)对PHP文件和目录有读取权限。
502 Bad Gateway:检查PHP-FPM是否运行,以及NGINX配置中的fastcgi_pass
地址是否正确。
文件未找到:确保SCRIPT_FILENAME
参数正确指向PHP文件路径。
性能调优:根据服务器资源调整PHP-FPM的进程管理设置(pm.max_children
等)。
通过以上步骤,你应该已经成功编译安装了支持PHP的NGINX服务器。