This issue only applies to QGIS Server version 2.18.x. QGIS Server 3.x doesn’t need this fix!

If you are using QGIS Server 2.18 you surely experienced QGIS Server crashed (HTTP Error 500 Internal Server Error) when using GetPrint request. This is quite serious issue and hard to examine because there aren’t any useful information about that in Apache or QGIS Server logs.  Testing this showed that error only occurs when you are using print template with text elements, if you are using just map is working (also with scale and legend).

Solution

This is only for Linux based systems (no matter if you are using complete Dektop install with GUI or just bash version), there is no known solution for Windows so far.

    1. Open command line interface and enter sudo mode with
      sudo -s
    2. Install Xvfb ( display server)
      apt-get install xvfb
    3. Add display number to Apache configuration
      FcgidInitialEnv DISPLAY ":99"
      For users of Extended QGIS Web client: edit gisapp.conf
      nano /etc/apache2/conf-enabled/gisapp.conf
      Find part with FcgidInitialEnv variables and add display number there
    4. Add cron job to start display server with number 99 on every reboot
      crontab -e
      Add this two lines at the bottom of the file
      #fake X server, needed for QGIS Server 2.18 printing to PDF
      @reboot /usr/bin/Xvfb :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset

      Save
    5. Reboot server
      reboot

Now you can try again and QGIS Server should return you valid PDF print.

Source

https://issues.qgis.org/issues/18747

https://docs.qgis.org/testing/en/docs/user_manual/working_with_ogc/server/getting_started.html#id7