Ghost程序员

搭建Ghost博客详细教程之环境搭建及Ghost部署

2018-02-07  本文已影响115人  Bug生活2048

网上大多数的教程都是安装Ghost0.7.4的,相对来说比较老了,安装起来也比较复杂了,Ghost1.0之后Ghost自带了cli安装工具,相较于以前方便很多。

如果想安装Ghost0.7.4的可参考 手把手教你搭建一个属于自己的Ghost博客

下面根据官方文档搭建Ghost1.X版本:

前期准备
yum update #更新yum源  
yum groupinstall "Development Tools" #安装开发工具包  
yum install wget #安装wget下载工具  
安装Node.js 6.x LTS

官方建议6.x,我装的时候没注意,直接最新的8.x了,不过感觉问题也不是很大,最好还是安装官方的来吧

curl -sL https://rpm.nodesource.com/setup_6.x | bash -  
yum install nodejs

验证是否安装配置成功: node -v

安装Mysql

CentOS7默认数据库是mariadb ,但是CentOS7的yum源中默认好像是没有mysql的。所以得自己下载源安装了。

wget http://repo.mysql.com/mysql57-community-
release-el7-8.noarch.rpm 

rpm -ivh mysql57-community-release-el7-8.noarch.rpm 

yum -y install mysql-server    

安装完之后,密码为随机密码,所以需要重置密码,输入下面指令查看随机密码

 grep "password" /var/log/mysqld.log    

然后输入下面指令进入MySql

 mysql -u root -p 密码

接下来重置密码(为了安全密码,必须包含 数字字母符号)

 alter user 'root'@'localhost' identified by '你的密码';  

为了更好的体验和安全,还可以进行一些常规设置。

 chkconfig mysqld on # 设置开机启动Mysql  
 anonymous users? [Y/n] # 删除匿名用户  
 Disallow root login remotely? [Y/n] # 禁止root用户远程登录 
 Remove test database and access to it? [Y/n]  #删除默认的 test 数据库 
 Reload privilege tables now? [Y/n] # 刷新授权表使修改生效 

为了避免数据库存放的中文是乱码,我们还需要设置Mysql的编码:

vi /etc/my.cnf  

写入以下内容:

[client]
default-character-set=utf8  
[mysql]
default-character-set=utf8  
[mysqld]
character-set-server=utf8  
collation-server=utf8_general_ci 

保存退出,重启Mysql:

service mysqld restart 

最后我们需要新建一个数据库,用来存放博客的数据:

mysql -u root -p # 输入设置好的密码 

create database ghost; # 创建ghost数据库  

grant all privileges on ghost.* to 'ghost'@'%' identified by '123456'; # 新建一个用户ghost,密码为123456

flush privileges # 重新读取权限表中的数据到内存,不用重启mysql就可以让权限生效  
安装Nginx
vi /etc/yum.repos.d/nginx.repo 

写入以下内容:

[nginx] 
name=nginx repo  
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/  
gpgcheck=0  
enabled=1  

保存退出。

(按i编辑,按Esc结束编辑,:x 保存修改并退出,:q! 强制退出,放弃修改)

继续执行以下指令:

yum install nginx -y # 安装Nginx  
service nginx start # 动Nginx  
chkconfig nginx on # 设置开机启动Nginx  

这样Nginx就安装成功了,在浏览器中输入你的VPS的IP就可以看到提示:“Welcome to Nginx!”

配置Nginx

安装好了nginx后,我们需要设置一个代理服务器让我们的博客可以使用域名访问。 在/etc/nginx/conf.d目录下创建一个配置文件ghost.conf:

vi /etc/nginx/conf.d/ghost.conf  

写入以下内容:

server {  
    listen 80;
    server_name example.com; #将 example.com 改为你的域名或ip。

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

保存退出,重启nginx:

service nginx restart 
安装Ghost-CLI
npm i -g ghost-cli
添加 ghost 运行用户和创建安装目录

假设你的博客要放在/var/www/ghost目录,那么我们就创建一个并赋予权限

adduser ghost
mkdir /var/www/ghost
chown ghost /var/www/ghost
安装 Ghost

默认以SQLite3为数据库安装模式,我们安装Mysql模式

cd /var/www/ghost
ghost install local --db=mysql
修改配置

config.development.json 文件,修改自己相关配置即可

启动 Ghost
ghost start #启动
ghost restart #重启
ghost stop #停止

这里会让你填写mysql的地址、用户名、密码、数据库名称等,填写你之前设置的就可以了

开机自动启动Ghost

可以直接在==rc.local==中设置

vi /etc/rc.d/rc.local #打开rc.local

写入以下内容:

cd /var/www/ghost #你ghost安放的路径
ghost start #启动

保存退出后,可以试下重启下服务器。(之前的Mysql,Nginx都已经设置开机自启动)

这样如果不出意外的话,你的博客应该顺利起来了。

上一篇 下一篇

猜你喜欢

热点阅读