CentOS 生产环境配置

初始配置

对于一般配置来说,不需要安装 epel-release 仓库,本文主要在于希望跟随 RHEL 的配置流程,紧跟红帽公司对于服务器的配置说明。

# yum update

安装 centos-release-scl

# yum install centos-release-scl-rh

实际上 CentOS extra 仓库有两个包关于 SCL,一个是 centos-release-scl 还有一个是 centos-release-scl-rh,它们两者的区别在于一个是 SoftwareCollection 小组所有的打包 RPM,另一个则只包含 RedHat 官方打包内容,个人建议使用后者。

SoftwareCollection 包含的内容

rh-java-common
rh-mariadb100
rh-mariadb101
rh-maven33
rh-mongodb26
rh-mongodb30upg
rh-mongodb32
rh-mysql56
rh-nginx18
rh-nodejs4
rh-perl520
rh-php56/
rh-postgresql94/
rh-postgresql95/
rh-python35/
rh-ror42/
rh-ruby23/
rh-varnish4/

除此之外,还包含了一项重要内容 Devtoolset,如果在 CentOS 上使用过 node-gyp 的朋友应该对此非常熟悉,上面这些 SoftwareCollection 足以包含目前主流的应用环境部署,所以,如果有朋友说 RHEL “太老”,肯定是对 RHEL 的软件策略不熟悉。

Nginx

目前 stable 版本的 Nginx 是 nginx1.12 版本,总共有五个包

essential package:
rh-nginx112
rh-nginx112-nginx
rh-nginx112-runtime
optional package:
rh-nginx112-build
rh-nginx112-scldevel

熟悉 RHEL 的朋友应该看出来了,rh-nginx112 是总包,rh-nginx112-runtime 是各类配置脚本,rh-nginx112-build 是编译配置,rh-nginx112-scldevel 是开发包。

# yum install rh-nginx112

nginx 就安装好了,然后启动nginx:

# systemctl list-unit-files |grep nginx
rh-nginx112-nginx.service                     disabled
# systemctl start rh-nginx112-nginx.service

PHP

目前 PHP SCL 版本为 7.1,不过 RHEL 7.3 已经发布,并且其中 SCL 版本升级到了 PHP 7,可以说是重大利好,PHP 7 的高性能是很多 Web 应用急需的,而且作为官方的打包,其质量还是很可靠的(SoftwareCollection 实际上不是官方打包的,但是是第三方社区根据 RedHat 释出的编译配置重编译产生的,可靠性还是有保证的)
由于 PHP 可选软件包过多,这里就不列出所有的软件包列表了,只需要安装总包,就能将必须的软件包安装到服务器上,不过总包不包含 FPM,因此需要手动将其安装。
在安装 PHP-MySQL、PHP-PostgreSQL 这样的数据库支持库的时候,可能有人发现并没有安装 SCL 中提供的 MySQL-Libs 或者 PostgreSQL-libs,而是使用了官方自带的库文件,RedHat 官方也给出了解释是为了保持兼容性,而且使用官方自带老版本的 libs 不会导致问题的。

在yum中搜PHP:

#yum search php
...
php54-runtime.x86_64
php55-runtime.x86_64
rh-php56-runtime.x86_64
rh-php70-runtime.x86_64
...

rh- 前缀是RedHat的意思,告诉你这是官方提供的PHP而不是第三方库提供的。作者猜测这个前缀规范是2015年左右制定的,PHP5.4 & 5.5是在这之前发布的,为了保证你的程序的兼容性(比如你的某些程序里已经写了yum install php55,加个前缀程序就挂了),就没带上rh- 前缀。

接下来,安装PHP7.1套件和它的插件:

# yum install rh-php71 rh-php71-php-fpm

至于其他的包则可以通过 yum 继续安装,这里只示范 PHP-FPM。

安装完之后,PHP实际会安装在/opt/rh目录下。

此时如果你运行php命令,系统依然会提示你command not found。这是因为,SCL的风格就是把软件对系统的影响减少到最小,甚至安装完PHP,php命令都不会被添加到 $PATH 变量中,所以你没法直接执行软件中的命令的。需要通过 scl enable 命令显示执行:

先看看SCL安装了哪些软件:

# scl -l
rh-nginx112
rh-php71

可以看到我们在系统安装了一个rh-php71,先启用它并执行命令:

# scl enable rh-php71 "php -v"
PHP 7.1.8 (cli) (built: Aug  9 2017 13:20:06) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

命令成功执行了。要是每一条命令都要这么执行,太麻烦了!不要紧,你可以不用每次执行一条命令,而是直接执行 bash 命令,那么新开的shell就能自动识别php了:

# scl enable rh-php71 "php -v"
PHP 7.1.8 (cli) (built: Aug  9 2017 13:20:06) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
# scl enable rh-php71 bash
# php -v
PHP 7.1.8 (cli) (built: Aug  9 2017 13:20:06) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
# php -m
[PHP Modules]
bz2
calendar
……

SCL以 scl enable 方式管理软件,虽然在使用上增加了一点点的麻烦,但这绝对地保证了系统稳定性。而且这么做还有一个好处:就是系统上可以多个PHP版本共存而互相不冲突。方便你测试代码或者框架,在各个版本PHP上的兼容性。

运行php-fpm

# 运行php-fpm
/opt/rh/rh-php71/root/sbin/php-fpm
nginx配置php-fpm

# 安装
yum install rh-nginx112-nginx
# 查看服务名称systemctl list-unit-files
rh-nginx112-nginx.service
# 启动,即可访问ip浏览
systemctl start rh-nginx112-nginx.service


配置php-fpm环境

# 查找fpm
ls /opt/rh/rh-php71/root/sbin/php-fpm
# 没有就安装
yum install rh-php71-php-fpm
# 运行php-fpm
/opt/rh/rh-php71/root/sbin/php-fpm
# 修改php.ini之后,平滑重启fpm
# INT, TERM 立刻终止
# QUIT 平滑终止
# USR1 重新打开日志文件
# USR2 平滑重载所有worker进程并重新载入配置和二进制模块
# 参看进程号
ps aux | grep php-fpm
# 主进程master
kill -USR2 1354
# 查看php-fpm环境配置文件
# 位置在/etc/opt/rh/rh-php71/php-fpm.conf
ps -aux |grep php-fpm
# 查看端口9000,fpm默认端口是9000
# command not found: netstat
yum install net-tools
netstat -tnl | grep 9000
# 显示:
# tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN 
# 所有端口
netstat -ntlp
# 查看nginx环境配置文件
# 位置在/etc/opt/rh/rh-nginx112/nginx/nginx.conf
# 修改:
location {}
# 替换为:
location / {
     index index.php index.html index.htm; #添加了一个 index.php
}
# 添加:
location ~ \.php$ {
        root /opt/rh/rh-nginx112/root/usr/share/nginx/html; #指定php的根目录
        fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}
# 检测配置文件是不是ok
/opt/rh/rh-nginx112/root/usr/sbin/nginx -t
# 提示:
# nginx: the configuration file /etc/opt/rh/rh-nginx112/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/opt/rh/rh-nginx112/nginx/nginx.conf test is successful
# 重启
/opt/rh/rh-nginx112/root/usr/sbin/nginx -s reload
# 新建php文件
cd /opt/rh/rh-nginx112/root/usr/share/nginx/html
mv index.html index.html.bak
touch index.php
nano index.php
# 添加
<?php
phpinfo();
# 访问http://192.168.122.163/index.php即可
# 开机启动php-fpm
nano /etc/rc.local
# 加入
/opt/rh/rh-php71/root/sbin/php-fpm

#开机自启动服务
systemctl list-unit-files|grep php
systemctl enable rh-php71-php-fpm.service
systemctl list-unit-files|grep nginx
systemctl enable rh-nginx112-nginx.service

MariaDB

自从 MySQL 被 Oracle 收购后,各个发行版和企业都开始转移阵地,一些企业开发自有分支,比如像阿里巴巴开发 AliSQL,还有就是转向 MySQL 之父的 MariaDB,由于 RHEL 7 使用 MariaDB 替代了 MySQL,所以系统自带的是 MariaDB-libs,因此这里也使用 MariaDB 做示范。

# yum install rh-mariadb102

转载于:https://my.oschina.net/clin003/blog/2875253

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值