Jump to content
View in the app

A better way to browse. Learn more.

PHP论坛人

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Debian 13 系統資源限制調校 Part 1 (VPS 2C/2GB RAM)

Featured Replies

Debian 13 系統資源限制調校 Part 1 (VPS 2C/2GB RAM)

----------
前言
----------

本文針對在Debian 13系統上,以2GB RAM VPS架設高效能、穩定運行的 LNMP (Linux、Nginx、MariaDB、PHP) 論壇而設計

重點在於 Linux 系統的資源限制調校,確保論壇在遭遇流量高峰時能平穩運行,避免因資源耗盡而崩潰



核心觀念:Linux 兩種資源限制

在開始設定前,務必理解以下兩個不同層級的資源限制機制



1. /etc/security/limits.conf (登入Shell限制)

作用對象:透過SSH或本機終端機登入的使用者所執行的程序 (如 vim,bash,ls)

對服務的影響:對由 systemd 啟動的系統服務 (如 Nginx、MariaDB、PHP-FPM) 完全無效

原因說明:systemd 作為 PID 1 (1號行程),會跳過傳統的 PAM (Pluggable Authentication Modules) 登入認證流程,因此完全不讀取 limits.conf






2. systemd Override (服務單元限制)

作用對象:由 systemd 初始化並管理的所有系統服務

對服務的影響:這是現代Linux發行版中,設定服務資源限制的唯一正確途徑



常見誤區:修改了 limits.conf 後重啟服務,發現 /proc/<PID>/limits 數值未改變。這是正常現象,請直接前往 systemd override 進行設定




----------------------
系統層級調校
----------------------

此部分設定會影響所有使用者登入後的環境,以及整個Linux核心的行為




--------------------------------
使用者登入限制 limits.conf
--------------------------------

雖然此設定對 systemd 管理的服務本身無效,但仍有設定價值:可保護管理者登入後的操作環境,防止 vim、grep 等指令因開啟檔案過多而失敗



編輯
vi /etc/security/limits.conf



在末尾加入以下內容



# 格式: <domain> <type> <item> <value>
# domain: * 代表所有使用者
# type: soft (軟限制,可超越至hard) / hard (硬限制,絕對上限)

# 所有使用者
*    soft    nofile    65535
*    hard    nofile    65535
*    soft    nproc     65535
*    hard    nproc     65535
*    soft    stack     10240
*    hard    stack     10240

# root 使用者特殊設定 (通常需要較高的行程與檔案操作權限)
root    soft    nofile    65535
root    hard    nofile    65535
root    soft    nproc     unlimited
root    hard    nproc     unlimited






儲存檔案並離開vi編輯器
按 Esc,輸入 :wq,按 Enter




說明:nofile 為可開啟的最大檔案描述符數量;nproc 為最大行程數;stack 為堆疊大小 (單位 KB)






重開機使設定生效
reboot





----------------------------------------
全域核心 Kernel sysctl 優化
----------------------------------------


此部分等待 LNMP 配置完後,再來修改


Debian 13 系統資源限制調校 Part 2 全域核心優化

Edited by Jack

Create an account or sign in to comment

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.