Help Center> Elastic Cloud Server> Best Practices> Manually Deploying a Ghost Blog
Updated at: 2022-04-24 GMT+08:00

Manually Deploying a Ghost Blog

Ghost is an open source blog platform based on Node.js and makes writing and release more convenient. This section walks you through the deployment of a Ghost blog on an ECS running Ubuntu 16.04.

Installing GCC and g++

  1. Run the following command to install the common development and compilation tool packages:

    sudo apt-get install build-essential

  2. Run the following command to install the GNU Compiler Collection (GCC):

    apt-get install gcc

  3. Run the following command to query the GCC version:

    gcc --version

    The following information is displayed:

    root@ecs-c47c:~# gcc --version
    gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
    Copyright (C) 2015 Free Software Foundation, Inc.
  4. Run the following command to install g++:

    sudo apt-get install g++

  5. Run the following command to query the g++ version:

    g++ --version

    The following information is displayed:

    root@ecs-c47c:~# g++ --version
    g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
    Copyright (C) 2015 Free Software Foundation, Inc.

Installing Node.js

  1. Run the following commands to install Node.js:

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

    sudo apt-get install -y nodejs

  2. Run the following commands to query the version of Node.js and Node Package Manager (npm), respectively:

    node -v

    npm -v

    The following information is displayed:

    root@ecs-c47c:~# node -v
    v9.11.2
    root@ecs-c47c:~# npm -v
    5.6.0

Installing Nginx

Before deploying the Ghost blog, you need to install Nginx and use it as an HTTP server. The following operations use Nginx 1.10.0 as an example.

  1. Run the following commands to install Nginx:

    sudo apt-get update

    sudo apt-get install nginx

  2. (Optional) Configure the firewall.

    Uncomplicated Firewall (UFW) is an iptables interface that simplifies the firewall configuration. By default, Ubuntu is installed with UFW. Run the following command to check the firewall status:

    sudo ufw status

    If you do not want to enable the firewall, skip this step. If you want to enable the firewall, run the following command:

    sudo ufw enable

    Verify that the firewall is enabled.

    Before testing Nginx, you need to reconfigure the firewall to allow access to Nginx. Run the following command to automatically register Nginx with UFW:

    sudo ufw app list

    The following information is displayed:

    Available applications:
      Nginx Full
      Nginx HTTP
      Nginx HTTPS
      ...
    • Nginx Full: Port 80 is enabled to distribute normal and unencrypted web traffic, and port 443 to handle encrypted TLS/SSL traffic.
    • Nginx HTTP: Only port 80 is enabled to distribute normal and unencrypted web traffic.
    • Nginx HTTPS: Only port 443 is enabled to distribute encrypted TLS/SSL traffic.

    Run the following command to ensure that the firewall allows HTTP and HTTPS connections:

    sudo ufw allow 'Nginx Full'

  3. Verify that Nginx can work properly.

    Use the domain name or IP address to access Nginx. The Welcome to nginx page is displayed if Nginx is started normally.

    Enter http://Nginx IP address in a browser address bar to visit Nginx. If the following page is displayed, Nginx has been installed.

  4. Configure Nginx.
    1. Create a configuration file.

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

    2. Add the following content to the configuration file:
      server {
          listen 80;
          server_name 119.3.xx.xxx.com; #Domain name or IP address
          location / {
              proxy_set_header   X-Real-IP $remote_addr;
              proxy_set_header   Host      $http_host;
              proxy_pass         http://127.0.0.1:2368;
          }
      }

      The reverse proxy has been written. You only need to set the value of server_name to your own top-level domain name.

    1. Run the following command to create a soft link between the configuration file and the sites-enabled directory:

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

    1. Restart Nginx.

      sudo service nginx restart

Creating a User

Performing operations as user root is not recommended by Ghost. Therefore, you need to create a new user and grant permissions to it.

  1. Run the following commands to create a user:

    adduser <user>

    The following information is displayed:

    Enter new UNIX password: 
    Retype new UNIX password: 
    passwd: password updated successfully
  2. Run the following command to add the newly created user to the user group:

    usermod -aG sudo <user>

    The following information is displayed:

    Changing the user information for sxm
    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] 
  3. Run the following command to switch to the created user:

    su - <user>

Installing MySQL

MySQL is an open-source database management system, which is usually installed as a part of the popular LAMP (Linux, Apache, MySQL, and PHP/Python/Perl) stack. MySQL uses relational databases and the structured query language (SQL) to manage data.

  1. Install MySQL.
    • Run the following command to update the software package:

      sudo apt-get update

    • Run the following command to install the mysql-server software package (during the installation, you will be asked to set the password of user root):

      sudo apt-get install mysql-server

  2. Configure MySQL.

    Run the following command and perform operations as prompted to harden MySQL:

    mysql_secure_installation

    Securing the MySQL server deployment.
    
    Enter password for user root:    #Enter the obtained password of user root.
    The existing password for the user account root has expired. Please set a new password.
    
    New password:  #Enter the new password.
    
    Re-enter new password:   #Enter the new password again.
    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   #Asks you whether to change the password of user root. Press 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   #Asks you whether to remove anonymous users. Press 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   #Asks you whether to forbid remote login of user root. Press 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   #Asks you whether to delete the test database and cancel access permissions to it. Press 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   #Asks you whether to reload privilege tables. Press y.
    Success.
    
    All done!
  3. Test MySQL.

    Run the following command to check the MySQL status:

    systemctl status mysql.service

    If MySQL is in normal status, the following information is displayed:

    ● 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. To avoid garbled characters in the database, run the following command to set the MySQL code:

    sudo vi /etc/my.cnf

    Copy and paste the following content:

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

    Save and exit. Then, run the following command to restart MySQL:

    sudo /usr/sbin/service mysql restart

  5. Create a Ghost database.

    Log in to MySQL as user root, create a database named ghost, and verify that the database has been successfully created.

    mysql -u root -p;

    mysql> create database ghost;

    mysql> show databases;

    mysql> exit

Installing and Configuring Ghost

Ghost-CLI has been added to Ghost v1.0.0 and later versions. You can directly install and configure Ghost-CLI.

  1. Run the following command to install Ghost-CLI:

    sudo npm i -g ghost-cli

  2. Create a folder named ghost under /var/www/.

    sudo mkdir -p /var/www/ghost

    If ghost is created under /root, Ghost cannot work properly.

  3. Run the following command to grant user permissions to ghost:

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

    [user] is the newly created user.

  4. Run the following command to switch to the created folder:

    cd /var/www/ghost/

  5. Run the following command to install Ghost using Ghost-CLI: ghost install

    If a message is displayed indicating that the node version does not match, obtain the required version on the official website of Node.js and reinstall Ghost.

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

  6. Configure Ghost.

    If ghost install is successfully executed in the /var/www/ghost/ directory, configure some items as prompted.

    You can run the following command to modify the configuration:

    vi config.production.json

    The following is a sample for your reference.

Verifying Blog Access

If Ghost is successfully installed, you can access the Ghost blog using the domain name.

close