Qgis-server...Installation on Ubuntu 16.04 LTS

Before we start it is always a good idea to remove and re-install apache2 web server so that we start from the same set up:-

To remove Apache2:-

$ sudo apt --purge remove apache2
$ sudo apt autoremove

To re-install Apache2:-

$ sudo apt install apache2
$ sudo /etc/init.d/apache2 restart
# or
$ sudo service apache2 restart

1. $ sudo apt install apache2 (if not installed already done so as above)
2. $ sudo apt install qgis-server libapache2-mod-fcgid
3. $ sudo a2enmod fcgid
4. $ sudo a2enconf serve-cgi-bin
5. $ sudo service apache2 restart
6. add the following code and don't forget to save the changes:-

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin/">
Options ExecCGI FollowSymLinks
Require all granted
AddHandler fcgid-script .fcgi
</Directory>


into /etc/apache2/sites-available/000-default.conf

$ sudo gedit /etc/apache2/sites-available/000-default.conf

# so that it looks like this:-

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin/">
    Options ExecCGI FollowSymLinks
    Require all granted
    AddHandler fcgid-script .fcgi
    </Directory>


    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

7.  $ sudo service apache2 restart

8.  Test the 'GetCapabilities' request by clicking on the following url:-

http://localhost/cgi-bin/qgis_mapserv.fcgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities

9. You should then see the following:-


10. Congratulations! ... Now create a QGIS project with some vector layers from your 'postgres/postgis' database and save it in /home/yourdirectorychoice/qgisprojectfile.qgs 

Add a 'project' folder inside 'cgi-bin' 

It is advisable to use a separate folder for each QGIS project you want to use in a browser client such as 'qgis-web-client' or 'lizmap-web-client' (we will look into Lizmap in a later post).

If you choose to create a 'project' folder within the 'cgi-bin' folder (as for example in /usr/lib/cgi-bin/project/) remember to symlink in both the 'wms_metadata.xml' and the 'qgis_mapserv.fcgi' as well as linking your 'projectname.qgs' file (in my case from my.../home/paul/project directory) into the '/usr/lib/cgi-bin/project' folder:-

$ cd /usr/lib/cgi-bin/project
$ sudo ln -s ../qgis_mapserv.fcgi .
$ sudo ln -s ../wms_metadata.xml .
$ sudo ln -s /home/paul/project/southuk.qgs /usr/lib/cgi-bin/project/southuk.qgs
$ sudo ln -s /home/paul/project/southuk.qgs.cfg /usr/lib/cgi-bin/project/southuk.qgs.cfg

Your 'project' folder should now look as above. You will not have the 'southuk.qgs.cfg' or the 'southuk.qgs.png' files (just yet) because these will be added with the 'QGIS Lizmap Plugin' in the next post.

OK..so now we can update the apache2 server:-

$ sudo service apache2 restart

Your 'GetCapabilities' request should now include the added 'project' folder:

http://localhost/cgi-bin/project/qgis_mapserv.fcgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities


 

Comments

  1. hi Paul,
    how do I see the map, not the script?

    ReplyDelete
    Replies
    1. Try this:
      http://localhost/cgi-bin/qgis_mapserv.fcgi
      ?MAP=/home/qgis/projects/world.qgs
      &SERVICE=WMS
      &VERSION=1.3.0
      &REQUEST=GetMap
      &BBOX=-432786,4372992,3358959,7513746
      &SRS=EPSG:3857
      &WIDTH=665
      &HEIGHT=551
      &LAYERS=countries
      &FORMAT=image/jpeg

      Delete
  2. This is what I get from the project URL:

    There was an error reading the project file or the SLD configuration

    Any hints on how to debug it? Thanks.

    ReplyDelete

  3. Hello, Thanks for sharing such a information it is really amazing and helpful. Useful to best Php Jobs In Hyderabad

    ReplyDelete
  4. Thanks for the great tutorial. I had the issue "There was an error reading the project file or the SLD configuration" which I could solve thanks to this post changing file permissions: https://gis.stackexchange.com/questions/212611/qgis-server-2-14-raises-error-reading-project-file-on-ubuntu-16-04

    ReplyDelete
  5. First of all thanks so much for this very clear and nice tutorial, but one thing is not function at me. At this request: http://localhost/cgi-bin/project/qgis_mapserv.fcgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
    comes the 500internal server error. If I use this request without ../project/.. everything is ok. I also changed the folder access to read&write, but doesn't help. I also reinstalled apache2 also without success. I observed that you linked the wms_metadata.xml but in your picture you have a file named wms_mapserv.xml, which I don't have at all, but I have a file "mapserv" without dot. Hope you can help me...

    ReplyDelete
  6. Spot on with this write-up, I truly think this website needs much more consideration. I?ll probably be again to read much more, thanks for that info.
    es file apk download

    ReplyDelete
  7. This is the only article that worked for me. This is the third time I have tried to install QGIS and it worked first time. I have followed it up to and including point 9. I am a newbie to GIS. Now I need to set up a very simple test project.

    ReplyDelete
  8. Very good script, thank you, I use this in my Docker container, but I've tried to start Apache in cmd.sh, but without success, is there a right way to do this ?
    This is my cmd.sh file: it has the proper rights to execute

    #!/bin/bash

    # Start Apache2 in the background
    apache2ctl -D FOREGROUND

    [[ $DEBUG == "1" ]] && env
    exec /usr/bin/xvfb-run --auto-servernum --server-num=1 /usr/bin/spawn-fcgi -p 49155 -n -d /home/qgis -- /usr/lib/cgi-bin/qgis_mapserv.fcgi

    # Keep the script running to keep the container running
    tail -f /dev/null

    ReplyDelete

Post a Comment

Popular posts from this blog

Qgis-server...Installing the QGIS Lizmap Plugin & Lizmap Web Client

Install QGIS 3.22 LTS with QGIS-SERVER-3.22 and Apache2 on Ubuntu 22.04 LTS