文档首页> 弹性云服务器 ECS> 最佳实践> 手工搭建Ghost博客(Ubuntu 20.04)
更新时间:2023-07-03 GMT+08:00
分享

手工搭建Ghost博客(Ubuntu 20.04)

Ghost 是基于Node.js的开源博客平台,可以为用户提供更加便捷的写作与发布平台,本文指导用户基于华为云弹性云服务器(以Ubuntu 20.04操作系统云服务器为例)部署Ghost博客。

安装gcc和g++

  1. 执行以下命令,安装常用的开发编译工具包。

    sudo apt-get install build-essential

  2. 执行如下命令,安装gcc。

    apt-get install gcc

  3. 执行以下命令查看gcc版本。

    gcc --version

    回显信息:

    root@ecs-c47c:~# gcc --version
    gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.
  4. 执行以下命令安装g++。

    sudo apt-get install g++

  5. 执行以下命令查看g++版本。

    g++ --version

    回显信息:

    root@ecs-c47c:~# g++ --version
    g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.

安装Node.js

  1. 执行以下命令,安装Node.js

    sudo curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -

    sudo apt-get install -y nodejs

  2. 安装完成后,执行以下命令查看Node.js版本和npm版本。

    node -v

    npm -v

    回显信息:

    root@ecs-c47c:~# node -v
    v18.16.1
    root@ecs-c47c:~# npm -v
    9.5.1

安装Nginx

部署Ghost博客,需要安装Nginx作为HTTP服务器,本节以安装Nginx 1.10.0 版本为例。

  1. 输入以下命令安装Nginx。

    sudo apt-get update

    sudo apt-get install nginx

  2. 调整防火墙(可选)。

    UFW(Uncomplicated Firewall)是一个iptables的接口,可以简化配置防火墙的过程。Ubuntu默认安装了UFW,执行以下命令查看防火墙的状态。

    sudo ufw status

    如果你没有也不想开启防火墙,则可以直接跳过此步骤,如果你想要开启防火墙可以通过以下命令实现。

    sudo ufw enable

    之后再次检查防火墙状态验证是否成功开启防火墙。

    在测试Nginx之前,需要重新配置我们的防火墙软件以允许访问Nginx。执行以下命令,将Nginx自动 注册在UFW。

    sudo ufw app list

    回显信息:

    Available applications:
      Nginx Full
      Nginx HTTP
      Nginx HTTPS
      ...
    • Nginx Full:此配置文件打开端口 80(正常,未加密的Web流量)和端口443(TLS / SSL加密流量)
    • Nginx HTTP:此配置文件仅打开端口 80(正常,未加密的Web流量)
    • Nginx HTTPS:此配置文件仅打开端口 443(TLS / SSL加密流量)

    执行以下命令确保防火墙允许HTTP和HTTPS连接。

    sudo ufw allow 'Nginx Full'

  3. 验证Nginx是否正常工作。

    在浏览器中通过域名或者IP地址进行访问Nginx,如果Nginx正常启动则会打开Welcome to nginx的欢迎页面。

    使用浏览器访问 “http://云服务器IP地址”,显示如下页面,说明Nginx安装成功。

  4. 配置Nginx。
    1. 新建配置文件。

      vim /etc/nginx/sites-available/ghost.conf

    2. 把以下配置内容粘贴进你的配置文件中。
      server {
          listen 80;
          server_name 119.3.xx.xxx.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;
          }
      }

      以上配置文件已经把反向代理写好了, 唯一需要修改的地方是: 把server_name改成你自己的顶级域名.

    1. 把配置文件软链接到sites-enabled中。

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

    1. 重启Nginx。

      sudo service nginx restart

创建新用户

由于Ghost官方不推荐使用root用户直接操作,因此我们需要重新创新的用户,并为其配置权限。

  1. 执行以下命令,创建新用户。

    adduser <user>

    回显信息如下:

    Adding user `user' ...
    Adding new group `user' (1000) ...
    Adding new user `user' (1000) with group `user' ...
    Creating home directory `/home/user' ...
    Copying files from `/etc/skel' ...
    New password:
    Retype new password:
    passwd: password updated successfully
    Changing the user information for user
    Enter the new value, or press ENTER for the default
            Full Name []:
            Room Number []:
            Work Phone []:
            Home Phone []:
            Other []:
    Is the information correct? [Y/n] Y
  2. 执行以下命令,将新创建的用户添加到组。

    usermod -aG sudo <user>

  3. 执行以下命令,切换到<user>用户。

    su - <user>

安装MySQL

MySQL是一种开源数据库管理系统,通常作为流行的LAMP(Linux,Apache,MySQL,PHP / Python / Perl)堆栈的一部分安装。它使用关系数据库和SQL结构化查询语言)来管理其数据。

  1. 安装MySQL。
    • 执行以下命令更新软件包。

      sudo apt-get update

    • 执行以下命令安装mysql-server 软件包。(安装过程中会要求配置MySQL的root用户密码)

      sudo apt-get install mysql-server

  2. 配置MySQL。

    执行以下命令,并按照回显提示信息进行操作,加固MySQL。

    mysql_secure_installation

    Securing the MySQL server deployment.
    
    Enter password for user root:    #输入上一步骤中获取的安装MySQL时自动设置的root用户密码
    The existing password for the user account root has expired. Please set a new password.
    
    New password:  #设置新的root用户密码
    
    Re-enter new password:   #再次输入密码
    The 'validate_password' plugin is installed on the server.
    The subsequent steps will run with the existing configuration of the plugin.
    Using existing password for root.
    
    Estimated strength of the password: 100
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : N   #是否更改root用户密码,输入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? (Press y|Y for Yes, any other key for No) : Y   #是否删除匿名用户,输入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.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #禁止root远程登录,输入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.
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #是否删除test库和对它的访问权限,输入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? (Press y|Y for Yes, any other key for No) : Y   #是否重新加载授权表,输入Y
    Success.
    
    All done!
  3. 测试MySQL。

    输入以下命令来查看MySQL的状态:

    systemctl status mysql.service

    结果正常会提示:

    ● mysql.service - MySQL Community Server
       Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2019-01-07 10:57:27 CST; 2min 42s ago
     Main PID: 26065 (mysqld)
       CGroup: /system.slice/mysql.service
               └─26065 /usr/sbin/mysqld
  4. 为了避免数据库存放的中文是乱码,执行以下命令设置MySQL的编码。

    sudo 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生效:

    sudo /usr/sbin/service mysql restart

  5. 建立Ghost数据库。

    以 root 身份登录MySQL然后创建一个名为ghost的数据库并验证创建结果:

    mysql -u root -p;

    mysql> create database ghost;

    mysql> show databases;

    mysql> exit

Ghost 安装与配置

Ghost v1.0.0 及以上版本已加入了Ghost-CLI,因此可以直接安装配置Ghost-CLI。

  1. 安装Ghost-CLI。

    sudo npm i -g ghost-cli

  2. 创建一个文件夹,之前介绍过Nginx的重要文件结构 /var/www/ 就是我们将要创建文件夹的地方。

    sudo mkdir -p /var/www/ghost

    ghost在/root文件夹中安装Ghost 将无法正常工作。

  3. 配置权限。

    sudo chown [user]:[user] /var/www/ghost

    [user] 是我们在安装阶段创建的用户。

  4. 进入我们刚才创建的文件夹。

    cd /var/www/ghost/

  5. 执行以下命令,用Ghost-CLI 安装Ghost。
    ghost install

    如果Ghost安装时提示node版本不匹配,可以在nodejs官方网站查询相应的版本重新安装。

    https://nodejs.org/en/download/

  6. 配置Ghost。

    如果上面在 /var/www/ghost/ 目录下运行ghost install成功的话,会要求配置一些东西:

    您可以根据需要进行简单的配置。如果配置后需要修改可以通过以下命令去文件中进行配置:

    vi config.production.json

    打开之后就是我们的生产环境配置内容了。参考配置见下图:

验证Ghost搭建完成

如果成功的安装Ghost之后,就可以通过域名访问到Ghost博客。

分享:

    相关文档

    相关产品