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

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


一、准备工作

1. 更新系统

sudo yum update -y

2. 安装 EPEL 源(推荐)

sudo yum install epel-release -y

二、LAMP 方式搭建 WordPress(Apache)

步骤 1:安装 Apache

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

步骤 2:安装 MariaDB(MySQL 替代品)

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

运行安全配置脚本:

sudo mysql_secure_installation

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

步骤 3:安装 PHP 及相关扩展

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

注:CentOS 7 默认使用 PHP 5.4 或 7.x,如需更高版本,可添加 Remi 源:

# 添加 Remi 源(以 PHP 7.4 为例)
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
sudo yum-config-manager --enable remi-php74
sudo yum install php php-mysql php-gd php-xml php-mbstring php-json -y

重启 Apache:

sudo systemctl restart httpd

步骤 4:创建数据库和用户

登录 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;

步骤 5:下载并配置 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/

步骤 6:配置 WordPress

进入网站目录:

cd /var/www/html
cp wp-config-sample.php wp-config.php
vim wp-config.php

修改以下内容:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');

步骤 7:配置防火墙(如果开启)

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

步骤 8:访问 WordPress 安装向导

打开浏览器访问:

http://你的服务器IP地址

按照提示完成安装。


三、LNMP 方式搭建 WordPress(Nginx + MySQL + PHP-FPM)

步骤 1:安装 Nginx

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

步骤 2:安装 MariaDB(同上)

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

步骤 3:安装 PHP-FPM 和扩展

sudo yum install php-fpm php-mysql php-gd php-xml php-mbstring php-json -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 start php-fpm
sudo systemctl enable php-fpm

步骤 4:创建站点目录和下载 WordPress

sudo mkdir -p /var/www/html/wordpress
sudo chown -R nginx:nginx /var/www/html

下载 WordPress:

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

步骤 5:创建数据库(同 LAMP)

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

步骤 6:配置 Nginx 虚拟主机

创建配置文件:

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

写入以下内容(根据实际情况修改 server_name):

server {
    listen 80;
    server_name your_domain_or_ip;

    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 restart nginx

步骤 7:设置权限

sudo chown -R nginx:nginx /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress

步骤 8:配置 wp-config.php(同 LAMP)

cd /var/www/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php

填入数据库信息。

步骤 9:开放防火墙端口

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

步骤 10:访问安装页面

浏览器访问:

http://你的服务器IP地址/wordpress

或直接:

http://你的IP地址

如果将文件放在根目录。


四、常见问题解决

  1. 上传文件大小限制
    修改 /etc/php.ini

    upload_max_filesize = 64M
    post_max_size = 64M

    重启服务(Apache 或 PHP-FPM)。

  2. WordPress 提示需要 FTP
    设置目录权限:

    sudo chown -R nginx:nginx /var/www/html/wordpress
  3. Nginx 显示 403 错误
    检查 index.php 是否存在,nginx 用户是否有读取权限。


总结

方案 优点 推荐场景
LAMP (Apache) 配置简单,兼容性好 初学者、小项目
LNMP (Nginx) 高性能、低资源占用 高并发、生产环境

选择哪种方案取决于你的需求。对于新手,建议从 LAMP 开始;追求性能可用 LNMP


✅ 完成以上步骤后,你就可以成功在 CentOS 上部署 WordPress 站点了!

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