跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

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

最后验证

  1. 检查 DNS 解析:确保 ai-jupyter.com 正确解析到 100.100.100.1 服务器的 IP 地址。
  2. 测试访问:在浏览器中访问 https://ai-jupyter.com,确保请求被正确分发到两台后端服务器。

通过这些步骤,您可以使用 100.100.100.1 服务器作为负载均衡器,并将 ai-jupyter.com 域名的流量分发到 100.100.100.1100.100.100.2 两台服务器。这将提高您的网站的可用性和性能。

大家在看

要使用 .pem 文件通过 SSH 连接到 Amazon Lightsail 服务器,您可以按照以下完整步骤进行操作:

步骤 1:下载 .pem 文件

首先,从 Amazon Lightsail 控制台下载您的 .pem 文件。假设文件名为 LightsailDefaultKey-ap-southeast-1-2gmemory.pem

步骤 2:将 .pem 文件移动到安全的位置

将下载的 .pem 文件移动到 ~/.ssh/ 目录(或其他安全位置)。

bash
Copy code
mv ~/Downloads/LightsailDefaultKey-ap-southeast-1-2gmemory.pem ~/.ssh/

步骤 3:修改 .pem 文件权限

为了确保文件安全,您需要将 .pem 文件的权限修改为仅限于用户读取。

bash
Copy code
chmod 400 ~/.ssh/LightsailDefaultKey-ap-southeast-1-2gmemory.pem

步骤 4:查找您的实例的公共 IP 地址

在 Amazon Lightsail 控制台中找到您的实例,并记下其公共 IP 地址。例如,假设公共 IP 地址是 54.151.146.222

步骤 5:通过 SSH 连接到实例

使用 ssh 命令通过 .pem 文件连接到您的 Amazon Lightsail 实例。假设您的实例用户名是 ubuntu,您可以使用以下命令:

bash
Copy code
ssh -i ~/.ssh/LightsailDefaultKey-ap-southeast-1-2gmemory.pem ubuntu@54.151.146.222

完整连接步骤示例

  1. 下载 .pem 文件并将其移动到 ~/.ssh/ 目录:
bash
Copy code
mv ~/Downloads/LightsailDefaultKey-ap-southeast-1-2gmemory.pem ~/.ssh/
  1. 修改 .pem 文件权限:
bash
Copy code
chmod 400 ~/.ssh/LightsailDefaultKey-ap-southeast-1-2gmemory.pem
  1. 查找实例的公共 IP 地址(假设为 54.151.146.222)。

  2. 使用 SSH 连接到实例:

bash
Copy code
ssh -i ~/.ssh/LightsailDefaultKey-ap-southeast-1-2gmemory.pem ubuntu@54.151.146.222

通过这些步骤,您应该能够成功地通过 SSH 连接到您的 Amazon Lightsail 实例。如果在连接过程中遇到问题,请确保 .pem 文件路径和权限设置正确,并且公共 IP 地址和用户名无误。

京ICP备20031037号-1 | AI之家 | AI资讯 | Python200 | 数据分析