Centos7上安装wordpress

1. 前言

当前环境说明:

OS: Centos 7.9

2. 安装要求

安装WordPress之前,服务器要求具备两个基本元素:

  • PHP

    版本: 7.4 或更高版本

  • MySQL数据库:

    版本: MySQL 5.7 or MariaDB version 10.3

    用以存储所有BuddyPress数据 如果没有已创建的数据库,可以创建一个用户具有所有优先权(比如说更新、插入、删除、选择)的数据库。 记住之后系统提供的用户名/密码以及数据库名称。

3. 安装 PHP

为什么要安装php73

在不更新yum源的情况下, centos7上能安装的php为5.4版本, 目前已经与最新版本的wordpress(目前是6.2.2版本)不兼容, 是用旧版本会遇到php文件无法正确解析的情况, 所以需要安装新版的php, 经测试php73是一个被较好兼容的版本.

安装 REMI yum源

1
2
3
4
5
# CentOS 7
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# CentOS 6
yum install -y http://rpms.remirepo.net/enterprise/remi-release-6.rpm

安装PHP软件

1
yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll

查看php是否安装成功

1
2
3
4
5
$php73 -v
PHP 7.3.33 (cli) (built: Feb 14 2023 14:26:12) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.33, Copyright (c) 1999-2018, by Zend Technologies

配置php73-php-fpm

修改run-as用户为nginx与nginx web server保持一致避免后面upload文件出现权限问题.

vi /etc/opt/remi/php73/php-fpm.d/www.conf

1
2
3
4
5
6
7
8
9

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

设置开机启动、运行服务

1
2
3
4

systemctl enable php73-php-fpm
systemctl start php73-php-fpm

4. 安装mariaDB

在centos上安装mariaDB可以参考鹏叔的技术博客

安装完成后创建一个新的wordpress数据库

1
2
3

CREATE DATABASE wordpress

5. 下载wordpress

下载

1
2
cd /tmp
wget https://wordpress.org/latest.zip

解压缩

1
2
3

unzip -d /var/www /tmp/latest.zip

并将/var/www/wordpress的owner修改为nginx

1
chown -R nginx:nginx /var/www/wordpress

6. 安装nginx

安装nginx可以参考我的博客nginx安装教程

7. 配置nginx

修改 /etc/nginx/conf.d/default.conf, 新建一个server或在已有server基础上修改如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

server {
listen 80;
server_name localhost;

root /var/www/wordpress;

location / {
index index.php index.html index.htm;
}

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;
}
}

nginx与php的协作机制以及nginx参数的详细讲解, 请参考Nginx和PHP的配置

使配置生效:

1
2
3
4
5
6
7

nginx -s reload

# 或者 重启nginx

systemctl restart nginx

如果一切顺利的话, 重启后即可打开浏览器访问wordpress网站了

如果出现问题, 可以查看nginx的访问日志和错误日志以及php-fpm的相关日志

nginx的日志位于 /var/login/nginx

php-fpm的日志位于/var/log/php-fpm

8. 相关文章

更多linux相关知识, 请参考鹏叔的技术博客 - linux, 获取实时更新的博客文章.

9. 参考文档

BuddyPress安装指南

建站教程(三):在Ubuntu上配置Nginx+MySQL+PHP7

如何在 CentOS 7上安装使用 Nginx 的 WordPress

Nginx和PHP的配置

CentOS7安装和配置ftp服务,ftp命令详解