前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ghost - Birth of my blog

Ghost - Birth of my blog

作者头像
Mitchell
发布2018-09-30 09:39:22
1.4K0
发布2018-09-30 09:39:22
举报
文章被收录于专栏:西二旗一哥西二旗一哥

Mitchell


文章参考自 kitten 的这篇文章 和 Ghost 官网王赛写的这篇文章 那么既然已经有人写了很详细的文章,为什么我还要写呢?因为坑是填不满的,本人按照教程第一次搭建了两天,没搭成功,第二次又搭建了半天才完成,我会将教程+上我填上的坑告诉大家,希望大家能够成功搭建成自己的博客。


一、购买域名

  • 这个渠道很多,就不多加赘述,这里我是在万网上买的域名。
  • 域名解析。

二、购买服务器

  • 我买的是阿里云 50 多块钱的服务器,配置的是 ubuntu64 位系统。
  • 如果由于前两步我描述的过于简单会为你的配置造成困扰的话,请移步这里查看 kitten 写的前两章节的内容,如果还是不够解决你的问题,那么请自行百度,笔者也是从一个服务器一点不通的小白慢慢搭建成功的。

三、配置服务器

  • 3.1 打开终端(terminal) 输入命令行:
ssh root@你的IP地址  

点击回车,输入你的服务器密码。

进入服务器.png
进入服务器.png

如果出现以上文字,那么恭喜你,进入服务器成功。 + 3.2 安装 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  

注意:敲上面这句代码可能会出现这样的问题: add-apt-repository: command not found 说的是你的 command 没有安装,解决方法就是先安装命令:

sudo apt-get install python-software-properties  
sudo apt-get install software-properties-common  

安装完成之后继续执行下面的命令:

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs  

都执行完之后用 -v 命令查看安装是否成功:

node -v  
v0.10.37  
npm -v  
1.4.28  
  • 3.3 安装 Nginx
sudo apt-get install nginx  
  • 3.4 安装 MySQL
    • Ghost 默认采用 Sqlite3 数据库,但是我还是建议用 MySQL,避免将来由于数据多、访问量多而导致性能下降。
sudo apt-get install mysql-server mysql-client  

安装过程中系统会让你给 root(MySQL 数据库的管理账号) 用户设置密码。

-----2014-08-22-16-27-05.png
-----2014-08-22-16-27-05.png

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

sudo mysql_secure_installation  
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!  

MySQL 到此安装结束。 下面设置 utf8 执行这行命令:

cd /etc/mysql  

编辑 my.conf 文件

vi my.cnf  

你将看到这么个文本摆在你的面前。

[client]
port            = 3306  
socket          = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock  
nice            = 0  
[mysqld]
#
# * Basic Settings
#
user            = mysql  
pid-file        = /var/run/mysqld/mysqld.pid  
socket          = /var/run/mysqld/mysqld.sock  
port            = 3306  
basedir         = /usr  
datadir         = /var/lib/mysql  
tmpdir          = /tmp  
lc-messages-dir = /usr/share/mysql  
skip-external-locking  

那么你的任务是什么呢?将光标定位到 [mysqld] 位置,按 "i" ,这时可以修改了,添加如下设置:

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

这是修改之后的文本:

[client]
port            = 3306  
socket          = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock  
nice            = 0  
[mysqld]
collation-server = utf8_unicode_ci  
init-connect='SET NAMES utf8'  
character-set-server = utf8  
#
# * Basic Settings
#
user            = mysql  
pid-file        = /var/run/mysqld/mysqld.pid  
socket          = /var/run/mysqld/mysqld.sock  
port            = 3306  
basedir         = /usr  
datadir         = /var/lib/mysql  
tmpdir          = /tmp  
lc-messages-dir = /usr/share/mysql  
skip-external-locking  

修改完成之后,先按 esc ,然后输入 :wq ,退出MySQL。 之后我们来检一下是否修改完成? 1.进入 MySQL 命令行界面:

mysql -uroot -p  

之后输入之前你自己设置的MySQL密码。 2.输入指令

show variables like 'char%';  

 查看输出是否如下所示:

数据库配置.png
数据库配置.png
注意:这里如果和我这个 Vlaue 值有的对应不上需要重新启动数据库,那么怎么在 ubuntu 下重新启动 MySQL 呢?输入:
service mysql restart  

这个时候再进入 mysql 去查看一下配置就和我这个一样啦。 3.输入指令

show variables like 'collation%';  

检查结果是否如图:如果一样,恭喜你可以跳入下一个坑。

屏幕快照 2015-10-29 下午7.37.31.png
屏幕快照 2015-10-29 下午7.37.31.png

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

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

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

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

然后输入下面命令 , 编辑ghost.conf

sudo touch ghost.conf  
sudo vi ghost.conf  
server  
 {
 listen 80; 
server_name mitchell-dream-god.com; //替换为你自己的域名!  
 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.7 安装 forever 如果是通过 npm start 启动 Ghost 的话,只要你关闭了远程连接,Ghost 也就停了,这个我们当然不希望喽。幸好,有 forever 工具帮我们解决这个问题。接下来执行以下指令来安装forever :
sudo npm install forever -g  
  • 3.8 安装 Ghost
  • 3.8.1 下载解压工具
sudo apt-get install unzip  
  • 3.8.2 下载 Ghost 压缩包
cd /srv/  
sudo curl -L http://dl.ghostchina.com/Ghost-0.5.0.zip -o ghost.zip  

如果想下载更新的版本去 Ghost 官网 查看地址即可。

  • 3.8.3 解压
sudo unzip ghost.zip -d ghost  

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

  • 3.8.4 修改 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.8.5 安装 Ghost 依赖库 打开 Ghost 系统的目录下面的 package.json 文件,将 "sqlite3": "2.2.0", 这一行删除掉(注意,你看到的 sqlite 版本可能会不一样,但是,只是 sqlite3 字样,删除即可,不要注释上,一定要把那一行删除了)。 接下来,进入存放 Ghost 系统的目录并安装 Ghost 所依赖的 npm  包:
cd /srv/ghost/  
sudo npm install --production  

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

  • 3.8.6 启动 Ghost
执行如下指令重启 Nginx、启动 Ghost:
>```
sudo service nginx restart  

如果发现重新启动失败,怎么办?这里的原因是因为 /etc/nginx/sites-available 中的default文件夹和 我们创建的 ghost.conf 文件冲突产生的,解决方式就是删除我们创建的文件,先进入 /etc/nginx/sites-available文件夹并使用删除命令:

/etc/nginx/sites-available# rm -rf ghost.conf

这个时候你再次输入上面代码重新启动 nginx 会发现成功了。接着执行下面的命令。

cd /srv/ghost  
sudo NODE_ENV=production forever start index.js  
注意:至此,所有的操作都做完了。现在打开浏览器并输入你的域名查看一下状态!如果是这样就成功了:
Ghost.png
Ghost.png
如果出现 Nginx 欢迎页面,那就出现问题啦!!!(我几次都是躺在了这里,几个大神貌似没遇到这种问题,但是我确实遇到啦!接下来我来说说怎么解决?)

四、最后的坑的解决方案

  • 还记得我们所做的 Nginx 配置么? 进入我们的 Nginx 目录,我们来查看一下我们的 Nginx 的引用,
cd /etc/nginx/  
vi nginx.conf  

查看 include 这两行,其实主要是下面这行,我们可以看到我们 Nginx 引用的是 nginx 文件夹下 sites-enabled 文件夹中的文件,接着我们去看看 sites-enabled 中的文件是否配置正确?

include.png
include.png

进入 sites-enabled 文件夹,查看文件

cd /etc/nginx/sites-enabled  
ls  

你将会看到两个文件,一个是 default 和 ghost.conf ,还能想起来我们的 ghost.conf 么?我们是在 sites-available 中创建的 ghost.conf 并且软链接到这里产生的 ghost.conf 文件,那么你用 vim 来打开这个文件

vi ghost.conf  

打开之后,傻啦,啥东西都没有啊,咋办把之前在 available 中添加的那些内容添加到这里:

server  
{
 listen 80;
 server_name mitchell-dream-god.com; //替换为你自己的域名!
 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 退出编辑。

好了,这回应该没有问题了。如果还有什么问题,欢迎继续和我讨论。QQ:416922992


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、购买域名
  • 二、购买服务器
  • 三、配置服务器
    • 注意:这里如果和我这个 Vlaue 值有的对应不上需要重新启动数据库,那么怎么在 ubuntu 下重新启动 MySQL 呢?输入:
      • 注意:至此,所有的操作都做完了。现在打开浏览器并输入你的域名查看一下状态!如果是这样就成功了:
        • 如果出现 Nginx 欢迎页面,那就出现问题啦!!!(我几次都是躺在了这里,几个大神貌似没遇到这种问题,但是我确实遇到啦!接下来我来说说怎么解决?)
        • 四、最后的坑的解决方案
        相关产品与服务
        云数据库 MySQL
        腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档