在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集群。根据你的具体需求,可能还需要进行更多的配置和优化。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何在Debian上安装LNMP