在Debian上實現LNMP(Linux, Nginx, MySQL/MariaDB, PHP)集群涉及多個步驟,包括設置負載均衡、配置Nginx、安裝和配置MySQL/MariaDB以及PHP。以下是一個基本的指南:
在所有節點上安裝Debian操作系統。
在所有節點上安裝Nginx:
sudo apt update
sudo apt install nginx
在所有節點上安裝MySQL或MariaDB:
sudo apt update
sudo apt install mysql-server # 或 mariadb-server
確保MySQL/MariaDB在所有節點上運行,并且可以互相復制數據。
編輯/etc/mysql/mysql.conf.d/mysqld.cnf(或/etc/mysql/mariadb.conf.d/50-server.cnf):
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = your_database_name
auto_increment_increment = 2
auto_increment_offset = 1
重啟MySQL服務:
sudo systemctl restart mysql
編輯/etc/mysql/mysql.conf.d/mysqld.cnf(或/etc/mysql/mariadb.conf.d/50-server.cnf):
[mysqld]
server-id = 2
relay_log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = your_database_name
read_only = 1
重啟MySQL服務:
sudo systemctl restart mysql
在主服務器上創建一個復制用戶:
CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';
FLUSH PRIVILEGES;
在從服務器上設置主服務器信息:
CHANGE MASTER TO
MASTER_HOST='master_ip',
MASTER_USER='replicator',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=107;
START SLAVE;
在所有節點上安裝PHP和必要的PHP擴展:
sudo apt update
sudo apt install php-fpm php-mysql
在負載均衡器上安裝Nginx:
sudo apt update
sudo apt install nginx
編輯/etc/nginx/nginx.conf或創建一個新的配置文件在/etc/nginx/conf.d/目錄下:
http {
upstream backend {
server master_ip:3306;
server slave_ip:3306;
}
server {
listen 80;
location / {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
重啟Nginx服務:
sudo systemctl restart nginx
確保所有節點上的Nginx和MySQL/MariaDB都在運行,并且可以從負載均衡器訪問。
通過以上步驟,你應該能夠在Debian上實現一個基本的LNMP集群。根據你的具體需求,可能還需要進行更多的配置和優化。