也许博客已经不再流行,但是 wordpress 始终还有一大片的忠实用户。本实验带您从准备 lnmp 环境开始,一步步搭建起一个属于自己的 wordpress 博客。


1.准备 lnmp 环境

lnmp 是 linux、nginx、mysql 和 php 的缩写,是 wordpress 博客系统依赖的基础运行环境。我们先来准备 lnmp 环境

  • 安装 nginx

    使用 yum安装 nginx:

    yum install nginx -y

    修改 /etc/nginx/conf.d/default.conf,去除对 ipv6 地址的监听,可参考下面的示例:

示列代码

  • 修改完成后,启动 nginx:

    nginx

    此时,可访问实验机器外网 http 服务(http://119.29.219.137)来确认是否已经安装成功。

安装完成

  • 将 nginx 设置为开机自动启动:

    chkconfig nginx on

  • 安装 mysql

    使用 yum安装 mysql:

    yum install mysql-server -y

    安装完成后,启动 mysql 服务:

    service mysqld restart

启动完毕

  • 设置 mysql 账户 root 密码:

    /usr/bin/mysqladmin -u root password ‘mypas$word4word_press’

    将 mysql 设置为开机自动启动:

    chkconfig mysqld on

  • 安装 php

    使用 yum安装 php:

    yum install php-fpm php-mysql -y

    安装之后,启动 php-fpm 进程:

    service php-fpm start

    启动之后,可以使用下面的命令查看 php-fpm 进程监听哪个端口

    netstat -nlpt | grep php-fpm

    把 php-fpm 也设置成开机自动启动:

    chkconfig php-fpm on

2.安装并配置 wordpress

  • 安装 wordpress

    配置好 lnmp 环境后,继续使用yum来安装 wordpress:

    yum install wordpress -y

    安装完成后,就可以在 /usr/share/wordpress 看到 wordpress 的源代码了。

源码

  • 配置数据库

    进入 mysql:

    mysql -uroot –password=’mypas$word4word_press’

    为 wordpress 创建一个数据库:

    create database wordpress;

    mysql 部分设置完了,我们退出 mysql 环境:

    exit

    把上述的 db 配置同步到 wordpress 的配置文件中,可参考下面的配置:

示例代码:/etc/wordpress/wp-config.php

<?php/*** the base configuration for wordpress** the wp-config.php creation script uses this file during the* installation. you don’t have to use the web site, you can* copy this file to “wp-config.php” and fill in the values.** this file contains the following configurations:** * mysql settings* * secret keys* * database table prefix* * abspath** @link https://codex.wordpress.org/editing_wp-config.php** @package wordpress*/// ** mysql settings – you can get this info from your web host ** ///** the name of the database for wordpress */define(‘db_name’, ‘wordpress’);/** mysql database username */define(‘db_user’, ‘root’);/** mysql database password */define(‘db_password’, ‘mypas$word4word_press’);/** 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’, ”);/**#@+* authentication unique keys and salts.** change these to different unique phrases!* you can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ wordpress.org secret-key service}* you can change these at any point in time to invalidate all existing cookies. this will force all users to have to log in again.** @since 2.6.0*/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’);/**#@-*//*** wordpress database table prefix.** you can have multiple installations in one database if you give each* a unique prefix. only numbers, letters, and underscores please!*/$table_prefix = ‘wp_’;/*** see http://make.wordpress.org/core/2013/10/25/the-definitive-guide-to-disabling-auto-updates-in-wordpress-3-7*//* disable all file change, as rpm base installation are read-only */define(‘disallow_file_mods’, true);/* disable automatic updater, in case you want to allow above file_mods for plugins, themes, … */define(‘automatic_updater_disabled’, true);/* core update is always disabled, wp_auto_update_core value is ignore *//*** for developers: wordpress debugging mode.** change this to true to enable the display of notices during development.* it is strongly recommended that plugin and theme developers use wp_debug* in their development environments.** for information on other constants that can be used for debugging,* visit the codex.** @link https://codex.wordpress.org/debugging_in_wordpress*/define(‘wp_debug’, false);/* that’s all, stop editing! happy blogging. *//** absolute path to the wordpress directory. */if ( !defined(‘abspath’) ) define(‘abspath’, ‘/usr/share/wordpress’);/** sets up wordpress vars and included files. */require_once(abspath . ‘wp-settings.php’);

如果你上面的步骤没有使用教程创建的密码,请修改下面命令中的密码登录

  • 配置 nginx

    wordpress 已经安装完毕,我们配置 nginx 把请求转发给 php-fpm 来处理

    首先,重命名默认的配置文件:

    cd /etc/nginx/conf.d/mv default.conf defaut.conf.bak

    在 /etc/nginx/conf.d 创建 wordpress.conf 配置,参考下面的内容:

参考代码

  • 配置后,通知 nginx 进程重新加载:

    nginx -s reload

3.准备域名和解析

  • 域名注册

    域名注册每篇关于linux系统的操作我都说了,可以参考网上资料,去搜索怎么注册域名,这里就不多说了。

  • 域名解析

    域名购买完成后, 需要将域名解析到云主机上,小桀的云主机的 ip 为:

    119.29.219.137

    域名设置解析后需要过一段时间才会生效,通过 ping命令检查域名是否生效 ,如:

    ping www.xjwlgf.com

    如果 ping 命令返回的信息中含有你设置的解析的 ip 地址,说明解析成功。


  • 大功告成!

    恭喜,您的 wordpress 博客已经部署完成,您可以通过浏览器访问博客查看效果。

    通过ip地址查看:

    博客访问地址:http://这里换成你的ip地址/wp-admin/install.php

完成

  • 通过域名查看:

    博客访问地址:http://www.xjwlgf.com/wp-admin/install.php,其中替换 www.xjwlgf.com为之前申请的域名。

好了,感谢大家耐心的看完,本次操作很简单,去上机编写一下吧!如果觉得本篇文章不错关注一下吧!