使用WordPress建立个人博客



我的博客:https://www.swzdl.club
本文地址:https://www.swzdl.club/?p=70
博客上排版更好看


按照本文流程搭建WordPress个人博客之前,你需要:

  1. 拥有一台云服务器.阿里云腾讯云Vultr等平台购买,推荐阿里云和腾讯云.Vultr为国外的平台.另外阿里云和腾讯云都有学生服务器,如果你是24岁以下,那么可以以每个月10元左右的价格购买一台云服务器(具体价格以官网为准).
  2. 云服务器装Cent OS 7.6 64位系统(本文是以此为例,但是只要是Cent OS就可以.可能有一点区别,文中会有提示,无法解决可以在下方留言或者自己查找资料)
  3. 连接云服务器推荐工具:PuTTY.这个一定要有.
  4. 方便查看云服务器文件夹的推荐工具:WinSCP.这个可有可无,但是以后用起来很方便.
  5. 其余的只要照着文中输入命令回车就好了

1 更换软件源

首先备份 CentOS-Base.repo

sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

编辑CentOS-Base.repo

vi /etc/yum.repos.d/CentOS-Base.repo

将原内容替换为以下内容

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

2 配置LNMP环境

2.1 安装Nginx

2.1.1 安装Nginx:

获取Nginx软件源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装Nginx

 yum install nginx -y

注: CentOS 6不支持对IPV6的监听,这里用的是CentOS 7,
故不需要修改配置取消对IPV6的监听

2.1.2 安装完成后,使用nginx命令启动服务.

nginx

此时可以使用外网访问http://ip,看到Nginx默认界面

2.1.3 设置Nginx为开机自启动:

systemctl enable nginx

注: CentOS 6使用命令:chkconfig nginx on




2.2 安装MySQL

对于CentOs 7,直接执行yum install mysql命令是无效的,因为CentOS 7默认是Mariadb.
因此应当进行以下操作步骤

2.2.1 删除可用的mysql:

yum remove mysql

2.2.2 下载mysql的repo源:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2.2.3 安装mysql-community-release-el7-5.noarch.rpm包:

sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

注:安装这个包会获得两个mysql的yum repo源:
/etc/yum.repos.d/mysql-community.repo
/etc/yum.repos.d/mysql-community-source.repo

2.2.4 安装mysql:

    sudo yum install mysql-server -y

2.2.5 安装完成后查看mysql:

rpm -qa | grep mysql

2.2.6 设置开机自启动:

systemctl enable mysqld

注:Cent OS 6使用命令chkconfig mysqld on

2.2.7 设置MySQL的root账户密码:

/usr/bin/mysqladmin -u root password 'YourPwd'




2.3 安装PHP

2.3.1 执行命令:

    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2.3.2 安装PHP:

yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64

2.3.3 安装php-fpm:

yum install php70w-fpm php70w-opcache

2.3.4 启动php-fpm:

systemctl start php-fpm

2.3.5 设置开机自启:

systemctl enable php-fpm
systemctl daemon-reload




3 安装配置Wordpress

3.1 安装WordPress

3.1.1 执行命令:

yum install wordpress -y

注:安装完成后,就可以在/usr/share/wordpress 看到WordPress的源码




3.2 配置数据库:

3.2.1 登录数据库:

    mysql -uroot --password='YOURPWD'


3.2.2 创建名为wordpress的数据库:

CREATE DATABASE wordpress;

3.2.3 退出数据库:

exit;

3.2.4 将数据库配置同步到WordPress配置中:

cd /etc/wordpress/
ls
nano wp-config.php

找到其中关于数据库配置的部分,参考下图修改:

<?php
/**只要修改这里的数据库名称,账号,密码*/
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', 'YOURPWD');

/**以下部分不用修改*/
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');


define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

$table_prefix  = 'wp_';
define('WP_DEBUG', false);
if ( !defined('ABSPATH') )
    define('ABSPATH', '/usr/share/wordpress');
require_once(ABSPATH . 'wp-settings.php');

如图所示:




3.3 配置Nginx

3.3.1 WordPress 已经安装完毕,接着配置 Nginx 把请求转发给 PHP-FPM 来处理

nano /etc/nginx/nginx.conf

3.3.2 修改其中server中的部分:

 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        将原来root后的地址改为WordPress的地址
        #root /usr/share/nginx/html;
        root /usr/share/wordpress;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        添加location中的内容:
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php index.php;
        }

#添加如下代码:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    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;
    }



        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

如下图所示:

3.3.3 重新载入nginx:

nginx -s reload

注:此时访问http://YourIP,就可以进入WordPress的设置页面

如图所示:




4 其他功能的实现


4.1 删除一些不必要的组件:

如图所示,删除其中的:"文章RSS""评论RSS"、"WordPress.org":

编辑footer.php
nano wp-content/themes/twentyeleven/footer.php;

/**删除以下内容或者修改为自己想要的文字*/
<a href="<?php echo esc_url( __( 'http://swzdl.club/', 'twentyseventeen' ) ); ?>" class="imprint">
    <?php printf( __( 'Copyright © 2018<script>new Date().getFullYear()>2018&&document.write("-"+new Date().getFullYear());</script>-2019 Mr.Sheng. All Rights Reserved.' ), 'WordPress' ); ?>
</a>

编辑class-wp-widget-meta.php

 nano wp-include/widgets/class-wp-widget-meta.php


/**删除以下内容*/
<li><a href="<?php echo esc_url( get_bloginfo( 'rss2_url' ) ); ?>"><?php _e( 'Entries <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>
<li><a href="<?php echo esc_url( get_bloginfo( 'comments_rss2_url' ) ); ?>"><?php _e( 'Comments <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>

去除WordPress.org

/**删除以下内容*/
            /**
             * Filters the "Powered by WordPress" text in the Meta widget.
             *
             * @since 3.6.0
             * @since 4.9.0 Added the `$instance` parameter.
             *
             * @param string $title_text Default title text for the WordPress.org link.
             * @param array  $instance   Array of settings for the current widget.
             */

echo apply_filters(
            'widget_meta_poweredby',
            sprintf(
                '<li><a href="%s" title="%s">%s</a></li>',
                esc_url( __( 'https://wordpress.org/' ) ),
                esc_attr__( 'Powered by WordPress, state-of-the-art semantic personal publishing platform.' ),
                _x( 'WordPress.org', 'meta widget link text' )
            ),
            $instance
        );




4.2 实现发邮件功能:插件Easy WP SMTP

配置如下图:



4.3 新用户注册或者用户密码找回时,点击链接,提示链接无效

原因是邮箱收到邮件后,会将密码重置链接地址及其前后的“<>”一起当成链接地址生成超链接,点击此超链接后,由于传给wordpress的参数不对(多了个"<>"),例如把鼠标移到下图的红色框的连接上,并看到浏览器左下角的URL提示连接,会发现多了“<>”,所以wordpress提示密码重设链接无效。

解决方法:

4.3.1 解决找回密码时提示“您的密码重设链接无效”:

打开WP根目录下的 wp-login.php,找到如下代码(390行左右):
$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

修改为

  :`$message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "\r\n";


4.3.2 解决新用户注册时,点击邮件中的重置密码链接提示“您的密码重设链接无效”:

打开WP安装目录下的/wp-includes/pluggable.php,找到如下代码(1981行左右):
 $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n";

修改为:

  $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";


注:每次升级WordPress都要重新修改




4.4 部署Nginx证书,实现https访问:

证书申请请自行解决

4.4.1 修改Nginx配置:

nano /etc/nginx/nginx.conf

4.4.2 对其中两个server配置如下:

    server {
         listen       80 default_server;
        listen       [::]:80 default_server;
#填入你的域名
        server_name  YourDomainName.com;
        rewrite ^(.*)$ https://$host$1 permanent;
#替换目录
         root /usr/share/wordpress;
        #root /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
#添加以下两行
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php index.php;
        }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#!!!add this 'location'!!!
   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;
    }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


# Settings for a TLS enabled server.
    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  swzdl.club;
        root /usr/share/wordpress;
#写你存放证书的目录
        ssl_certificate "/etc/pki/nginx/example.crt";
        ssl_certificate_key "/etc/pki/nginx/example.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            index index.php index.html index.htm;
            try_files $uri $uri/ /index.php index.php;
       }

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       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;
    }

    error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
  }

注:上传到/etc/pki/nginx/目录中,如果无此目录则新建,权限为777

4.4.3 而后重启nginx

systemctl stop nginx.service
nginx


4.4.4 如果此时报错:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

如图所示:

3.4.5 则查看端口,杀掉占用的进程:

netstat -ntpl

如图所示:

发现php-fpm正在运行且占用了80端口,暂时关闭php-fpm,待启动nginx后再启动:systemctl stop php-fpm.service,然后kill其他占用80端口的服务:

3.4.6 再次使用nginx命令启动Nginx;启动php-fpm:

4.5 其他

  • 查看开机自启项: systemctl list-unit-files

  • 仅看允许的开机自启项:systemctl list-unit-files | grep enable

  • WordPress配置文件路径:/etc/wordpress/

  • WordPress资源路径:/usr/share/wordpress/

  • 插件商店:https://cn.wordpress.org/plugins/

  • 插件上传文件夹:/usr/share/wordpress/wp-content/plugins

  • 主题商店:https://wordpress.org/themes/

  • 主题上传文件夹:/usr/share/wordpress/wp-content/themes

  • 设置中文的方法:
    进入WordPress官方网址:https://cn.wordpress.org/download/releases/
    下载对应版本的语言包,解压.将解压得到的文件夹中wp-content中的langage文件夹上传到服务器中的/usr/share/wordpress/wp-content文件夹内,而后在WordPress后台界面设置中将语言改为简体中文,刷新即可

最后放一下我的博客首页的截图:


我的博客首页_0
我的博客首页_1

可以访问网址:https://www.swzdl.club查看我的博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279