pip3 install pandas openpyxl 安装错误解决

[admin@NAS ~]# pip3 install pandas openpyxl
Collecting pandas
Could not fetch URL https://pypi.python.org/simple/pandas/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] unknown error (_ssl.c:748) – skipping

这个错误通常是由于 SSL 证书验证失败导致的,可能是因为 Python 的 pip 工具无法验证 PyPI(Python 包索引)网站的 SSL 证书。你可以尝试以下几种解决方案:

解决方案
升级 pip:
确保你使用的是最新版本的 pip。可以通过以下命令升级:

复制
python3 -m pip install –upgrade pip
使用 –trusted-host 选项:
如果你信任 PyPI,可以在安装时添加 –trusted-host 选项:

复制
pip3 install –trusted-host pypi.python.org pandas openpyxl
检查系统时间:
确保你的系统时间和日期是正确的,不正确的系统时间可能导致 SSL 证书验证失败。

安装 CA 证书:
在某些情况下,可能需要安装或更新 CA 证书。你可以尝试以下命令:

复制
sudo apt-get install –reinstall ca-certificates
使用 HTTP 而不是 HTTPS(不推荐):
如果以上方法都无效,你可以临时使用 HTTP,但这会降低安全性:

复制
pip3 install –index-url=http://pypi.python.org/simple/ pandas openpyxl
其他建议
网络问题:
确保你的网络连接正常,有时网络问题也会导致 SSL 验证失败。

代理设置:
如果你在使用代理,确保代理设置正确。

Posted in 网络相关 | Leave a comment

qnap安装 Entware,通过entware安装python3 pip

在家中NAS系统Qnap上安装python,发现版本是2.7.18。于是通过威联通的APP Center安装3.12.6.1,结果python -V发现版本还是2.7.18,本想将就用吧。结果:
pip3 install pandas openpyxl
-sh: pip: command not found
郁闷了,pip也没有。于是一路apt-get,yum,dnf,apk安装器都没有。。真无语。
查看系统uname -a
Linux NAS 5.10.60-qnap #1 SMP Wed Jan 8 04:05:31 CST 2025 x86_64 GNU/Linux
发现没有Qnap系统下的安装器。真的抓狂了。好在还有deepseek,于是问了下:

The error -sh: ipkg: command not found indicates that the ipkg package manager is not installed on your QNAP NAS. ipkg is a lightweight package manager often used in embedded systems, but it is not included by default on all QNAP devices.

Here’s how you can proceed:


1. Install Entware (Recommended)

Entware is a modern package manager for embedded devices like QNAP NAS. It provides a wide range of precompiled software packages, including gcc, python3, and pip.

Steps to Install Entware:

  1. Enable SSH Access on Your QNAP NAS:

    • Log in to your QNAP web interface.
    • Go to Control Panel > Network & File Services > Telnet/SSH.
    • Enable SSH access and note the port number (default is 22).
  2. Connect to Your NAS via SSH:

    • Use an SSH client (e.g., ssh on Linux/macOS or PuTTY on Windows) to connect to your NAS:
      ssh admin@<your-nas-ip>
    • Replace <your-nas-ip> with the IP address of your NAS.
  3. Download and Run the Entware Installer:

    • Run the following commands to install Entware:
      wget -O - http://pkg.entware.net/binaries/x86-64/installer/entware_install.sh | /bin/sh
    • This script will download and install Entware on your NAS.
  4. Add Entware to Your PATH:

    • Add the following line to your shell profile (e.g., ~/.bashrc or ~/.profile):
      export PATH=/opt/bin:/opt/sbin:$PATH
    • Reload the profile:
      source ~/.bashrc
  5. Verify Entware Installation:

    • Check if opkg (the Entware package manager) is installed:
      opkg --version

2. Install Required Packages Using Entware

  • Update the package list:
     opkg update
  • Install gcc, python3, and pip:
     opkg install python3 python3-pip
  • Verify the installation:
     python3 --version
     pip3 --version

3. Install pandas and openpyxl

  • Use pip3 to install the required packages:
     pip3 install pandas openpyxl

4. Alternative: Manually Install Python

If you prefer not to use Entware, you can manually download and install Python 3.x from source. However, this requires a C compiler (gcc), which you may need to install manually.

Steps to Manually Install Python:

  1. Download Python Source Code:
      wget https://www.python.org/ftp/python/3.10.12/Python-3.10.12.tgz
  2. Extract and Build Python:
      tar -xvzf Python-3.10.12.tgz
      cd Python-3.10.12
      ./configure --enable-optimizations
      make
      sudo make install
  3. Verify Installation:
      python3 --version

5. Troubleshooting

  • If you encounter disk space issues, ensure you have enough storage on your NAS.
  • If you run into permission issues, use sudo or switch to the admin user.
  • If pip installation fails, try upgrading it:
     python3 -m pip install --upgrade pip

Let me know if you need further assistance! 😊

Posted in 网络相关 | Leave a comment

为服务器开启https访问

要开启 HTTPS 访问,你需要为你的域名配置 SSL 证书,并修改 Nginx 配置以支持 HTTPS。以下是具体步骤:


1. 获取 SSL 证书

推荐使用 Let’s Encrypt 免费 SSL 证书。你可以使用 certbot 工具来获取证书。

在主机A上安装 Certbot

sudo apt update
sudo apt install certbot

获取 SSL 证书

运行以下命令获取证书(确保域名已解析到主机A的IP地址):

sudo certbot certonly --standalone -d your-domain.com
  • your-domain.com 替换为你的实际域名。
  • 证书会保存在 /etc/letsencrypt/live/your-domain.com/ 目录下。

2. 修改 Nginx 配置文件

更新主机A上的 Nginx 配置文件,使其支持 HTTPS。

编辑之前创建的 reverse-proxy.conf 文件:

nano ~/nginx-proxy/conf.d/reverse-proxy.conf

修改为以下内容:

server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri; # 将所有 HTTP 请求重定向到 HTTPS
}

server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://<服务器B的IP地址>:3000;
        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;
    }
}

3. 修改 Docker Compose 文件

为了让 Nginx 容器能够访问主机上的 SSL 证书文件,需要将证书目录挂载到容器中。

编辑 docker-compose.yml 文件:

nano ~/nginx-proxy/docker-compose.yml

修改为以下内容:

version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./conf.d:/etc/nginx/conf.d
      - /etc/letsencrypt:/etc/letsencrypt
    restart: always

4. 重启 Nginx 容器

更新配置后,重启 Nginx 容器以应用更改:

cd ~/nginx-proxy
docker-compose down
docker-compose up -d

5. 测试 HTTPS 访问

现在,你可以通过以下方式测试 HTTPS 是否正常工作:

  1. 访问 https://your-domain.com,确保能够正常加载服务器B的内容。
  2. 检查浏览器地址栏,确认显示为安全的 HTTPS 连接(小锁图标)。

6. 自动更新 SSL 证书(可选)

Let’s Encrypt 证书有效期为 90 天,需要定期更新。可以通过以下步骤设置自动更新。

手动测试更新

sudo certbot renew --dry-run

设置定时任务

编辑 crontab:

sudo crontab -e

添加以下内容,每天凌晨 2 点检查并更新证书:

0 2 * * * certbot renew --quiet --post-hook "docker restart nginx-proxy"

7. 防火墙配置

确保主机A的防火墙允许 HTTPS 流量(443 端口):

sudo ufw allow 443

总结

通过以上步骤,你已经成功配置了 HTTPS 访问:

  1. 使用 Let’s Encrypt 获取 SSL 证书。
  2. 修改 Nginx 配置以支持 HTTPS。
  3. 挂载 SSL 证书到 Docker 容器。
  4. 重启 Nginx 并测试 HTTPS 访问。

如果遇到问题,可以检查 Nginx 日志(docker logs nginx-proxy)或 Certbot 日志(/var/log/letsencrypt/)来排查问题。

Posted in 网络相关 | Tagged , , | Leave a comment

通过 Docker 部署 Nginx 并配置 HTTPS 反向代理

通过 Docker 部署 Nginx 并配置 HTTPS 反向代理,可以分为以下几个步骤:


1. 准备工作

  1. 获取 SSL 证书

    • 你可以从 Let’s Encrypt 或其他证书颁发机构获取免费的 SSL 证书。
    • 证书通常包括两个文件:
      • 证书文件(如 certificate.crt)。
      • 私钥文件(如 private.key)。
  2. 准备配置文件

    • 创建一个 Nginx 配置文件,用于定义反向代理和 HTTPS 配置。
  3. 安装 Docker

    • 确保你的服务器上已经安装了 Docker。如果没有安装,可以参考 Docker 官方文档

2. 创建 Nginx 配置文件

在宿主机上创建一个目录(如 /root/nginx),用于存放 Nginx 配置文件和 SSL 证书。

目录结构

/root/nginx/
├── nginx.conf
├── ssl/
│   ├── certificate.crt
│   └── private.key

Nginx 配置文件(nginx.conf

events {}

http {
    server {
        listen 80;
        server_name example.com;
        return 301 https://$host$request_uri;  # 将 HTTP 请求重定向到 HTTPS
    }

    server {
        listen 443 ssl;
        server_name example.com;

        ssl_certificate /etc/nginx/ssl/certificate.crt;  # SSL 证书路径
        ssl_certificate_key /etc/nginx/ssl/private.key;  # SSL 私钥路径

        ssl_protocols TLSv1.2 TLSv1.3;  # 支持的 SSL 协议版本
        ssl_ciphers HIGH:!aNULL:!MD5;   # 加密套件

        location / {
            proxy_pass http://backend-server:8080;  # 反向代理到后端服务
            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;
        }
    }
}
  • 说明
    • listen 80:监听 HTTP 请求,并将其重定向到 HTTPS。
    • listen 443 ssl:监听 HTTPS 请求。
    • ssl_certificatessl_certificate_key:指定 SSL 证书和私钥的路径。
    • proxy_pass:将请求反向代理到后端服务(如 backend-server:8080)。

3. 运行 Nginx 容器

使用 Docker 运行 Nginx 容器,并将配置文件和 SSL 证书挂载到容器中。

运行命令

docker run -d \
  --name my-nginx \
  -p 80:80 \
  -p 443:443 \
  -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf \
  -v /root/nginx/ssl:/etc/nginx/ssl \
  nginx
  • 参数说明
    • -d:以后台模式运行容器。
    • --name my-nginx:为容器命名为 my-nginx
    • -p 80:80:将宿主机的 80 端口映射到容器的 80 端口(HTTP)。
    • -p 443:443:将宿主机的 443 端口映射到容器的 443 端口(HTTPS)。
    • -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf:挂载 Nginx 配置文件。
    • -v /root/nginx/ssl:/etc/nginx/ssl:挂载 SSL 证书目录。
    • nginx:使用官方 Nginx 镜像。

4. 测试访问

  1. HTTP 测试

    • 访问 http://example.com,应该会自动重定向到 https://example.com
  2. HTTPS 测试

    • 访问 https://example.com,确保页面正常加载,并且浏览器显示安全连接(锁图标)。
  3. 后端服务测试

    • 确保后端服务(如 backend-server:8080)正常运行,并且可以通过 Nginx 反向代理访问。

5. 其他注意事项

  1. 证书更新

    • 如果使用 Let’s Encrypt 证书,证书有效期为 90 天,需要定期更新。
    • 更新证书后,重启 Nginx 容器以加载新证书:
      docker restart my-nginx
  2. 防火墙配置

    • 确保服务器的防火墙允许 80(HTTP)和 443(HTTPS)端口的流量。
  3. 日志查看

    • 如果遇到问题,可以查看 Nginx 容器的日志:
      docker logs my-nginx
  4. 性能优化

    • 可以根据需要调整 Nginx 的 SSL 配置(如启用 HTTP/2、优化加密套件等)。

6. 总结

通过 Docker 部署 Nginx 并配置 HTTPS 反向代理的步骤如下:

  1. 准备 SSL 证书和 Nginx 配置文件。
  2. 使用 Docker 运行 Nginx 容器,挂载配置文件和证书。
  3. 测试 HTTP 和 HTTPS 访问,确保反向代理正常工作。

如果你有其他问题,欢迎随时提问!

Posted in 网络相关 | Tagged , , | Leave a comment

多应用共享80端口解决方案(探讨docker compose 有点)

直接在宿主机上配置Nginx确实可以解决问题。然而,使用Docker Compose结合Nginx容器化的方式有一些额外的优势,具体取决于你的需求和场景。以下是两者的对比以及使用Docker Compose的意义:


1. 直接在宿主机上配置Nginx

  • 优点

    • 简单直接,不需要额外的Docker容器。
    • 适合小型项目或不需要复杂容器化管理的场景。
    • 性能稍高,因为没有额外的容器化开销。
  • 缺点

    • 与宿主机环境耦合,迁移或扩展时需要手动调整配置。
    • 如果宿主机环境发生变化(如操作系统升级),可能影响Nginx的运行。
    • 不方便与其他容器化服务统一管理。

2. 使用Docker Compose结合Nginx容器化

  • 优点

    • 环境隔离:Nginx和其他应用都在容器中运行,与宿主机环境解耦,避免依赖冲突。
    • 可移植性:整个服务栈(Nginx + 应用)可以通过docker-compose.yml一键部署到任何支持Docker的环境中。
    • 统一管理:所有服务(Nginx、公司站点、PDF工具等)可以通过Docker Compose统一管理,方便启动、停止和扩展。
    • 版本控制:Nginx配置和Docker Compose文件可以纳入版本控制,方便团队协作和回滚。
    • 扩展性:如果需要添加更多服务(如数据库、缓存等),可以直接在docker-compose.yml中定义,无需额外配置宿主机。
  • 缺点

    • 稍微复杂一些,需要编写docker-compose.yml和Nginx配置文件。
    • 有一定的容器化开销(性能影响通常可以忽略)。

3. 使用Docker Compose的意义

如果你已经使用Docker来运行应用,那么将Nginx也容器化并与Docker Compose结合,可以带来以下好处:

  1. 一致性:所有服务(包括Nginx)都在容器中运行,保持环境一致性。
  2. 简化部署:通过docker-compose up -d即可启动整个服务栈,无需手动配置宿主机。
  3. 易于扩展:如果需要添加更多服务或调整配置,只需修改docker-compose.yml和Nginx配置文件,然后重新启动服务。
  4. 适合CI/CD:Docker Compose可以轻松集成到持续集成/持续部署(CI/CD)流程中,实现自动化部署。

4. 示例:Docker Compose + Nginx

以下是一个完整的docker-compose.yml示例,展示如何将Nginx和其他应用容器化并通过反向代理提供服务:

version: '3'
services:
  # 公司站点应用
  company-site:
    image: your-company-site-image
    ports:
      - "3000:3000"  # 内部端口3000

  # PDF在线转换工具
  pdf-tool:
    image: your-pdf-tool-image
    ports:
      - "4000:4000"  # 内部端口4000

  # Nginx反向代理
  nginx:
    image: nginx:latest
    ports:
      - "80:80"  # 对外暴露80端口
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf  # 挂载Nginx配置文件
    depends_on:
      - company-site
      - pdf-tool

Nginx配置文件(nginx.conf

events {}

http {
    server {
        listen 80;
        server_name company.com;

        location / {
            proxy_pass http://company-site:3000;  # 指向公司站点容器
            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;
        }
    }

    server {
        listen 80;
        server_name tools.company.com;

        location / {
            proxy_pass http://pdf-tool:4000;  # 指向PDF工具容器
            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;
        }
    }
}

启动服务

docker-compose up -d

5. 总结

  • 如果你只需要简单部署,直接在宿主机上配置Nginx是更直接的选择。
  • 如果你需要环境隔离、可移植性和统一管理,使用Docker Compose结合Nginx容器化是更好的选择。

最终选择取决于你的具体需求和项目规模。如果你的应用已经容器化,并且未来可能扩展或迁移,推荐使用Docker Compose的方式。

Posted in 网络相关 | Tagged , | Leave a comment

多应用共享80端口解决方案)

要解决多个应用通过80端口对外提供服务的问题,可以使用反向代理服务器(如Nginx或Apache)来管理流量。以下是具体步骤:

1. 使用反向代理服务器

反向代理服务器可以根据请求的域名或路径将流量分发到不同的内部端口。

使用Nginx作为反向代理

  1. 安装Nginx

    sudo apt-get update
    sudo apt-get install nginx
  2. 配置Nginx
    编辑Nginx配置文件(通常位于/etc/nginx/sites-available/default/etc/nginx/nginx.conf),添加以下内容:

    server {
       listen 80;
       server_name company.com;
    
       location / {
           proxy_pass http://localhost:3000;  # 公司站点应用的内部端口
           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;
       }
    }
    
    server {
       listen 80;
       server_name tools.company.com;
    
       location / {
           proxy_pass http://localhost:4000;  # PDF在线转换工具的内部端口
           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;
       }
    }

    这里假设:

    • company.com 是公司站点的域名,映射到3000端口。
    • tools.company.com 是在线工具的域名,映射到4000端口。
  3. 重启Nginx

    sudo systemctl restart nginx

使用Apache作为反向代理

  1. 安装Apache

    sudo apt-get update
    sudo apt-get install apache2
  2. 启用代理模块

    sudo a2enmod proxy
    sudo a2enmod proxy_http
  3. 配置Apache
    编辑Apache配置文件(通常位于/etc/apache2/sites-available/000-default.conf),添加以下内容:

    <VirtualHost *:80>
       ServerName company.com
    
       ProxyPreserveHost On
       ProxyPass / http://localhost:3000/
       ProxyPassReverse / http://localhost:3000/
    </VirtualHost>
    
    <VirtualHost *:80>
       ServerName tools.company.com
    
       ProxyPreserveHost On
       ProxyPass / http://localhost:4000/
       ProxyPassReverse / http://localhost:4000/
    </VirtualHost>
  4. 重启Apache

    sudo systemctl restart apache2

2. 使用Docker Compose

如果使用Docker Compose管理容器,可以在docker-compose.yml中定义服务并通过Nginx或Apache进行反向代理。

version: '3'
services:
  web:
    image: your-company-site-image
    ports:
      - "3000:3000"
  pdf-tool:
    image: your-pdf-tool-image
    ports:
      - "4000:4000"
  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - web
      - pdf-tool

3. DNS配置

确保域名解析正确:

  • company.com 指向服务器IP。
  • tools.company.com 也指向服务器IP。

总结

通过反向代理服务器(如Nginx或Apache),你可以将多个应用通过80端口对外提供服务,并根据域名或路径将流量分发到不同的内部端口。

Posted in 网络相关 | Tagged , | Leave a comment

推荐几个wordpress下的markdown格式发布插件

WP Githuber MD 插件因安全问题已关闭,我们可以选择其他可靠的Markdown插件来替代。以下是几个推荐的Markdown插件:


推荐替代插件

  1. Jetpack(由Automattic开发)

    • Jetpack 是一个多功能插件,其中包含Markdown支持。
    • 下载地址:Jetpack on WordPress.org
    • 安装后,启用“Markdown”模块即可使用。
  2. WP-Markdown

  3. Markdown Editor

  4. Easy Markdown


安装步骤

方法一:通过WordPress后台安装

  1. 登录WordPress后台。
  2. 进入“插件” > “安装插件”。
  3. 在搜索框中输入插件名称(如“Jetpack”或“WP-Markdown”)。
  4. 找到插件后,点击“安装”,然后点击“激活”。

方法二:手动下载并安装

  1. 访问插件的WordPress页面(如上面的下载地址)。
  2. 下载插件的ZIP文件。
  3. 登录WordPress后台,进入“插件” > “安装插件” > “上传插件”。
  4. 上传ZIP文件并激活。

注意事项

  1. 备份网站:在安装新插件前,建议备份你的网站数据和数据库。
  2. 兼容性:确保插件支持WordPress 4.9.26版本。如果不确定,可以在插件的WordPress页面查看兼容性信息。
  3. 安全性:选择官方推荐的插件,避免使用来源不明的插件,以防止安全问题。
Posted in 网络相关 | Tagged | Leave a comment

如何让wordpress支持markdown格式的文章发布

要让WordPress支持Markdown格式的文章发布,可以按照以下步骤操作:

方法一:使用插件

  1. 安装Markdown插件

    • 登录WordPress后台,进入“插件” > “安装插件”。
    • 搜索“Markdown”插件,如“Jetpack”或“WP Githuber MD”。
    • 安装并激活插件。
  2. 配置插件

    • 激活后,进入插件设置页面,启用Markdown支持。
    • 部分插件可能需额外配置,如“WP Githuber MD”允许在文章编辑器中直接使用Markdown语法。

方法二:手动添加Markdown支持

  1. 编辑主题文件

    • 通过FTP或文件管理器,找到当前主题的functions.php文件。
    • 在文件末尾添加以下代码:
      php
      function enable_markdown_for_posts() {
      add_post_type_support( 'post', 'markdown' );
      }
      add_action( 'init', 'enable_markdown_for_posts' );
  2. 使用Markdown解析库

    • 下载并安装Markdown解析库,如Parsedown。
    • 将库文件上传到主题目录,并在functions.php中引入:
      php
      require_once get_template_directory() . '/parsedown/Parsedown.php';
  3. 在主题中解析Markdown

    • 修改主题模板文件,使用Parsedown解析Markdown内容:
      php
      $parsedown = new Parsedown();
      echo $parsedown->text( get_the_content() );

方法三:使用Markdown编辑器

  1. 安装Markdown编辑器插件
    • 搜索并安装Markdown编辑器插件,如“WP Editor.md”或“Markdown Editor”。
    • 安装后,激活插件即可在文章编辑器中使用Markdown格式。

注意事项

  • 备份:修改主题文件前,务必备份。
  • 插件冲突:某些插件可能与Markdown插件冲突,需测试兼容性。
  • 主题支持:部分主题可能不支持Markdown,需手动调整。

通过这些步骤,WordPress即可支持Markdown格式的文章发布。

Posted in 生活 | Leave a comment

内网穿透下的 wordpress 地址冲突问题与 https下的后台登陆问题

问题背景

当我们将个人博客搭建在 NAS 或软路由上,并通过 Cloudflare Zero Trust 或其他工具进行内网穿透以便公网访问时,可能会遇到以下两个问题:

  1. 内网穿透下的地址冲突
    使用内网穿透将本地的 WordPress 服务暴露到公网时,如果同时希望通过内网 IP 访问后台,会遇到 WordPress 主站地址冲突的问题。WordPress 无法确定是将内网 IP 还是公网域名作为主站地址。

  2. HTTPS 协议下无法登录管理后台
    使用 HTTPS 协议访问 WordPress 时,可能会遇到无法登录管理后台的问题。


解决方案

1. 内网穿透下的地址冲突

WordPress 有两个重要参数:WordPress 地址站点地址。通常情况下,这两个值是相同的。如果希望通过外网域名和内网 IP 同时访问,可以通过修改 wp-config.php 文件动态设置这两个参数。

wp-config.php 中添加以下代码:

$http_type = is_numeric(substr($_SERVER['HTTP_HOST'], 0, 1)) ? 'http://' : 'https://';
define('WP_SITEURL', $http_type . $_SERVER['HTTP_HOST']);
define('WP_HOME', $http_type . $_SERVER['HTTP_HOST']);

代码说明

  • 通过 is_numeric 判断访问地址的第一个字符是否为数字(即是否为 IP 地址)。
  • 如果是 IP 地址,使用 http://;如果是域名,使用 https://
  • 动态设置 WP_SITEURLWP_HOME,避免地址冲突。

2. HTTPS 登录管理后台

当使用 HTTPS 访问 WordPress 时,可能会遇到无法登录管理后台的问题。这是因为 WordPress 默认未强制启用 SSL。可以通过修改 wp-config.php 文件强制开启 SSL 访问。

wp-config.php 中添加以下代码:

$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

代码说明

  • 强制将 $_SERVER['HTTPS'] 设置为 on,确保 WordPress 识别 HTTPS 协议。
  • 启用 FORCE_SSL_LOGINFORCE_SSL_ADMIN,强制后台使用 HTTPS 登录。

综合解决方案

如果既需要通过 HTTPS 登录后台,又需要保留内网 IP 访问的能力,可以将上述代码结合使用:

if (is_numeric(substr($_SERVER['HTTP_HOST'], 0, 1))) {
    $http_type = 'http://';
} else {
    $http_type = 'https://';
    $_SERVER['HTTPS'] = 'on';
    define('FORCE_SSL_LOGIN', true);
    define('FORCE_SSL_ADMIN', true);
}

define('WP_SITEURL', $http_type . $_SERVER['HTTP_HOST']);
define('WP_HOME', $http_type . $_SERVER['HTTP_HOST']);

代码说明

  • 如果是内网 IP 访问,使用 http://,不强制 HTTPS。
  • 如果是域名访问,使用 https://,并强制开启 HTTPS 登录。

总结

通过上述方法,可以解决内网穿透下的地址冲突问题,并确保 HTTPS 协议下能够正常登录 WordPress 管理后台。将代码添加到 wp-config.php 文件的最下方即可。

Posted in 网络相关 | Tagged , , | Leave a comment

Docker搭建WordPress绑定域名教程(二)

在上一节我们讲解了如何通过Docker快速搭建wordpress,这一节我们介绍下在Docker环境下,有哪些工具和程序可以帮助你轻松绑定域名并管理多个站点。以下是几种常用的解决方案:


1. Nginx Proxy Manager

Nginx Proxy Manager 是一个基于 Nginx 的反向代理管理工具,提供友好的 Web 界面来管理域名绑定、SSL 证书等。

安装步骤

  1. 创建 docker-compose.yml 文件:
    version: '3'
    services:
     app:
       image: 'jc21/nginx-proxy-manager:latest'
       restart: always
       ports:
         - '80:80'
         - '81:81'
         - '443:443'
       volumes:
         - ./data:/data
         - ./letsencrypt:/etc/letsencrypt
  2. 启动容器:
    docker-compose up -d
  3. 访问管理界面:
    • 打开浏览器,访问 http://your-server-ip:81
    • 默认登录信息:
  4. 在管理界面中添加域名和反向代理规则,绑定到你的 WordPress 容器。

2. Traefik

Traefik 是一个现代化的反向代理和负载均衡工具,专为容器化环境设计,支持自动发现 Docker 容器并绑定域名。

安装步骤

  1. 创建 docker-compose.yml 文件:

    version: '3'
    services:
     traefik:
       image: traefik:latest
       command:
         - --api.insecure=true
         - --providers.docker
         - --entrypoints.web.address=:80
       ports:
         - '80:80'
         - '8080:8080'
       volumes:
         - /var/run/docker.sock:/var/run/docker.sock
    
     wordpress:
       image: wordpress:latest
       environment:
         - WORDPRESS_DB_HOST=mysql
         - WORDPRESS_DB_USER=root
         - WORDPRESS_DB_PASSWORD=your_password
       labels:
         - "traefik.http.routers.wordpress.rule=Host(`yourdomain.com`)"
         - "traefik.http.routers.wordpress.entrypoints=web"
  2. 启动容器:
    docker-compose up -d
  3. 访问 Traefik 仪表板:
    • 打开浏览器,访问 http://your-server-ip:8080
  4. 访问 WordPress:
    • 打开浏览器,访问 http://yourdomain.com

3. Caddy

Caddy 是一个易于使用的 Web 服务器,支持自动 HTTPS 和反向代理。

安装步骤

  1. 创建 docker-compose.yml 文件:

    version: '3'
    services:
     caddy:
       image: caddy:latest
       ports:
         - '80:80'
         - '443:443'
       volumes:
         - ./Caddyfile:/etc/caddy/Caddyfile
         - ./caddy_data:/data
    
     wordpress:
       image: wordpress:latest
       environment:
         - WORDPRESS_DB_HOST=mysql
         - WORDPRESS_DB_USER=root
         - WORDPRESS_DB_PASSWORD=your_password
  2. 创建 Caddyfile 文件:
    yourdomain.com {
       reverse_proxy wordpress:80
    }
  3. 启动容器:
    docker-compose up -d
  4. 访问 WordPress:
    • 打开浏览器,访问 https://yourdomain.com(Caddy 会自动配置 HTTPS)。

4. Nginx + Let’s Encrypt(手动配置)

如果你更喜欢手动配置,可以使用 Nginx 和 Let’s Encrypt 结合 Docker 绑定域名。

安装步骤

  1. 创建 docker-compose.yml 文件:

    version: '3'
    services:
     nginx:
       image: nginx:latest
       ports:
         - '80:80'
         - '443:443'
       volumes:
         - ./nginx.conf:/etc/nginx/nginx.conf
         - ./certs:/etc/letsencrypt
    
     wordpress:
       image: wordpress:latest
       environment:
         - WORDPRESS_DB_HOST=mysql
         - WORDPRESS_DB_USER=root
         - WORDPRESS_DB_PASSWORD=your_password
  2. 创建 nginx.conf 文件:

    server {
       listen 80;
       server_name yourdomain.com;
    
       location / {
           proxy_pass http://wordpress:80;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
    }
  3. 启动容器:
    docker-compose up -d
  4. 使用 Certbot 获取 SSL 证书:
    sudo certbot --nginx -d yourdomain.com

总结

  • Nginx Proxy Manager:适合需要 Web 界面管理的用户。
  • Traefik:适合动态容器环境,自动发现服务。
  • Caddy:简单易用,自动 HTTPS。
  • Nginx + Let’s Encrypt:适合需要完全手动配置的用户。

根据你的需求选择合适的工具即可轻松绑定域名。

Posted in 网络相关 | Tagged , , , | Leave a comment