前言
博客终于搭建起来了, 这是我的第一篇文章, 就不聊为什么要创建博客了, 先记录一下搭建wordpress的操作, 正好给大家分享如何利用服务器搭建一个自己的博客系统
正文
### 服务器信息 ###
服务器: 阿里云服务器 (1核2G的学生机🙈)
操作系统: CentOS Linux release 7.7.1908 (Core)
如果希望用最新版本可以使用以下命令更新下yum里的包,再进行后续操作
yum -y update
当使用不同软件版本时,可能需要根据实际情况调整命令和参数配置
操作步骤
1. 搭建 LNMP 环境
2. 安装 WordPress
3. 配置域名访问(可选)
步骤一:搭建 LNMP 环境(linux、nginx、mysql、php)
1. 安装 Nginx
运行命令安装 nginx
yum -y install nginx
运行命令查看nginx版本
nginx -v
如果出现以下结果,表示安装成功
nginx version: nginx/1.16.1
2. 安装 MySQL
运行命令更新 yum 源
rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7-2.noarch.rpm
运行命令安装 mysql
yum -y install mysql-community-server
运行命令查看 mysql 版本,注意是大 V 哦
mysql -V
如果出现以下结果表示安装成功
mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
3. 安装 PHP
运行命令安装 php72, 这里的72是版本,最好安装新版的,不然安装 wordpress 会提示版本过低
sudo yum install php72
运行命令安装 php72-php-fpm、php72-php-mysqlnd
sudo yum install php72-php-fpm php72-php-mysqlnd
运行命令查看 php 版本
php72 -v
如果出现以下结果表示安装成功
PHP 7.2.28 (cli) (built: Feb 18 2020 11:21:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.28, Copyright (c) 1999-2018, by Zend Technologies
4. 配置 Nginx 环境
运行命令备份 Nginx 配置文件, 以免配置出错,启动不了 nginx 服务,还可以恢复
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
修改Nginx配置文件,添加Nginx对PHP的支持。否则会出现页面打不开的情况
vim /etc/nginx/nginx.conf
按 i 进入编辑模式, 修改以下配置信息
# 除下面提及的需要添加的配置信息外,其他配置保持默认值即可。
location / {
# 在location大括号内添加以下信息,配置网站被访问时的默认首页
index index.php index.html index.htm;
}
# 添加下列信息,配置 Nginx 通过 fastcgi 方式处理 PHP 请求
location ~ .php$ {
root /usr/share/nginx/html; # 将 /usr/share/nginx/html 替换网站根目录,使用默认路径即可
fastcgi_pass 127.0.0.1:9000; # Nginx通过本机的 9000 端口将 PHP 请求转发给 php-fpm 进行处理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; # Nginx 调用 fastcgi 接口处理 PHP 请求
}
按下 Esc 键后,输入 :wq 并回车以保存并关闭配置文件
5. 配置 MySQL 环境 (版本 8.0)
运行命令启动 mysql 服务
systemctl start mysqld
设置 mysql 开机自启动
systemctl enable mysqld
运行命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码, 重置root用户密码时,会使用该初始密码
grep 'temporary password' /var/log/mysqld.log
结果如下:
2019-12-17T06:36:31.467798Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ?py+cwe4qziX
运行以下命令配置MySQL的安全性
mysql_secure_installation
重置root账号密码
Enter password for user root: # 输入上一步获取的root用户初始密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? (Press y|Y for Yes, any other key for No) : Y # 是否更改root用户密码,输入 Y
New password: # 输入新密码,长度为 8 至 30 个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: # 再次输入新密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
输入 Y 删除匿名用户账号
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
Success.
输入 Y 禁止 root 账号远程登录。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.
输入Y删除test库以及对test库的访问权限
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # 是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.
输入Y重新加载授权表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!
6.配置 PHP 环境
运行命令设置 php-fpm 开机自启
sudo systemctl enable php72-php-fpm.service
运行命令启动 php-fpm 服务
# 开启服务
$ sudo systemctl start php72-php-fpm.service
其他 php-fpm 服务相关命令
# 停止服务
$ sudo systemctl stop php72-php-fpm.service
# 查看状态
$ sudo systemctl status php72-php-fpm.service
运行命令查看 nginx 服务的用户和用户组
egrep '^(user|group)' /etc/nginx/nginx.conf
出现以下运行结果
user nginx;
更改 php-fpm 下的 www.conf 配置,一般默认位置: “/etc/opt/remi/php72/php-fpm.d/www.conf”, 修改执行 php-fpm 的权限
sudo vim /etc/opt/remi/php72/php-fpm.d/www.conf
按 i 进入编辑模式,修改用户、用户组为 nginx
user = nginx
group = nginx
按 Esc 键后,输入 :wq 并回车以保存并关闭配置文件
新建 php 文件,用于展示 php 信息(测试完记得删除这个文件哦 ~,我会再提醒你的)
vim /usr/share/nginx/html/phpinfo.php
按 i 进入编辑模式,写入以下内容
<?php echo phpinfo(); ?>
按Esc键后,输入:wq并回车以保存并关闭配置文件。
运行命令重启 php-fpm 服务
sudo systemctl restart php72-php-fpm.service
7. 测试访问 LNMP 平台
- 打开浏览器
- http://<公网IP地址>/phpinfo.php
返回结果如下图所示,表示LNMP环境部署成功。你已经成功一大半了,加油别放弃!
(再次提醒:如果成功了,记得删除 phpinfo.php 的文件哦,为了安全考虑 使用命令rm -rf /usr/share/nginx/html/phpinfo.php
进行删除,不要删错了哦🤪)
如果出现 404, 或者直接下载文件的情况,说明你的 nginx 文件配置的不对哦!
步骤二:安装 WordPress
看到这是不是很累了,已经快好了,安装 WordPress
1. 新建 WordPress 数据库,并为此数据库配置用户
运行命令进入MySQL
mysql -u root -p
输入root用户的密码进入数据库,创建 wordpress 数据库
create database wordpress;
为wordpress创建一个新用户(mysql 8.0 以上,如果是低版本,可以查阅相关的命令创建用户)密码需要是数字,字母,字符串三种组合
create user 'blog'@'localhost' identified by 'blog1234!';
修改加密规则( 这里一定要注意注意再注意!!!如果不修改加密规则,即使你 WordPress 的配置信息是正确的,也会导致 WordPress 连接不上数据库,至少目前 WordPress 版本是这样的,不知以后会不会修复,在此记录大坑)
alter user 'blog'@'localhost' identified with mysql_native_password by 'blog1234!';
为用户添加 wordpress数据库访问权限
grant all privileges on wordpress.* to 'blog'@'localhost';
刷新权限,使配置生效
flush privileges;
退出 mysql
exit;
ok, 到这你可以试试用数据库连接工具,看是否能够连接上,如果不能成功连接根据错误代码查看其原因
2. 下载 WordPress
使用 wget 下载中文版, 也可以使用 yum 方式安装英文版的,如果不知道版本信息,可以进入WordPress中文官网查看最新版本的 WordPress,也可以直接下载 tar.gz 包到本机再上传到服务器上,建议下载到路径 /usr/share/nginx/html/ 下
wget https://cn.wordpress.org/wordpress-<版本信息>.tar.gz
我下载的是 5.3.2 版本的
wget https://cn.wordpress.org/wordpress-5.3.2-zh_CN.tar.gz
运行命令解压下载的文件包
tar zxvf wordpress-5.3.2-zh_CN.tar.gz
3. 修改WordPress配置文件
将 WordPress 安装目录下的 wp-config-sample.php 文件复制到 wp-config.php 文件中,并将 wp-config-sample.php 文件作为备份
cd /usr/share/nginx/html/wordpress # 进入安装目录
cp wp-config-sample.php wp-config.php # 拷贝文件
编辑wp-config.php文件。
vim wp-config.php
按 i 键切换至编辑模式,根据配置完成的wordpress数据库信息,修改 MySQL 相关配置信息,修改代码如下所示。
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'blog');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'blog1234!');
/** MySQL主机 */
define('DB_HOST', 'localhost'); // 这里 localhost 一般不需要改
修改完成后,按下Esc键后,输入:wq并回车以保存并关闭配置文件
4. 修改 nginx 配置文件
打开之前配置的 nginx.conf 文件,进行改写
vim /etc/nginx/nginx.conf
按 i 进入编辑模式,将 server 内容替换成以下内容:
server {
# 默认 80 端口
listen 80;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# 根目录地址
root /usr/share/nginx/html/wordpress;
location / {
index index.html index.php;
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;
}
}
# 支持 php 方式, 如果没有的话,会打不开网页的
location ~ \.php$ {
root /usr/share/nginx/html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
修改完成后,按下Esc键后,输入:wq并回车以保存并关闭配置文件
运行命令重启 nginx 服务,全配置生效
systemctl restart nginx
如果重启报错了,多半是你配置错了哦,或者端口被占用,排查下问题吧
重启成功后用本地浏览器打开网址:http://公网IP/wp-admin/install.php 进入安装页面,如果你能顺利打开安装了,说明你的努力没有白费,如果遇到打开网址出现,数据库连接不上的情况,别着急
-
请检查 WordPress 的配置信息
-
请检查数据库是否运行
-
请检查是不是没有更改数据库的加密规则
如果都是正确的还是连接不上怎么办,一个大佬的办法,写个测试文件测试一下喽!还记得之前创建的 phpinfo.php 这个文件吗,已经删除了?那就按那个方法再建一个 test.php 文件,只不过文件的内容不再是<?php echo phpinfo(); ?>
这个了,写入以下测试数据库信息代码:
<?php
$mysqli = new mysqli("127.0.0.1", "用户名", "密码", "wordpress", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} echo $mysqli->host_info . "\n"; $mysqli->close();
?>
再次访问http://公网IP/wp-admin/install.php 试试,看下返回的具体报错,相信你很快就能解决
好了,WordPress 博客系统就安装好了,尽情折腾吧,什么?IP 地址不好看,要域名?那就下往下看喽
步骤三:添加域名访问(可选)
前提需要一个域名,且已经备好案的,申请好SSL证书,已经有很多种申请免费域名SSL证书的方式,这里就简单说一下 nginx 配置吧
1. 下载 SSL 证书
如果你申请到 SSL 证书,就下载下来上传到服务器里,建议存放路径为:/etc/pki/nginx/ 以后所有证书都可以放这里
2. 配置 nginx
这里可以配置两个 server 了,一个是 80 端口的,一个是 443 端口,当访问到 80 端口会自动代理到 443 端口。(最好不要将 80 商品和 443 端口放在一个 server 里混合使用)
修改之前配置好的 server 修改为:
server {
listen 80;
server_name xxx.cn www.xxx.cn;
rewrite ^(.*)$ https://$host$1 permanent;
}
将 xxx.cn www.xxx.cn 改为你的域名哦,再添加一个 server 内容为:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name xxx.cn www.xxx.cn;
root /usr/share/nginx/html/wordpress;
ssl_certificate "/etc/pki/nginx/xxx.cn.pem"; # 你的证书文件
ssl_certificate_key "/etc/pki/nginx/xxx.cn.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.html index.php;
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;
}
}
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;
}
}
好了,重启下 nginx 服务吧,命令还记得吗🤨
systemctl restart nginx
先用你之前的IP地址打开后台,更改下站点的域名哦,再使用你的域名进行访问。或者进入服务器使用 mysql 的语句进行更改:
update wp_options set option_value = replace(option_value, 'https://实例公网IP/', 'https://你的域名/') where option_name = 'home' OR option_name = 'siteurl';
注意:在调试 nginx 的时候,最好把浏览器的禁用缓存给勾上,否则刷新之后感觉还是之前的配置
好了,就写到这了,有什么意见和建议欢迎大家留言,有什么不对的还请各位见谅!!