CentOS系统下如何通过LNMP或LAMP搭建WordPress?

在 CentOS 系统下,可以通过 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)或 LAMP(Linux + Apache + MySQL/MariaDB + PHP)搭建 WordPress。以下是详细的步骤说明。


一、准备工作

1. 更新系统

sudo yum update -y

2. 安装常用工具(可选)

sudo yum install wget curl vim net-tools epel-release -y

二、选择方案:LNMP 或 LAMP


方案一:LNMP 搭建 WordPress(推荐用于高性能)

1. 安装 Nginx

sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

检查是否运行:

systemctl status nginx

2. 安装 MariaDB(MySQL 替代品)

sudo yum install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb

运行安全初始化:

sudo mysql_secure_installation

按提示设置 root 密码、删除匿名用户等。

3. 创建 WordPress 数据库和用户

登录 MariaDB:

mysql -u root -p

执行以下 SQL 命令:

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

4. 安装 PHP 及相关扩展

sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-curl php-zip -y

配置 PHP-FPM:

sudo vim /etc/php-fpm.d/www.conf

确保以下配置正确:

user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx

启动并启用 PHP-FPM:

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

5. 配置 Nginx 虚拟主机

创建站点配置文件:

sudo vim /etc/nginx/conf.d/wordpress.conf

写入以下内容(替换 your_domain.com):

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
    root /var/www/html/wordpress;
    index index.php index.html;

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

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.ht {
        deny all;
    }
}

测试配置并重启 Nginx:

sudo nginx -t
sudo systemctl restart nginx

6. 下载并安装 WordPress

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo mkdir -p /var/www/html/wordpress
sudo cp -r /tmp/wordpress/* /var/www/html/wordpress/
sudo chown -R nginx:nginx /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress

7. 配置 WordPress

访问 http://your_server_ip_or_domain,进入 WordPress 安装向导。

填写数据库信息:

  • 数据库名:wordpress
  • 用户名:wpuser
  • 密码:你设置的密码
  • 数据库主机:localhost
  • 表前缀:wp_(可自定义)

完成安装即可。


方案二:LAMP 搭建 WordPress

1. 安装 Apache

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

2. 安装 MariaDB(同上)

sudo yum install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation

创建数据库(同 LNMP 步骤)。

3. 安装 PHP(Apache 模式)

sudo yum install php php-mysql php-gd php-xml php-mbstring php-curl php-zip -y

注意:LAMP 中 PHP 通过 mod_php 模块与 Apache 集成,无需单独启动 php-fpm。

重启 Apache:

sudo systemctl restart httpd

4. 下载并部署 WordPress

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo cp -r /tmp/wordpress/* /var/www/html/
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html

5. 配置防火墙(开放 HTTP)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

6. 访问网站

浏览器访问 http://your_server_ip,开始 WordPress 安装向导。


三、安全建议

  1. 开启防火墙(已启用 http/https):

    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
  2. 配置 SSL(Let’s Encrypt)
    使用 Certbot 获取免费 HTTPS 证书(适用于 Nginx/Apache):

    sudo yum install certbot python3-certbot-nginx  # for Nginx
    sudo certbot --nginx -d your_domain.com
  3. 定期更新系统和软件包


四、总结对比

特性 LNMP LAMP
Web服务器 Nginx(轻量、高并发) Apache(功能丰富、易用)
PHP处理方式 PHP-FPM(独立进程) mod_php(集成到Apache)
性能 更高(尤其静态资源) 稍低但足够日常使用
配置复杂度 略高 简单直观
推荐场景 高流量、性能敏感型网站 初学者、小中型项目

✅ 两种方式均可成功部署 WordPress。根据需求选择:

  • 想要高性能、现代架构 → 选 LNMP
  • 想要简单快速上手 → 选 LAMP

如有域名和公网 IP,建议后续配置 HTTPS 提升安全性。

未经允许不得转载:云计算 » CentOS系统下如何通过LNMP或LAMP搭建WordPress?