294 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
			
		
		
	
	
			294 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
| # LXC 容器浏览器自动化环境配置方案
 | ||
| 
 | ||
| ## 1. LXC 容器基础配置
 | ||
| 
 | ||
| ```bash
 | ||
| # 创建 Ubuntu 22.04 基础容器
 | ||
| lxc launch ubuntu:22.04 chrome-automation
 | ||
| 
 | ||
| # 配置容器资源限制
 | ||
| lxc config set chrome-automation limits.cpu 2
 | ||
| lxc config set chrome-automation limits.memory 4GB
 | ||
| 
 | ||
| # 映射端口(如果需要外部访问)
 | ||
| lxc config device add chrome-automation proxy-port8080 proxy listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:8080
 | ||
| ```
 | ||
| 
 | ||
| ## 2. 容器内环境配置
 | ||
| 
 | ||
| ### 2.1 基础系统包安装
 | ||
| ```bash
 | ||
| # 进入容器
 | ||
| lxc exec chrome-automation -- bash
 | ||
| 
 | ||
| # 更新系统
 | ||
| apt update && apt upgrade -y
 | ||
| 
 | ||
| # 安装基础开发工具和图形支持
 | ||
| apt install -y \
 | ||
|     curl \
 | ||
|     wget \
 | ||
|     unzip \
 | ||
|     git \
 | ||
|     build-essential \
 | ||
|     xvfb \
 | ||
|     x11-utils \
 | ||
|     x11-xserver-utils \
 | ||
|     xdg-utils \
 | ||
|     libnss3 \
 | ||
|     libatk-bridge2.0-0 \
 | ||
|     libdrm2 \
 | ||
|     libxkbcommon0 \
 | ||
|     libxcomposite1 \
 | ||
|     libxdamage1 \
 | ||
|     libxrandr2 \
 | ||
|     libgbm1 \
 | ||
|     libxss1 \
 | ||
|     libasound2 \
 | ||
|     fonts-liberation \
 | ||
|     libappindicator3-1 \
 | ||
|     xdg-utils \
 | ||
|     libsecret-1-dev \
 | ||
|     libgconf-2-4
 | ||
| ```
 | ||
| 
 | ||
| ### 2.2 安装 Chrome 浏览器
 | ||
| ```bash
 | ||
| # 下载并安装 Google Chrome
 | ||
| wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
 | ||
| echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
 | ||
| apt update
 | ||
| apt install -y google-chrome-stable
 | ||
| ```
 | ||
| 
 | ||
| ### 2.3 安装浏览器自动化工具
 | ||
| ```bash
 | ||
| # 安装 Node.js 和 npm
 | ||
| curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
 | ||
| apt install -y nodejs
 | ||
| 
 | ||
| # 安装 Python 和相关工具
 | ||
| apt install -y python3 python3-pip python3-venv
 | ||
| 
 | ||
| # 安装 Selenium 和浏览器驱动
 | ||
| pip3 install selenium webdriver-manager
 | ||
| 
 | ||
| # 下载 ChromeDriver
 | ||
| npm install -g chromedriver
 | ||
| ```
 | ||
| 
 | ||
| ### 2.4 配置无头模式运行环境
 | ||
| ```bash
 | ||
| # 创建自动化脚本目录
 | ||
| mkdir -p /opt/browser-automation
 | ||
| cd /opt/browser-automation
 | ||
| 
 | ||
| # 创建 Chrome 无头模式启动脚本
 | ||
| cat > chrome-headless.sh << 'EOF'
 | ||
| #!/bin/bash
 | ||
| export DISPLAY=:99
 | ||
| Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
 | ||
| sleep 2
 | ||
| google-chrome --headless --no-sandbox --disable-dev-shm-usage --disable-gpu --remote-debugging-port=9222 --disable-extensions --disable-plugins --disable-images &
 | ||
| sleep 3
 | ||
| EOF
 | ||
| 
 | ||
| chmod +x chrome-headless.sh
 | ||
| ```
 | ||
| 
 | ||
| ## 3. 自动化工具配置
 | ||
| 
 | ||
| ### 3.1 Python Selenium 配置示例
 | ||
| ```python
 | ||
| # selenium_automation.py
 | ||
| from selenium import webdriver
 | ||
| from selenium.webdriver.chrome.options import Options
 | ||
| from selenium.webdriver.chrome.service import Service
 | ||
| from webdriver_manager.chrome import ChromeDriverManager
 | ||
| 
 | ||
| def create_chrome_driver():
 | ||
|     chrome_options = Options()
 | ||
|     chrome_options.add_argument("--headless")
 | ||
|     chrome_options.add_argument("--no-sandbox")
 | ||
|     chrome_options.add_argument("--disable-dev-shm-usage")
 | ||
|     chrome_options.add_argument("--disable-gpu")
 | ||
|     chrome_options.add_argument("--remote-debugging-port=9222")
 | ||
|     chrome_options.add_argument("--disable-extensions")
 | ||
|     chrome_options.add_argument("--disable-plugins")
 | ||
|     chrome_options.add_argument("--window-size=1920,1080")
 | ||
|     
 | ||
|     service = Service(ChromeDriverManager().install())
 | ||
|     driver = webdriver.Chrome(service=service, options=chrome_options)
 | ||
|     return driver
 | ||
| 
 | ||
| # 使用示例
 | ||
| driver = create_chrome_driver()
 | ||
| driver.get("https://www.example.com")
 | ||
| print(driver.title)
 | ||
| driver.quit()
 | ||
| ```
 | ||
| 
 | ||
| ### 3.2 Node.js Puppeteer 配置示例
 | ||
| ```javascript
 | ||
| // puppeteer_automation.js
 | ||
| const puppeteer = require('puppeteer');
 | ||
| 
 | ||
| async function runAutomation() {
 | ||
|   const browser = await puppeteer.launch({
 | ||
|     headless: true,
 | ||
|     args: [
 | ||
|       '--no-sandbox',
 | ||
|       '--disable-setuid-sandbox',
 | ||
|       '--disable-dev-shm-usage',
 | ||
|       '--disable-gpu',
 | ||
|       '--window-size=1920,1080'
 | ||
|     ]
 | ||
|   });
 | ||
|   
 | ||
|   const page = await browser.newPage();
 | ||
|   await page.goto('https://www.example.com');
 | ||
|   const title = await page.title();
 | ||
|   console.log(title);
 | ||
|   
 | ||
|   await browser.close();
 | ||
| }
 | ||
| 
 | ||
| runAutomation();
 | ||
| ```
 | ||
| 
 | ||
| ## 4. 容器启动配置
 | ||
| 
 | ||
| ### 4.1 启动脚本
 | ||
| ```bash
 | ||
| cat > /opt/browser-automation/start.sh << 'EOF'
 | ||
| #!/bin/bash
 | ||
| 
 | ||
| # 启动 Xvfb 虚拟显示
 | ||
| export DISPLAY=:99
 | ||
| Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
 | ||
| sleep 2
 | ||
| 
 | ||
| # 启动 Chrome 浏览器
 | ||
| google-chrome --headless --no-sandbox --disable-dev-shm-usage --disable-gpu --remote-debugging-port=9222 --disable-extensions --disable-plugins --disable-images &
 | ||
| sleep 3
 | ||
| 
 | ||
| # 可选:启动自动化服务
 | ||
| # python3 /opt/browser-automation/service.py
 | ||
| 
 | ||
| echo "Browser automation environment ready!"
 | ||
| EOF
 | ||
| 
 | ||
| chmod +x /opt/browser-automation/start.sh
 | ||
| ```
 | ||
| 
 | ||
| ### 4.2 系统服务配置
 | ||
| ```bash
 | ||
| cat > /etc/systemd/system/browser-automation.service << 'EOF'
 | ||
| [Unit]
 | ||
| Description=Browser Automation Service
 | ||
| After=network.target
 | ||
| 
 | ||
| [Service]
 | ||
| Type=forking
 | ||
| ExecStart=/opt/browser-automation/start.sh
 | ||
| Restart=always
 | ||
| User=root
 | ||
| Environment=DISPLAY=:99
 | ||
| 
 | ||
| [Install]
 | ||
| WantedBy=multi-user.target
 | ||
| EOF
 | ||
| 
 | ||
| systemctl enable browser-automation.service
 | ||
| ```
 | ||
| 
 | ||
| ## 5. 安全配置
 | ||
| 
 | ||
| ### 5.1 非 root 用户配置
 | ||
| ```bash
 | ||
| # 创建专用用户
 | ||
| useradd -m -s /bin/bash browser-user
 | ||
| usermod -a -G sudo browser-user
 | ||
| 
 | ||
| # 设置 Chrome 以非 root 用户运行
 | ||
| echo 'chrome --no-sandbox --user-data-dir=/home/browser-user/.config/google-chrome' > /home/browser-user/run-chrome.sh
 | ||
| chown browser-user:browser-user /home/browser-user/run-chrome.sh
 | ||
| ```
 | ||
| 
 | ||
| ### 5.2 网络安全
 | ||
| ```bash
 | ||
| # 配置防火墙(如果需要)
 | ||
| ufw allow 22/tcp
 | ||
| # 仅在需要外部访问时开放特定端口
 | ||
| # ufw allow 8080/tcp
 | ||
| ```
 | ||
| 
 | ||
| ## 6. 监控和日志
 | ||
| 
 | ||
| ### 6.1 日志配置
 | ||
| ```bash
 | ||
| # 创建日志目录
 | ||
| mkdir -p /var/log/browser-automation
 | ||
| 
 | ||
| # 配置日志轮转
 | ||
| cat > /etc/logrotate.d/browser-automation << 'EOF'
 | ||
| /var/log/browser-automation/*.log {
 | ||
|     daily
 | ||
|     missingok
 | ||
|     rotate 30
 | ||
|     compress
 | ||
|     delaycompress
 | ||
|     notifempty
 | ||
|     create 644 root root
 | ||
| }
 | ||
| EOF
 | ||
| ```
 | ||
| 
 | ||
| ## 7. 备份和恢复
 | ||
| 
 | ||
| ### 7.1 创建容器快照
 | ||
| ```bash
 | ||
| # 创建快照
 | ||
| lxc snapshot chrome-automation initial-setup
 | ||
| 
 | ||
| # 列出快照
 | ||
| lxc info chrome-automation --snapshots
 | ||
| 
 | ||
| # 恢复快照
 | ||
| lxc restore chrome-automation initial-setup
 | ||
| ```
 | ||
| 
 | ||
| ### 7.2 配置文件备份
 | ||
| ```bash
 | ||
| # 备份重要配置
 | ||
| lxc file pull chrome-automation/etc/systemd/system/browser-automation.service ./
 | ||
| lxc file pull chrome-automation/opt/browser-automation/start.sh ./
 | ||
| ```
 | ||
| 
 | ||
| ## 8. 性能优化
 | ||
| 
 | ||
| ### 8.1 Chrome 启动参数优化
 | ||
| ```bash
 | ||
| CHROME_OPTS="--headless \
 | ||
| --no-sandbox \
 | ||
| --disable-dev-shm-usage \
 | ||
| --disable-gpu \
 | ||
| --remote-debugging-port=9222 \
 | ||
| --disable-extensions \
 | ||
| --disable-plugins \
 | ||
| --disable-images \
 | ||
| --disable-javascript \
 | ||
| --memory-pressure-off \
 | ||
| --max_old_space_size=4096 \
 | ||
| --js-flags=--max-old-space-size=2048"
 | ||
| ```
 | ||
| 
 | ||
| ### 8.2 容器资源优化
 | ||
| ```bash
 | ||
| # 在容器配置中设置资源限制
 | ||
| lxc config set chrome-automation limits.cpu 2
 | ||
| lxc config set chrome-automation limits.memory 4GB
 | ||
| lxc config set chrome-automation limits.memory.swap false
 | ||
| ```
 | ||
| 
 | ||
| 这个配置方案提供了完整的LXC容器环境,专门用于浏览器自动化任务,具有良好的性能、安全性和可维护性。 |