在CentOS 6.7上安装OSM地图服务器。
安装环境:CentOS-6.7-x86_64-minimal.iso
curl -O https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-3.noarch.rpm
sudo rpm -ivh pgdg-centos94-9.4-3.noarch.rpm
sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo yum install postgresql94-server postgis22_94 postgresql94-devel
初始化数据库:
初始化,开启,添加到启动项
service postgresql-9.4 initdb
service postgresql-9.4 start
chkconfig postgresql-9.4 on
下载GCC4.9,编译安装
参考http://blog.techbeta.me/2015/10/linux-centos6-5-upgrade-gcc/
安装C和C++的静态库
yum install -y glibc-static libstdc++-static
安装旧版本的gcc、c++编译器
yum install gcc gcc-c++
安装wget:
yum install wget
wget ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.9.0/gcc-4.9.0.tar.bz2
tar jxvf gcc-4.9.0.tar.bz2
cd gcc-4.9.0
下载一些必备的依赖程序
./contrib/download_prerequisites
./configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make -j4
sudo make install
更改libstdc++动态连接库
参考https://itbilu.com/linux/management/NymXRUieg.html
寻找动态链接库
sudo find / -name "libstdc++.so*"
找到以后复制
sudo cp /home/zhao/gcc-4.9.0/stage1-x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.20 /usr/lib64
cd /usr/lib64
sudo rm -rf libstdc++.so.6
sudo ln -s libstdc++.so.6.0.20 libstdc++.so.6
strings /usr/lib64/libstdc++.so.6 | grep GLIBC
结果显示有GLIBCXX_3.4.20
检查GCC版本,gcc –version显示为4.9即可
Boost版本使用1.50,这个是经过多次尝试以后确定的。Boost编译时候要带上with-python选项。
tar -zxvf boost_1_50_0.tar.gz
./bootstrap.sh
./b2 ./b2 -j4 --with-python
sudo ./b2 -j4 --with-python install
sudo bash -c "echo '/usr/local/lib' > /etc/ld.so.conf.d/boost.conf"
sudo ldconfig
如果后续仍有环境变量问题,加入环境变量
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
如果需要卸载的话,移除默认的头文件目录/usr/local/include /boost和库文件目录/usr/local/lib/
sudo yum install httpd httpd-devel
service httpd start
service httpd stop
service httpd restart
sudo chkconfig httpd on
在mapnik目录下面./configure一下,需要安装很多依赖,使用yum或者源码编译安装。如果全部解决了,./configure会提示可以编译了。
需要的依赖有
sudo yum install git
sudo yum install libicu-devel libtiff-devel libpng-devel libjpeg-devel zlib-devel proj proj-devel cairo pycairo cairo-devel pycairo-devel libxml2-devel freetype freetype-devel python-devel gdal gdal-devel
如果Boost库没有加载,将其加入环境变量:
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
安装时候的参考
https://github.com/mapnik/mapnik/wiki/CentOS_RHEL
https://github.com/mapnik/mapnik/blob/master/INSTALL.md
git clone git://github.com/mapnik/mapnik
cd mapnik
git branch 2.2 origin/2.2.x
git checkout 2.2
export PATH=$PATH:/usr/pgsql-9.4/bin:/usr/pgsql-9.4/lib:/usr/local/lib
./configure
make -j4
检查/mapnik/plugins/input目录,确保下面有postgis.input文件,如果该文件没有出现,检查postgresql94-devel是否安装,检查环境变量是否加入export PATH=$PATH:/usr/pgsql-9.4/bin:/usr/pgsql-9.4/lib:/usr/local/lib。
sudo make install
sudo ldconfig
进入python界面,import mapnik成功导入不报错,则安装成功
import mapnik
检查/usr/local/lib/mapnik/目录下面是否有fonts文件夹,如果没有,需要创建fonts文件夹,并将mapnik源代码下面的fonts拷贝到该目录下面。
sudo mkdir /usr/local/lib/mapnik/fonts
sudo cp /home/zhao/mapnik/fonts/unifont-5.1.20080907.ttf /usr/local/lib/mapnik/fonts/
sudo cp -r /home/zhao/mapnik/fonts/dejavu-fonts-ttf-2.33 /usr/local/lib/mapnik/fonts/
git clone git://github.com/openstreetmap/mod_tile.git
sudo yum install autoconf automake libtool
cd mod_tile
./autogen.sh
./configure
make
sudo make install
sudo make install-mod_tile
sudo ldconfig
这步make会报错,提示GCC版本太低。让make使用GCC-4.9:
Make默认使用了/usr/bin/gcc,这里会造成编译错误,将链接修改一下
mv /usr/bin/gcc /usr/bin/gcc4.4.7
ln -s /usr/local/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++4.4.7
ln -s /usr/local/bin/g++ /usr/bin/g++
mv /usr/bin/cc /usr/bin/cc4.4.7
ln -s /usr/local/bin/cc /usr/bin/cc
mv /usr/bin/c++ /usr/bin/c++4.4.7
ln -s /usr/local/bin/c++ /usr/bin/c++
安装osm2pgsql
git clone git://github.com/openstreetmap/osm2pgsql.git
sudo yum install cmake bzip2-devel lua-devel
cd osm2pgsql
mkdir build && cd build && cmake ..
make
sudo make install
下面所有的username根据电脑情况进行更改
sudo -u postgres -i
createuser username
createdb -E UTF8 -O username gis
psql
\c gis
CREATE EXTENSION postgis;
ALTER TABLE geometry_columns OWNER TO username;
ALTER TABLE spatial_ref_sys OWNER TO username;
\q
exit
安装Nodejs 6.X:
curl -sL https://rpm.nodesource.com/setup_6.x | bash -
yum install nodejs
安装Carto:
npm install -g carto
如果速度慢就改成淘宝源:
sudo npm install cnpm -g --registry=https://registry.npm.taobao.org
sudo cnpm install -g carto
然后下载编译风格文件
sudo yum install unzip
git clone git://github.com/gravitystorm/openstreetmap-carto.git
cd openstreetmap-carto
git checkout `git rev-list -n 1 --before="2016-12-04 00:00" master`
编译风格文件
carto project.mml > mapnik.xml
./get-shapefiles.sh
不同的风格:
OSMBright:https://github.com/mapbox/osm-bright
参考配置文件,手动写入配置文件。配置以后重启Apache
sudo service httpd restart
手动修改配置 Renderd
vim /usr/local/etc/renderd.conf
修改
# Edit where your paths and number of threads differ
socketname=/var/run/renderd/renderd.sock
num_threads=4
plugins_dir=/usr/local/lib/mapnik/input
font_dir= /usr/local/lib/mapnik/fonts
XML= /home/zhao/openstreetmap-carto/mapnik.xml
HOST=localhost
配置 mod_tile
vim /etc/httpd/conf.d/mod_tile.conf
修改
# Edit the ServerName and ServerAlias to suit your server
# Also update LoadTileConfigFile and ModTileRenderdSocketName if this differs on your server
LoadModule tile_module /etc/httpd/modules/mod_tile.so
ServerName map.zhao.io
DocumentRoot /var/www/html
# Specify the default base storage path for where tiles live. A number of different storage backends
# are available, that can be used for storing tiles. Currently these are a file based storage, a memcached
# based storage and a RADOS based storage.
# The file based storage uses a simple file path as its storage path ( /path/to/tiledir )
# The RADOS based storage takes a location to the rados config file and a pool name ( rados://poolname/path/to/ceph.conf )
# The memcached based storage currently has no configuration options and always connects to memcached on localhost ( memcached:// )
#
# The storage path can be overwritten on a style by style basis from the style TileConfigFile
ModTileTileDir /var/lib/mod_tile
# You can either manually configure each tile set with the default png extension and mimetype
# AddTileConfig /folder/ TileSetName
# or manually configure each tile set, specifying the file extension
# AddTileMimeConfig /folder/ TileSetName js
# or load all the tile sets defined in the configuration file into this virtual host.
# Some tile set specific configuration parameters can only be specified via the configuration file option
LoadTileConfigFile /usr/local/etc/renderd.conf
# Specify if mod_tile should keep tile delivery stats, which can be accessed from the URL /mod_tile
# The default is On. As keeping stats needs to take a lock, this might have some performance impact,
# but for nearly all intents and purposes this should be negligable ans so it is safe to keep this turned on.
ModTileEnableStats On
# Turns on bulk mode. In bulk mode, mod_tile does not request any dirty tiles to be rerendered. Missing tiles
# are always requested in the lowest priority. The default is Off.
ModTileBulkMode Off
ModTileRequestTimeout 3
# Timeout before giving up for a tile to be rendered that is otherwise missing
ModTileMissingRequestTimeout 10
# If tile is out of date, don't re-render it if past this load threshold (users gets old tile)
ModTileMaxLoadOld 16
# If tile is missing, don't render it if past this load threshold (user gets 404 error)
ModTileMaxLoadMissing 50
# Sets how old an expired tile has to be to be considered very old and therefore get elevated priority in rendering
ModTileVeryOldThreshold 31536000000000
# Unix domain socket where we connect to the rendering daemon
ModTileRenderdSocketName /var/run/renderd/renderd.sock
# Alternatively you can use a TCP socket to connect to renderd. The first part
# is the location of the renderd server and the second is the port to connect to.
# ModTileRenderdSocketAddr renderd.mydomain.com 7653
##
## Options controlling the cache proxy expiry headers. All values are in seconds.
##
## Caching is both important to reduce the load and bandwidth of the server, as
## well as reduce the load time for the user. The site loads fastest if tiles can be
## taken from the users browser cache and no round trip through the internet is needed.
## With minutely or hourly updates, however there is a trade-off between cacheability
## and freshness. As one can't predict the future, these are only heuristics, that
## need tuning.
## If there is a known update schedule such as only using weekly planet dumps to update the db,
## this can also be taken into account through the constant PLANET_INTERVAL in render_config.h
## but requires a recompile of mod_tile
## The values in this sample configuration are not the same as the defaults
## that apply if the config settings are left out. The defaults are more conservative
## and disable most of the heuristics.
##
## Caching is always a trade-off between being up to date and reducing server load or
## client side latency and bandwidth requirements. Under some conditions, like poor
## network conditions it might be more important to have good caching rather than the latest tiles.
## Therefor the following config options allow to set a special hostheader for which the caching
## behaviour is different to the normal heuristics
##
## The CacheExtended parameters overwrite all other caching parameters (including CacheDurationMax)
## for tiles being requested via the hostname CacheExtendedHostname
#ModTileCacheExtendedHostname cache.tile.openstreetmap.org
#ModTileCacheExtendedDuration 2592000
# Upper bound on the length a tile will be set cacheable, which takes
# precedence over other settings of cacheing
ModTileCacheDurationMax 604800
# Sets the time tiles can be cached for that are known to by outdated and have been
# sent to renderd to be rerendered. This should be set to a value corresponding
# roughly to how long it will take renderd to get through its queue. There is an additional
# fuzz factor on top of this to not have all tiles expire at the same time
ModTileCacheDurationDirty 900
# Specify the minimum time mod_tile will set the cache expiry to for fresh tiles. There
# is an additional fuzz factor of between 0 and 3 hours on top of this.
ModTileCacheDurationMinimum 10800
# Lower zoom levels are less likely to change noticeable, so these could be cached for longer
# without users noticing much.
# The heuristic offers three levels of zoom, Low, Medium and High, for which different minimum
# cacheing times can be specified.
#Specify the zoom level below which Medium starts and the time in seconds for which they can be cached
ModTileCacheDurationMediumZoom 13 86400
#Specify the zoom level below which Low starts and the time in seconds for which they can be cached
ModTileCacheDurationLowZoom 9 518400
# A further heuristic to determine cacheing times is when was the last time a tile has changed.
# If it hasn't changed for a while, it is less likely to change in the immediate future, so the
# tiles can be cached for longer.
# For example, if the factor is 0.20 and the tile hasn't changed in the last 5 days, it can be cached
# for up to one day without having to re-validate.
ModTileCacheLastModifiedFactor 0.20
## Tile Throttling
## Tile scrappers can often download large numbers of tiles and overly straining tileserver resources
## mod_tile therefore offers the ability to automatically throttle requests from ip addresses that have
## requested a lot of tiles.
## The mechanism uses a token bucket approach to shape traffic. I.e. there is an initial pool of n tiles
## per ip that can be requested arbitrarily fast. After that this pool gets filled up at a constant rate
## The algorithm has two metrics. One based on overall tiles served to an ip address and a second one based on
## the number of requests to renderd / tirex to render a new tile.
## Overall enable or disable tile throttling
ModTileEnableTileThrottling Off
# Specify if you want to use the connecting IP for throtteling, or use the X-Forwarded-For header to determin the
# IP address to be used for tile throttling. This can be useful if you have a reverse proxy / http accellerator
# in front of your tile server.
# 0 - don't use X-Forward-For and allways use the IP that apache sees
# 1 - use the client IP address, i.e. the first entry in the X-Forwarded-For list. This works through a cascade of proxies.
# However, as the X-Forwarded-For is written by the client this is open to manipulation and can be used to circumvent the throttling
# 2 - use the last specified IP in the X-Forwarded-For list. If you know all requests come through a reverse proxy
# that adds an X-Forwarded-For header, you can trust this IP to be the IP the reverse proxy saw for the request
ModTileEnableTileThrottlingXForward 0
## Parameters (poolsize in tiles and topup rate in tiles per second) for throttling tile serving.
ModTileThrottlingTiles 10000 1
## Parameters (poolsize in tiles and topup rate in tiles per second) for throttling render requests.
ModTileThrottlingRenders 128 0.2
###
###
# increase the log level for more detailed information
LogLevel debug
从https://www.openstreetmap.org/ 和http://download.geofabrik.de/网站下载地图。
osm2pgsql --slim -d gis -C 1600 --number-processes 4 china-latest.osm.pbf
防火墙:
查看端口情况netstat -ntlp,查看80端口是否为监听状态,如果不是,需要打开80端口。
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo /etc/rc.d/init.d/iptables save
sudo /etc/rc.d/init.d/iptables restart
在浏览器中访问服务器,能够出现Apache的默认页面。
关闭访问控制SELinux:
查看selinux的详细状态,如果为enable则表示为开启
/usr/sbin/sestatus –v
临时性关闭(立即生效,但是重启服务器后失效)
setenforce 0 #设置selinux为permissive模式(即关闭)
setenforce 1 #设置selinux为enforcing模式(即开启)
永久关闭:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
创建必要的目录
sudo mkdir /var/run/renderd
sudo chown username /var/run/renderd
sudo mkdir /var/lib/mod_tile
sudo chown username /var/lib/mod_tile
开启
renderd -f -c /usr/local/etc/renderd.conf
访问http://localhost/osm_tiles/0/0/0.png,其中localhost替换为服务器地址。如果出现世界地图,则安装成功