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

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


一、准备工作

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

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;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. 安装 PHP 和相关模块

# 安装 EPEL 和 Remi 仓库(提供最新 PHP 版本)
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

# 启用 PHP 7.4(或其他版本,如 8.0/8.1)
sudo yum-config-manager --enable remi-php74

# 安装 PHP 及必要扩展
sudo yum install php php-fpm php-mysql php-gd php-xml php-mbstring php-json php-curl -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 支持 WordPress

创建站点配置文件:

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

写入以下内容(替换 your_domain.com 为你的域名或 IP):

server {
    listen 80;
    server_name your_domain.com;

    root /var/www/html/wordpress;
    index index.php index.html index.htm;

    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 reload 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 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,进入 WordPress 安装向导。

填写数据库信息:

  • 数据库名:wordpress
  • 用户名:wpuser
  • 密码:你设置的密码
  • 数据库主机:localhost
  • 表前缀:可默认 wp_

完成后续安装即可。


方案二:LAMP 搭建 WordPress(适合初学者)

1. 安装 Apache

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

2. 安装 MariaDB(同 LNMP 步骤)

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

创建数据库(同上):

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

3. 安装 PHP

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

重启 Apache:

sudo systemctl restart httpd

4. 下载 WordPress

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo cp -r 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

按照向导填写数据库信息,完成安装。


三、常见问题解决

1. 权限问题

确保 Web 目录权限正确:

sudo chown -R apache:apache /var/www/html   # LAMP
sudo chown -R nginx:nginx /var/www/html     # LNMP

2. SELinux 限制(CentOS 默认开启)

临时关闭(不推荐生产环境):

sudo setenforce 0

或配置 SELinux 允许 Web 写入:

sudo setsebool -P httpd_can_network_connect_db 1
sudo setsebool -P httpd_read_user_content 1

3. 防火墙开放端口

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

四、后续建议

  • 配置 SSL(使用 Let’s Encrypt + Certbot)
  • 备份数据库和网站文件
  • 定期更新系统和 WordPress 插件

✅ 总结:

方案 优点 适用场景
LNMP 高性能、低资源占用 高并发、VPS 推荐
LAMP 简单易用、兼容性好 初学者、小型网站

根据需求选择即可。两者都能成功运行 WordPress。

如需自动化脚本,可使用 宝塔面板LNMP.org 一键包 简化部署。

未经允许不得转载:CLOUD技术博 » CentOS系统下如何通过LNMP或LAMP搭建WordPress?