512M内存的服务器如何流畅的运行Wordpress?

|

这篇文章详细介绍了如何在内存仅有 512MB 的 Ubuntu 22.04 服务器上,优化 Nginx、PHP 和 MariaDB 的配置,以更好地运行 WordPress。文章还示范了如何使用 Certbot 生成 SSL 证书实现 HTTPS,并配置 Nginx 实现 HTTP 到 HTTPS 以及 www 到非 www 重定向。此外,文章讲解了如何设置 FastCGI 和 Redis 缓存以提升 WordPress 的性能,包括缓存管理和手动清除缓存的方法。

通过本文,您将能够在资源有限的情况下,实现一个高效、安全、性能优化的 WordPress 环境,并详细了解各种缓存机制的配置和管理方法。

一、安装必要的软件

  1. 更新系统包
sudo apt update && sudo apt upgrade -y
  1. 安装 NginxPHPMariaDBRedisCertbot
sudo apt install nginx php-fpm php-common php-mysql php-xml php-xmlrpc php-curl php-gd php-imagick php-cli php-dev php-imap php-mbstring php-opcache php-redis php-soap php-zip unzip mariadb-server redis-server certbot python3-certbot-nginx -y
  1. 安装 WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

二、优化配置

1. 优化 Nginx

  1. 创建缓存和日志目录
sudo mkdir -p /var/www/your_domain/cache
sudo chmod -R 755 /var/www/your_domain/cache

sudo mkdir -p /var/www/your_domain/logs
sudo chmod -R 755 /var/www/your_domain/logs
  1. 编辑 Nginx 主配置文件 /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 1024;
}

http {
    # FastCGI cache configuration
    fastcgi_cache_path /var/www/your_domain/cache levels=1:2 keys_zone=WORDPRESS:10m inactive=60m use_temp_path=off;

    # Other standard settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

    # Gzip configuration
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    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/www/your_domain/logs/access.log main;
    error_log /var/www/your_domain/logs/error.log;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

2. 优化 PHP

编辑 PHP-FPM 池配置文件 /etc/php/8.1/fpm/pool.d/www.conf(路径和版本号可能需要调整,根据你实际的 PHP 版本):

pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s;
pm.max_requests = 500

php_admin_value[memory_limit] = 64M
php_admin_value[post_max_size] = 8M
php_admin_value[upload_max_filesize] = 8M

重新启动 PHP-FPM:

sudo systemctl restart php8.1-fpm

3. 优化 MariaDB

编辑 MariaDB 配置文件 /etc/mysql/mariadb.conf.d/50-server.cnf

[mysqld]
innodb_buffer_pool_size = 64M
key_buffer_size = 16M
query_cache_limit = 1M
query_cache_size = 16M

max_connections = 20

skip-name-resolve
table_open_cache = 200
thread_cache_size = 4
innodb_log_file_size = 16M
innodb_flush_log_at_trx_commit = 2

重新启动 MariaDB:

sudo systemctl restart mariadb

三、配置 Redis

  1. 编辑 Redis 配置文件

打开并编辑 /etc/redis/redis.conf

supervised systemd
maxmemory 64mb   # Setting to ensure it doesn't use too much memory
maxmemory-policy allkeys-lru

重新启动 Redis 服务器:

sudo systemctl restart redis

四、配置网站和下载安装 WordPress

1. 配置 MariaDB 数据库

  1. 运行安全配置
sudo mysql_secure_installation
  1. 创建数据库和用户
sudo mysql -u root -p

CREATE DATABASE your_dbname;
CREATE USER 'your_dbuser'@'localhost' IDENTIFIED BY 'your_dbpassword';
GRANT ALL PRIVILEGES ON your_dbname.* TO 'your_dbuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

2. 使用 WP-CLI 安装 WordPress

sudo mkdir -p /var/www/your_domain/public
sudo chown -R $(whoami):www-data /var/www/your_domain/public
cd /var/www/your_domain/public

wp core download --path=/var/www/your_domain/public
wp config create --dbname=your_dbname --dbuser=your_dbuser --dbpass=your_dbpassword --dbhost=localhost
wp core install --url="https://your_domain" --title="Your Blog Title" --admin_user="your_admin_user" --admin_password="your_admin_password" --admin_email="[email protected]"

3. 配置 Nginx

  1. 编辑 Nginx 配置文件

打开并编辑 /etc/nginx/sites-available/your_domain

server {
    listen 80;
    server_name your_domain www.your_domain;

    # Redirect HTTP to HTTPS
    return 301 https://your_domain$request_uri;
}

server {
    listen 443 ssl;
    server_name your_domain;

    root /var/www/your_domain/public;
    index index.php index.html index.htm;

    # SSL certificate configuration
    ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_ecdh_curve secp384r1;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    access_log /var/www/your_domain/logs/access.log;
    error_log /var/www/your_domain/logs/error.log;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;

        fastcgi_cache WORDPRESS;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503 http_504;

        fastcgi_cache_bypass $http_cookie;
        fastcgi_no_cache $http_cookie;

        fastcgi_cache_valid 200 60m;

        add_header X-FastCGI-Cache $upstream_cache_status;
    }

    location ~ /\.ht {
        deny all;
    }
}

# Redirect www to non-www
server {
    listen 443 ssl;
    server_name www.your_domain;

    ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;

    return 301 https://your_domain$request_uri;
}
  1. 启用新的 Nginx 配置
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

4. 生成 SSL 证书

  1. 使用 Certbot 生成 SSL 证书
sudo certbot --nginx -d your_domain -d www.your_domain

按照提示进行操作,Certbot 会自动配置 SSL 证书。

5. 配置 WordPress 使用 Redis 缓存

  1. 安装并激活 Redis Object Cache 插件

进入 WordPress 管理后台,搜索并安装 Redis Object Cache 插件,然后激活。

  1. 配置 wp-config.php

/var/www/your_domain/public/wp-config.php 文件中添加以下行:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_CACHE', true);
  1. 启用 Redis 缓存

进入 WordPress 后台的 Redis 插件设置页面,点击 “Enable Object Cache”。

6. 启用 Swap 空间

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

确保 Swap 文件在系统启动时自动启用:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

总结

通过以上步骤,您可以在仅有 512MB 内存的服务器上,优化 Nginx、PHP 和 MariaDB 配置,然后安装和配置 WordPress,并使用 HTTPS、FastCGI 和 Redis 缓存来优化和加速 WordPress 的性能。定期监控服务器性能和内存使用情况,确保系统稳定性和优化效果。

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注