在Aliyun Linux 2上安装Postgresql和Node.js
Aliyun Linux 2上部署环境,遇到了几个问题。记录如下。
安装Postgresql 11 client
官方Guide
- Postgresql官方在红帽系Linux的安装Guide地址: https://www.postgresql.org/download/linux/redhat/
- Guide中选取对应操作系统后,可以得到Postgresql在RHEL 7系列上的yum源地址是: https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
在RHEL 7系列上安装方法为:
1 | yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm |
Aliyun Linux 2安装问题
但该方法在Alinux Linux 2上没法成功安装,因为官方源依赖于yum的变量$releasever,$basearch。在Aliyun Linux 2中,这些变量和RHEL和CentOS上不相同。导致没法直接用官方rpm yum包来安装postgresql11。
解决方法
在安装完pgdg-redhat-all.repo过后,可以手动来修改yum源,Aliyun Linux 2的官方描述中说Aliyun Linux 2兼容CentOS 7。使用下列命令修改/etc/yum.repos.d/pgdg-redhat-all.repo中的baseurl为具体的RHEL 7的地址即可。
1 | sed -i.bak 's/$releasever/7/g' /etc/yum.repos.d/pgdg-redhat-all.repo |
然后再yum install postgresql11即可安装。
安装高版本Node.js
安装高版本的原因
Aliyun Linux 2中的Node.js版本和CentOS 7的版本一样,都是6.x版本。但现在很多npm包都需要8.x以上版本。 因此需要手动安装高版本Node.js。
安装问题
安装nodejs多种方法,可以源码编译,也可以使用nvm。不过我比较偏爱的方式是从NodeSource Repository中安装。
以nodejs 10.x为例,正常安装方法是
1 | curl –sL https://rpm.nodesource.com/setup_10.x | sudo bash - |
这个步骤在CentOS 7.x和Amazon Linux上都可以work, 但是在Aliyun Linux 2上会提示系统版本不兼容。
1 | ## You don't appear to be running a supported version of Enterprise Linux. Please contact NodeSource at https://github.com/nodesource/distributions/issues if you think this is incorrect or would like your architecture to be considered for support. Include your 'distribution package' name: alinux-release-2.1903-4.al7.x86_64. |
原因调查
下载了setup_10.x的源码来进行查看,发现有这么一段检查OS的代码。
1 | DISTRO_PKG=$(rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release) |
哈哈,从逻辑上看,setup_10.x的脚本能识别Oracle Linux和Amazon Linux,但是貌似并不支持Aliyun Linux。
解决办法
简单修改了下,让脚本将Aliyun Linux识别为el 7。
1 | sed -i.bak 's/\^(enterprise|system)-release-/\^(enterprise|system|alinux)-release-/g' setup_10.x |
再进行安装,就能够work了。
1 | sudo bash setup_10.x |
额外延伸
获取yum变量的小命令
可以使用如下python命令来获取yum的变量
1 | python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)' |
下面是该命令在Aliyun Linux 2,Amazon Linux 2和CentOS 7.6上的输出。
** Aliyun Linux 2的结果 **
1 | $ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)' |
** Amazon Linux 2上的运行结果 **
1 | $ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)' |
** CentOS 7.6上的运行结果 **
1 | $ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)' |
各系统redhat-release的信息
命令rpm -q --whatprovides redhat-release在各个系统Aliyun Linux 2,Amazon Linux 2和CentOS 7.6上的输出。
** Aliyun Linux 2的结果 **
1 | $ rpm -q --whatprovides redhat-release |
** Amazon Linux 2上的运行结果 **
1 | $ rpm -q --whatprovides redhat-release |
** CentOS 7.6上的运行结果 **
1 | $ rpm -q --whatprovides redhat-release |
安装Postgresql 11 Server
按照上文所述,添加pgdg的repository。
安装Server包
1 | yum install -y postgresql11-server |
初始化数据库和开启开机启动。
1 | /usr/pgsql-11/bin/postgresql-11-setup initdb |
根据实际需要,修改绑定地址
打开/var/lib/pgsql/11/data/postgresql.conf, 找到listen_address, 进行修改。
1 | listen_address = '*' # defaults to 'localhost'; use '*' for all |
修改postgres账号默认密码
1 | $ su - postgres |
修改/var/lib/pgsql/11/data/pg_hba.conf, 添加如下允许任意地址访问或修改为实际允许访问的地址。
1 | # Accept from anywhere |
感悟
写一个或者定制一个系统,已经是一件不容易的事情了。需要构建一个生态,就更加地不容易。
查了下nodesource最新的setup_14.x脚本, 发现还是没支持Aliyun自定制的Linux。
为了方便以后可能还要用到,每次手动改脚本比较麻烦。就fork了一份nodesource/distributions到自己的Github上jibing57/distributions, 研究了一下,添加了Aliyun Linux 2的支持。commit - Add Aliyun Linux 2 support
以后要安装时,可以直接从自己的Github上拿setup脚本安装。
1 | curl -sL 'https://raw.githubusercontent.com/jibing57/distributions/master/rpm/setup_10.x' | sudo bash - |
添加完后,顺带也给nodesource/distributions提了个pull request, 不知道会不会被采纳。