基于 nginx + mysql + php7.4 搭建 WordPress 站点

2,386 阅读3分钟

image.png

WordPress 概述

WordPress 是基于 PHP 和 MySQL 的免费开源内容管理系统(CMS)。 它是全球使用最广泛的CMS软件,截至2019年5月,它为排名前1000万个网站中提供了超过30%的支持,并拥有在使用 CMS构建的所有网站中,估计有60%的市场份额。

并且,WordPress 不止具备 CMS 功能,搭配 WordPress 生态插件,还能实现功能更加丰富的站点,例如 WordPress + WooCommerce 商城系统。

WordPress 始于 2003 年,最开始仅为一款简单的博客系统,但现已发展成为具有数千款插件,小工具和主题功能完整的CMS系统。它是根据开源协议通用公共许可证(GPLv2 或更高版本)进行授权。

一句话描述:WordPress 是一个开源建站工具。

中文官网:cn.wordpress.org/

自己动手搭建一个

WordPress 基于 PHP 和 MySQL 开发,所以搭建一个自己的站点大致分为 2 步:

  1. 基础环境部署
  2. WordPress 下载及配置

本文侧重实际操作,对部分原理不做过多解释,目的是让大家能按照本文一步一步顺利将站点搭建起来。

基础环境部署

环境:CentOS Linux release 7.6.1810 (Core)

目录:用户目录 ~/

基础组件:

  1. MySQL5.7 安装
  2. PHP7.4 安装
  3. Nginx 安装

mysql5.7 安装

下载

wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安装

rpm -ivh mysql57-community-release-el7-8.noarch.rpm
cd /etc/yum.repos.d/
yum -y install mysql-server

启动

systemctl start mysqld

找出临时密码

grep 'temporary password' /var/log/mysqld.log

WX20210719-160730@2x.png

使用临时密码登录数据库

mysql -uroot -p临时密码

修改 MySQL 的密码最短长度,方便我们设置新的密码

set global validate_password_length=5;

修改 MySQL 密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '你的数据库密码';

退出数据库

quit;

关闭系统防火墙

systemctl disable firewalld

使用你刚刚设置的新密码登录数据库

mysql -uroot -p你的数据库密码

为 wordpress 创建一个库

mysql> CREATE DATABASE wordpress;

// 出现如下内容代表建库成功
Query OK, 1 row affected (0.12 sec);

允许数据库被远程访问

mysql -uroot -p你的数据库密码
use mysql;
update user set Host='%' where User='root';
flush privileges;

php7.4 安装

使用 yum 安装 php7.4,该过程耗时稍长,因为会下载很多包,遇到下载提示,一律 yes

yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install yum-utilsyum-config-manager --enable remi-php74yum update
yum install php php-cli
yum install php  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json

检查是否安装成功

php -v

启动 php

systemctl start php-fpm

nginx 安装

yum -y install nginx

启动 Nginx 服务

systemctl start nginx

配置 Nginx

打开 /etc/nginx/nginx.conf 进行编辑,将 nginx.conf 中的一个 server 节点替换成如下内容,主要改一下 server_name 网站域名 即可

vi /etc/nginx/nginx.conf

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}

server {
        ## Your website name goes here.
        server_name 网站域名;
        ## Your only path reference.
        root /etc/nginx/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ .php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

下载及配置 WordPress

  1. 下载 WordPress 包
  2. 配置 wp-config.php 文件
  3. 设置 apache 用户组权限

下载 WordPress 包

先在 /etc/nginx/ 路径下创建一个 www 目录

cd /etc/nginx
mkdir www

下载 WordPress 包,版本自行选择

cd /etc/nginx/www

// 中文版
wget https://cn.wordpress.org/latest-zh_CN.zip

// 英文版
wget https://wordpress.org/latest.zip

// 解压
unzip latest-zh_CN.zip

配置 wp-config.php 文件

/etc/nginx/www/wordpress 目录下找到 wp-config-example.php 文件

// 重命名
mv wp-config-example.php wp-config.php

// 配置
vi wp-config.php

// 主要修改如下几个配置即可
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', '数据库用户名' );
define( 'DB_PASSWORD', '数据库密码' );
define( 'DB_HOST', 'localhost' );

// 另外再加上如下配置
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

授权 apache 用户组对 wordpress 目录的权限

chown -R apache:apache /etc/nginx/www/wordpress

到这里,我们的 wordpress 站点已经搭建完成了,可以试试访问你服务器的 IP 或者解已析好的域名。

image.png

image.png

成功访问到后台界面代表我们搭建成功!