如何在 centos7 上安装 WordPress 所需要的环境 LEMP 参考上一篇文章
在centos7上安装 LEMP

安装 WordPress

  1. 下载并解压 WordPress 源码
  2. 为 WordPress 创建数据库
  3. 将数据库信息配置到 WordPress 的 配置文件中
  4. 将 WordPress 放到指定目录(建议 放到 /var/www目录下 ),并配置 nginx
  5. 访问 wp-admin/install.php 启动安装程序

下载并解压 WordPress

wget http://wordpress.org/latest.tar.gz # 下载
tar -xzvf latest.tar.gz # 解压

创建 WordPress 的 database

# 使用 root 用户登录 数据库
mysql -u root -p

# 创建一个名为 wordpress 的数据库
CREATE DATABASE wordpress;

# 创建一个 用户并授权 wordpress 的权限
# wordpressusername: 要创建的用户名
# password : 密码
# hostname : 数据库地址 本机为 localhost
GRANT ALL PRIVILEGES ON wordpress.* TO "wordpressusername"@"hostname" IDENTIFIED BY "password";

# 立刻生效
FLUSH PRIVILEGES;

# 退出数据库
EXIT

配置 wp-config.php

回到 wordpress 目录下,将 wp-config-sample.php copy一份为 wp-config.php

cp wp-config-sample.php wp-config.php

编辑 wp-config.php 文件 将上一步创建的数据库信息填写进去

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress'); // 上一步创建的 数据库 名称 此处为 wordpress

/** MySQL database username */
define('DB_USER', 'wordpress'); // 上一步创建的数据库 用户名 此处为 wordpress

/** MySQL database password */
define('DB_PASSWORD', 'your_password'); // 上一步创建数据库用户时指定的 密码

/** MySQL hostname */
define('DB_HOST', 'localhost'); // 数据库地址 一般为 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. 相关值 可以直接 访问 https://api.wordpress.org/secret-key/1.1/salt/ 将生成的内容 copy 到文件对应位置即可。

/**#@+
* 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 放到指定位置

将 wordpress 文件夹 copy 到你想放置的位置 如 /usr/share/nginx/wordprss

cp -r wordpress /usr/share/nginx/

配置 nginx

server {
listen 80;
server_name server_domain_name_or_IP;

root /usr/share/nginx/wordpress; # 网站目录
index index.html index.htm index.php;

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

location ~ \.php$ {
try_files $uri =404;

include fastcgi.conf;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # 视情况而定
}
}

修改 wordpress 文件权限

chown nginx:nginx wordpress # 修改 wordpress 文件夹所属者

cd wordpress # 进入网站根目录

find -type d -exec chmod 0755 {} \; # 将此目录下所有文件夹的权限设置为755

find -not -type d -exec chmod 644 {} \; # 将此目录下所有文件的权限设置为644

## centos 7 由于 SELinux 的问题 可能会导致 文件权限问题
## 参考 https://wordpress.stackexchange.com/questions/302778/installation-failed-could-not-create-directory-centos-7

使用 nginx -t 检查配置文件配置是否有问题。

然后重启 nginx 使修改生效。

访问 wp-admin/install.php 启动安装程序

按照提示完成安装即可。


参考链接

  • https://codex.wordpress.org/Installing_WordPress
  • http://blog.jobbole.com/50121/