LAMP遷移LNMP Nginx多站點配置全流程

文章目錄

  • 前言
  • 備份與停止服務
  • nginx安裝與配置
    • nginx 編譯安裝
    • 配置服務
  • php-fpm多站點配置
    • phf-fpm介紹
    • 多站點配置
  • nginx 多站點配置
  • nginx ssl 配置
  • 參考

前言

之前服務器使用的是 LAMP環境,想充分利用服務器資源,再運行另外一個站點

在LAMP環境下應該是也可以實現多站點,

但考慮到nginx輕量性,以及當前主要開發生產用到nginx比較多,配置好后也方便學習

于是決定進行更換

這里面有一個多用戶權限的問題,最好統一基于已有的用戶進行設置如www用戶;目前下面的方案能夠實現想要的效果,但可能在規范和安全上做的并不好

備份與停止服務

備份,備份網站,備份配置文件

# 備份網站目錄(假設默認路徑是 /var/www/html)
sudo cp -r /var/www/html /var/www/html_backup# 備份 Apache 配置文件
sudo cp -r /etc/apache2 /etc/apache2_backup   # Debian/Ubuntu
sudo cp -r /etc/httpd /etc/httpd_backup       # CentOS/RHEL

停止 apache 服務

# 停止 apache服務
sudo systemctl stop apache2     # Ubuntu/Debian
sudo systemctl stop httpd       # CentOS# 禁止開機自啟
sudo systemctl disable apache2    # Ubuntu/Debian
sudo systemctl disable httpd      # CentOS# 更老版本centos 會提示 httpd,service is not a native service, redirecting to sbin/chkconfid Executing /sbin/chkconfid httpd off
# 也是執行成功了# 檢查是否禁用
/sbin/chkconfig --list httpd# httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off 全是off 禁止開機自啟動成功# 如需卸載 可執行
# ubuntu/debian
sudo apt remove --purge apache2
sudo apt autoremove# centos
sudo yum remove httpd

nginx安裝與配置

nginx 編譯安裝

下載源代碼 nginx: download ;然后解壓

geoip 安裝 ;參考

configure

配置很多 自行搜索添加

./configure --prefix=/www/server/nginx --conf-path=/www/server/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-pcre --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_realip_module --with-cc-opt="-O2 -pipe -fPIC" --with-ld-opt="-Wl,-Bsymbolic-functions -Wl,--as-needed"

configuring 完成后 會輸出 configuration summary

然后 構建安裝

make
make install

配置服務

配置服務 nginx.service 放到 /etc/systemd/system 下

[Unit]
Description=nginx
After=network.target[Service]
Type=forking
PIDFile=/www/server/nginx/logs/nginx.pid
ExecStartPre=/www/server/nginx/sbin/nginx -t
ExecStart=/www/server/nginx/sbin/nginx
ExecReload=/www/server/nginx/sbin/nginx -s reload
ExecStop=/www/server/nginx/sbin/nginx -s stop
AmbientCapabilities=CAP_NET_BIND_SERVICE
PrivateTmp=true
User=root
Group=root[Install]
WantedBy=multi-user.target

注意修改路徑 根據你的安裝路徑進行調整

注意這里的 user group 都使用的 root,后面的 nginx 主配置中 設置 user www;

服務以root用戶啟動,后面設置 user www; 才能生效

注意 AmbientCapabilities=CAP_NET_BIND_SERVICE ;沒添加會提示 nginx 綁定端口失敗;更多處理方法見 下面參考博客;

啟動服務

systemctl daemon-reloadsystemctl start nginxps -aux | grep nginx

下面展示是基于后面的 nginx 配置【設置了 user www;】啟動的效果

一個主進程 root用戶,一個工作線程 www 用戶

后續過程涉及多次 nginx 配置修改,需要重啟驗證,下面的命令會多次用到

nginx -tsystemctl status nginxsystemctl reload nginxsystemctl stop nginxsystemctl start nginxps -aux | grep nginx

php-fpm多站點配置

phf-fpm介紹

php-fpm 是 PHP FastCGI Process Manager 的縮寫,是 PHP 提供的一種高性能的 FastCGI 實現 ,專門用于處理 PHP 請求。

在 Web 開發中,Web 服務器(如 Nginx、Apache)本身無法直接解析和執行 PHP 文件。為了讓 Web 服務器能夠運行 PHP 腳本,就需要一個中間程序來處理這些請求 —— 這就是 FastCGI 的作用。

FastCGI 是一種協議標準,用于 Web 服務器與后端應用服務器之間的通信。

php-fpm 是實現這個協議的工具,主要功能有

功能描述
接收 PHP 請求從 Nginx 或 Apache 接收 PHP 腳本請求
解析并執行 PHP 腳本把 PHP 代碼轉換為 HTML 頁面或 JSON 數據
管理進程池控制 PHP 進程數量,提高并發性能
支持多站點配置每個網站可以使用不同的用戶、權限和配置
支持 Unix Socket 和 TCP更靈活地與 Web 服務器通信
日志記錄與錯誤監控記錄慢腳本、錯誤日志等信息
[瀏覽器] → [Nginx] → [PHP-FPM] → [MySQL / Redis / 其他服務]

用戶發出訪問,nginx接收請求,nginx通過fastcgi_pass 轉發給 php-fpm,php-fpm執行php腳本,連接數據庫,生成html內容,php-fpm 返回結果給nginx,由nginx返回給瀏覽器。

多站點配置

php-fpm.conf.default

全局配置 php-fpm.conf

[global]
pid = /www/server/php/74/var/run/php-fpm.pid
error_log = /www/server/php/74/var/log/php-fpm.log
log_level = notice
include = /www/server/php/74/etc/php-fpm.d/*.conf[www]
listen = /tmp/php-cgi-74.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.status_path = /phpfpm_74_status
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
request_terminate_timeout = 100
request_slowlog_timeout = 30
slowlog = var/log/slow.log

在這里加入了一條 include

include = /www/server/php/74/etc/php-fpm.d/*.conf

用于加載各個站點配置

A站點 php-fpm.d/a.conf

[a]
listen = /var/run/php/a.sock
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

B站點 php-fpm.d/b.conf

[b]
listen = /var/run/php/b.sock
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

上面給出的是主要配置,其中最重要的是 listen 對應一個站點應用建立的 sock 通信文件

這里的 listen 也支持

; ‘ip.add.re.ss:port’ - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; ‘[ip:6:addr:ess]:port’ - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; ‘port’ - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; ‘/path/to/unix/socket’ - to listen on a unix socket.

其他完整的配置 可以參考提供的 xxx.conf.default ,然后進行設置;建議直接拷貝后重命名為站點配置 在里面修改就行了

錯誤排查 主要通過查看日志 php-fpm.log 、slow.log

php-fpm 執行

pkill php-fpm/www/server/php/74/sbin/php-fpm -y /www/server/php/74/etc/php-fpm.conf -c /www/server/php/74/etc/php.ini 

image-20250717110423055

創建的 sock

image-20250717110623908

主進程 master process

加載全局配置;監聽sock文件或端口;管理子進程池 pool;

子進程池 pool

每個 pool 對應一個 PHP 應用(這里也就是 WordPress);每個 pool 可以設置不同用戶、監聽地址、資源限制等;子進程負責實際執行 PHP 腳本

nginx 多站點配置

nginx 安裝路徑 /www/server/nginx

conf/nginx.conf


#user  nobody;
user www;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;include /www/server/nginx/conf/site-enabled/*.conf;}

原有的配置基本被刪除掉

通過

include /www/server/nginx/conf/site-enabled/*.conf;

將啟用的 站點引入起來

site-available\

A站點配置 a.conf

server {listen 443 ssl;server_name a.top www.a.top;root /www/wwwroot/a.top;index index.php index.html;access_log /var/log/nginx/a.top.access.log;error_log /var/log/nginx/a.top.error.log;ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {fastcgi_pass unix:/var/run/php/a.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}location ~ /\.ht {deny all;}location ~ ^/wp-config\.php$ {deny all;}# 防止上傳目錄執行php文件location ~ ^/wp-content/uploads/.*\.php$ {deny all;}
}server {listen 80;server_name a.top www.a.top;return 301 https://$host$request_uri;
}

B站點配置 b.conf

server {listen 80;server_name b.top;root /www/wwwroot/b.top;index index.php index.html;access_log /var/log/nginx/b.access.log;error_log /var/log/nginx/b.error.log;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {fastcgi_pass unix:/var/run/php/b.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}location ~ /\.ht {deny all;}location ~ ^/(uploads|files)/.*\.php$ {deny all;}
}

注意配置文件中 fastcgi_pass 使用的 sock 要 和 php-fpm 中站點使用的sock文件 一致

conf/site-enabled

ln /www/server/nginx/site-avaiable/a.conf /www/server/nginx/conf/site-enabled/ln /www/server/nginx/site-avaiable/b.conf /www/server/nginx/conf/site-enabled/

會在 conf/site-enabled 建立兩個軟鏈接 指向 我們 需要管理的網站 nginx 配置,在nginx.conf 主配置那里通過 include 進行了引入

這種分離式配置,可以分別設置不同網站的配置,通過軟鏈接進行啟用 方便管理,無需全都放在nginx.conf 主配置中 變得很長難以查詢管理

nginx ssl 配置

具體參考 A站點 a.conf

ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_certificate 
ssl_certificate_key

設置 ssl 證書和私鑰 文件路徑

這里要確保 nginx 能夠對 證書和私鑰有讀取權限

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

是 ssl 相關配置 協議版本, 套件,緩存,超時等

然后

listen 443 ssl;

啟用 ssl

如果通過瀏覽器測試 提示 502 或 curl 測試 提示 curl: (35) SSL received a record that exceeded the maximum permissible length

檢查是不是listen忘記添加了 ssl;

nginx 安全策略

針對 wordpress 添加的規則

# 禁止 /wp-content/uploads/ *.php
location ~ ^/wp-content/uploads/.*\.php$ {deny all;
}

AI提供的更細致的規則 可控參考設置

# 1. 禁止訪問 .php 文件
location ~ ^/wp-content/uploads/.*\.php$ {deny all;
}# 2. 禁止訪問敏感擴展名
location ~ ^/wp-content/uploads/.*\.(sh|pl|exe|bat|cgi|phps|phar)$ {deny all;
}# 3. 圖片等資源正常訪問
location ~ ^/wp-content/uploads/.*\.(jpg|jpeg|png|gif|webp|mp4|ogg|ogv|webm)$ {expires 30d;add_header Cache-Control "public";
}# 4. 禁止訪問 wp-config.php
location ~ ^/wp-config\.php$ {deny all;
}# 5. 禁止訪問 .htaccess 等隱藏文件
location ~ /\. {deny all;
}

參考

  1. CentOS下為Nginx安裝GeoIP擴展

  2. nginx常見問題(四):端口無權限_nginx: emerg bind() to 0.0.0.0:80 failed (13: pe

  3. nginx多站點獨立配置 Include引入

  4. www.sock failed (13: Permission denied)

  5. 從零手寫實現 nginx-16-nginx.conf 支持配置多個 server

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/914880.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/914880.shtml
英文地址,請注明出處:http://en.pswp.cn/news/914880.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Nginx屏蔽國外IP訪問

下載IP列表 # 下載到文件 wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest # 直接輸出到終端 curl -sSL https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest得到一份國內IP配置 # 原始IP列表格式:apnic|CN|ipv4|218.78.0.0|1310…

stl-string模擬

1.介紹主要進行cpp中string的模擬,方便我們更好的對stl進行使用,string沒有模板,我們將頭文件和函數寫在兩個不同的文件2.頭文件3.cpp文件如有問題,歡迎糾正!

基于MATLAB的極限學習機ELM的數據回歸預測方法應用

說明:這是一個機器學習實戰項目(附帶數據代碼文檔),如需數據代碼文檔可以直接到文章最后關注獲取 或者私信獲取。 1.項目背景 在當今的數據驅動時代,準確且高效的預測模型對于解決復雜問題至關重要。極限學習機&#…

芯谷科技--雙四通道模擬/數字多路復用器74HC4052

在電子系統中,信號的多路復用與解復用是常見的需求,特別是在需要對多個信號源進行選擇和切換的場景中。芯谷科技推出的 74HC4052 雙四通道模擬/數字多路復用器/解復用器,以其高效、靈活的設計,為工程師提供了可靠的解決方案。產品…

基于MATLAB的極限學習機ELM的數據分類預測方法應用

說明:這是一個機器學習實戰項目(附帶數據代碼文檔),如需數據代碼文檔可以直接到文章最后關注獲取 或者私信獲取。 1.項目背景 在現代數據挖掘與機器學習領域,面對日益復雜的數據結構和快速增長的數據量,開…

復合機器人在生物制藥實驗室上下料搬運案例

在醫療行業的物料搬運環節,傳統的人工操作模式逐漸暴露出諸多弊端,成為制約企業發展的瓶頸。富唯智能通過引入先進的復合機器人技術,為醫療企業提供了高效、智能的上下料搬運解決方案,助力醫療行業實現自動化與智能化升級。?客戶…

嵌入式學習-PyTorch(7)-day23

損失函數的調用import torch from torch import nn from torch.nn import L1Lossinputs torch.tensor([1.0,2.0,3.0]) target torch.tensor([1.0,2.0,5.0])inputs torch.reshape(inputs, (1, 1, 1, 3)) target torch.reshape(target, (1, 1, 1, 3)) #損失函數 loss L1Loss…

用 Ray 跨節點調用 GPU 部署 DeepSeek 大模型,實現分布式高效推理

在大模型時代,單節點 GPU 資源往往難以滿足大模型(如 7B/13B 參數模型)的部署需求。借助 Ray 分布式框架,我們可以輕松實現跨節點 GPU 資源調度,讓大模型在多節點間高效運行。本文將以 DeepSeek-llm-7B-Chat 模型為例&…

快速了解 HTTPS

1. 引入 在 HTTP 協議 章節的 reference 段,曾提到過 HTTPS。這里對HTTPS進行詳細介紹。 HTTPS 是在 HTTP 的基礎上,引入了一個加密層 (SSL)。HTTP 是明文傳輸的 (不安全)。當下所見到的大部分網站都是 HTTPS 的。 起初是拜運營商劫持所賜(…

mysql備份與視圖

要求:1.將mydb9_stusys數據庫下的student、sc 和course表,備份到本地主機保存為st_msg_bak.sql文件,然后將數據表恢復到自建的db_test數據庫中;2.在db_test數據庫創建一視圖 stu_info,查詢全體學生的姓名,性別,課程名&…

【數據結構】 鏈表 + 手動實現單鏈表和雙鏈表的接口(圖文并茂附完整源碼)

文章目錄 一、 鏈表的概念及結構 二、鏈表的分類 ?編輯 三、手動實現單鏈表 1、定義單鏈表的一個節點 2、打印單鏈表 3、創建新節點 4、單鏈表的尾插 5、單鏈表的頭插 6、單鏈表的尾刪 7、單鏈表的頭刪 8、單鏈表的查找 9、在指定位置之前插入一個新節點 10、在指…

Go語言時間控制:定時器技術詳細指南

1. 定時器基礎:從 time.Sleep 到 time.Timer 的進化為什么 time.Sleep 不夠好?在 Go 編程中,很多人初學時會用 time.Sleep 來實現時間控制。比如,想讓程序暫停 2 秒,代碼可能是這樣:package mainimport (&q…

C# 轉換(顯式轉換和強制轉換)

顯式轉換和強制轉換 如果要把短類型轉換為長類型,讓長類型保存短類型的所有位很簡單。然而,在其他情況下, 目標類型也許無法在不損失數據的情況下容納源值。 例如,假設我們希望把ushort值轉化為byte。 ushort可以保存任何0~65535的…

淺談自動化設計最常用的三款軟件catia,eplan,autocad

筆者從上半年開始接觸這三款軟件,掌握了基礎用法,但是過了一段時間不用,發現再次用,遇到的問題短時間解決不了,忘記的有點多,這里記錄一下,防止下次忘記Elpan:問題1QF01是柜安裝板上的一個部件&…

網絡編程7.17

練習&#xff1a;服務器&#xff1a;#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <pthread.h> #include &…

c++ 模板元編程

聽說模板元編程能在編譯時計算出常量&#xff0c;簡單測試下看看&#xff1a;template<int N> struct Summation {static constexpr int value N Summation<N - 1>::value; // 計算 1 2 ... N 的值 };template<> struct Summation<1> { // 遞歸終…

【深度學習】神經網絡過擬合與欠擬合-part5

八、過擬合與欠擬合訓練深層神經網絡時&#xff0c;由于模型參數較多&#xff0c;數據不足的時候容易過擬合&#xff0c;正則化技術就是防止過擬合&#xff0c;提升模型的泛化能力和魯棒性 &#xff08;對新數據表現良好 對異常數據表現良好&#xff09;1、概念1.1過擬合在訓練…

JavaScript的“硬件窺探術”:瀏覽器如何讀取你的設備信息?

JavaScript的“硬件窺探術”&#xff1a;瀏覽器如何讀取你的設備信息&#xff1f; 在Web開發的世界里&#xff0c;JavaScript一直扮演著“幕后魔術師”的角色。從簡單的頁面跳轉到復雜的實時數據處理&#xff0c;它似乎總能用最輕巧的方式解決最棘手的問題。但你是否想過&#…

論安全架構設計(層次)

安全架構設計&#xff08;層次&#xff09; 摘要 2021年4月&#xff0c;我有幸參與了某保險公司的“優車險”項目的建設開發工作&#xff0c;該系統以車險報價、車險投保和報案理賠為核心功能&#xff0c;同時實現了年檢代辦、道路救援、一鍵挪車等增值服務功能。在本項目中&a…

滾珠導軌常見的故障有哪些?

在自動化生產設備、精密機床等領域&#xff0c;滾珠導軌就像是設備平穩運行的 “軌道”&#xff0c;為機械部件的直線運動提供穩準導向。但導軌使用時間長了&#xff0c;難免會出現這樣那樣的故障。滾珠脫落&#xff1a;可能由安裝不當、導軌損壞、超負荷運行、維護不當或惡劣環…