在Debian上部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)集群涉及多個步驟。以下是一個基本的指南,幫助你在Debian系統上設置一個簡單的LNMP集群。
首先,更新你的包列表并安裝Nginx:
sudo apt update
sudo apt install nginx
啟動Nginx并設置開機自啟動:
sudo systemctl start nginx
sudo systemctl enable nginx
接下來,安裝MySQL或MariaDB。這里以MariaDB為例:
sudo apt update
sudo apt install mariadb-server mariadb-client
啟動MariaDB并設置開機自啟動:
sudo systemctl start mariadb
sudo systemctl enable mariadb
運行安全腳本以設置root密碼和其他安全選項:
sudo mysql_secure_installation
安裝PHP及其常用擴展:
sudo apt update
sudo apt install php-fpm php-mysql
啟動PHP-FPM并設置開機自啟動:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
編輯Nginx配置文件以處理PHP請求。假設你的網站根目錄是/var/www/html
:
sudo nano /etc/nginx/sites-available/default
修改配置文件,使其包含以下內容:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存并退出編輯器,然后測試Nginx配置:
sudo nginx -t
重新加載Nginx以應用更改:
sudo systemctl reload nginx
創建一個新的數據庫和用戶,并授予適當的權限。例如:
CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
將你的應用程序代碼上傳到/var/www/html
目錄,或者使用Git等版本控制系統進行部署。
如果你需要一個真正的集群環境,可以考慮使用負載均衡器(如HAProxy或Nginx本身)來分發請求到多個Web服務器實例。
sudo apt install nginx
編輯第二個Nginx實例的配置文件(例如/etc/nginx/sites-available/loadbalancer
):
upstream backend {
server 192.168.1.1:80;
server 192.168.1.2:80;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
通過以上步驟,你應該能夠在Debian上成功部署一個基本的LNMP集群。根據你的具體需求,可能還需要進行更多的配置和優化。