在华为云ECS实例上基于EulerOS 2.0搭建WordPress网站该怎么操作?

在华为云ECS实例上基于EulerOS 2.0搭建WordPress网站的详细操作步骤如下:


一、前期准备

  1. 购买华为云ECS实例

    • 选择EulerOS 2.0作为操作系统(建议选择最新稳定版)。
    • 配置安全组:开放 80(HTTP)443(HTTPS)22(SSH) 端口。
    • 建议配置至少2核4GB及以上规格。
  2. 连接ECS实例

    ssh root@<你的ECS公网IP>

二、安装必要环境

1. 更新系统并安装常用工具

yum update -y
yum install -y wget vim git

2. 安装并配置Web服务(Nginx/Apache)

方案一:Nginx(推荐)

# 安装Nginx
yum install -y nginx

# 启动Nginx并设置开机自启
systemctl start nginx
systemctl enable nginx

方案二:Apache

yum install -y httpd
systemctl start httpd
systemctl enable httpd

3. 安装数据库(MariaDB)

# 安装MariaDB
yum install -y mariadb-server mariadb

# 启动并设置开机自启
systemctl start mariadb
systemctl enable mariadb

# 运行安全配置向导
mysql_secure_installation

按提示设置root密码、移除匿名用户、禁止远程root登录等。

4. 安装PHP 7.4+

# 添加EPEL和Remi仓库
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

# 启用PHP 7.4
yum-config-manager --enable remi-php74

# 安装PHP及扩展
yum install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml

启动PHP-FPM并设置开机自启:

systemctl start php-fpm
systemctl enable php-fpm

三、配置数据库

  1. 登录MariaDB

    mysql -u root -p
  2. 创建WordPress数据库和用户

    CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '你的强密码';
    GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;

四、安装WordPress

  1. 下载并解压WordPress

    cd /tmp
    wget https://wordpress.org/latest.tar.gz
    tar -xzvf latest.tar.gz
  2. 移动到Web目录

    # 如果是Nginx,默认目录通常是 /usr/share/nginx/html
    mv wordpress /usr/share/nginx/html/
    # 如果是Apache,目录通常是 /var/www/html
    # mv wordpress /var/www/html/
  3. 配置权限

    chown -R nginx:nginx /usr/share/nginx/html/wordpress  # Nginx用户
    # 如果是Apache:chown -R apache:apache /var/www/html/wordpress
    chmod -R 755 /usr/share/nginx/html/wordpress

五、配置Web服务器

Nginx配置示例

  1. 创建配置文件:

    vim /etc/nginx/conf.d/wordpress.conf
  2. 写入以下内容(根据实际情况修改域名和路径):

    server {
    listen 80;
    server_name 你的域名或ECS公网IP;
    root /usr/share/nginx/html/wordpress;
    index index.php index.html;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ .php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    }
  3. 测试并重载Nginx:

    nginx -t
    systemctl reload nginx

六、完成WordPress安装

  1. 在浏览器访问:http://<你的ECS公网IP>
  2. 按提示选择语言,填写数据库信息:
    • 数据库名:wordpress
    • 用户名:wpuser
    • 密码:你设置的密码
    • 数据库主机:localhost
    • 表前缀:wp_(建议修改为其他前缀如wp123_以增强安全性)
  3. 设置网站标题、管理员账号密码,完成安装。

七、安全加固建议

  1. 配置防火墙

    # 开放80、443端口
    firewall-cmd --permanent --add-service=http
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload
  2. 配置SSL证书(HTTPS)

    • 可使用Let’s Encrypt免费证书,安装Certbot:
      yum install -y certbot python3-certbot-nginx
      certbot --nginx -d 你的域名
  3. 限制PHP执行权限

    • 在Nginx配置中禁用敏感目录的PHP执行:
      location ~* /wp-content/uploads/.*.php$ {
      deny all;
      }
  4. 定期更新

    yum update -y

常见问题排查

  1. 502 Bad Gateway

    • 检查PHP-FPM是否运行:systemctl status php-fpm
    • 确认Nginx配置中的fastcgi_pass路径是否正确。
  2. 数据库连接失败

    • 确认MariaDB服务运行:systemctl status mariadb
    • 检查数据库用户权限:SHOW GRANTS FOR 'wpuser'@'localhost';
  3. 文件权限问题

    • 确保Web目录所属用户正确(Nginx默认用户为nginx)。

按照以上步骤操作,即可在华为云EulerOS 2.0上成功部署WordPress。如需更高级的优化(如缓存配置、CDN提速等),可根据实际需求进一步调整。

云服务器