KittenYang

博客第一篇:记录这个网站的诞生过程

这是本博客的第一篇文章,我决定用来记录这个网站的诞生过程(即如何在阿里云服务器ECS_Ubuntu系统上安装Ghost博客)。一来可以把这个难忘的建站经历记录下来,二来也可以总结一些成功的经验供未来通过搜索引擎找到这儿的网友一些帮助。文章比较细,遇到你熟悉的部分可以跳过不看。嗯,开始吧。。。


1. 购买域名

域名就是网址。比如我这个网站的域名就是 kittenyang.comGodaddy 是全球最大的域名供应商,所以我在Godaddy(网友亲切称其为“狗爹”)购买了域名。

首页中大大的输入框(Enter a domain name)里面就可以输入你想要注册的域名,然后godaddy会告诉你这个有没有被注册。如果被注册了,godaddy会建议你使用其他后缀名比如.net .org .me ...

*tips:

Godaddy可以使用支付宝支付,只要把顶部货币栏切换到CNY即可。

购买完成之后,进入你的 My Account,点击Domains,点击Launch.

然后你会进入到域名的配置页面。在这里我们需要把域名和我们的服务器绑定起来。其实很简单,就一句话,把域名绑定到服务器的IP地址即可。这个过程,就是传说中的解析

怎么解析呢? 请看下图:

点击编辑,在 Points to 一栏中填入服务器的IP地址即可。这里的 Host 其实就是前缀。这里普及两个名词: 顶级域名二级域名 。我们平常所知道的所谓的so-called正确网址是www.XXX.xxx。其实不然, 我们只要输入XXX.xxx就可以了,不需要输入前面的www。www只是一个前缀。www.XXX.xxx就是一个二级域名,类似的还有mail.qq.comopen.weibo.com 等。而顶级域名就是XXX.xxx ,比如baidu.comqq.com。所以我们在解析的时候第一栏的Host Name就只是个前缀。顶级域名一般用@表示。

域名配置好了,接下来我们就要去配置服务器(本文以阿里云服务器为例)了。

由于笔者用的是Mac,所以我只能用Mac上的方法来写这篇博文。但Windows用户也不用害怕,流程是对的,只是有些环节的手段不太一样。如果遇到不懂的,请善用搜索引擎,俗称黄金法则


2. 配置服务器

首先启动终端terminal(Mac上开启一个文件或者一个应用的最快方法:control + 空格,呼出spotlight,输入想要的文件或应用的名称,如terminal,回车)。

输入命令行:ssh root@你的IP地址 ,回车。输入服务器密码。

这段命令的作用是请求连接服务器。出现如下界面表示连接成功。

2.1 安装Node.js

依次执行以下指令:

sudo apt-get update  
sudo apt-get install -y python-software-properties python g++ make  
sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs  

确认是否安装成功:

node -v  
v0.10.30  
npm -v  
1.4.21  

2.2 安装Nginx

执行以下指令:

sudo apt-get install nginx  

2.3 安装MySQL

Ghost 默认采用 Sqlite3 数据库,但是我还是建议用 MySQL,避免将来由于数据多、访问量多而导致性能下降。

执行如下指令开始安装 MySQL 吧:

sudo apt-get install mysql-server mysql-client  

安装过程中,系统会提示你给 root 用户(这里的 root 是 MySQL 数据库的管理账号) 设置个密码,建议设置的复杂些,更加安全些。如下图:

MySQL 安装成功后,我们执行以下指令进一步加强 MySQL 的安全设置:

sudo mysql_secure_installation  

此工具所做的增强安全设置包括:移除所有匿名账户、限制远程 root 账户登录、删除 test 数据库等。下面列出详细设置:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL  
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current  
password for the root user.  If you've just installed MySQL, and  
you haven't set the root password yet, the password will be blank,  
so you should just press enter here.

//输入安装 MySQL 时为 root 账户设置的密码
Enter current password for root (enter for none):  
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL  
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

//是否修改 root 账户的密码?前面设置过 root 账户的密码了,如果不打算修改密码的话,输入 'n'
Change the root password? [Y/n] n  
 ... skipping.

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? [Y/n] y  
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This  
ensures that someone cannot guess at the root password from the network.

//是否禁止 root 账户远程登录?
Disallow root login remotely? [Y/n] y  
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can  
access.  This is also intended only for testing, and should be removed  
before moving into a production environment.

//是否删除 MySQL 默认创建的 test 数据库,并删除所有对 test 数据库的权限设置?
Remove test database and access to it? [Y/n] y  
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far  
will take effect immediately.

//是否重新加载权限表?
Reload privilege tables now? [Y/n] y  
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL  
installation should now be secure.

Thanks for using MySQL!  

OK!MySQL 到此就安装好了!

下面,我们为为 MySQL 设置默认字符集。将数据库、htnl页面、源码文件都设置为统一的字符集会减少很多麻烦,当然,utf8 是最好的选择。

执行如下命令:

cd /etc/mysql  

这条命令的作用是进入 /etc/mysql 文件夹。

然后输入:

vi my.cnf  

这条命令的作用是用 Vim 编辑器打开 /etc/mysql 目录下的 my.cnf 文件,然后就可以在终端对文件进行修改了。就像下面这样的界面:

将光标定位到 [mysqld] 位置,按 "i" ,这时可以修改了,添加如下设置:

[mysqld]
collation-server = utf8_unicode_ci  
init-connect='SET NAMES utf8'  
character-set-server = utf8  

修改完成之后,先按 esc ,然后输入 :wq ,退出MySQL。

为了确保设置成功,我们检查一下:

  • 进入 MySQL 命令行界面:mysql -uroot -p,输入之前你自己设置的MySQL密码。
  • 输入指令: show variables like 'char%'; 输出是否如下所示:

  • 再输入指令:show variables like 'collation%'; 检查一下结果:

如果是上面的结果,恭喜你,搞定 MySQL 了!!!


3. 安装Ghost

3.1 创建数据库

我们希望 Ghost 搭配 MySQL 数据库运行,因此需要为 Ghost 创建一个 MySQL 数据库。前面已经安装好 MySQL 了,现在我们就来创建数据库吧:

mysql -uroot -p -e 'create database ghost;'  

系统会提示你输入 MySQL 数据库的 root 账户密码(还记得前一章节安装 MySQL 时设置的密码吗?)。指令执行之后就创建了一个叫做 ghost 的数据库,将来,你的文章就是存在这里喽!

3.2 配置Nginx

我们希望利用 Nginx 做 Ghost 的前端代理服务。OK, 输入下面命令进入 /etc/nginx/sites-available/ 目录:

cd /etc/nginx/sites-available/

然后输入下面指令:

sudo touch ghost.conf  
sudo vi ghost.conf  

还记得 vi 指令的作用吗?最后一条指令的作用是 vim 编辑器打开 ghost.conf 文件进行编辑。我们输入如下内容:

server {  
    listen 80;
    server_name kittenyang.com *.kittenyang.com 115.28.223.69; //替换为你自己的域名!

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

完成之后按etc :wq 退出编辑。

然后我们为 ghost.conf 文件做一个软链接到 /etc/nginx/sites-enabled/ 目录下:

sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf  

3.3 安装forever

如果是通过 npm start 启动 Ghost 的话,只要你关闭了远程连接,Ghost 也就停了,这个我们当然不希望喽。幸好,有 forever 工具帮我们解决这个问题。接下来执行以下指令来安装 forever

sudo npm install forever -g  

3.4 Ghost,走你!

3.4.1 下载Ghost

Ghost 安装包是经过压缩的,在 Linux 上我们需要用 unzip 工具对其解压,因此,首先要安装 unzip 工具:

sudo apt-get install unzip  

接下来我们下载 Ghost 安装包:

cd /srv/  
sudo curl -L http://dl.ghostchina.com/Ghost-0.5.0.zip -o ghost.zip  

上面命令中网址的版本号可以根据ghostchina.com 上的最新版本修改,我写这篇文章的最新版本是0.5.0,所以我输入的是0.5.0

将其解压缩:

sudo unzip ghost.zip -d ghost  

现在,/srv/ghost/ 目录下面就是我们的 Ghost 系统了!

3.4.2 修改Ghost配置文件

我们进入 Ghost 系统目录,为 Ghost 增加配置文件并配置数据库:

cd /srv/ghost/  
sudo cp config.example.js config.js  
sudo vi config.js  

最后一条指令是用 vim 打开 config.js 文件进行编辑。我们只修改 production 一节的配置信息,修改为如下形式(注意按照你自己的实际情况替换!):

// ### Production
    // When running Ghost in the wild, use the production environment
    // Configure your URL and mail settings here
    production: {
        url: 'http://ghostchina.com', //替换为你自己的域名。
        mail: {},
        database: {
            updateCheck: false,
            client: 'mysql',
            connection: {
                host     : '127.0.0.1',
                user     : 'root', //我们暂且用 MySQL 的 root 账户
                password : '123456', //输入你的 MySQL 密码
                database : 'ghost', //我们前面为 Ghost 创建的数据库名称
                charset  : 'utf8'
            }
        },
        server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '127.0.0.1',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        }
    },

完成之后按etc :wq 退出编辑。

3.4.3 安装 Ghost 依赖库

打开 Ghost 系统的目录下面的 package.json 文件,将 "sqlite3": "2.2.0", 这一行删除掉(注意,你看到的 sqlite 版本可能会不一样,但是,只要是 sqlite3 字样,删除即可)。

接下来,进入存放 Ghost 系统的目录并安装 Ghost 所依赖的 npm 包:

cd /srv/ghost/  
sudo npm install --production  

很快,所有依赖包就安装好了,当前目录下会多出一个 node_modules 目录。

3.4.3 启动 Ghost

执行如下指令重启 Nginx、启动 Ghost:

sudo service nginx restart  
cd /srv/ghost  
sudo NODE_ENV=production forever start index.js  

OK,至此,所有的操作都做完了。现在打开浏览器并输入你的域名看看吧!


Q&A:

Q1. 主页显示很慢,界面很久才展示出来.

A:还是因为google被墙了,需要删除三处引用了谷歌字体的地方。/core/server/views 下的两个default.hbsuser-error.hbs 以及主题目录content/themes/你的主题/default.hbs中的:

<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Noto+Serif:400,700,400italic|Open+Sans:700,400" />  

Q2. service nginx restart 重启Nginx失败.

A:原因在于你已经把端口映射的配置写进default这个默认的配置文件了,但是你又写了一份同样的配置到ghost.conf,结果这两个配置同时生效,重启时冲突了。所以你应该:

输入以下命令:/etc/nginx/sites-available# rm -rf ghost.conf.

检查:/etc/nginx/sites-available# ls

应该就只会显示:default

然后 service nginx restart重启Nginx 就行了 重启成功了。

Q3. npm start ghost 正常运行以后,访问ghost, 无法进入后台,web端显示的状态码为302.

A:进入ghost 后台系统会自动检测更新,是否有新版本,但是国内无法访问这个地址 update.ghost.org.
所以你应该在config.js中加入updateCheck: false, 看起来像这样:

development: {  
       updateCheck: false,
        database: {
            client: 'mysql',
            connection: {
                host     : '127.0.0.1',
                ......

Q4. 如何备份现有的文件?

A:cp /srv/ghost /srv/ghost_bak
这样就可以把原来的文件备份到ghost_bak文件夹.

Q5. 如何备份数据库?

A:先按cd /srv 进入/srv目录,再mysqldump -uroot -p你的密码 ghost > ghost.sql
这样就会把你的数据库内容备份到srv目录的ghost.sql这个文件里.


转载请注明出处,不胜感激!

KittenYang

写写代码,做做设计,看看产品。