Wordpress搭建教程

一、概述

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

二、安装MySQL

安装

1、下载安装MySQL官方的Yum源

yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

2、安装MySQL

yum install mysql-community-server

3、启动MySQL服务

systemctl start mysqld

4、设置为开机启动服务

systemctl enable mysqld

5、此时MySQL已经开始正常运行,需要找出它的root账户的临时密码

grep "password" /var/log/mysqld.log

6、修改MySQL的root密码

登录MySQL

mysql -u root -p

这里会遇到一个问题,新密码设置过于简单会报错,可通过如下命令修改密码规则。

set global validate_password.policy=0;

set global validate_password.length=1;

修改密码:

ALTER USER root@localhost IDENTIFIED BY 'sqlrootpassword';

创建WordPress用的数据库

create database test_com;

创建WordPress专用的MySQL用户

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、设置yum源

CentOS的官方源并没有nginx,因此我们需要添加nginx官方提供的yum源。

/etc/yum.repos.d目录下创建repo文件:

vim /etc/yum.repos.d/nginx.repo

从官网http://nginx.org/en/linux_packages.html#stable 拷贝对应linux版本的yum源,这里使用稳定版(stable)。

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2、安装

yum -y install nginx

3、配置

编辑/etc/nginx/conf.d/default.conf,对照着下面操作:

设置默认首页为index.php

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

设置上传文件的最大大小

server {
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;

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

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

运行

systemctl start nginx

设置为开机启动服务

systemctl enable nginx

四、安装PHP

安装

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

1、安装epel源

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

2、安装Remi源

yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

3、启用PHP7.4的repo

编辑/etc/yum.repos.d/remi-php74.repo,将[remi-php74]区块下enabled=0改为enabled=1,保存退出。

4、安装PHP 7.4

yum -y install php

5、安装PHP扩展模块

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

配置

编辑/etc/php-fpm.d/www.conf,修改用户为nginx

user = nginx

group = nginx

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

upload_max_filesize = 10M

post_max_size = 10M

运行

systemctl start php-fpm

设置为开机启动服务

systemctl enable php-fpm

测试

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

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

<?php phpinfo(); ?>

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

五、安装WordPress

1、下载

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

2、解压缩

将压缩包解压:

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

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

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

3、安装

访问站点,按提示操作即可,如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用户运行的。

接着切换至网站wordpress的根目录下,然后运行chown -R nginx:nginx wp-content/plugins即可。

留下评论

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