如何在Debian 8上部署Ghost v0.11 LTS
杨和超
・17 分钟阅读
使用不同的系统?
Ghost是一个开源博客平台,自2013年发布以来,在开发人员和普通用户中越来越受欢迎。它把焦点放在内容和博客上,Ghost最吸引人的地方在于它简洁,干净,反应灵敏的设计。你可以用手机写你的博客文章,Ghost的内容使用Markdown语言编写。
在本指南中,我们将使用Let's Encrypt,certbot,Node.js,mysQL ,NPM,nginx,在Debian 8 VPS上设置和部署安全的Ghost v0.11 .x LTS。
要求
- 注册(购买)域名。
- Debian 8服务器实例,最小内存为1GB。
- sudo用户,
开始之前
检查Debian版本:
lsb_release -ds # Debian GNU/Linux 8.9 (jessie)
创建新的非root用户帐户:
adduser johndoe --gecos"John Doe"
将它添加到
sudo
组中,使它成为超级用户:usermod -aG sudo johndoe
切换到新用户:
su -johndoe
更新你的操作系统的操作系统:
sudo apt-get update && sudo apt-get upgrade -y
设置时区:
sudo dpkg-reconfigure tzdata
安装所需工具:
sudo apt-get install -y build-essential zip unzip git apt-transport-https
如果需要,重新引导系统:
sudo shutdown -r now
安装Certbot
注意:在开始此步骤之前,请确保已为你的域名设置了DNS记录。
我们将使用 Let's Encrypt CA和EFF的Certbot客户端来获取我们的Ghost博客的SSL/TLS证书,不要忘记用你的域名替换blog.domain.tld
的所有实例。
安装Certbot (以前叫let'sencrypt client )证书管理软件:
sudo -s printf"deb http://ftp.debian.org/debian jessie-backports main" >> /etc/apt/sources.list.d/jessie-backports.list exit sudo apt-get update sudo apt-get install -y certbot -t jessie-backports
检查Certbot版本:
certbot --version # certbot 0.10.2
使用独立身份验证方法(插件)获得RSA证书:
sudo certbot certonly --standalone --domains blog.domain.tld --rsa-key-size 2048 --must-staple --email admin@domain.tld --agree-tos # IMPORTANT NOTES: # - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/blog.domain.tld/fullchain.pem. # Your cert will expire on YYYY-MM-DD. . . . # . . .
经过前面的步骤后,你的证书和私钥将在
/etc/letsencrypt/live/blog.domain.tld
目录。
安装Node.js和NPM
注意:Ghost目前只支持Node.js版本4.5 +和6.9 +。
Ghost是基于node.js构建的,我们将安装v6 Boron LTS的推荐版本。
下载并安装Node.js v6 :
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs
检查Node.js和NPM版本:
node -v && npm -v # v6.11.2 # 3.10.10
安装MySQL
默认情况下,ghost配置为使用SQLite数据库,该数据库不需要配置。
Ghost也可以通过改变数据库配置与MySQL数据库一起使用,必须先创建一个数据库和用户,然后才能更改现有的sqlite3配置。
下载并安装MySQL :
sudo apt-get install -y mysql-server
注意:安装期间,会提示你输入"root"用户密码,你应该为MySQL"root"用户设置一个安全密码。
检查MySQL版本:
mysql --version # mysql Ver 14.14 Distrib 5.5.57, for debian-linux-gnu (x86_64) using readline 6.3
检查MySQL守护进程是否已启动,并且正在运行:
sudo systemctl status mysql.service sudo systemctl is-enabled mysql.service
运行
mysql_secure_installation
脚本,使你的数据库安全一点:sudo mysql_secure_installation
作为root用户登录到MySQL :
mysql -u root -p # Enter password:
创建一个新的MySQL数据库和用户:
create database dbname; grant all on dbname.* to 'user' identified by 'password';
退出MySQL :
exit
安装Nginx
下载并安装官方Nginx版本库中最新版本的Nginx :
wget https://nginx.org/keys/nginx_signing.key sudo apt-key add nginx_signing.key rm nginx_signing.key sudo -s printf"deb https://nginx.org/packages/mainline/debian/ `lsb_release -sc` nginx ndeb-src https://nginx.org/packages/mainline/debian/ `lsb_release -sc` nginx n" >> /etc/apt/sources.list.d/nginx_mainline.list exit sudo apt-get update sudo apt-get install -y nginx nginx-module-geoip nginx-module-image-filter nginx-module-njs nginx-module-perl nginx-module-xslt nginx-nr-agent
通过检查Nginx版本来验证它是否已安装:
sudo nginx -v # nginx version: nginx/1.13.4
检查状态,启用并启动Nginx服务(守护进程):
sudo systemctl status nginx.service # inactive (dead) sudo systemctl enable nginx.service sudo systemctl start nginx.service
创建/etc/nginx/ssl目录,并且生成一个新的Diffie-Hellman (DH )组:
sudo mkdir -p /etc/nginx/ssl sudo openssl dhparam -out /etc/nginx/ssl/dhparams-2048.pem 2048
为
blog.domain.tld
虚拟主机创建日志目录:sudo mkdir -p /var/log/nginx/blog.domain.tld
将Nginx配置为HTTP (S)反向代理服务器:
sudo vim /etc/nginx/conf.d/ghost.conf
在
/etc/nginx/conf.d/ghost.conf
中粘贴以下内容:# domain: blog.domain.tld # public: /var/www/ghost upstream ghost_app { server 127.0.0.1:2368; keepalive 32; } server { listen [::]:80 default_server; listen 80 default_server; listen [::]:443 ssl http2 default_server; listen 443 ssl http2 default_server; server_name blog.domain.tld; # Change to your domain/hostname root /var/www/ghost; # Change to the path where Ghost is error_log /var/log/nginx/blog.domain.tld/error.log; access_log /var/log/nginx/blog.domain.tld/access.log; client_max_body_size 100M; ssl_certificate /etc/letsencrypt/live/blog.domain.tld/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/blog.domain.tld/privkey.pem; ssl_dhparam ssl/dhparams-2048.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS'; ssl_prefer_server_ciphers on; ssl_buffer_size 4K; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50M; ssl_session_tickets off; ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certificate /etc/letsencrypt/live/blog.domain.tld/chain.pem; resolver 8.8.8.8 8.8.4.4 valid=300s; location / { proxy_pass http://ghost_app; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_hide_header X-Powered-By; proxy_http_version 1.1; proxy_set_header Connection""; } }
保存,并且测试NGINX配置是否存在语法错误:
sudo nginx -t
重新加载Nginx配置:
sudo systemctl reload nginx.service
安装Ghost
注意:如果要在同一VPS上托管多个Ghost博客,则每个Ghost实例必须在单独的端口上运行。
创建文档root目录:
sudo mkdir -p /var/www/
创建一个新的
ghost
用户:sudo adduser --shell /bin/bash --gecos 'Ghost application' --disabled-password ghost
下载Ghost :
curl -L https://github.com/TryGhost/Ghost/releases/download/0.11.11/Ghost-0.11.11.zip -o ghost.zip
将Ghost解压到
/var/www/ghost
目录(推荐安装位置):sudo unzip -uo ghost.zip -d /var/www/ghost rm ghost.zip
导航到新的
ghost
目录:cd/var/www/ghost
更改
/var/www/ghost
目录的所有权:sudo chown -R ghost:ghost .
切换到新
ghost
用户:sudo su - ghost
导航到文档root
/var/www/ghost
:cd/var/www/ghost
仅安装有生产相关性的Ghost ,完成这个操作后,将安装ghost :
npm install --production
通过在
config.js
文件内部更改生产对象的url
,mail
和database
属性配置Ghost :cp config.example.js config.js vim /var/www/ghost/config.js var path = require('path'), config; config = { // ### Production // When running Ghost in the wild, use the production environment. // Configure your URL and mail settings here production: { url: 'https://blog.domain.tld', mail: { transport: 'SMTP', options: { service: 'Mailgun', auth: { user: '', pass: '' } } }, database: { client: 'mysql', connection: { host: '127.0.0.1', user: 'your_database_user', password: 'your_database_password', database: 'your_database_name', charset: 'utf8' }, debug: false }, // . . . // . . .
注:你还应该配置邮件设置。
在生产环境中启动Ghost :
npm start --production
Ghost现在正在运行。你可以打开你的浏览器,并且访问
https://blog.domain.tld
站点,不要忘记用你的域名替换blog.domain.tld
。通过按
CTRL
+C
,并且退出ghost
用户回到root用户关闭虚拟进程:exit
将Ghost作为系统服务运行
创建
ghost.service
Systemd单元文件,运行sudo vim /etc/systemd/system/ghost.service
复制/粘贴以下内容:[Unit] Description=Ghost - the professional publishing platform Documentation=https://docs.ghost.org/v0.11.11/docs After=network.target [Service] Type=simple # Edit WorkingDirectory, User and Group as needed WorkingDirectory=/var/www/ghost User=ghost Group=ghost ExecStart=/usr/bin/npm start --production ExecStop=/usr/bin/npm stop --production Restart=always SyslogIdentifier=Ghost [Install] WantedBy=multi-user.target
启用并启动
ghost.service
:sudo systemctl enable ghost.service && sudo systemctl start ghost.service
检查
ghost.service
状态:sudo systemctl status ghost.service && sudo systemctl is-enabled ghost.service
导航到
https://blog.domain.tld/ghost/
,并且创建一个Ghost管理员用户,
结束语
而已 ,我们现在有一个功能齐全的Ghost博客。如果要将名为casper的默认Ghost主题更改为自定义主题,你只需下载主题,并且解压缩到/var/www/ghost/content/themes文件夹中,然后通过位于https://blog.domain.tld/ghost的Ghost管理界面选择它。