插件窝 干货文章 如何在Linux上搭建容器化的高性能Web服务器?

如何在Linux上搭建容器化的高性能Web服务器?

Nginx Docker PHP Compose 1043    来源:    2025-04-14

在Linux上搭建容器化的高性能Web服务器方案

方案概述

本方案将使用Docker容器化技术搭建一个高性能的Web服务器环境,包含Nginx作为Web服务器、PHP-FPM作为应用处理器(可选),并配置性能优化参数。

准备工作

  1. 确保Linux系统已安装Docker和Docker Compose

    # 安装Docker
    curl -fsSL https://get.docker.com | sh
    sudo systemctl enable --now docker
    
    # 安装Docker Compose
    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    
  2. 创建项目目录结构

    mkdir -p web-server/{nginx,php,html,logs}
    cd web-server
    

配置方案

1. 基础Nginx配置

创建nginx/nginx.conf文件:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  10240;
    multi_accept on;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    keepalive_timeout  65;
    keepalive_requests 100000;
    reset_timedout_connection on;

    client_max_body_size 100M;
    client_body_buffer_size 128k;

    gzip  on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_buffers 16 8k;

    include /etc/nginx/conf.d/*.conf;
}

2. 站点配置

创建nginx/conf.d/default.conf文件:

server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
        access_log off;
        add_header Cache-Control "public";
    }

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;
}

3. Docker Compose配置

创建docker-compose.yml文件:

version: '3.8'

services:
  nginx:
    image: nginx:1.21-alpine
    container_name: web-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./html:/var/www/html
      - ./logs/nginx:/var/log/nginx
    networks:
      - web-network
    restart: unless-stopped
    depends_on:
      - php

  php:
    image: php:8.1-fpm-alpine
    container_name: web-php
    volumes:
      - ./html:/var/www/html
      - ./logs/php:/var/log/php
    environment:
      - PHP_MEMORY_LIMIT=256M
      - PHP_MAX_EXECUTION_TIME=300
      - PHP_UPLOAD_MAX_FILESIZE=100M
      - PHP_POST_MAX_SIZE=100M
    networks:
      - web-network
    restart: unless-stopped

networks:
  web-network:
    driver: bridge

4. 创建测试页面

html目录下创建index.php文件:

<?php phpinfo(); ?>

启动服务

docker-compose up -d

性能优化建议

  1. Nginx优化

    • 调整worker_processes为CPU核心数
    • 增加worker_connections
    • 启用sendfile, tcp_nopush, tcp_nodelay
    • 配置合理的keepalive超时
  2. PHP-FPM优化

    • 调整pm.max_children根据服务器内存
    • 设置pm.start_servers, pm.min_spare_servers, pm.max_spare_servers
    • 启用OPcache
  3. 容器优化

    • 使用资源限制:--memory, --cpus
    • 考虑使用--network host模式减少网络开销
    • 对静态资源使用单独的Nginx容器
  4. 扩展功能

    • 添加Redis缓存
    • 配置HTTP/2
    • 添加SSL证书(使用Let's Encrypt)

监控与维护

  1. 查看容器日志:

    docker-compose logs -f
    
  2. 监控性能:

    docker stats
    
  3. 更新配置后重载:

    docker-compose exec nginx nginx -s reload
    

此方案提供了一个基础但高性能的容器化Web服务器环境,可以根据实际需求进一步调整和扩展。