Skip to main content
  1. Posts/

Optimize Ghost for SEO - SiteMap generator

·403 words·2 mins· loading · loading · ·
Marco Mornati
Author
Marco Mornati
I build, self-host and sometimes break software in production — writing about AI tooling, coding agents and infrastructure.

Overview
#

An important thing to be well referenced on search engines (is there anything other than Google? :D) is the sitemap.xml. In this file you should list all the pages of your website with some parameters to instruct the reader (the search engine robot for example) about the last update of a page, the update frequency and page priority.

SiteMap.xml

After some tests of scripts found online, I decided to create a specific sitemap generator for the Ghost blogging platform. The reasons is that all scripts I found and tests check for pages contacting Ghost through the web server (asking for pages and checking all links on each page). This is surely a good way to identify all pages and resources of your website automatically… BUT it could take a long time to be generated.

The Script
#

With the script I created, which you can find on github repository, all pages for the sitemap file are generated simply reading the database:

This way, your sitemap is generated in seconds.

Currently the script works only for MySQL database (that is my actual installation), but with some little extensions we can add any other possible database.

Usage
#

If you execute the script without parameters a help document is shown on the screen:

ruby generate_ghost_sitemap.rb 
Missing options: site, priority, frequency, destfile, hostname, user, password, dbname
Usage: generate_ghost_sitemap.rb [options]
    -h, --help                       Display this screen
    -s, --site SITE                  Site base URL. EX: blog.mornati.net
    -f, --frequency FREQUENCY        Update Frenquency. One of: always,hourly,daily,weekly,monthly,yearly,never
    -p, --priority PRIORITY          Update priority. Values between 0.0 et 1.0
    -d, --destfile DESTFILE          Sitemap destination file. Ex. /usr/share/server/sitemap.xml
    -t, --test                       Do not ping Google after sitemap generation
    -v, --verbose                    Verbose execution
    -m, --mysql HOSTNAME             MySQL hostname
    -u, --user USERNAME              MySQL Username
    -w, --password PASSWORD          MySQL Password
    -b, --dbname DBNAME              Database name

Automation
#

So, to use it, you can simply add a cron job with all required parameters to access to your Ghost DB and to generate the sitemap file. The user executing cron must have access to the sitemap folder (so normally should be: root/apache/nginx).

For example:

0 0 * * * /usr/bin/ruby /root/generate_ghost_sitemap.rb -s blog.mornati.net -p 0.5 -f daily -m localhost -u ghost -w mypasswd -b ghost -v -d /usr/share/nginx/ghost/sitemap.xml

The script’s execution is scheduled any day at midnight with all the options you can read ;)