速卖通素材
奋斗

在CentOS或Ubuntu上安装WordPress的步骤和注意事项有哪些?

服务器

在 CentOS 或 Ubuntu 上安装 WordPress,通常采用 LAMP/LNMP 环境 + MySQL/MariaDB 的方式。以下是详细步骤和关键注意事项,适用于生产环境部署(以 Ubuntu 20.04/22.04 和 CentOS Stream 8/9 为例):


✅ 一、前置准备

1. 系统要求

  • 操作系统:Ubuntu 20.04+ 或 CentOS 8+/Stream 8+
  • 内存 ≥ 512MB(推荐 1GB+)
  • 磁盘空间 ≥ 2GB
  • 开放端口:80(HTTP)、443(HTTPS,可选但强烈推荐)

2. 更新系统

# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y

# CentOS/RHEL
sudo yum update -y

🛠️ 二、安装基础服务(LAMP 或 LNMP)

推荐使用 Apache + PHP-FPM + MySQL/MariaDB;若需高性能可改用 Nginx。

方案 A:Apache + PHP + MariaDB(通用推荐)

1. 安装 Apache

# Ubuntu
sudo apt install apache2 -y

# CentOS
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

2. 安装 PHP 及扩展(7.4 或 8.x 版本)

# Ubuntu (PHP 8.1)
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl -y

# CentOS (PHP 8.1 via EPEL + Remi repo)
sudo yum install epel-release -y
sudo yum config-manager --set-enabled powertools
sudo yum install remi-release-el9 -y  # CentOS 9; CentOS 8 用 remi-release-el8
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.1 -y
sudo yum install php php-fpm php-mysqlnd php-common php-cli php-gd php-mbstring php-xml php-zip -y

⚠️ 注意:CentOS 默认无 PHP 源,需添加 Remi 仓库;Ubuntu 可用 ondrej/php PPA 获取新版 PHP。

3. 安装 MariaDB(MySQL 替代品,性能更优)

# Ubuntu
sudo apt install mariadb-server mariadb-client -y
sudo mysql_secure_installation  # 设置 root 密码等

# CentOS
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo mysql_secure_installation

4. 创建 WordPress 数据库与用户

登录 MariaDB:

mysql -u root -p

执行 SQL:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

🔐 安全提示:

  • 使用强密码(大小写+数字+符号)
  • 禁止远程 root 登录
  • 仅允许本地连接('wp_user'@'localhost'

📦 三、下载并配置 WordPress

1. 下载 WordPress

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo mv wordpress/* .
sudo rmdir wordpress

2. 设置权限

sudo chown -R www-data:www-data /var/www/html  # Ubuntu
# 或
sudo chown -R apache:apache /var/www/html      # CentOS
sudo chmod -R 755 /var/www/html

3. 生成 wp-config.php

sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

修改以下项:

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wp_user' );
define( 'DB_PASSWORD', 'StrongPassword123!' );
define( 'DB_HOST', 'localhost' );
// 建议添加:
define( 'WP_HOME', 'http://your-domain.com' );
define( 'WP_SITEURL', 'http://your-domain.com' );

🌐 四、配置 Web 服务器

方案 A:Apache 虚拟主机(Ubuntu/CentOS 通用)

创建站点配置:

# Ubuntu
sudo nano /etc/apache2/sites-available/wordpress.conf

# CentOS
sudo nano /etc/httpd/conf.d/wordpress.conf

内容示例(替换 your-domain.com):

<VirtualHost *:80>
    ServerName your-domain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/wordpress-error.log
    CustomLog ${APACHE_LOG_DIR}/wordpress-access.log combined
</VirtualHost>

启用站点并重启 Apache:

# Ubuntu
sudo a2ensite wordpress
sudo a2enmod rewrite
sudo systemctl restart apache2

# CentOS
sudo systemctl restart httpd

方案 B:Nginx + PHP-FPM(高性能场景)

  1. 安装 Nginx & PHP-FPM(略,类似上文)
  2. 配置 /etc/nginx/sites-available/wordpress

    server {
       listen 80;
       server_name your-domain.com;
       root /var/www/html;
       index index.php index.html;
    
       location / {
           try_files $uri $uri/ /index.php?$args;
       }
    
       location ~ .php$ {
           include fastcgi_params;
           fastcgi_pass unix:/run/php/php8.1-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           fastcgi_buffer_size 128k;
           fastcgi_buffers 4 256k;
       }
    }
  3. 启用站点:sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
  4. 重载:sudo nginx -t && sudo systemctl reload nginx

🚀 五、完成安装

访问 http://your-domain.com → 进入 WordPress 安装向导:

  • 选择语言
  • 填写网站标题、管理员账号(用户名、邮箱、强密码)
  • 提交后自动跳转至后台

🔒 六、关键注意事项(安全 & 运维)

类别 建议
HTTPS 必装!使用 Let’s Encrypt:certbot --apachecertbot --nginx
文件权限 禁止 chmod 777;核心目录应为 755,上传目录 755775(组为 web 用户)
防火墙 仅开放 80/443;关闭 SSH 非标准端口(22→自定义),禁用 root 登录
定期备份 数据库 + 文件目录(/var/www/html + /etc/mysql);可用 mysqldump + rsync 脚本
插件安全 仅安装可信插件;及时更新 WordPress 核心、主题、插件
隐藏敏感信息 移除 /wp-admin/install.php 访问限制(通过 .htaccess 或 Nginx 规则);禁用 XML-RPC(防暴力破解)
日志监控 定期检查 /var/log/apache2/error.log/var/log/httpd/error_log
SELinux(CentOS) 若开启 SELinux,需授权:
sudo setsebool -P httpd_can_network_connect 1
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
sudo restorecon -Rv /var/www/html

🧪 验证检查清单

✅ Apache/Nginx 运行正常
✅ PHP 版本 ≥ 7.4(推荐 8.1+)
✅ MySQL/MariaDB 可连接且权限正确
wp-config.php 已配置
.htaccess(Apache)或 try_files(Nginx)支持重写
✅ HTTPS 证书有效(生产环境必需)
✅ 防火墙策略最小化
✅ 备份机制已部署


需要我提供:

  • 自动化部署脚本(Ansible/Bash)?
  • Docker 一键部署方案?
  • 高可用架构(负载均衡 + 主从数据库)设计?

欢迎告诉我你的具体场景,我可以进一步定制方案。

未经允许不得转载:轻量云Cloud » 在CentOS或Ubuntu上安装WordPress的步骤和注意事项有哪些?