从 0 开始在 CentOS 部署 Ghost 博客[覆盖 CentOS7 教程]

本文来自:imoe.moe,在V2EX上获取文章数据: https://www.v2ex.com/t/153708

AirScript对一些逻辑上的错误进行了修改(鉴于原博客一直处于“维护”的状态,我在这里重新进行排版,并且恢复了图像)。

看见别人写过一次,不尽人意,看起来乱七八糟而且部分步骤我觉得非常麻烦,干脆自己写一个吧,在V2EX,算是第一个发布的稿子吧

因为怕一些人不会拿到新的VPS不会部署,在这里也一并说了算是造福社会吧2333

接下来按照步骤走就对啦

本文包括基于Centos7 64位系统搭建Ghost博客系统,附带指导安装Nginx,mariadb,nodejs,ghost~~嗯,就是这样,那么让我们开始吧

没有抄袭官方的文档,不信你可以去对比,全部经验走起

PS:为了方便大家,在本文结尾处附带了2个安装版本,请慢慢读下去吧,文末有惊喜哦~

TOC

一、Centos 7 安装Nginx

1、安装依赖

2、通过源安装Nginx

二、Centos 7安装mariadb

1、安装Mariadb Mariadb-server

2、配置Mariadb

3、配置mariadb以防止乱码

三、安装nodejs

四、安装Ghost博客

1、配置一下nginx

五、配置systemd

六、完成

一、Centos 7 安装Nginx[有的人请跳过]

1、安装依赖

yum install gcc-c++

yum install pcre pcre-devel

yum install zlib zlib-devel

yum install openssl openssl–devel

2、通过源安装Nginx

Centos7 默认是没有Nginx源的,需要自己添加。当然你也可以手动编译最新版的包,但是貌似不能用systemd管理。这里通过源来安装

wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx- release-centos-7-0.el7.ngx.noarch.rpm

rpm -ivh noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install nginx

//让nginx随机子启动而启动并启动nginx

systemctl enable nginx

systemctl start nginx

//查看下状态

systemctl status nginx

Centos7默认是安装在/etc/nginx 配置文件在/etc/nginx/conf.d/default.conf

二、Centos 7安装mariadb

Centos7源中没有了mysql而是改用了mariadb,他们关系自行google

1、安装Mariadb Mariadb-server

yum install mariadb mariadb-server

systemctl enable mariadb

systemctl start mariadb

systemctl status mariadb

2、配置Mariadb

mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current

password for the root user. If you’ve just installed MariaDB, and

you haven’t set the root password yet, the password will be blank,

so you should just press enter here.

//这里直接按回车

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 MariaDB

root user without the proper authorisation.

//提示输入root账户密码,是mysql账户的密码,不是系统的

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB 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] yes

… Success!

Normally, root should only be allowed to connect from ‘localhost’. This

ensures that someone cannot guess at the root password from the network.

//是否禁止远程登陆

Disallow root login remotely? [Y/n] y

… Success!

By default, MariaDB 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.

//删除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 MariaDB

installation should now be secure.

Thanks for using MariaDB!

3、配置mariadb以防止乱码

vi /etc/my.cnf

在[mysqld]字段下面添加类似于这样

[mysqld]

//添加下面这三行到[mysqld]字段

collation-server = utf8_unicode_ci

init-connect=’SET NAMES utf8′

character-set-server = utf8

//重启mariadb

systemctl restart mariadb

//登陆查看下是否改变为utf-8

mysql -u root -p

show variables like ‘char%’;

show variables like ‘collation%’;

mariadb_chara1.png

mariadb_char2.png

三、安装nodejs

nodejs编译的很慢,我是直接下载的二进制包

wget http://nodejs.org/dist/v0.10.33/node-v0.10.33-linux-x64.tar.gz

tar -zxvf node-v0.10.33-linux-x64.tar.gz

mv node-v0.10.33-linux-x64 node-v0.10.33

cp -r node-v0.10.33 /opt

export PATH=/opt/node-v0.10.33/bin:$PATH //建议把这句添加到.bashrc配置文件省的每次登陆后都要执行一次

node -v //测试一下

v0.10.33

npm -v

1.4.28 //出现同样的效果

还有一种非常简易的方法,两个命令解决node部署与npm依赖

curl -sL https://rpm.nodesource.com/setup | bash –

yum install -y nodejs

node -v //测试一下

v0.10.33

npm -v

1.4.28 //同样的效果

四、安装Ghost博客系统

Ghost是一个轻量级的Blog系统,基于nodejs构建。相较于wp我觉得Ghost更适合写作,本Blog就是搭建于Ghost博客。你可以访问官方网站了解它。

$ wget https://ghost.org/zip/ghost-latest.zip

$ unzip ghost-latest.zip -d ghost //如没有unzip请执行$ sudo yum install unzip 来安装

$ cd ghost

//注意production前面是两个-

$ npm install -–production

$ mysql -uroot -p -e ‘create database ghost;’ //ghost默认的是sqlite数据库,但是在各大云平台上都不是太好,这里更换为mariadb数据库。这里新建一个ghost数据库。

$ cp config.example.js config.js

$ vim config.js

//修改config.js类似于这样,主要是mysql配置

production: {

url: ‘http://your_blog_url‘,

mail: {},

database: {

client: ‘mysql’,

connection: {

host : ‘127.0.0.1’,

user : ‘root’, //暂且直接用root账户

password : ‘password’, //你的root账户密码

database : ‘ghost’,

charset : ‘utf8’

},

debug: false

},

server: {

// Host to be passed to node’s net.Server#listen()

host: ‘127.0.0.1’,//改为0.0.0.0,否则他人无法访问

// Port to be passed to node’s net.Server#listen(), for iisnode set this to process.env.PORT

port: ‘2368’

}

},

1、配置一下nginx

vim /etc/nginx/conf.d/default.conf

//删除里面内容添加

server {

listen 80;

server_name xxx.com www.xxxx.com; //替换为你自己的域名!

location / {

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header Host $http_host;

proxy_pass http://127.0.0.1:2368;

}

}

如果你已经启用了Nginx的反向代理那么可以执行npm start –-production来看看效果,前提你已经把域名解析到IP,如果没有解析那么可以用IP访问,切记,–-production不可省

五、配置systemd

当我们断开ssh的时候,nodejs的进程也会死掉,所以我们要让ghost自动重启,官方推荐用forever如果你想用可以去看看。在这里我们用Centos新的配置工具systemd来守护Ghost,让她死掉后能自动重启。关于systemd请自行google

vim /etc/systemd/system/ghost.service

//添加以下内容

[Service]

WorkingDirectory=你的ghost博客目录

ExecStart=/opt/node-v0.10.33/bin/node index.js

Restart=always

StandardOutput=syslog

StandardError=syslog

SyslogIdentifier=ghostblog

User=你想运行ghost的用户

Group=组

Environment=PATH=/opt/node-v0.10.33/bin:$PATH

Environment=’NODE_ENV=production’

[Install]

WantedBy=multi-user.target

让Ghost自启动

systemctl enable ghost

systemctl start ghost

systemctl status ghost

六、安装完成

说好的福利呢!

PS:福利在此,很多人说在国内无法使用npm依赖,原因是长城拦住了,于是在此附带最新的0.5.6的中文汉化版本,内附node_modules依赖,你只需使用npm start命令运行即可,其余的均已经帮你配置好了。

下载地址: http://pan.baidu.com/s/1bnhcjmR

最后希望这个博客能帮助到你,欢迎来到ghost的世界╮( ̄▽ ̄”)╭

转载标明出处”imoe.moe”,这是基本的素质和修养问题,个人博客imoe.moe,欢迎做客(^__^)

Arnold Knegt

Read more posts by this author.

or subscribe via RSS with Feedly!
由于相关政策限制,评论系统对中国大陆用户不可见。详情请点击。
Due to some policy reasons, you're not able to access to our comment system in mainland China. Click here for details.