Wordpress搭建教程(CentOS 8)

一、概述

操作系统采用CentOS 8,Web服务采用Nginx,数据库采用MySQL CE。以下教程只描述主要步骤,并假定你以有一定Linux、MySQL和百度使用基础。

二、安装MySQL Server

安装

1、安装MySQL Server

参考文章:《How To Install MySQL on CentOS 8

dnf install mysql-server

2、启动MySQL服务

systemctl start mysqld

3、设置为开机启动服务

systemctl enable mysqld

4、修改MySQL的root密码

mysql_secure_installation

安装引导修改密码。

5、创建WordPress用的数据库

连接MySQL:

mysql -u root -p

创建数据库,假定名字为test_com

create database test_com;

6、创建WordPress专用的MySQL用户

假定用户名为testuser,密码为testpassword

create user testuser@localhost identified by 'testpassword';

grant all on test_com.* to testuser@localhost;

alter user testuser@localhost identified with mysql_native_password by 'testpassword';

三、安装nginx

1、安装

dnf -y install nginx

2、运行

systemctl start nginx

浏览器访问服务器,看是否能打开nginx默认主页。

3、配置

编辑nginx默认配置文件/etc/nginx/nginx.conf,在Server{ }块里找到并修改以下字段:

设置默认首页为index.php

location / {
  root /usr/share/nginx/html;
  index index.php index.html index.htm;
}

设置上传文件的最大大小

client_max_body_size 10M;

修改php配置

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .php$ {
	root		/usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

解决wordpress设置固定链接后404问题

在某个server{}里添加/修改如下字段:

location / {    
    if (-f $request_filename/index.html){   
        rewrite (.*) $1/index.html break;   
    }   
    if (-f $request_filename/index.php){   
        rewrite (.*) $1/index.php;   
    }   
    if (!-f $request_filename){   
        rewrite (.*) /index.php;   
    }   
}   
  
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

4、下载Wordpress

wget https://cn.wordpress.org/wordpress-latest-zh_CN.zip -O /usr/share/nginx/html/wordpress.zip 

将压缩包解压:

unzip /usr/share/nginx/html/wordpress.zip

移动wordpress目录下的所有文件至网站根目录下:

mv /usr/share/nginx/html/wordpress/* /usr/share/nginx/html/

5、设置用户nginx对html目录的所有权限

chown -R nginx:nginx /usr/share/nginx/html/

6、重启nginx

systemctl restart nginx

7、设置为开机启动服务

systemctl enable nginx

四、安装PHP 7.4

CentOS官方源的PHP版本是5.4,远远不够,因此我们要使用第三方源(epel和Remi)。

参考:https://rpms.remirepo.net/wizard/

1、安装epel和Remi源

dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

dnf install yum-utils

2、安装PHP 7.4

dnf module reset php

dnf module install php:remi-7.4

dnf update

3、安装PHP扩展模块

dnf -y install php-cli php-common php-devel php-fpm php-mysqlnd php-gd php-opcache php-mbstring php-bcmath php-imagick

4、配置

编辑/etc/php-fpm.d/www.conf

user = nginx
group = nginx
listen = 127.0.0.1:9000

编辑/etc/php.ini,修改上传文件的最大限制

upload_max_filesize = 10M
post_max_size = 10M

5、运行

systemctl start php-fpm

重启一下nginx:systemctl restart nginx

6、设置为开机启动服务

systemctl enable php-fpm

7、测试

创建一个简易php文件,测试PHP运行是否正常。

在网站根目录(/usr/share/nginx/html/)下创建1.php文件,加入以下代码并保存。

<?php phpinfo(); ?>

然后用浏览器访问1.php,如果出现PHP详细信息的表格,说明一切正常。

五、安装WordPress

访问站点,按提示操作即可,如http://192.168.1.100/index.php

如果在访问站点时,默认没有打开index.php而是打开index.html文件,则删了index.html文件即可O(∩_∩)O

六、常见问题

在遇到运行错误时(例如HTTP ERROR 500),打开调试模式,可以打开WordPress的调试模式以显示错误信息。

编辑wp-config.php文件,修改 define(‘WP_DEBUG’, falase);define(‘WP_DEBUG’, true);

更新失败:因为我们不能复制一些文件,升级未被安装

有时更新插件时会遇到更新失败,提示“更新失败:因为我们不能复制一些文件,升级未被安装”。一般是由于权限设置错误引起的,当你用SSH或者FTP直接上传插件到Wordpress目录中,那么你上传的文件所属的所有组和所有人就是root或者ftp user。而如果你的php或者nginx的运行者不是这两者,就会出现这个错误。

既然如此,那么我们将网站目录的拥有者改为与nginx相同的用户即可。

例如,用ps aux|grep nginx查得nginx是以nginx用户运行的。

运行chown -R nginx:nginx /usr/share/nginx/html即可。

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注