apache2如何做负载均衡
编辑日期: 2024-07-16 文章阅读: 次
输入网址 https://ai-jupyter.com,现在只有一台服务器,现在准备再扩展一台,并且想做负载均衡。如何从零实现。 从两台服务器中选择一台 作为 负载均衡器。
步骤 1: 配置 100.100.100.1 作为负载均衡器
1.1 安装和配置 Apache
在 100.100.100.1
服务器上,确保安装了 Apache 并启用了必要的模块:
sudo apt-get update
sudo apt-get install apache2
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod lbmethod_bybusyness
sudo a2enmod ssl
1.2 配置虚拟主机
编辑 Apache 的虚拟主机配置文件:
sudo nano /etc/apache2/sites-available/ai-jupyter.conf
添加以下内容:
<VirtualHost *:80>
ServerName ai-jupyter.com
DocumentRoot /var/www/html/dist
<Directory /var/www/html/dist>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
<Proxy "balancer://mycluster">
BalancerMember "http://100.100.100.1:8099"
BalancerMember "http://100.100.100.2:8099"
ProxySet lbmethod=bybusyness
</Proxy>
ProxyPass "/" "balancer://mycluster/"
ProxyPassReverse "/" "balancer://mycluster/"
ErrorLog ${APACHE_LOG_DIR}/ai-jupyter-error.log
CustomLog ${APACHE_LOG_DIR}/ai-jupyter-access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName ai-jupyter.com
DocumentRoot /var/www/html/dist
<Directory /var/www/html/dist>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/ai-jupyter.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ai-jupyter.com/privkey.pem
<Proxy "balancer://mycluster">
BalancerMember "http://100.100.100.1:8099"
BalancerMember "http://100.100.100.2:8099"
ProxySet lbmethod=bybusyness
</Proxy>
ProxyPass "/" "balancer://mycluster/"
ProxyPassReverse "/" "balancer://mycluster/"
ErrorLog ${APACHE_LOG_DIR}/ai-jupyter-error.log
CustomLog ${APACHE_LOG_DIR}/ai-jupyter-access.log combined
</VirtualHost>
1.3 启用虚拟主机配置
启用新的虚拟主机配置文件:
sudo a2ensite ai-jupyter.conf
禁用默认的虚拟主机配置文件(如果不需要):
sudo a2dissite 000-default.conf
1.4 重启 Apache 服务
重启 Apache 服务以应用更改:
sudo systemctl restart apache2
步骤 2: 配置 100.100.100.2 服务器
确保 100.100.100.2
服务器也配置好 Apache 和 SSL 证书。
2.1 安装和配置 Apache
在 100.100.100.2
服务器上,确保安装了 Apache:
sudo apt-get update
sudo apt-get install apache2
sudo a2enmod ssl
2.2 复制证书文件
从 100.100.100.1
服务器复制 Let's Encrypt 证书文件到 100.100.100.2
服务器:
scp -r /etc/letsencrypt/live/ai-jupyter.com/ bitnami@100.100.100.2:/etc/letsencrypt/live/
scp -r /etc/letsencrypt/archive/ai-jupyter.com/ bitnami@100.100.100.2:/etc/letsencrypt/archive/
scp /etc/letsencrypt/renewal/ai-jupyter.com.conf bitnami@100.100.100.2:/etc/letsencrypt/renewal/
2.3 配置 Apache 虚拟主机
编辑 /etc/apache2/sites-available/ai-jupyter.conf
:
sudo nano /etc/apache2/sites-available/ai-jupyter.conf
添加以下内容:
<VirtualHost *:80>
ServerName ai-jupyter.com
Redirect permanent / https://ai-jupyter.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@ai-jupyter.com
ServerName ai-jupyter.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/ai-jupyter.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ai-jupyter.com/privkey.pem
ErrorLog ${APACHE_LOG_DIR}/ai-jupyter-error.log
CustomLog ${APACHE_LOG_DIR}/ai-jupyter-access.log combined
</VirtualHost>
2.4 启用虚拟主机配置
启用新的虚拟主机配置文件:
sudo a2ensite ai-jupyter.conf
禁用默认的虚拟主机配置文件(如果不需要):
sudo a2dissite 000-default.conf
2.5 重启 Apache 服务
重启 Apache 服务以应用更改:
sudo systemctl restart apache2
步骤 3: 配置域名解析
确保您的域名 ai-jupyter.com
指向 100.100.100.1
服务器的 IP 地址。在您的域名注册服务商(例如 Amazon Route 53)中,配置 DNS 记录。
3.1 添加 A 记录
- Name:
ai-jupyter.com
- Type:
A
- Value: 100.100.100.1
最后验证
- 检查 DNS 解析:确保
ai-jupyter.com
正确解析到100.100.100.1
服务器的 IP 地址。 - 测试访问:在浏览器中访问
https://ai-jupyter.com
,确保请求被正确分发到两台后端服务器。
通过这些步骤,您可以使用 100.100.100.1
服务器作为负载均衡器,并将 ai-jupyter.com
域名的流量分发到 100.100.100.1
和 100.100.100.2
两台服务器。这将提高您的网站的可用性和性能。