CentOS7 环境部署 (Python Django项目)


CentOS7 默认自带Python2.7,采用yum代替yum作为包管理器

安装基本环境

  • 安装gcc、gcc-c++编译环境
  • yum install -y gcc gcc-c++
  • 安装automake autoconf等基本工具
  • yum install -y automake autoconf libtool make
  • 安装开发工具
  • yum groupinstall 'Development Tools'
  • 安装wget、vim、bzip2、net-tools等基本工具
  • yum install -y wget vim bzip2 net-tools
  • 安装相关devel库
  • yum install -y openssl openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
  • yum install -y libffi-devel
  • yum install -y curl-devel expat-devel gettext-devel zlib-devel

安装Python3.8

CentOS7自带Python2.7

  1. 下载python3.8源码
  • 将源码放在/usr/local/src目录下
  • cd /usr/local/src
  • wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
  1. 安装
  • 解压: tar -xzvf Python-3.8.2.tgz
  • cd Python-3.8.2
  • 把Python3.8安装到 /usr/local 目录
  • ./configure --prefix=/usr/local
  • make
  • make install
  1. 更改/usr/bin/python超链接
  • cd /usr/bin
  • mv python python.backup
  • ln -s /usr/local/bin/python3/bin/python3.8 /usr/bin/python
  • ln -s /usr/local/bin/python3/bin/python3.8 /usr/bin/python3
  • ln -s /usr/local/bin/python3/bin/python3.8 /usr/bin/python3.8
  • ln -s /usr/local/bin/python3/bin/pip3.8 /usr/bin/pip
  • ln -s /usr/local/bin/python3/bin/pip3.8 /usr/bin/pip3
  • ln -s /usr/local/bin/python3/bin/pip3.8 /usr/bin/pip3.8
  1. 添加/usr/local/bin/python3/bin至环境变量PATH中
  2. 修改pip源
  • 修改 ~/.pip/pip.conf (没有就创建一个)
[global]
  index-url = https://pypi.tuna.tsinghua.edu.cn/simple

安装subversion、git

  • yum install -y subversion
  • yum install -y perl-ExtUtils-MakeMaker
  • yum install -y git

安装tcl

  • 下载源码
  • wget https://jaist.dl.sourceforge.net/project/tcl/Tcl/8.6.10/tcl8.6.10-src.tar.gz
  • 解压
  • tar -xzvf tcl8.6.10-src.tar.gz
  • 安装
  • cd tcl8.6.10/unix
  • ./configure
  • make
  • make install

安装redis

  • 下载源码
  • wget http://download.redis.io/releases/redis-5.0.8.tar.gz
  • 解压
  • tar -xzvf redis-5.0.8.tar.gz
  • 安装
  • cd redis-5.0.8
  • make
  • make install
  • cp redis.conf /usr/local/redis/
  • 修改redis.conf配置文件
  • vim /usr/local/redis/redis.conf
  • 1.后台启动,daemonize yes
  • 2.数据存放路径,dir /usr/local/redis/log rdb
  • 3.指定持久化方式,appendonly yes
  • 4.配置密码:requirepass
  • 配置服务
  • 创建配置文件:/usr/lib/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
[Install]
WantedBy=multi-user.target
  • 启动服务
  • systemctl start redis
  • systemctl status redis #查看服务状态
  • 设置服务开机启动
  • systemctl enable redis

安装node

  • 安装nodejs模块(包含npm)
  • yum module install nodejs
  • 添加/usr/local/nodejs/bin至环境变量PATH中
  • 设置npm淘宝镜像源
  • npm config set registry http://registry.npm.taobao.org/
  • 安装yarn
  • npm install -g yarn
  • 设置yarn淘宝镜像源
  • yarn config set registry https://registry.npm.taobao.org --global
  • yarn config set disturl https://npm.taobao.org/dist --global

安装postgresql 10

  • 安装libicu依赖
  • yum install -y libicu-devel libicu
  • 设置yum源
  • yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  • 安装postgresql
  • sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  • sudo yum install -y postgresql10-server
  • 初始化数据库
  • sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
  • 设置开机启动
  • sudo systemctl enable postgresql-10
  • 启动服务
  • sudo systemctl start postgresql-10
  • 设置postgres密码
  • su - postgres
  • psql #默认无密码登录
  • \password postgres #重置密码,需要输入两次
  • \q #退出
  • 将/usr/pgsql-10/bin添加到环境变量PATH
  • 设置数据库本地访问权限
  • 修改/var/lib/pgsql/10/data/pg_hba.conf文件:
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
  • 重启服务
  • systemctl restart postgresql-10

安装地理信息库

系统采用GeoDjango实现GIS服务,PostGIS实现GIS数据库,需要依赖GEOS、PROJ.4、GDAL等地理信息库。
参考https://docs.djangoproject.com/en/3.0/ref/contrib/gis/install/geolibs/,安装相应地理信息依赖库。
参考https://postgis.net/docs/manual-2.5/postgis_installation.html#install_requirements,安装PostGIS相应依赖。

1.安装GEOS

GEOS是用于执行几何运算的C ++库,并且是GeoDjango使用的默认内部几何表示形式。
GEOS采用3.7.3版本

  • 下载源码
  • cd /usr/local/src # 将所有源码下载到/usr/local/src目录
  • wget https://download.osgeo.org/geos/geos-3.7.3.tar.bz2
  • 解压
  • tar xjf geos-3.7.3.tar.bz2
  • 安装
  • cd geos-X.Y.Z
  • ./configure
  • make
  • make install
  • cd ..

2.安装PROJ.4

PROJ.4是用于将地理空间数据转换为不同坐标参考系统的库。
PROJ.4采用5.2.0版本

  • 下载PROJ.4源代码和数据移动文件
  • wget https://download.osgeo.org/proj/proj-5.2.0.tar.gz
  • wget https://download.osgeo.org/proj/proj-datumgrid-1.8.tar.gz
  • 解压
  • tar xzf proj-5.2.0.tar.gz
  • cd proj-5.2.0/nad
  • tar xzf ../../proj-datumgrid-1.8.tar.gz
  • cd ..
  • 安装
  • ./configure
  • make
  • make install
  • cd ..

3.安装GDAL

GDAL是一个出色的开源地理空间库,它支持读取大多数矢量和栅格空间数据格式。目前,GeoDjango仅支持GDAL的矢量数据功能。 在构建GDAL之前,应先安装GEOS和PROJ.4。
GDAL采用2.4.3版本

  • 下载源码
  • wget https://download.osgeo.org/gdal/X.Y.Z/gdal-2.4.3.tar.gz
  • 解压
  • tar xzf gdal-2.4.3.tar.gz
  • 安装
  • cd gdal-2.4.3
  • ./configure
  • make
  • make install
  • cd ..

4.将lib库添加至pgsql

  • cp /usr/local/lib/libgeos_c.so* /usr/pgsql-10/lib/
  • cp /usr/local/lib/* /usr/pgsql-10/lib/
  • cp /usr/local/lib/* /usr/pgsql-10/lib/

5.安装LibXML2

LibXML2当前用于某些导入功能(ST_GeomFromGML和ST_GeomFromKML)。

  • yum安装
  • yum install libxml2

6.安装JSON-C

JSON-C当前用于通过功能ST_GeomFromGeoJson导入GeoJSON。

  • yum安装
  • yum install json-c

7.安装hdf5

  • 下载源码
  • wget https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.10/hdf5-1.10.6/src/hdf5-1.10.6.tar.gz
  • 解压
  • tar -zxf hdf5-1.10.6.tar.gz
  • 安装
  • cd hdf5-1.10.6
  • ./configure
  • make
  • make install
  • cd ..

安装PostGIS

PostGIS是对象关系型数据库系统PostgreSQL的一个扩展,PostGIS提供如下空间信息服务功能:空间对象、空间索引、空间操作函数和空间操作符。同时,PostGIS遵循OpenGIS的规范。
PostGIS采用2.5.4版本

  • 下载源码
  • wget https://download.osgeo.org/postgis/source/postgis-2.5.4.tar.gz
  • 解压
  • tar -zxf postgis-2.5.4.tar.gz
  • 安装
  • cd postgis-2.5.4
  • ./configure
  • make
  • make install

项目(project)安装

  • 将项目安装在/opt/project目录
  • 创建venv虚拟环境
  • 根据requirements.txt安装相关依赖库

安装supervisor

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。

  • yum install supervisor # 安装
  • supervisor配置
    默认配置文件:/etc/supervisor/supervisord.conf,按默认配置即可,不用修改
    子进程配置文件路径:/etc/supervisord.d/

默认子进程配置文件为ini格式,可在supervisor主配置文件中修改。

  • 配置sdpew子进程
  • 进入/etc/supervisord.d/目录,创建sdpew.ini文件,输入如下内容:
[program:sdpew] 
  directory=/opt/project
  command=/opt/project/venv/bin/python main.py
  autostart=true 
  autorestart=false 
  stdout_logfile=/opt/project/log/out.log
  stdout_logfile_maxbytes=10MB
  stdout_logfile_backups=10
  stderr_logfile=/opt/project/log/err.log
  stderr_logfile_maxbytes=1MB
  stderr_logfile_backups=10
  #user = root
  • 启动supervisord服务
  • systemctl start supervisord # 启动服务
  • systemctl enable supervisord # 设置开机启动
  • systemctl status supervisord # 查看服务状态
  • 通过supervisord启动子进程
  • supervisorctl status # 查看所有进程的状态
  • supervisorctl stop sdpew # 停止sdpew
  • supervisorctl start sdpew # 启动sdpew
  • supervisorctl restart # 重启
  • supervisorctl update # 配置文件修改后使用该命令加载新的配置
  • supervisorctl reload # 重新启动配置中的所有程序

防火墙设置

CentOS7开始采用firewalld替代之前的iptables作为系统防火墙。

  • firewall-cmd --permanent --add-port=80/tcp # 设置开放80/tcp端口
  • firewall-cmd --permanent --add-port=8000/tcp # 设置开放8000/tcp端口
  • firewall-cmd --permanent --add-port=502/tcp # 设置开放502/tcp端口
  • firewall-cmd --permanent --add-port=503/tcp # 设置开放503/tcp端口
  • firewall-cmd --reload # 重新加载防火墙
  • systemctl start firewalld # 启动防火墙
  • systemctl enable firewalld # 设置防火墙开机启动
  • systemctl status firewalld # 查看防火墙状态

安装nginx

  • 安装nginx
  • yum install -y nginx