服务器搭建WordPress个人博客网站

2020-06-09 14:22:23 浏览数 (1)

WordPress 是世界上使用最广泛的博客系统之一,是一款开源的PHP软件。有丰富的插件模板资源,使用WordPress可以快速搭建独立的博客网站。

本教程软件环境基于CentOS 6.8 64位,从配置LNMP环境开始一步步搭建属于你自己的WordPress博客网站。

一. 配置LNMP环境

LNMP是Linux、Nginx、MySQL和PHP的缩写,是WordPress博客系统依赖的基础环境,我们首先需要准备LNMP环境。

1. 安装Nginx

使用yum安装Nginx:

代码语言:javascript复制
yum install nginx -y

2.修改Nginx默认配置

去除对IPv6的监听,因为CentOS 6不支持IPv6,需要取消对IPv6地址的监听,否则Nginx不能成功启动。

a) 创建default.conf配置文件

代码语言:javascript复制
touch /etc/nginx/conf.d/default.conf

b) 编辑配置文件

代码语言:javascript复制
vi /etc/nginx/conf.d/default.conf

c) 配置文件示例代码

代码语言:javascript复制
server {
    listen       80 default_server;
    # listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

d) 修改完成后启动Nginx

代码语言:javascript复制
nginx

e) 这篇教程中我的IP地址为114.115.162.204,浏览器访问该IP地址,查看服务器Nginx是否安装成功,

如果显示以上界面即表示Nginx已经安装成功。 f) 将Nginx设为开机自动启动:

代码语言:javascript复制
chkconfig nginx on

3.安装MySQL

a) 使用yum安装MySQL:

代码语言:javascript复制
yum install mysql-server -y

b) 安装完成后,启动MySQL服务:

代码语言:javascript复制
service  mysqld restart

c) 设置MySQL账户root的密码:

我们这里设置root账户的密码为‘abc12345678’,你可以设置其它密码,但需要把这里的密码记住,后面的步骤还需要使用。

代码语言:javascript复制
/usr/bin/mysqladmin -u root password 'abc12345678'

d) 将MySQL设置为开机自动启动

代码语言:javascript复制
chkconfig mysqld on

4.安装PHP

a) 使用yum安装PHP:

代码语言:javascript复制
yum install php-fpm php-mysql -y

b)安装完成后,启动PHP-FPM进程:

代码语言:javascript复制
service php-fpm start

c) 查看php-fpm进程监听那个端口

代码语言:javascript复制
netstat -nlpt | grep php-fpm

php-fpm默认监听9000端口

d) 将PHP-FPM设为开机自启动:

代码语言:javascript复制
chkconfig php-fpm on

以上我们的LNMP环境就配置好了!

二、安装配置WordPress

1.安装WordPress

配置好LNMP环境后,使用yum安装WordPress

代码语言:javascript复制
yum install wordpress -y

安装完成后,在目录usrsharewordpress目录下能看到WordPress的源码

2.配置数据库

a) 进入MySQL

代码语言:javascript复制
mysql -uroot --password='abc12345678'

此时会进去MySQL编辑界面

b)为WordPress创建一个数据库

代码语言:javascript复制
CREATE DATABASE wordpress;

c) 创建数据库完成,退出MySQL环境

代码语言:javascript复制
exit

d) 把数据库配置同步到WordPress配置文件中: 编辑WordPress配置文件:

代码语言:javascript复制
vi /etc/wordpress/wp-config.php

i进入编辑模式 参考配置文件如下:

代码语言:javascript复制
<?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', 'abc12345678');

/** 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');

3. 配置Nginx

WordPress已经安装好了,这时我们配置Nginx,把请求转发给PHP-FPM来处理 a) 备份Nginx默认配置文件

代码语言:javascript复制
cd /etc/nginx/conf.d/
mv default.conf default.conf.bak

b) 在Nginx配置文件中创建WordPress配置文件wordpress.conf

代码语言:javascript复制
touch /etc/nginx/conf.d/wordpress.conf
vi /etc/nginx/conf.d/wordpress.conf

示例代码:

代码语言:javascript复制
server {
    listen 80;
    root /usr/share/wordpress;
    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;
    }
}

c) 配置成功后,重新加载nginx

代码语言:javascript复制
nginx -s reload

d)浏览器打开相应IP查看是否成功

定义好站点名、管理员用户名和密码后,浏览器中再次打开该IP地址

这样,你的WordPress就搭建成功啦~

0 人点赞