# 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。