12.1. Lesson: Installer QGIS Server¶
The goal for this lesson: To learn how to install QGIS Server on Debian Stretch. With negligible variations you can also follow it for any Debian based distribution like Ubuntu and its derivatives.
Note
In Ubuntu you can use your regular user, prepending sudo
to
commands requiring admin permissions. In Debian you can work as admin (root
),
without using sudo
.
12.1.1.
Installer Follow Along: depuis les packages¶
In this lesson we’re going to do only the install from packages as shown here .
Installez QGIS Server avec:
apt install qgis-server
# if you want to install server plugins, also:
apt install python-qgis
En production, il est préférable d’utiliser QGIS Server sans installer QGIS Desktop (avec le serveur X associé)sur la même machine.
12.1.2.
Follow Along: Exécutable QGIS Server¶
L’exécutable QGIS Server est qgis-mapserv.fcgi
. Vous pouvez vérifier à quel endroit il a été installé au moyen de la commande: find / -name 'qgis_mapserv.fcgi'
qui devrait renvoyer quelque chose comme /usr/lib/cgi-bin/qgis_mapserv.fcgi
.
De manière facultative, si vous voulez tester l’exécutable en ligne de commande vous pouvez lancer: /usr/lib/cgi-bin/qgis_mapserv.fcgi
, ce qui devrait produire un résultat semblable à:
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="https://www.opengis.net/ogc">
<ServiceException code="Service configuration error">Service unknown or unsupported</ServiceException>
</ServiceExceptionReport>
C’est une bonne chose. Ce message signifie que nous sommes sur le bon chemin puisque le serveur répond que nous n’avons demandé aucun service supporté. Nous verrons plus loin comment faire des requêtes WMS.
12.1.3. Follow Along: Configuration HTTP du Serveur¶
Pour accéder au serveur QGIS depuis un navigateur internet, il faut mettre en place un serveur HTTP.
In this lesson we’re going to use the Apache HTTP server, colloquially called Apache.
First we need to install Apache by running the following command in a terminal:
apt install apache2 libapache2-mod-fcgid
You can run QGIS server on your default website, or configure a virtualhost specifically for this, as follows.
Dans le répertoire /etc/apache2/sites-available
créons un fichier nommé qgis.demo.conf
, avec ce contenu:
<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 https://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 https://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 nano /etc/apache2/sites-available/qgis.demo.conf
.
Note
Quelques unes des options de configuration sont expliquées dans la section Server Configuration avancée.
Créons maintenant les répertoires de stockage des logs de QGIS Server et de la base d’authentification.
mkdir /var/log/qgis/
chown www-data:www-data /var/log/qgis
mkdir /home/qgis/qgisserverdb
chown www-data:www-data /home/qgis/qgisserverdb
Note
www-data
est l’utilisateur Apache sur les systèmes basés sur Debian et il faut qu’Apache accède à ces répertoires et aux fichiers qu’ils contiennent. La commande chown www-data...
attribue la propriété des fichiers et répertoires à www-data
.
Il est temps maintenant d’activer le Virtual host <https://httpd.apache.org/docs/2.4/vhosts>`_, le mode fcgid si ce n’est pas déjà le cas et de redémarrer le service apache2
.
a2enmod fcgid
a2ensite qgis.demo
service apache2 restart
Note
Si vous avez installé QGIS Server sans serveur X (inclus avec Linux Desktop) et si vous voulez aussi utiliser la commande GetPrint
alors il faut installer un serveur X factice et dire à QGIS Server de l’utiliser. Pour ce faire, il faut entre les commandes suivantes:
Installer xvfb:
apt install xvfb
Créer le fichier de service:
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"
Activer, démarrer et vérifier le statut de xvfb.service
:
systemctl enable xvfb.service
systemctl start xvfb.service
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 https://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 sh -c "echo '127.0.0.1 qgis.demo' >> /etc/hosts"
.
Replace 127.0.0.1
with the IP of your server.
Note
Souvenez-vous que les deux fichiers myhost.conf
et /etc/hosts
doivent être configurés pour que notre installation fonctionne. Vous pouvez aussi tester l’accès à QGIS Server depuis d’autres clients sur le réseau (e.g. des machines Windows ou MacOS) en éditant leur fichier /etc/hosts
et en pointant le nom myhost
vers l’IP du serveur sur le réseau. Vous pouvez être sûr que cette IP n’est pas 127.0.0.1
puisque c’est l’IP local, seulement accessible sur la machine locale. Sur les machines *nix
le fichier hosts
est situé dans /etc
, alors que sous Windows il se trouve dans le dossier C:\Windows\System32\drivers\etc
. Sous Windows vous devez démarrer votre éditeur de texte avec les privilèges administrateur avant d’ouvrir le fichier hosts.
Nous pouvons tester un des serveur QGIS installé avec une requête http depuis la ligne de commande curl http://qgis.demo/cgi-bin/qgis_mapserv.fcgi
qui devrait afficher en sortie :
<ServiceExceptionReport version="1.3.0" xmlns="https://www.opengis.net/ogc">
<ServiceException code="Service configuration error">Service unknown or unsupported</ServiceException>
</ServiceExceptionReport>
Note
curl can be installed with apt install curl
.
Apache est maintenant configuré.
Also, from your web browser you can check the capabilities of the server:
http://qgis.demo/cgi-bin/qgis_mapserv.fcgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
12.1.4.
Follow Along: Créer un autre virtual host¶
Créons un autre virtual host Apache pointant sur QGIS Server. Vous pouvez choisir n’importe quel nom vous faisant plaisir (coco.bango
, super.duper.training
, example.com
, etc.) mais pour faire simple nous allons choisir myhost
.
Let’s set up the
myhost
name to point to the localhost IP by adding127.0.0.1 x
to the/etc/hosts
with the following command:sh -c "echo '127.0.0.1 myhost' >> /etc/hosts"
or by manually editing the file withgedit /etc/hosts
.Nous pouvons vérifier que
myhost
pointe sur le localhost en exécutant dans le terminal la commandeping myhost
qui devrait afficher :
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
Voyons si nous pouvons accéder à QGIS Server depuis le site
myhost
en exécutant :curl http://myhost/cgi-bin/qgis_mapserv.fcgi
ou en accédant à l’URL depuis le navigateur de votre box Debian. Vous obtiendrez probablement :
<!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 amyhost.conf
file in the/etc/apache2/sites-available
directory that has the same content asqgis.demo.conf
except for theServerName
line that should beServerName 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
a2ensite myhost.conf
and then reload the Apache service withservice apache2 reload
.Si vous essayez de nouveau d’accéder à http://myhost/cgi-bin/qgis_mapserv.fcgi vous verrez que tout fonctionne maintenant !
12.1.5. In Conclusion¶
Vous avez appris comment installer les différents versions de QGIS Server depuis les paquets, comment configurer Apache avec QGIS Server, sur Debian basé sur Linux.
12.1.6. What’s Next?¶
Maintenant que vous avez installé QGIS Server et qu’il est accessible à travers le protocol HTTP, nous avons besoin d’apprendre comme accéder à certains des services qu’il propose. Le sujet de la prochaine leçon est d’apprendre comment accéder au services WMS de QGIS Server.