0 前言

之所以会写这篇教程,是因为发生了黑客攻击我的博客并删除数据库的事件,导致我一怒之下直接重装了服务器的系统。。。

所以,我要首先对攻击我的服务器的黑客献上最崇高的敬意:

1 准备工作

为了搭建一个完全属于自己的个人博客,你只需要以下 3 样东西:

  • 自己有一台 VPS 或独立主机(提供 VPS 的服务商很多,例如搬瓦工DigitalOceanLinode等)
  • 在域名提供商注册一个域名(可以通过阿里云,便宜且简单快捷)
  • 好奇心和一点点动手能力

准备好了,我们就开始!

2 域名解析

简单的说,域名解析就是把你购买的域名绑定到你的 VPS 对应的 IP 地址上。

本教程以阿里云为例:

首先登录阿里云,进入”控制台”,进入菜单”域名”-“域名列表”。

这里列出了你持有的全部域名,找到你想要解析的域名,在右边的”操作”中选择”解析”。

进入解析设置界面,按照上图添加两条记录,”记录值”字段填写你的 VPS 的 IP 地址。

这样,域名解析就配置完成了。你可以使用自己的电脑 ping 一下刚才解析的域名,若正确无误,应该可以 ping 通。

3 搭建 LNMP 环境

LNMP 指的是一个基于 CentOS/Debian 编写的 Nginx、PHP、MySQL、phpMyAdmin、eAccelerator 一键安装包。可以在 VPS、独立主机上轻松的安装 LNMP 生产环境。

LNMP代表的就是:Linux + Nginx + MySQL + PHP 这种网站服务器架构。

实际上,在 LNMP 官网上也有相关的安装教程,推荐大家参考。

接下来的操作,都是要在 VPS 上完成。因此,我们需要通过 ssh 命令连接到服务器。

SSH(Secure Shell)即安全外壳协议,是目前较可靠、专为远程登录会话和其他网络服务提供安全性的协议。

  • 对于 macOS 和 Linux 用户,可以直接在终端内使用 ssh 命令。
    • 默认端口连接 ssh root@ip(ssh 默认端口为22)
    • 指定端口连接 ssh -p 端口号 root@ip
  • 对于 Windows 用户,需要使用别的方式,我推荐一个 Chrome App:Secure Shell,当然,你也可以安装别的 ssh 工具例如 PuTTY 等。

成功登录后,创建一个名为 “lnmp” 的 screen 会话:

screen -S lnmp

下载并解压,执行安装命令,进入安装配置界面:

wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp

选择 MySQL 数据库版本:

注意:MySQL 5.6,5.7 及 MariaDB 10 需要 1G 以上内存。

随后会设置 MySQL 的 root 密码,建议设置一个较强的密码并牢记。

接下来询问是否需要启用 MySQL InnoDB,InnoDB 引擎默认为开启,一般建议开启。

选择 PHP 版本:

注意:选择 PHP 7+ 版本时需要自行确认PHP版本是否与自己的程序兼容。

最后,选择是否安装内存优化:

这里做一个简单的科普。 TCMalloc 是 Google 开源的一个内存管理库, 作为 glibc malloc 的替代品。目前已经在 Chrome、Safari 等知名软件中运用; Jemalloc 是 facebook 推出的, 最早的时候是 freeBSD 的 libc malloc 实现,目前在 Firefox、Facebook 服务器各种组件中大量使用。

配置完成,回车,耐心等待安装完成。安装时间一般为几十分钟到几个小时。

安装完成后,显示 Install lnmp V1.5 completed! enjoy it. ,表示安装成功。

4 添加虚拟主机

LNMP 安装完成后,就可以添加虚拟主机了,执行:

lnmp vhost add

提示输入域名,输入你的域名:

回车,提示添加更多域名

注:带 www 和不带 www 的是不同的域名,如需带 www 和不带的 www 的域名都访问同一个网站,需要同时都绑定。

接下来,设置网站目录,这里我们可以直接回车采用默认目录

询问伪静态和是否启用日志,我这里全选了no

接下来,会询问是否添加数据库和数据库用户,输入 y 。输入之前设置的数据库root密码,然后,提示输入数据库名称,输入要创建的数据库名称,确认。同时,会创建一个和数据库同名的数据库用户,设置数据库用户密码,回车确认。

最后会询问是否添加 SSL 证书,推荐添加。然后选择 2,可以为你自动申请一个 SSL 证书。

5. 安装 WordPress

首先进入添加的虚拟主机目录

cd /home/wwwroot/www.piddnad.cn

下载 WordPress 最新安装包

wget http://wordpress.org/latest.tar.gz

下载完成后解压

tar -xzvf latest.tar.gz

将解压出来的WordPress文件全部移动到当前域名目录下

mv wordpress/* .

删除wordpress空文件夹和WordPress安装包

rm -rf wordpress
rm -rf latest.tar.gz

赋予根目录文件可写权限,避免因权限问题导致安装出错等问题

chmod -R 755 /home/wwwroot
chown -R www /home/wwwroot

至此,我们在VPS端的操作全部完成。

7 自定义你的博客!

打开浏览器,输入你的博客网址,进入 WordPress 安装程序

数据库名、数据库用户名和数据库密码都填写之前创建的,并建议妥善记录保管。

然后将进行”著名的WordPress五分钟安装程序”,最后,大功告成!

6 其他优化

6.1 添加IP访问

可以尝试一下在浏览器地址栏输入 VPS 的 IP 地址,将会看到LNMP安装成功的提示界面:

为解决这个问题,我们需要修改 LNMP 网站配置文件

vi /usr/local/nginx/conf/nginx.conf

找到server对应模块的代码,并将 root 后面的值更换为 /home/wwwroot/www.piddnad.cn

后记

其实还有很多可以优化的东西,比如开启防火墙,更改 SWAP 分区大小等。。今天就说到这里吧,以后慢慢写。

最后,要感谢 Simonyy(博客地址)一直以来的帮助支持,还有他的博客搭建教程,嘿嘿。

参考

  1. https://www.simonyy.com/2018/10/13/ru-keng-ling-ji-chu-da-jian-si-ren-bo-ke-jia-jian/#toc_8
  2. https://lnmp.org/install.html
  3. https://blog.csdn.net/junlon2006/article/details/77854898

0 Preface

The reason I wrote this guide: a hacker broke into my blog and deleted the entire database. In a fit of rage, I wiped and reinstalled the server from scratch.

My sincerest regards to that hacker:

1 What You Need

To host your own blog, you need just three things:

  • A VPS or dedicated server (providers include BandwagonHost, DigitalOcean, Linode, etc.)
  • A domain name (registrars like Namecheap or GoDaddy work fine)
  • Curiosity and a willingness to run some commands

Let's get started.

2 DNS Setup

In short: DNS maps your domain name to your VPS's IP address.

Log in to your domain registrar's control panel and find the DNS settings for your domain. Add two records — typically an A record for the root domain and one for www — both pointing to your VPS's IP address.

Once saved, verify with:

ping yourdomain.com

If the ping resolves to your VPS IP, DNS is working.

3 Installing the LNMP Stack

LNMP = Linux + Nginx + MySQL + PHP. It's a one-click installer that sets up a complete web server environment on CentOS/Debian.

All remaining steps happen on the VPS. Connect via SSH:

  • macOS/Linux: ssh root@your.ip (default port 22), or ssh -p PORT root@your.ip for a custom port
  • Windows: Use PuTTY or the Chrome Secure Shell extension

Once logged in, create a screen session so the install survives disconnection:

screen -S lnmp

Download and run the installer:

wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp

The installer will prompt you for:

  • MySQL version — Note: MySQL 5.6, 5.7, and MariaDB 10 require at least 1GB RAM.
  • MySQL root password — choose a strong one and remember it.
  • InnoDB — keep it enabled (default).
  • PHP version — make sure it's compatible with your app.
  • Memory allocator (TCMalloc / Jemalloc / none) — TCMalloc is from Google (used in Chrome/Safari); Jemalloc from Facebook (used in Firefox). Either is fine.

Installation takes anywhere from a few minutes to a couple hours. When you see Install lnmp V1.5 completed! enjoy it., you're done.

4 Adding a Virtual Host

lnmp vhost add

Follow the prompts:

  • Enter your domain (add both yourdomain.com and www.yourdomain.com — they're treated as different hosts).
  • Accept the default web root directory (just press Enter).
  • Skip URL rewrite and logging if you don't need them.
  • When asked about a database, enter y, provide your MySQL root password, then set a database name and user password.
  • Add an SSL certificate — choose option 2 to auto-provision a free certificate.

5 Installing WordPress

Navigate to your site's web root:

cd /home/wwwroot/www.yourdomain.com

Download and unpack WordPress:

wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* .
rm -rf wordpress latest.tar.gz

Fix file permissions:

chmod -R 755 /home/wwwroot
chown -R www /home/wwwroot

6 Finishing Up in the Browser

Open your domain in a browser. WordPress's setup wizard will appear — enter the database name, username, and password you created earlier. Run through the famous “five-minute install” and you're live.

7 Bonus: Fix Direct IP Access

Visiting your VPS's raw IP in a browser shows the LNMP default page instead of your site. To fix this, redirect IP traffic to your domain in the Nginx config:

vi /usr/local/nginx/conf/nginx.conf

Find the server block and update the root directive to point to your site's web root.

Afterword

There's plenty more to do — set up a firewall, adjust swap space, enable fail2ban, etc. I'll write about those separately.

Thanks to Simonyy (blog) for the help and inspiration when I first set this up.

References

  1. https://www.simonyy.com/2018/10/13/ru-keng-ling-ji-chu-da-jian-si-ren-bo-ke-jia-jian/#toc_8
  2. https://lnmp.org/install.html
  3. https://blog.csdn.net/junlon2006/article/details/77854898