QGIS Server 3.44 在 Ubuntu 22.04 部署:Apache 配置与 3 个常见错误排查
1. 环境准备与基础安装
在Ubuntu 22.04上部署QGIS Server需要先确保系统环境满足基本要求。建议使用至少4GB内存的服务器,并预留20GB以上的磁盘空间用于存储地理数据。以下是完整的依赖安装步骤:
# 更新系统包索引 sudo apt update sudo apt upgrade -y # 安装QGIS Server核心组件(不包含推荐和可选包以减少体积) sudo apt install qgis-server --no-install-recommends --no-install-suggests # 安装Python插件支持(可选) sudo apt install python3-qgis # 验证安装位置 ls -l /usr/lib/cgi-bin/qgis_mapserv.fcgi安装完成后,通过以下命令验证QGIS Server版本:
/usr/lib/cgi-bin/qgis_mapserv.fcgi --version典型输出应包含类似信息:
QGIS 3.44.4 'Solothurn' Qt version 5.15.3 Python version 3.10.6注意:生产环境中不建议在同一台服务器上同时安装QGIS Desktop和QGIS Server,这可能导致资源冲突和安全隐患。
2. Apache服务器深度配置
2.1 基础模块安装与激活
QGIS Server作为FastCGI应用运行,需要Apache的fcgid模块支持:
# 安装Apache和fcgid模块 sudo apt install apache2 libapache2-mod-fcgid # 启用必要模块 sudo a2enmod rewrite headers fcgid2.2 虚拟主机配置文件
创建专用配置文件/etc/apache2/sites-available/qgis-server.conf,内容如下:
<VirtualHost *:80> ServerAdmin admin@your-domain.com ServerName qgis.your-domain.com DocumentRoot /var/www/html # 日志单独存放 ErrorLog ${APACHE_LOG_DIR}/qgis-server-error.log CustomLog ${APACHE_LOG_DIR}/qgis-server-access.log combined # QGIS Server配置 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +FollowSymLinks Require all granted SetHandler fcgid-script </Directory> # 性能调优参数 FcgidMaxRequestLen 10MB FcgidConnectTimeout 60 FcgidIOTimeout 120 </VirtualHost>启用配置并重启服务:
sudo a2ensite qgis-server.conf sudo systemctl restart apache22.3 防火墙设置
如果使用UFW防火墙,需要放行HTTP流量:
sudo ufw allow 'Apache Full' sudo ufw enable3. 服务验证与功能测试
3.1 基础功能验证
通过curl测试服务是否正常运行:
curl "http://localhost/cgi-bin/qgis_mapserv.fcgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities"正常响应应返回XML格式的WMS服务能力文档。如果返回404错误,请检查:
/usr/lib/cgi-bin/qgis_mapserv.fcgi文件是否存在- Apache配置中
ScriptAlias路径是否正确 - 文件权限是否为可执行(755)
3.2 项目文件部署
创建项目存储目录并设置权限:
sudo mkdir -p /var/www/qgis-projects sudo chown -R www-data:www-data /var/www/qgis-projects测试项目可通过以下URL访问:
http://yourserver/cgi-bin/qgis_mapserv.fcgi?MAP=/var/www/qgis-projects/test.qgs&SERVICE=WMS&REQUEST=GetCapabilities4. 常见错误排查指南
4.1 错误1:404 Not Found
症状:访问服务URL返回404状态码
排查步骤:
检查文件是否存在:
ls -l /usr/lib/cgi-bin/qgis_mapserv.fcgi验证Apache配置:
sudo apache2ctl configtest检查模块是否加载:
sudo apache2ctl -M | grep fcgid
解决方案:
- 重新安装qgis-server包
- 确保
ScriptAlias指向正确路径 - 检查SELinux/AppArmor是否阻止访问
4.2 错误2:权限错误
典型报错:
Failed to load project file: /path/to/project.qgs Permission denied权限修复命令:
sudo chown www-data:www-data /var/www/qgis-projects/* sudo chmod 755 /var/www/qgis-projects重要:如果项目使用数据库连接,确保www-data用户有数据库访问权限
4.3 错误3:服务无法启动
日志分析位置:
tail -n 50 /var/log/apache2/error.log常见原因及解决:
| 错误类型 | 检测方法 | 解决方案 |
|---|---|---|
| 端口冲突 | ss -tulnp | 修改Apache监听端口 |
| 内存不足 | free -h | 增加swap空间 |
| 依赖缺失 | ldd /usr/lib/cgi-bin/qgis_mapserv.fcgi | 安装缺失库 |
5. 性能优化与安全加固
5.1 性能调优参数
编辑/etc/apache2/mods-available/fcgid.conf:
<IfModule mod_fcgid.c> FcgidMaxProcesses 10 FcgidMinProcessesPerClass 1 FcgidMaxProcessesPerClass 5 FcgidConnectTimeout 30 FcgidIOTimeout 300 FcgidBusyTimeout 300 </IfModule>5.2 安全配置建议
启用HTTPS加密:
sudo apt install certbot python3-certbot-apache sudo certbot --apache限制IP访问:
<Directory "/usr/lib/cgi-bin"> Require ip 192.168.1.0/24 </Directory>禁用敏感信息泄露:
ServerTokens Prod ServerSignature Off
6. 高级功能扩展
6.1 插件安装与管理
服务器插件需安装在特定目录:
sudo mkdir -p /usr/lib/qgis/server/plugins sudo chown www-data:www-data /usr/lib/qgis/server/plugins通过环境变量指定插件路径:
SetEnv QGIS_PLUGINPATH "/usr/lib/qgis/server/plugins"6.2 日志分析配置
定制化日志格式记录GIS操作:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{QGIS_PROJECT}e" qgis_combined CustomLog ${APACHE_LOG_DIR}/qgis-access.log qgis_combined6.3 负载均衡配置
多节点部署示例架构:
客户端 → 负载均衡器 (Nginx) ├─ QGIS Server节点1 ├─ QGIS Server节点2 └─ QGIS Server节点3Nginx配置片段:
upstream qgis_cluster { server 192.168.1.101:80; server 192.168.1.102:80; server 192.168.1.103:80; } server { location /qgis/ { proxy_pass http://qgis_cluster/cgi-bin/qgis_mapserv.fcgi; } }