Ceph 快速部署 ( Centos7 + Jewel )

这篇文章主要介绍了如何用三台虚拟机搭建一套Ceph分布式系统,步骤简洁但不失准确性。环境清理一小节可以解决绝大多数部署不成功的问题,最后一节介绍了常用的Ceph操作,希望能给刚搭建环境的同学一些帮助。

环境

三台装有CentOS 7的主机,每台主机有三个磁盘(虚拟机磁盘要大于100G),详细信息如下:

[root@ceph-1~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@ceph-1 ~]# lsblk 
NAME            MAJ:MINRM   SIZE RO TYPE MOUNTPOINT
sda              8:0    0   128G  0 disk 
├─sda1           8:1    0   500M  0 part /boot
└─sda2           8:2    0 127.5G  0 part 
 ├─centos-root 253:0    0    50G 0 lvm  /
 ├─centos-swap 253:1    0     2G 0 lvm  [SWAP]
  └─centos-home 253:2    0  75.5G  0 lvm /home
sdb              8:16   0     2T  0 disk 
sdc              8:32   0     2T  0 disk 
sdd              8:48   0     2T  0 disk 
sr0             11:0    1  1024M  0 rom  
[root@ceph-1 ~]# cat /etc/hosts
..
192.168.57.222ceph-1                                                                                                                                      
192.168.57.223ceph-2                                                                                                                                      
192.168.57.224 ceph-3

集群配置如下:

未分类

环境清理

如果之前部署失败了,不必删除ceph客户端,或者重新搭建虚拟机,只需要在每个节点上执行如下指令即可将环境清理至刚安装完ceph客户端时的状态!强烈建议在旧集群上搭建之前清理干净环境,否则会发生各种异常情况。

psaux|grep ceph |awk '{print $2}'|xargs kill -9
ps -ef|grep ceph
#确保此时所有ceph进程都已经关闭!!!如果没有关闭,多执行几次。
umount /var/lib/ceph/osd/*
rm -rf /var/lib/ceph/osd/*
rm -rf /var/lib/ceph/mon/*
rm -rf /var/lib/ceph/mds/*
rm -rf /var/lib/ceph/bootstrap-mds/*
rm -rf /var/lib/ceph/bootstrap-osd/*
rm -rf /var/lib/ceph/bootstrap-rgw/*
rm -rf /var/lib/ceph/tmp/*
rm -rf /etc/ceph/*
rm -rf /var/run/ceph/*

yum源及ceph的安装

需要在每个主机上执行以下指令:

yumclean all
rm -rf /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i '/aliyuncs/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i '/aliyuncs/d' /etc/yum.repos.d/epel.repo
sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo

增加ceph的源:

vim/etc/yum.repos.d/ceph.repo

添加以下内容:

[ceph]
name=ceph
baseurl=http://mirrors.163.com/ceph/rpm-jewel/el7/x86_64/
gpgcheck=0
[ceph-noarch]
name=cephnoarch
baseurl=http://mirrors.163.com/ceph/rpm-jewel/el7/noarch/
gpgcheck=0

安装ceph客户端:

yummakecache
yum install ceph ceph-radosgw rdate -y
关闭selinux&firewalld
sed-i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl stop firewalld 
systemctl disable firewalld

同步各个节点时间:

yum-y install rdate
rdate -s time-a.nist.gov
echo rdate -s time-a.nist.gov >> /etc/rc.d/rc.local 
chmod +x /etc/rc.d/rc.local

开始部署

在部署节点(ceph-1)安装ceph-deploy,下文的部署节点统一指ceph-1:

[root@ceph-1~]# yum -y install ceph-deploy
[root@ceph-1 ~]# ceph-deploy --version
1.5.34
[root@ceph-1 ~]# ceph -v
ceph version 10.2.2 (45107e21c568dd033c2f0a3107dec8f0b0e58374)

在部署节点创建部署目录并开始部署:

[root@ceph-1~]# cd
[root@ceph-1 ~]# mkdir cluster
[root@ceph-1 ~]# cd cluster/
[root@ceph-1 cluster]# ceph-deploy new ceph-1 ceph-2 ceph-3

如果之前没有ssh-copy-id到各个节点,则需要输入一下密码,过程log如下:

[ceph_deploy.conf][DEBUG] found configuration file at: /root/.cephdeploy.conf
[ceph_deploy.cli][INFO  ] Invoked (1.5.34): /usr/bin/ceph-deploy newceph-1 ceph-2 ceph-3
[ceph_deploy.cli][INFO  ] ceph-deploy options:
[ceph_deploy.cli][INFO  ] username                     : None
[ceph_deploy.cli][INFO  ] func                         : <function new at 0x7f91781f96e0>
[ceph_deploy.cli][INFO  ] verbose                      : False
[ceph_deploy.cli][INFO  ] overwrite_conf               : False
[ceph_deploy.cli][INFO  ] quiet                        : False
[ceph_deploy.cli][INFO  ] cd_conf                      : <ceph_deploy.conf.cephdeploy.Conf instance at 0x7f917755ca28>
[ceph_deploy.cli][INFO  ] cluster                      : ceph
[ceph_deploy.cli][INFO  ] ssh_copykey                  : True
[ceph_deploy.cli][INFO  ]  mon                          :['ceph-1', 'ceph-2', 'ceph-3']
..
..
ceph_deploy.new][WARNIN] could not connect via SSH
[ceph_deploy.new][INFO  ] will connect again with password prompt
The authenticity of host 'ceph-2 (192.168.57.223)' can't be established.
ECDSA key fingerprint is ef:e2:3e:38:fa:47:f4:61:b7:4d:d3:24:de:d4:7a:54.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ceph-2,192.168.57.223' (ECDSA) to the list of knownhosts.
root
root@ceph-2's password: 
[ceph-2][DEBUG ] connected to host: ceph-2 
..
..
[ceph_deploy.new][DEBUG ] Resolving host ceph-3
[ceph_deploy.new][DEBUG ] Monitor ceph-3 at 192.168.57.224
[ceph_deploy.new][DEBUG ] Monitor initial members are ['ceph-1', 'ceph-2','ceph-3']
[ceph_deploy.new][DEBUG ] Monitor addrs are ['192.168.57.222','192.168.57.223', '192.168.57.224']
[ceph_deploy.new][DEBUG ] Creating a random mon key...
[ceph_deploy.new][DEBUG ] Writing monitor keyring to ceph.mon.keyring...
[ceph_deploy.new][DEBUG ] Writing initial config to ceph.conf...

此时,目录内容如下:

[root@ceph-1cluster]# ls
ceph.conf  ceph-deploy-ceph.log  ceph.mon.keyring

根据自己的IP配置向ceph.conf中添加public_network,并稍微增大mon之间时差允许范围(默认为0.05s,现改为2s):

[root@ceph-1cluster]# echo public_network=192.168.57.0/24 >> ceph.conf 
[root@ceph-1 cluster]# echo mon_clock_drift_allowed = 2 >> ceph.conf 
[root@ceph-1 cluster]# cat ceph.conf 
[global]
fsid = 0248817a-b758-4d6b-a217-11248b098e10
mon_initial_members = ceph-1, ceph-2, ceph-3
mon_host = 192.168.57.222,192.168.57.223,192.168.57.224
auth_cluster_required = cephx
auth_service_required = cephx
auth_client_required = cephx

public_network=192.168.57.0/24
mon_clock_drift_allowed = 2

开始部署monitor:

[root@ceph-1cluster]# ceph-deploy mon create-initial
..
..若干log
[root@ceph-1 cluster]# ls
ceph.bootstrap-mds.keyring  ceph.bootstrap-rgw.keyring ceph.conf            ceph.mon.keyring
ceph.bootstrap-osd.keyring  ceph.client.admin.keyring  ceph-deploy-ceph.log

查看集群状态:

[root@ceph-1cluster]# ceph -s
    cluster 0248817a-b758-4d6b-a217-11248b098e10
     health HEALTH_ERR
            no osds
            Monitorclock skew detected 
     monmap e1: 3 mons at{ceph-1=192.168.57.222:6789/0,ceph-2=192.168.57.223:6789/0,ceph-3=192.168.57.224:6789/0}
            electionepoch 6, quorum 0,1,2 ceph-1,ceph-2,ceph-3
     osdmap e1: 0 osds: 0 up, 0 in
            flagssortbitwise
      pgmap v2: 64 pgs, 1 pools, 0 bytes data, 0objects
            0 kB used, 0kB / 0 kB avail
                 64 creating

开始部署OSD:

ceph-deploy--overwrite-conf osd prepare ceph-1:/dev/sdb ceph-1:/dev/sdc ceph-1:/dev/sddceph-2:/dev/sdb ceph-2:/dev/sdc ceph-2:/dev/sdd ceph-3:/dev/sdb ceph-3:/dev/sdcceph-3:/dev/sdd  --zap-disk
ceph-deploy --overwrite-conf osd activate ceph-1:/dev/sdb1 ceph-1:/dev/sdc1ceph-1:/dev/sdd1 ceph-2:/dev/sdb1 ceph-2:/dev/sdc1 ceph-2:/dev/sdd1ceph-3:/dev/sdb1 ceph-3:/dev/sdc1 ceph-3:/dev/sdd1

我在部署的时候出了个小问题,有一个OSD没成功(待所有OSD部署完毕后,再重新部署问题OSD即可解决),如果不出意外的话,集群状态应该如下:

[root@ceph-1cluster]# ceph -s
    cluster 0248817a-b758-4d6b-a217-11248b098e10
     health HEALTH_WARN
            too few PGsper OSD (21 < min 30)
     monmap e1: 3 mons at{ceph-1=192.168.57.222:6789/0,ceph-2=192.168.57.223:6789/0,ceph-3=192.168.57.224:6789/0}
            electionepoch 22, quorum 0,1,2 ceph-1,ceph-2,ceph-3
     osdmap e45: 9 osds: 9 up, 9 in
            flagssortbitwise
      pgmap v82: 64 pgs, 1 pools, 0 bytes data, 0objects
            273 MB used,16335 GB / 16336 GB avail
                 64 active+clean

去除这个WARN,只需要增加rbd池的PG就好:

[root@ceph-1cluster]# ceph osd pool set rbd pg_num 128
set pool 0 pg_num to 128
[root@ceph-1 cluster]# ceph osd pool set rbd pgp_num 128
set pool 0 pgp_num to 128
[root@ceph-1 cluster]# ceph -s
    cluster 0248817a-b758-4d6b-a217-11248b098e10
     health HEALTH_ERR
            19 pgs arestuck inactive for more than 300 seconds
            12 pgspeering
            19 pgs stuckinactive
     monmap e1: 3 mons at{ceph-1=192.168.57.222:6789/0,ceph-2=192.168.57.223:6789/0,ceph-3=192.168.57.224:6789/0}
            electionepoch 22, quorum 0,1,2 ceph-1,ceph-2,ceph-3
     osdmap e49: 9 osds: 9 up, 9 in
            flagssortbitwise
      pgmap v96: 128 pgs, 1 pools, 0 bytes data, 0objects
            308 MB used,18377 GB / 18378 GB avail
                103 active+clean
                 12 peering
                  9 creating
                  4 activating
[root@ceph-1 cluster]# ceph -s
    cluster 0248817a-b758-4d6b-a217-11248b098e10
     health HEALTH_OK
     monmap e1: 3 mons at {ceph-1=192.168.57.222:6789/0,ceph-2=192.168.57.223:6789/0,ceph-3=192.168.57.224:6789/0}
            electionepoch 22, quorum 0,1,2 ceph-1,ceph-2,ceph-3
     osdmap e49: 9 osds: 9 up, 9 in
            flagssortbitwise
      pgmap v99: 128 pgs, 1 pools, 0 bytes data, 0objects
            310 MB used,18377 GB / 18378 GB avail
                128 active+clean

至此,集群部署完毕。

config推送

请不要使用直接修改某个节点的/etc/ceph/ceph.conf文件的方式,而是去部署节点(此处为ceph-1:/root/cluster/ceph.conf)目录下修改。因为节点到几十个的时候,不可能一个个去修改的,采用推送的方式快捷安全!修改完毕后,执行如下指令,将conf文件推送至各个节点:

[root@ceph-1cluster]# ceph-deploy --overwrite-conf config push ceph-1 ceph-2 ceph-3

此时,需要重启各个节点的monitor服务,见下一节。

mon&osd启动方式

#monitor start/stop/restart
#ceph-1为各个monitor所在节点的主机名。
systemctl start ceph-mon@ceph-1.service
systemctl restart ceph-mon@ceph-1.service
systemctl stop ceph-mon@ceph-1.service

#OSD start/stop/restart 
#0为该节点的OSD的id,可以通过`ceph osd tree`查看
systemctl start/stop/restart ceph-osd@0.service

[root@ceph-1 cluster]# ceph osd tree
ID WEIGHT   TYPE NAME       UP/DOWNREWEIGHT PRIMARY-AFFINITY 
-1 17.94685 rootdefault                                     
-2  5.98228     hostceph-1                                  
 0  1.99409        osd.0        up 1.00000          1.00000 
 1  1.99409        osd.1        up 1.00000          1.00000 
 8  1.99409        osd.2        up 1.00000          1.00000 
-3  5.98228     hostceph-2                                  
 2  1.99409        osd.3        up 1.00000          1.00000 
 3  1.99409        osd.4        up 1.00000          1.00000 
 4  1.99409        osd.5        up 1.00000          1.00000 
-4  5.98228     hostceph-3                                  
 5  1.99409        osd.6        up 1.00000          1.00000 
 6  1.99409        osd.7        up  1.00000         1.00000 
 7  1.99409        osd.8        up 1.00000          1.00000

阿里云centos7.3编译安装NGINX+PHP7+MariaDB+MEMCACHED

一、安装前准备

  • 修改默认主机名称
[root@iZuf60c5bxd15kr9gycvv6Z ~]# hostnamectl set-hostname centos7
[root@iZuf60c5bxd15kr9gycvv6Z ~]# reboot
[root@iZuf60c5bxd15kr9gycvv6Z ~]# yum update
  • 安装依赖库
[root@centos7 ~]# yum -y install libaio libaio-devel bison bison-devel zlib-devel openssl openssl-devel ncurses ncurses-devel libcurl-devel libarchive-devel boost boost-devel lsof wget gcc gcc-c++ make cmake perl kernel-headers kernel-devel pcre-devel

二、删除系统默认数据库配置文件

  • 查询
[root@centos7 ~]# find -H /etc/ | grep my.c
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
/etc/pki/tls/certs/renew-dummy-cert
/etc/pki/tls/certs/make-dummy-cert
  • 删除
[root@centos7 ~]# rm -rf /etc/my.cnf /etc/my.cnf.d /etc/my.cnf.d/mysql-clients.cnf
  • 确认
[root@centos7 ~]# find -H /etc/ | grep my.c
/etc/pki/tls/certs/renew-dummy-cert
/etc/pki/tls/certs/make-dummy-cert

三、卸载系统自带mariadb-libs

  • 查询
[root@centos7 ~]# rpm -qa|grep mariadb-libs
mariadb-libs-5.5.52-1.el7.x86_64
  • 删除
[root@centos7 ~]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps

四、安装MariaDB数据库

  • 下载安装包
[root@centos7 ~]# cd /usr/local/src
[root@centos7 src]# wget https://downloads.mariadb.org/interstitial/mariadb-10.2.8/source/mariadb-10.2.8.tar.gz 
  • 解压
[root@centos7 src]# tar -zxvf mariadb-10.2.8.tar.gz
  • 创建数据库安装目录,数据存放目录,以及用户组、用户
# 创建mysql用户组
[root@centos7 mariadb-10.2.8]# groupadd -r mysql
# 创建mysql用户
[root@centos7 mariadb-10.2.8]# useradd -r -g mysql -s /sbin/nologin -d /usr/local/mysql -M mysql
# 创建安装目录
[root@centos7 mariadb-10.2.8]# mkdir -p /usr/local/mysql
# 创建数据存放目录
[root@centos7 mariadb-10.2.8]# mkdir -p /data/mysql
# 赋以mysql用户读写权限
[root@centos7 mariadb-10.2.8]# chown -R mysql:mysql /data/mysql
  • 编译安装
[root@centos7 mariadb-10.2.8]# cd /usr/local/src/mariadb-10.2.8
# 输入以下编译参数
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
-DMYSQL_DATADIR=/data/mysql 
-DSYSCONFDIR=/etc 
-DWITHOUT_TOKUDB=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_ARCHIVE_STPRAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWIYH_READLINE=1 
-DWIYH_SSL=system 
-DVITH_ZLIB=system 
-DWITH_LOBWRAP=0 
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci

# 如果编译失败请删除CMakeCache.txt
[root@centos7 mariadb-10.2.8]# rm -f CMakeCache.txt
# 让指令重新执行,否则每次读取这个文件,命令修改正确也是报错
# cmake没问题,可以编译并且安装了

# 开始安装,这个过程比较久,跟据电脑配制不同可能会有10-30分钟
[root@centos7 mariadb-10.2.8]# make && make install
  • 导入mysql系统表
[root@centos7 mariadb-10.2.8]# cd /usr/local/mysql/
[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 复制配制文件
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
  • 编写快捷启动脚本
[root@centos7 mysql]# vi /lib/systemd/system/mysql.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=MySQL Community Server
After=network.target

[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
PIDFile=/data/mysql/centos7.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/usr/local/mysql/support-files/mysql.server restart
ExecStop=/usr/local/mysql/support-files/mysql.server stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 mysql]# systemctl enable mysql.service
  • 其它命令说明
# 启动mysql
[root@centos7 mysql]# systemctl start mysql.service
# 停止mysql
[root@centos7 mysql]# systemctl stop mysql.service
# 重启mysql
[root@centos7 mysql]# systemctl restart mysql.service
# 如果提示:Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
# 请选结束mysql进程后再尝试执行上面的快捷操作
[root@centos7 mysql]# pkill -9 mysql
  • 配置环境变量(以便在任何目录下输入mysql命令)
# 创建独立环境变量文件
[root@localhost mysql]# touch /etc/profile.d/mysql.sh
# 写入变量值
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' > /etc/profile.d/mysql.sh 
# 赋以执行权限
[root@localhost mysql]# chmod 0777 /etc/profile.d/mysql.sh 
# 刷新生效
[root@localhost mysql]# source /etc/profile.d/mysql.sh
  • 初始化MariaDB
# 运行MariaDB初始化脚本
[root@localhost mysql]# ./bin/mysql_secure_installation

# --------------------------------------------------------------------------
# 根据相关提示进行操作
# 以下提示:
# --------------------------------------------------------------------------
Enter current password for root (enter for none):    输入当前root密码(没有输入)
Set root password? [Y/n]                             设置root密码?(是/否)
New password:                                        输入新root密码
Re-enter new password:                               确认输入root密码
Password updated successfully!                       密码更新成功
Remove anonymous users? [Y/n]                        删除匿名用户?(是/否)
Disallow root login remotely? [Y/n]                  不允许root登录远程?(是/否)
Reload privilege tables now? [Y/n]                   现在重新加载权限表(是/否)

#全部完成!如果你已经完成了以上步骤,MariaDB安装现在应该安装完成。
  • 创建外部管理员帐号(根据需要,请尽量保证密码的复杂性避免数据库外泄)
[root@localhost mysql] mysql -uroot -p
# 根据提示输入密码
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

五、安装NGINX

  • 下载安装包并解压
[root@localhost mysql]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@localhost src]# tar -zxvf nginx-1.12.1.tar.gz
  • 创建NGINX安装目录,WEB存放目录,以及用户组、用户
# 创建nginx用户组
[root@centos7 src]# groupadd -r www
# 创建nginx用户
[root@centos7 src]# useradd -r -g www -s /sbin/nologin -d /usr/local/nginx -M www
# 创建安装目录
[root@centos7 src]# mkdir -p /usr/local/nginx
# 创建数据存放目录
[root@centos7 src]# mkdir -p /data/web
# 赋以mysql用户读写权限
[root@centos7 src]# chown -R www:www /data/web
  • 编译安装
[root@localhost src]# cd nginx-1.12.1
# 输入以下参数
./configure 
--prefix=/usr/local/nginx 
--without-http_memcached_module 
--user=www  
--group=www 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_gzip_static_module

# 如果看到以下说明则编译成功
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

# 开始安装
[root@localhost nginx-1.12.1]# make && make install
  • 尝试启动
[root@centos7 nginx-1.12.1]# /usr/local/nginx/sbin/nginx
# 如果未提示错误即代表安装成功
  • 编写快捷启动脚本
[root@centos7 nginx-1.12.1]# vi /lib/systemd/system/nginx.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 nginx-1.12.1]# systemctl enable nginx.service
  • 其它命令说明
# 启动nginx
[root@centos7 nginx-1.12.1]# systemctl start nginx.service
# 停止nginx
[root@centos7 nginx-1.12.1]# systemctl stop nginx.service
# 重启nginx
[root@centos7 nginx-1.12.1]# systemctl restart nginx.service
# 如果提示:Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
# 请选结束nginx进程后再尝试执行上面的快捷操作
[root@centos7 nginx-1.12.1]# pkill -9 nginx
  • 修改配NGINX配制文件
[root@centos7 php-7.1.9]# cd /usr/local/nginx/conf
[root@centos7 conf]# vi nginx.conf
# 修改如下代码
# --------------------------------------------------------------------------
#user  nobody;去除前面#号,并将用户改为www www
user www www;
# --------------------------------------------------------------------------
#pid        logs/nginx.pid; 去除前面的#号 
pid        logs/nginx.pid;
# --------------------------------------------------------------------------
去除前面的#号 如下
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
# --------------------------------------------------------------------------
# gzip  on;去除前面的#号并加上隐藏版号代码 
server_tokens off;
# --------------------------------------------------------------------------
index  index.html index.htm;后面加上index.php 默认页
index  index.html index.htm index.php;
# --------------------------------------------------------------------------
去下以下前面的#号并做如下修改
location ~ .php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
  • 最后张节介绍虚拟主机配制,以及memcache配制

七、安装PHP7

  • 下载安装包并解压
[root@centos7 nginx-1.12.1]# cd /usr/local/src
[root@centos7 src]# wget http://cn.php.net/distributions/php-7.1.9.tar.gz
[root@centos7 src]# tar -zxvf php-7.1.9.tar.gz
  • 安装必要的相关扩展
[root@centos7 src]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
  • 编译安装
[root@localhost src]# cd php-7.1.9
# 输入以下参数
./configure 
--prefix=/usr/local/php 
--with-config-file-path=/usr/local/php/etc 
--enable-fpm 
--with-fpm-user=www  
--with-fpm-group=www 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared  
--enable-soap 
--with-libxml-dir 
--with-xmlrpc 
--with-openssl 
--with-mcrypt 
--with-mhash 
--with-pcre-regex 
--with-zlib 
--enable-bcmath 
--with-iconv 
--with-bz2 
--enable-calendar 
--with-curl 
--with-cdb 
--enable-dom 
--enable-exif 
--enable-fileinfo 
--enable-filter 
--with-pcre-dir 
--enable-ftp 
--with-gd 
--with-openssl-dir 
--with-jpeg-dir 
--with-png-dir 
--with-zlib-dir  
--with-freetype-dir 
--enable-gd-native-ttf 
--enable-gd-jis-conv 
--with-gettext 
--with-gmp 
--with-mhash 
--enable-json 
--enable-mbstring 
--enable-mbregex 
--enable-mbregex-backtrack 
--with-libmbfl 
--with-onig 
--enable-pdo 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-zlib-dir 
--with-pdo-sqlite 
--with-readline 
--enable-session 
--enable-shmop 
--enable-simplexml 
--enable-sockets  
--enable-sysvmsg 
--enable-sysvsem 
--enable-sysvshm 
--enable-wddx 
--with-libxml-dir 
--with-xsl 
--enable-zip 
--enable-mysqlnd-compression-support 
--with-pear 
--enable-opcache

# 开始安装
[root@localhost php-7.1.9]# make && make install
  • 配置环境变量(以便在任何目录下输入php命令)
# 创建独立环境变量文件
[root@localhost php-7.1.9]# touch /etc/profile.d/php.sh
# 写入变量值
[root@localhost php-7.1.9]# echo 'export PATH=$PATH:/usr/local/php/bin/' > /etc/profile.d/php.sh 
# 赋以执行权限
[root@localhost php-7.1.9]# chmod 0777 /etc/profile.d/php.sh 
# 刷新生效
[root@localhost php-7.1.9]# source /etc/profile.d/php.sh
  • 配制php.ini
[root@centos7 php-7.1.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@centos7 php-7.1.9]# vi /usr/local/php/etc/php.ini
# 做以下修改(时区,不显示版本号,开启opcache缓存加速PHP)
# --------------------------------------------------------------------------
1.找到:;date.timezone =                               修改为:date.timezone = PRC
2.找到:expose_php = On                                   修改为:expose_php = Off
3.找到:opcache.enable=0                              修改为:opcache.enable=1
4.在 Dynamic Extensions 代码块中添加 zend_extension=opcache.so
  • 配置php-fpm
[root@centos7 php-7.1.9]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@centos7 php-7.1.9]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@centos7 php-7.1.9]# cp sapi/fpm/init.d.php-fpm /usr/local/php/bin/php-fpm
[root@centos7 php-7.1.9]# chmod 0777 /usr/local/php/bin/php-fpm
  • 尝试启动
[root@centos7 php-7.1.9]# /usr/local/php/bin/php-fpm start
Starting php-fpm  done
# 如提示以上即表示安装成功
  • 编写快捷启动脚本
[root@centos7 php-7.1.9]# vi /lib/systemd/system/php-fpm.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/bin/php-fpm start
ExecReload=/usr/local/php/bin/php-fpm restart
ExecStop=/usr/local/php/bin/php-fpm stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 php-7.1.9]# systemctl enable php-fpm.service
  • 其它命令说明
# 启动php-fpm
[root@centos7 php-7.1.9]# systemctl start php-fpm.service
# 停止php-fpm
[root@centos7 php-7.1.9]# systemctl stop php-fpm.service
# 重启php-fpm
[root@centos7 php-7.1.9]# systemctl restart php-fpm.service
# 如果提示:Job for php-fpm.service failed because the control process exited with error code. See "systemctl status php-fpm.service" and "journalctl -xe" for details.
# 请选结束php-fpm进程后再尝试执行上面的快捷操作
[root@centos7 php-7.1.9]# pkill -9 php-fpm

八、NGINX虚拟主机配制

  • 打开配制文件
[root@centos7 php-7.1.9]# vi /usr/local/nginx/conf/nginx.conf
  • 在http{}节点尾加入以下代码
    ##############################################
    #LIANGZHI
    ##############################################
    server {
        listen          80;
        server_name     *.demo.com;
        root            /data/web/demo/www;
        access_log      /data/web/demo/log/access.log  main;
        error_log       /data/web/demo/log/error.log error;
        index           index.php;

        #THINKPHP伪静态
        location / {
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }
        }

        #解析PHP代码
        location ~ .php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        #静态资源缓存1天
        location ~ .*.(gif|jpg|jpeg|png|bmp|ico|swf|js|css)$ {
            expires     1d;
            access_log  off;
        }

        #字体文件跨域问题
        location ~ .*.(eof|ttf|ttc|otf|eof|woff|woff2|svg)(.*){
            add_header Access-Control-Allow-Origin *;
        }
    }

九、安装PHP支持插件Memcache

[root@centos7 www]# cd /usr/local/src
[root@centos7 src]# wget http://memcached.org/files/memcached-1.5.1.tar.gz
[root@centos7 src]# tar -zxvf memcached-1.5.1.tar.gz
[root@centos7 src]# cd memcached-1.5.1
[root@centos7 memcached-1.5.1]# yum install libevent*
[root@centos7 memcached-1.5.1]# ./configure --prefix=/usr/local/memcached
[root@centos7 memcached-1.5.1]# make && make install
  • 编写快捷启动脚本
[root@centos7 memcached-1.5.1]# vi /usr/local/memcached/memcached
# 输入以下代码(启动参数请在下面的脚本中修改,如端口,最大内存)
#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached

# Source function library.
. /etc/rc.d/init.d/functions

PORT=11211
USER=root 
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then
    . /etc/sysconfig/memcached
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0

start () {
    echo "Starting memcached ..."
    # insure that /var/run/memcached has proper permissions
    chown $USER /usr/local/memcached/bin/memcached
    /usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo "Stopping memcached ..."
    killproc memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f /var/run/memcached.pid
    fi
}

restart () {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
    stop
    ;;
    status)
    status memcached
    ;;
    restart|reload)
    restart
    ;;
    condrestart)
    [ -f /var/lock/subsys/memcached ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
    exit 1
esac
  • 脚本执行权限
[root@localhost memcached-1.5.1]# chmod 0777 /usr/local/memcached/memcached
编写服务启动脚本
[root@centos7 memcached-1.5.1]# vi /lib/systemd/system/memcached.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=memcached
After=network.target

[Service]
Type=forking
PIDFile=/var/run/memcached.pid
ExecStart=/usr/local/memcached/memcached start
ExecReload=/usr/local/memcached/memcached restart
ExecStop=/usr/local/memcached/memcached stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 memcached-1.5.1]# systemctl enable memcached.service
  • 其它命令说明
# 启动memcached
[root@centos7 memcached-1.5.1]# systemctl start memcached.service
# 停止memcached
[root@centos7 memcached-1.5.1]# systemctl stop memcached.service
# 重启memcached
[root@centos7 memcached-1.5.1]# systemctl restart memcached.service
# 如果提示:Job for memcached.service failed because the control process exited with error code. See "systemctl status memcached.service" and "journalctl -xe" for details.
# 请选结束memcached进程后再尝试执行上面的快捷操作
[root@centos7 memcached-1.5.1]# pkill -9 memcached
  • 为PHP增加memcache支持(官司网memcache-3.0.8暂时不支持PHP的编译)

官网memcache-3.0.8在编译的时候出现“php-smart_str.h”没有找到的错误!
但是我们可以在github里面找到pecl-memcache支持PHP7的分支,请按以下操作即可完成memcache中扩展安装

[root@centos7 memcached]# cd /usr/local/src/php-7.1.9/ext
# 如果已经安装git忽略yun install git
[root@centos7 memcached]# yum install git
[root@centos7 ext]# git clone https://github.com/websupport-sk/pecl-memcache memcache
[root@centos7 ext]# cd memcache
[root@centos7 ext]# yum install autoconf
[root@centos7 ext]# /usr/local/php/bin/phpize
[root@centos7 ext]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@centos7 ext]# make && make install
  • 修改php.ini
[root@centos7 ext]# vi /usr/local/php/etc/php.ini
# 在 Dynamic Extensions 代码块中添加如下扩展
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/memcache.so
  • 重启linux完成安装(或重启nginx php-fpm)
[root@centos7 ext]# reboot