Outdated version of the documentation. Find the latest one here.

12.1. Lesson: QGIS Server installeren

The goal for this lesson: To learn how to install QGIS Server on Debian Stretch. With negligible variations (prepending sudo to all admin commands) you can also follow it for any Debian based distribution like Ubuntu and its derivatives.

12.1.1. moderate Follow Along: Installeren vanuit pakketten

In this lesson we’re going to do only the install from packages as shown here .

First add the QGIS repository by creating the /etc/apt/sources.list.d/debian-qgis.list file with the following content:

# latest stable
deb http://qgis.org/debian stretch main
deb-src http://qgis.org/debian stretch main

After you add the qgis.org repository public key to your apt keyring (follow the above link on how to do it) you can run the apt-get update command to refresh the packages list and apt-get dist-upgrade to upgrade the packages.

Notitie

Currently Debian stable has LTR qgis packages in the source jessie-backports, so the above steps are not necessary. Just add the jessie-backports repository and install with the -t jessie-backports option.

QGIS Server installeren met:

apt-get install qgis-server python-qgis

Notitie

adding -y at the end of the apt-get command will run it straight away, without requiring confirmation.

QGIS Server in productie moeten worden gebruikt zonder geïnstalleerde QGIS Desktop (met de daarbij behorende X Server) op dezelfde machine.

12.1.2. moderate Follow Along: QGIS Server uitvoerbare bestand

Het voor QGIS Server uitvoerbare ebstand is qgis_mapserv.fcgi. U kunt controleren waar het is geïnstalleerd door find / -name 'qgis_mapserv.fcgi' uit te voeren wat iets uit zou moeten uitvoeren als /usr/lib/cgi-bin/qgis_mapserv.fcgi.

Indien u, optioneel, op dit moment een test op de opdrachtregel wilt uitvoeren, kunt u de opdracht /usr/lib/cgi-bin/qgis_mapserv.fcgi die iets uit zou moeten voeren als:

QFSFileEngine::open: No file name specified
Warning 1: Unable to find driver ECW to unload from GDAL_SKIP environment variable.
Warning 1: Unable to find driver ECW to unload from GDAL_SKIP environment variable.
Warning 1: Unable to find driver JP2ECW to unload from GDAL_SKIP environment variable.
Warning 1: Unable to find driver ECW to unload from GDAL_SKIP environment variable.
Warning 1: Unable to find driver JP2ECW to unload from GDAL_SKIP environment variable.
Content-Length: 206
Content-Type: text/xml; charset=utf-8

<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc">
 <ServiceException code="Service configuration error">Service unknown or unsupported</ServiceException>
</ServiceExceptionReport>

Dat is goed, het vertelt u dat we op het juiste spoor zitten omdat de server zegt dat we niet hebben gevraagd naar een ondersteunde service. We zullen later zien hoe verzoeken voor WMS te maken.

12.1.3. Follow Along: HTTP serverconfiguratie

We moeten een HTTP-server gebruiken om toegang te krijgen tot de geïnstalleerde QGIS-server via een internetbrowser.

In this lesson we’re going to use the Apache HTTP server, colloquially called Apache.

We moeten eerst Apache installeren door de volgende opdracht in een terminal uit te voeren: apt-get install apache2 libapache2-mod-fcgid.

Laten we in de map /etc/apache2/sites-available een bestand maken, genaamd qgis.demo.conf, met deze inhoud:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName qgis.demo

  DocumentRoot /var/www/html

  # Apache logs (different than QGIS Server log)
  ErrorLog ${APACHE_LOG_DIR}/qgis.demo.error.log
  CustomLog ${APACHE_LOG_DIR}/qgis.demo.access.log combined

  # Longer timeout for WPS... default = 40
  FcgidIOTimeout 120

  FcgidInitialEnv LC_ALL "en_US.UTF-8"
  FcgidInitialEnv PYTHONIOENCODING UTF-8
  FcgidInitialEnv LANG "en_US.UTF-8"

  # QGIS log (different from apache logs) see http://docs.qgis.org/testing/en/docs/user_manual/working_with_ogc/ogc_server_support.html#qgis-server-logging
  FcgidInitialEnv QGIS_SERVER_LOG_FILE /var/log/qgis/qgisserver.log
  FcgidInitialEnv QGIS_SERVER_LOG_LEVEL 0

  FcgidInitialEnv QGIS_DEBUG 1

  # default QGIS project
  SetEnv QGIS_PROJECT_FILE /home/qgis/projects/world.qgs

  # QGIS_AUTH_DB_DIR_PATH must lead to a directory writeable by the Server's FCGI process user
  FcgidInitialEnv QGIS_AUTH_DB_DIR_PATH "/home/qgis/qgisserverdb/"
  FcgidInitialEnv QGIS_AUTH_PASSWORD_FILE "/home/qgis/qgisserverdb/qgis-auth.db"

  # See http://docs.qgis.org/testing/en/docs/user_manual/working_with_vector/supported_data.html#pg-service-file
  SetEnv PGSERVICEFILE /home/qgis/.pg_service.conf
  FcgidInitialEnv PGPASSFILE "/home/qgis/.pgpass"

  # Tell QGIS Server instances to use a specific display number
  FcgidInitialEnv DISPLAY ":99"

  # if qgis-server is installed from packages in debian based distros this is usually /usr/lib/cgi-bin/
  # run "locate qgis_mapserv.fcgi" if you don't know where qgis_mapserv.fcgi is
  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin/">
    AllowOverride None
    Options +ExecCGI -MultiViews -SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>

 <IfModule mod_fcgid.c>
 FcgidMaxRequestLen 26214400
 FcgidConnectTimeout 60
 </IfModule>

</VirtualHost>

You can do the above in a linux Desktop system by pasting and saving the above configuration after doing sudo nano /etc/apache2/sites-available/qgis.demo.conf.

Notitie

Bekijk enkele van de opties voor configuratie die worden uitgelegd in het gedeelte voor de Server Gevorderde configuratie.

Laten we nu de mappen maken waarin de logboeken voor de QGIS Server en de database voor authenticatie zullen worden opgeslagen:

sudo mkdir /var/log/qgis/
sudo chown www-data:www-data /var/log/qgis

mkdir /home/qgis/qgisserverdb
sudo chown www-data:www-data /home/qgis/qgisserverdb

Notitie

www-data is de gebruiker van Apache op systemen die zijn gebaseerd op Debian en we dienen er voor te zorgen dat Apache toegang heeft tot deze locaties of bestanden. De opdrachten chown www-data... wijzigen de eigenaar van de respectievelijke mappen en bestanden naar www-data.

We kunnen nu de virtuele host inschakelen, schakel de module fcgid in als dat nog niet is gebeurd en start de service apache2 opnieuw:

sudo a2enmod fcgid
sudo a2ensite qgis.demo
sudo service apache2 restart

Notitie

Indien u QGIS Server installeerde zonder een X Server uit te voeren (opgenomen in Linux Desktop) en indien u ook de opdracht GetPrint wilt gebruiken dan zou u een namaak- X Server moeten installeren en QGIS Server moeten vertellen om die te gebruiken. U kunt dat doen door de volgende opdrachten uit te voeren.

Installeren van xvfb:

sudo apt-get install xvfb

Het servicebestand maken:

sudo sh -c \
"echo \
'[Unit]
Description=X Virtual Frame Buffer Service
After=network.target

[Service]
ExecStart=/usr/bin/Xvfb :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset

[Install]
WantedBy=multi-user.target' \
> /etc/systemd/system/xvfb.service"

Inschakelen, starten en de status controleren van de xvfb.service:

sudo systemctl enable xvfb.service
sudo systemctl start xvfb.service
sudo systemctl status xvfb.service

In the above configuration file there’s a FcgidInitialEnv DISPLAY ":99" that tells QGIS Server instances to use display no. 99. If you’re running the Server in Desktop then there’s no need to install xvfb and you should simply comment with # this specific setting in the configuration file. More info at http://www.itopen.it/qgis-server-setup-notes/.

Now that Apache knows that he should answer requests to http://qgis.demo we also need to setup the client system so that it knows who qgis.demo is. We do that by adding 127.0.0.1 qgis.demo in the hosts file. We can do it with sudo sh -c "echo '127.0.0.1 qgis.demo' >> /etc/hosts". Replace 127.0.0.1 with the IP of your server.

Notitie

Onthoud dat de beide bestanden myhost.conf en /etc/hosts zouden moeten worden geconfigureerd om onze instellingen te laten werken. U kunt ook de toegang tot uw QGIS Server testen vanaf andere cliënten op het netwerk (bijv. machines van Windows of MacOS) door te gaan naar hun bestand /etc/hosts en te verwijzen naar de naam myhost naar het IP dat de servermachine op het netwerk heeft. U kunt er zeker van zijn dat dat specifieke IP niet 127.0.0.1 is omdat dat het lokale IP is, dat alleen toegankelijk is vanaf de lokale machine. Op *nix-machines is het bestand hosts geplaatst in /etc, terwijl het op Windows staat in de map C:\Windows\System32\drivers\etc. Onder Windows dient u uw tekstbewerker te starten met rechten als administrator voordat u het bestand hosts opent.

We kunnen een van de geïnstalleerde QGIS-servers testen met een HTTP-verzoek vanaf de opdrachtregel met curl http://qgis.demo/cgi-bin/qgis_mapserv.fcgi wat terug zou moeten geven:

<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc">
<ServiceException code="Service configuration error">Service unknown or unsupported</ServiceException>
</ServiceExceptionReport>

Notitie

curl can be installed with sudo apt-get install curl.

Apache is nu geconfigureerd.

12.1.4. moderate Follow Along: Een andere virtuele host maken

Laten we eens een andere Apache virtuele host maken die verwijst naar QGIS Server. U kunt elke naam kiezen die u wilt (coco.bango, super.duper.training, example.com, etc.) maar omwille van de eenvoud gaan wij myhost gebruiken.

  • Let’s set up the myhost name to point to the localhost IP by adding 127.0.0.1 x to the /etc/hosts with the following command: sudo sh -c "echo '127.0.0.1 myhost' >> /etc/hosts" or by manually editing the file with sudo gedit /etc/hosts.
  • We kunnen controleren of myhost verwijst naar de localhost door in de terminal de opdracht ping myhost uit te voeren die terug zou moeten geven:

qgis@qgis:~$ ping myhost
PING myhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.029 ms
..
  • Laten we eens proberen of we toegang kunnen krijgen tot QGIS Server vanaf de site van myhost door uit te voeren: curl http://myhost/cgi-bin/qgis_mapserv.fcgi of door toegang te krijgen tot de URL via de browser van uw Debianbox. U zult waarschijnlijk terugkrijgen:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /cgi-bin/qgis_mapserv.fcgi was not found on this server.</p>
<hr>
<address>Apache/2.4.25 (Debian) Server at myhost Port 80</address>
</body></html>
  • Apache doesn’t know that he’s supposed to answer requests pointing to the server named myhost. In order to setup the virtual host the simplest way would be to make a myhost.conf file in the /etc/apache/sites-available directory that has the same content as file:qgis.demo.conf except for the ServerName line that should be ServerName myhost. You could also change where the logs go as otherwise the logs for the two virtual hosts would be shared but this is optional.
  • Let’s now enable the virtual host with sudo apt-get a2ensite myhost.conf and then reload the Apache service with sudo systemctl reload apache2.
  • Als u probeert toegang te krijgen tot de URL http://myhost/cgi-bin/qgis_mapserv.fcgi zult u merken dat nu alles werkt!

12.1.5. In Conclusion

U heeft geleerd verschillende versies van QGIS Server vanuit pakketten te installeren, hoe Apache te configureren met QGIS Server, op distributies voor Linux die zijn gebaseerd op Debian.

12.1.6. What’s Next?

Nu u QGIS Server hebt geïnstalleerd en het toegankelijk is via het protocol HTTP, moeten we leren hoe toegang te krijgen tot enkele services die het biedt. Het onderwerp van de volgende les is hoe te leren toegang te krijgen tot WMS-services van QGIS Server.