Step 1: Start Sphinx using Default Parameters

Objective: Create a Sphinx container from a project on GitHub without modifications.

It is helpful to start with a project that we know works before making modifications. We will use a project from GitHub called dldl/sphinx-server. GitHub is a repository for open-source projects.

Note

We will use port 20852 for this project.

4.1.1 Start Sphinx Container

  1. First, we will clone the dldl/sphinx-server project.

    Cloning a GitHub project using Ubuntu 18.04 is easy! You use the command git clone with the site to clone as the argument.

    1. Go to the dldl/sphinx-server project page

    2. Copy the project clone URL

    3. Clone the project using git clone <project-url.git>

      • Change to the root directory using cd ~ or change to the directory of your choice before cloning the project

    4. This will create a folder called sphinx-server

    ../../_images/git-sphinx-clone.png
    cd ~
    git clone https://github.com/dldl/sphinx-server.git
    cd sphinx-server
    
    Output
    root@vps298933:~#    cd ~
    root@vps298933:~#    git clone https://github.com/dldl/sphinx-server.git
    Cloning into 'sphinx-server'...
    remote: Enumerating objects: 242, done.
    remote: Total 242 (delta 0), reused 0 (delta 0), pack-reused 242
    Receiving objects: 100% (242/242), 38.09 KiB | 443.00 KiB/s, done.
    Resolving deltas: 100% (127/127), done.
    root@vps298933:~#    cd sphinx-server
    root@vps298933:~/sphinx-server#
    root@vps298933:~/sphinx-server# ls -lh
    total 16K
    -rw-r--r-- 1 root root  739 Apr  4 10:34 Dockerfile
    -rw-r--r-- 1 root root 1.1K Apr  4 10:34 LICENCE
    -rw-r--r-- 1 root root 2.9K Apr  4 10:34 README.md
    -rw-r--r-- 1 root root 3.5K Apr  4 10:34 server.py
    root@vps298933:~/sphinx-server#
    
  2. Now, let’s start the project using default parameters from the project instructions.

    1. From dldl/sphinx-server, Copy and paste the docker run command in section With autobuild enabled

    2. Change the port to an open port on the VPS. Let’s use 20852

    3. Verify the container is running using docker ps

    4. Use curl --head localhost:20852 to verify the container is responding to HTTP requests.

      • curl will respond with error HTTP/1.1 404 Not Found

      • However, we know the server is working based on the HTTP request (Server: TornadoServer/5.1.1)

    ../../_images/git-sphinx-docker-run.png
    docker run -itd -v "$(pwd)":/web -u $(id -u):$(id -g) -p 20852:8000 --name sphinx-server dldl/sphinx-server
    docker ps
    curl --head localhost:20852
    

    Here are the docker run arguments.

    -itd
    • it runs the container with an interactive shell.

    • d starts the container in daemon mode (runs in the background).

    -v "$(pwd)":/web

    Defines a volume (/web) to mount in the current directory.

    • $(pwd) command returns the current directory, the location where the user executes docker-run.

    • /web is the directory in the container to mount on the VPS. This directory contains the config file and web documents.

    • It will mount the directory on the localhost (VPS) at ~/sphinx-server/web

    -u $(id -u):$(id -g)

    Sets the user and user ID ofthe container to match that of the current user.

    -p 20852:8000

    Opens port 20852 on the VPS and directs that traffic to port 8000 on the container.

    • 20852: External port (in VPS)

    • 8000: Internal port (in container)

    --name sphinx-server

    Sets the name of the container

    dldl/sphinx-server

    The name of the container to run.

    • Docker looks for this container locally, on the VPS.

    • It downloads the container from Docker Hub if it does not reside on the VPS

    Output
    root@vps298933:~/sphinx-server# docker run -itd -v "$(pwd)":/web -u $(id -u):$(id -g) -p 20852:8000 --name sphinx-server dldl/sphinx-server
    5fd156bd14131f0bb2e17b23f8901cc97de1f6fa6c689a7a283757f7e7cae2f8
    root@vps298933:~/sphinx-server#
    root@vps298933:~/sphinx-server# docker ps
    CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                                          NAMES
    5fd156bd1413        dldl/sphinx-server   "python /opt/sphinx-…"   4 seconds ago       Up 2 seconds        8093/tcp, 35729/tcp, 0.0.0.0:20852->8000/tcp   sphinx-server
    76cbf38e5975        nextcloud            "/entrypoint.sh apac…"   4 days ago          Up 19 hours         0.0.0.0:20850->80/tcp                          nextclouddocker_app_1
    2906aaa944f5        mariadb              "docker-entrypoint.s…"   4 days ago          Up 19 hours         3306/tcp                                       nextclouddocker_db_1
    8f5b05b6c627        redis                "docker-entrypoint.s…"   4 days ago          Up 19 hours         6379/tcp                                       nextclouddocker_redis_1
    c60b6e70e4a9        wordpress:latest     "docker-entrypoint.s…"   4 days ago          Up 19 hours         0.0.0.0:20851->80/tcp                          wordpressdocker_wordpress_1
    386e4587b991        mariadb              "docker-entrypoint.s…"   4 days ago          Up 19 hours         3306/tcp                                       wordpressdocker_db_1
    913ff74dd94c        redis                "docker-entrypoint.s…"   4 days ago          Up 19 hours         6379/tcp                                       wordpressdocker_redis_1
    root@vps298933:~/sphinx-server# curl --head localhost:20852
    HTTP/1.1 404 Not Found
    Date: Thu, 04 Apr 2019 05:37:21 GMT
    Content-Length: 69
    Content-Type: text/html; charset=UTF-8
    Server: TornadoServer/5.1.1
    
    root@vps298933:~/sphinx-server#
    

    Note

    Using docker run or docker-compose.yml have the same end result. Chose the methods that works for you.

    • We can use docker run if we won’t have many arguments.

      • You have to remember the specific docker run arguments when you remove and restart the container.

    • We can build a docker-compose.yml file.

      • Using a docker-compose stores the data in a file.

      • It takes more time to write, but the configuration is stored for easy modification or restarting.

4.1.2 Configure Sphinx

Getting Started with Sphinx informs us of the setup procedure. We have to run sphinx-quickstart and then make html. We need to run both of these commands in the container.

  1. Fist, open a shell into the container using the name of the container.

docker exec -it sphinx-server /bin/sh

Tip

docker exec allows us to execute a command on a container. Let’s look at this command before moving on.

  • docker exec -it <container> <command>

    • -i runs the container in interactive mode that can accept user input.

    • -t creates a terminal inside of the container.

    • container to execute the command in.

    • command to execute, such as staring the sh shell.

  1. Then, run the init program sphinx-quickstart

    • Use the default option for most choices. Press Enter.

    • Separate source and build directories (y/n) [n]:

    • Name prefix for templates and static dir [_]:

    • Project name: Enter a name

    • Author name(s): Enter a name

    • Project release []:

    • Project language [en]: Press Enter or ru

    • Source file suffix [.rst]:

    • Name of your master document (without suffix) [index]:

    • autodoc: automatically insert docstrings from modules (y/n) [n]: y

    • doctest: automatically test code snippets in doctest blocks (y/n) [n]:

    • intersphinx: link between Sphinx documentation of different projects (y/n) [n]:

    • todo: write “todo” entries that can be shown or hidden on build (y/n) [n]:

    • coverage: checks for documentation coverage (y/n) [n]:

    • imgmath: include math, rendered as PNG or SVG images (y/n) [n]:

    • mathjax: include math, rendered in the browser by MathJax (y/n) [n]:

    • ifconfig: conditional inclusion of content based on config values (y/n) [n]:

    • viewcode: include links to the source code of documented Python objects (y/n) [n]:

    • githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:

    • Create Makefile? (y/n) [y]:

    • Create Windows command file? (y/n) [y]: Press Enter or n

    sphinx-quickstart
    
    Output
    root@vps298933:~# docker exec -it sphinx-server /bin/sh
    /web #
    /web # sphinx-quickstart
    Welcome to the Sphinx 1.8.5 quickstart utility.
    
    Please enter values for the following settings (just press Enter to
    accept a default value, if one is given in brackets).
    
    Selected root path: .
    
    You have two options for placing the build directory for Sphinx output.
    Either, you use a directory "_build" within the root path, or you separate
    "source" and "build" directories within the root path.
    > Separate source and build directories (y/n) [n]:
    
    Inside the root directory, two more directories will be created; "_templates"
    for custom HTML templates and "_static" for custom stylesheets and other static
    files. You can enter another prefix (such as ".") to replace the underscore.
    > Name prefix for templates and static dir [_]:
    
    The project name will occur in several places in the built documentation.
    > Project name: Test Sphinx Project
    > Author name(s):
    * Please enter some text.
    > Author name(s): jj8i.cc
    > Project release []:
    
    If the documents are to be written in a language other than English,
    you can select a language here by its language code. Sphinx will then
    translate text that it generates into that language.
    
    For a list of supported codes, see
    http://sphinx-doc.org/config.html#confval-language.
    > Project language [en]: ru
    
    The file name suffix for source files. Commonly, this is either ".txt"
    or ".rst".  Only files with this suffix are considered documents.
    > Source file suffix [.rst]:
    
    One document is special in that it is considered the top node of the
    "contents tree", that is, it is the root of the hierarchical structure
    of the documents. Normally, this is "index", but if your "index"
    document is a custom template, you can also set this to another filename.
    > Name of your master document (without suffix) [index]:
    Indicate which of the following Sphinx extensions should be enabled:
    > autodoc: automatically insert docstrings from modules (y/n) [n]: y
    > doctest: automatically test code snippets in doctest blocks (y/n) [n]:
    > intersphinx: link between Sphinx documentation of different projects (y/n) [n]:
    > todo: write "todo" entries that can be shown or hidden on build (y/n) [n]:
    > coverage: checks for documentation coverage (y/n) [n]:
    > imgmath: include math, rendered as PNG or SVG images (y/n) [n]:
    > mathjax: include math, rendered in the browser by MathJax (y/n) [n]:
    > ifconfig: conditional inclusion of content based on config values (y/n) [n]:
    > viewcode: include links to the source code of documented Python objects (y/n) [n]:
    > githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:
    
    A Makefile and a Windows command file can be generated for you so that you
    only have to run e.g. 'make html' instead of invoking sphinx-build
    directly.
    > Create Makefile? (y/n) [y]:
    > Create Windows command file? (y/n) [y]:
    
    Creating file ./conf.py.
    Creating file ./index.rst.
    Creating file ./Makefile.
    Creating file ./make.bat.
    
    Finished: An initial directory structure has been created.
    
    You should now populate your master file ./index.rst and create other
    documentation
    source files. Use the Makefile to build the docs, like so:
    make builder
    where "builder" is one of the supported builders, e.g. html, latex or linkcheck.
    
    /web #
    

    Tip

    File conf.py contains the configuration for Sphinx. The file was created after running sphinx-quickstart.

  2. Then, run make html to build the project

    make html
    
    Output
    /web # make html
    Running Sphinx v1.8.5
    loading translations [ru]... done
    building [mo]: targets for 0 po files that are out of date
    building [html]: targets for 1 source files that are out of date
    updating environment: 1 added, 0 changed, 0 removed
    reading sources... [100%] index
    looking for now-outdated files... none found
    pickling environment... done
    checking consistency... done
    preparing documents... done
    writing output... [100%] index
    generating indices... genindex
    writing additional pages... search
    copying static files... done
    copying extra files... done
    dumping search index in Russian (code: ru) ... done
    dumping object inventory... done
    build succeeded.
    
    The HTML pages are in _build/html.
    /web #
    
  3. Exit the shell and then verify that the Sphinx server is working using curl

    • We should see an HTTP/1.1 200 OK code

    curl --head localhost:20852
    curl localhost:20852
    
    Output
    root@vps298933:~# curl --head localhost:20852
    HTTP/1.1 200 OK
    Content-Length: 3848
    Accept-Ranges: bytes
    Server: TornadoServer/5.1.1
    Last-Modified: Fri, 05 Apr 2019 10:24:27 GMT
    Etag: "4548821785384f4640015849281549fd"
    Date: Fri, 05 Apr 2019 10:27:10 GMT
    Content-Type: text/html
    
    root@vps298933:~# curl localhost:20852
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="ru">
      <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Welcome to Test Sphinx Project’s documentation! &#8212; Документация Test Sphinx Project </title>
    
  4. Enable the sphinx_rtd_theme

    The default Sphinx theme is alabaster. We want to use Read the Docs Theme. The installation guide describes the change that we need to make to the config file.

    Note

    If you want to preview the alabaster theme, configure the reverse proxy, then enable the Read the Docs theme.

    • Edit the Sphinx config file: nano conf.py

    • Find property html_theme = 'alabaster' on line 78

    • Comment out that line using the #

    • Add Read the Docs theme: html_theme = 'sphinx_rtd_theme'

    conf.py
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    # -- Options for HTML output-------------------------------------------------
    
    # The theme to use for HTML and HTML Help pages.  See the documentation for
    # a list of builtin themes.
    #
    #html_theme = 'alabaster'
    html_theme = 'sphinx_rtd_theme'
    
    # Theme options are theme-specific and customize the look and feel of a theme
    # further.  For a list of options available for each theme, see the
    

4.1.3 Create a Reverse Proxy

Lastly, we need to create a reverse proxy.

  1. Create a subdomain and then link it to Sphinx container. Suggestions are sphinx, docs, or guides.

  2. Test public access to the service using curl.

  3. Create an SSL cert using certbot