首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Debian 系統手動部署 LNMP 環境完整教程(Nginx + MySQL + PHP-FPM + phpMyAdmin)

     編輯:PHP 相關知識

Debian 系統手動部署 LNMP 環境完整教程(Nginx + MySQL + PHP-FPM + phpMyAdmin)

本教程基於 Debian/Ubuntu 系統,通過 apt 包管理器分步安裝配置 Nginx、MySQL、PHP-FPM、phpMyAdmin,構建生產級 LNMP 環境,並講解虛擬主機配置、偽靜態規則、常見問題排查。

1. 安裝 MySQL

# 安裝服務端與客戶端
apt-get update
apt-get install -y mysql-server mysql-client

# 安全初始化(設置 root 密碼、移除匿名用戶、禁止 root 遠程登錄、移除測試庫)
mysql_secure_installation

2. 安裝 PHP-FPM 及常用擴展

apt-get install -y php5-fpm php5-gd php5-mysql php5-memcache php5-curl

# 安裝 Memcached 服務端
apt-get install -y memcached

# 驗證 PHP 版本
php5-fpm -v

3. 安裝 Nginx(全功能版)

apt-get install -y nginx-full
service nginx start

4. 配置 Nginx 支持 PHP-FPM

vim /etc/nginx/sites-available/default

# 在 server 塊中添加/修改 location ~ \.php$ 塊
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # 注意:php.ini 中需設置 cgi.fix_pathinfo = 0
    fastcgi_pass unix:/var/run/php5-fpm.sock;  # 使用 Unix Socket,性能優於 TCP
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  # 關鍵參數
}
service nginx restart

5. 驗證 PHP 運行

vim /usr/share/nginx/html/phpinfo.php
<?php phpinfo(); ?>

瀏覽器訪問 https://IP/phpinfo.php,出現 PHP 信息頁即成功。

6. 新增站點(虛擬主機)

cd /etc/nginx/conf.d
vim yourdomain.com.conf
server {
    listen 80;
    root /usr/share/nginx/html/yourdomain.com;
    index index.php index.html index.htm;
    server_name yourdomain.com www.yourdomain.com;

    # WordPress 偽靜態
    include wordpress.conf;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
service nginx reload

7. 安裝 phpMyAdmin(可選)

apt-get install -y phpmyadmin
# 安裝過程選擇 nginx(空格選中),配置資料庫選 Yes,設置登錄密碼
# 軟鏈接到站點目錄
ln -s /usr/share/phpmyadmin /usr/share/nginx/html/yourdomain.com/phpmyadmin

8. 常用偽靜態規則

WordPress

location / {
    try_files $uri $uri/ /index.php?$args;
}

Discuz! X

rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/([a-z]+)-(.+)\.html$ $1/$2.php?rewrite=$3 last;
if (!-e $request_filename) { return 404; }

9. 生產環境優化清單

  • PHP-FPM 池配置:/etc/php5/fpm/pool.d/www.conf 調整 pm.max_children、pm.start_servers、pm.max_requests
  • Nginx 緩存:啟用 fastcgi_cache、proxy_cache、靜態資源 expires
  • 安全加固:隱藏版本號、限制敏感目錄訪問、配置 SSL/TLS (Let's Encrypt)、fail2ban 防暴力破解
  • 日誌切割:logrotate 管理 Nginx/PHP-FPM/MySQL 日誌
  • 監控告警:Prometheus + Grafana 監控 QPS、慢查詢、錯誤率、磁盤/內存

常見問題排查

手動部署雖繁瑣,但能深入理解各組件交互,為後續排錯、優化、自動化運維打下堅實基礎。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
現象可能原因解決
502 Bad GatewayPHP-FPM 未啟動/ Sock 文件路徑錯誤/ 權限不足service php5-fpm status; 檢查 fastcgi_pass 路徑; chown www-data:www-data /var/run/php5-fpm.sock
File not foundSCRIPT_FILENAME 參數缺失/錯誤確保 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
權限拒絕目錄/文件擁有者非 www-datachown -R www-data:www-data /usr/share/nginx/html/
上傳大文件失敗client_max_body_size / upload_max_filesize / post_max_size 過小Nginx: client_max_body_size 100M; PHP: upload_max_filesize=100M post_max_size=100M