资源干货首页投稿(暂停使用,暂停投稿)生活小觑

Ubuntu搭建Ghost个人博客

2016-01-15  本文已影响1461人  Springer

博客地址

1. 域名购买

域名就是网址,比如你输入img421.com(一级域名),而我的博客blog.img421.com(二级域名).域名的购买你可以到万网Godaddy等大型域名供应商购买。

万网
万网
Godaddy
Godaddy
首页中输入框就是输入你要命名的域名。如果这个域名被注册,万网会推荐你使用其他的后缀名比如 .net .org .com.cn . . . 也会推荐在你使用的名字后面添加数字比如 baidu123 baidu1234 . . .

tips (博主使用的是万网提供的域名,Godaddy请转到这儿

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

2. 配置服务器

博主使用的是AWS(亚马逊服务器),AWS提供新用户免费一年的使用时间,但是注册以后必须使用信用卡才行。(说好的免费呢,怎么绑定还扣我预授权6RMB? !-. - )

根据教程配置安全组、密钥(请重点保护好)、实例(Ubuntu免费实例)

2.1 远程服务器

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

desktop

2.2 安装Node.js

  1. Node.js是一个可以快速构建网络服务及应用的平台,基于Chrome's JavaScript runtime,即Google V8引擎,是一款高性能的服务架构平台。

  2. Ubuntu系统上执行以下代码即可安装Node.js

    apt-get update      
    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
    
  3. 安装过程中会有一些提示,直接回车,继续安装。

  4. 确认是否安装成功:

    node -v
    
    npm  -v
    
install

2.3 安装Nginx

输入如下安装命令
sudo apt-get install nginx

2.4 安装MySQL

Ghost 默认采用 Sqlite3 数据库。
安装MySQL执行如下命令:

    sudo apt-get install mysql-server mysql-client  

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

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.
...

//是否删除匿名用户?
Remove anonymous users? [Y/n] y  
... Success!
...


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


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


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

OK!MySQL 到此就安装好了!

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

执行如下命令:

cd /etc/mysql 

然后输入:

sudo vi my.cnf 

将光标定位到 [mysqld] 位置,按 "i" ,添加如下红线框内设置:

mysqlSet
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

修改完后,按ESC,输入:wq ,保存退出。

3. 安装Ghost

3.1 创建数据库

为Ghost博客创建一个数据库,里边存储用户的登录,文章等信息。

执行如下命令:

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

输入MySQL数据库的root 账户密码.

3.2 配置Nginx

利用 Nginx 做 Ghost 的前端代理服务
输入:

cd /etc/nginx/sites-available/
sudo touch ghost.conf  
sudo vi ghost.conf 

修改如下内容:

域名设置

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

3.3 建立软链接

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

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

3.4 安装forever

为了使ghost永远运行,使用 forever 工具帮我们解决这个问题。接下来执行以下指令来安装 forever,执行如下命令:

sudo npm install forever -g 

3.5 安装Ghost

3.5.1 下载Ghost

Ghost 安装包是一个压缩包,在Linux 上我们需要用unzip 工具进行解压。

  1. 安装unzip 工具:

      sudo apt-get install unzip 
    
  2. 下载 Ghost 安装包

      cd /srv/
      sudo curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
    
  3. 使用以下命令解压存档

     unzip -uo ghost.zip -d ghost
    

3.5.2 修改Ghost配置

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

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

修改production 的配置信息,修改为如下

ghostConfig

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

3.5.3 安装Ghost依赖库

打开 Ghost 系统的目录下面的 package.json 文件,将 "sqlite3": "x.x.0", 这一行删除掉。

进入Ghost 目录,安装依赖库

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

安装好后,会在目录下多一个node_modules目录

3.5.4 启动Ghost

启动并运行Ghost博客

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

ok,这时将你的域名解析到你的服务器IP上,待DNS生效后,打开域名,就可以看到Ghost博客已经成功运行了。

Q&A:

1. service nginx restart 重启Nginx失败.

A:

  1. 运行nginx -t 查看错误内容

    nginx: [emerg] unknown directive "*********" in /etc/nginx/sites-enabled/ghost.conf:5
    nginx: configuration file /etc/nginx/
    nginx.conf test failed
    

打开nginx.conf 修改内容:

将
##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

修改为:

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;

再次运行

sudo service nginx restart

2. AWS通过root用户登录,非密钥登录?

A:

修改三个参数为:

    PermitRootLogin yes
    PasswordAuthentication yes
    UsePAM no

注意:不要退出现在的远程控制.另打开一个终端(Terminal)
输入命令:

        ssh root@你的公共IP地址
rootLogin

提示输入你设置的root密码,按回车键等待登录成功。


loginIn

如果切换用户为其他用户,比如ubuntu用户
终端输入:

           su ubuntu
上一篇 下一篇

猜你喜欢

热点阅读