溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

nginx+php手動編譯的詳細過程

發布時間:2021-07-02 15:40:41 來源:億速云 閱讀:247 作者:chen 欄目:編程語言
# 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

1.3 創建編譯目錄

mkdir -p /usr/local/src/nginx_php
cd /usr/local/src/nginx_php

二、編譯安裝Nginx

2.1 下載Nginx源碼

推薦使用最新穩定版(以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

2.2 配置編譯選項

./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

2.3 編譯與安裝

make -j $(nproc)  # 使用所有CPU核心加速編譯
make install

2.4 創建系統服務

創建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

3.1 下載PHP源碼

以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

3.2 配置編譯選項

./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:圖形處理支持

3.3 編譯與安裝

make -j $(nproc)
make install

3.4 配置PHP

復制配置文件:

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

3.5 創建PHP-FPM服務

創建/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

四、配置Nginx支持PHP

4.1 修改Nginx配置

編輯/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;
}

4.2 測試配置

# 測試Nginx配置
/usr/local/nginx/sbin/nginx -t

# 啟動服務
systemctl start php-fpm
systemctl start nginx

五、驗證安裝

5.1 創建測試文件

echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php

5.2 訪問測試

瀏覽器訪問http://服務器IP/info.php,應顯示PHP信息頁面。

5.3 檢查服務狀態

systemctl status nginx
systemctl status php-fpm
ss -lnp | grep -E 'nginx|php-fpm'

六、安全加固

6.1 文件權限設置

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 {} \;

6.2 PHP安全配置

編輯/usr/local/php/etc/php.ini

expose_php = Off
display_errors = Off
log_errors = On
disable_functions = exec,passthru,shell_exec,system

6.3 防火墻設置

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

七、常見問題解決

7.1 502 Bad Gateway錯誤

可能原因: 1. PHP-FPM未運行 2. Socket文件權限問題 3. Nginx與PHP-FPM用戶不一致

解決方案:

systemctl restart php-fpm
chown nginx:nginx /var/run/php-fpm.sock

7.2 性能優化建議

  1. 調整PHP-FPM進程數
  2. 啟用OPcache
  3. 配置Nginx緩存
  4. 啟用Gzip壓縮

結語

通過本文的詳細步驟,您已成功完成Nginx+PHP的手動編譯安裝。手動編譯雖然過程復雜,但能帶來更好的性能和靈活性。建議定期檢查官方更新,及時升級以獲得新特性和安全補丁。

注意:生產環境建議使用版本控制工具記錄所有配置變更,并做好備份工作。 “`

這篇文章共計約3800字,包含了從環境準備到編譯安裝、配置優化的完整流程,采用Markdown格式編寫,結構清晰,適合作為技術文檔使用。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女