目前,zabbix的最新版已经到了3.2.x,遗憾的是,因为centos6自带的各类组件版本太老,zabbix自3.0起,官方的repo就不在支持在CentOS6上面安装zabbix server了(agent客户端还是支持的)。但是,实际生产环境中,CentOS6还远未被淘汰,甚至还有很多公司用RHEL5,或者CentOS5的,所以有必要讲解下,在CentOS6(RHEL6)环境下,自己安装nginx最新稳定版1.10.1,php7最新稳定版7.0.12,以及zabbix3.2.1版本。

基础信息

本机IP:192.168.42.15,已安装了EPEL6的源。从官方下载php-7.0.12.tar.gz  zabbix-3.2.1.tar.gz两个源码到CentOS上面。

安装Nginx:

本来Nginx我是想自己编译安装的,但是现在EPEL上已经有了最新的稳定版1.10.1,我就直接yum安装了,安装方法:

yum install nginx
对权限和selinux进行一些处理:
cd /usr/share/nginx
chown -R nginx:nginx html/
semanage fcontext -a -t httpd_sys_content_t "/usr/share/nginx/html(/.*)?"
restorecon -Rv html/

iptables开放http端口:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

安装PHP7

我们决定自己编译php7,为适应zabbix官方的对php组件的要求,必须安装一些必要的组件:
首先,既然是编译安装,我们先安装编译器gcc

yum -y install gcc

安装编译php7需要的一些组件:

yum install libxml2-devel gettext-devel bison re2c libpng-devel libjpeg-turbo-devel freetype-devel openssl-devel -y

解包php7

tar xf php-7.0.12.tar.gz

切换到php7源码目录

cd php-7.0.12
[root@template php-7.0.12]# ./configure --disable-cgi --with-openssl \
> --with-config-file-path=/etc --sysconfdir=/etc --with-mysql-sock=/var/lib/mysql/mysql.sock \
> --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx \
> --with-gd --enable-bcmath --with-libxml-dir --enable-sockets --enable-mbstring \
> --with-gettext --with-mysqli --with-jpeg-dir --with-freetype-dir

LEMP用Nginx+php-fpm的组合,一般不需要cgi功能,另外一些图片上的组件,主要是为了满足zabbix官方对php的要求。

configure结束后,会提示这些:

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

接下来开始编译:

make -j 4

我的机器是4核i5,所以加了-j 4编译参数。

编译成功后,安装

make install

输出如下:

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP FPM binary:        /usr/local/sbin/
Installing PHP FPM config:        /etc/
Installing PHP FPM man page:      /usr/local/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/fpm/
Installing phpdbg binary:         /usr/local/bin/
Installing phpdbg man page:       /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:           /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/root/php-7.0.12/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

要记得这些安装目录,如果你想卸载或者重新编译,升级等,知道现在的安装目录是有必要的。

添加php-fpm的init启动脚本:

[root@template php-7.0.12]# cp /root/php-7.0.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@template php-7.0.12]# chmod a+x /etc/init.d/php-fpm
[root@template php-7.0.12]# restorecon -v /etc/init.d/php-fpm

拷贝php.ini配置文件

[root@template php-7.0.12]# cp /root/php-7.0.12/php.ini-production /etc/php.ini

编辑下vim /etc/php.ini 改变如下参数:

cgi.fix_pathinfo=0
date.timezone = Asia/Shanghai
post_max_size = 16M
max_execution_time = 300
max_input_time = 300

继续拷贝配置:

[root@template php-7.0.12]# cp /etc/php-fpm.conf.default /etc/php-fpm.conf
[root@template php-7.0.12]# cp /etc/php-fpm.d/www.conf.default /etc/php-fpm.d/www.conf

编辑php-fpm.conf,我们需要重新配置下日志的路径,放到常用的/var/log下面,方便查看日志
vim /etc/php-fpm.conf

error_log = /var/log/php-fpm/error.log

建立日志目录,进行一些权限处理:

[root@template php-7.0.12]# mkdir -m 700 /var/log/php-fpm
[root@template php-7.0.12]# chown nginx:nginx /var/log/php-fpm
[root@template php-7.0.12]# semanage fcontext -a -t httpd_log_t /var/log/php-fpm
[root@template php-7.0.12]# restorecon -Rv /var/log/php-fpm

系统写入pid的目录,会因为selinux而被拒绝写入pid,我们设置下selinux标签:

[root@template php-7.0.12]# semanage fcontext -a -t httpd_var_run_t /usr/local/var/run
[root@template php-7.0.12]# restorecon -v /usr/local/var/run

再给程序本身设置下selinux标签:

[root@template php-7.0.12]# semanage fcontext -a -t httpd_exec_t /usr/local/sbin/php-fpm
[root@template php-7.0.12]# restorecon -v /usr/local/sbin/php-fpm

编辑下www.conf的配置:
[root@template php-7.0.12]# vim /etc/php-fpm.d/www.conf

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

新建立目录,并做一些权限处理:

[root@template php-7.0.12]# mkdir /var/run/php-fpm
[root@template php-7.0.12]# restorecon -v /var/run/php-fpm

配置nginx加载php-fpm

编辑下默认的server配置

[root@template php-7.0.12]# cd /etc/nginx/conf.d/
[root@template conf.d]# vim default.conf

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    index  index.php index.html index.htm;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }   

}

启动nginx、php-fpm

启动php-fpm,并加入开机启动:

[root@template php-7.0.12]# service php-fpm start
Starting php-fpm done
[root@template php-7.0.12]# chkconfig php-fpm on

启动nginx,并加入开机启动:

[root@template php-7.0.12]# service nginx start
Starting nginx:                                            [  OK  ]
[root@template php-7.0.12]# chkconfig nginx on

phpinfo测试

写一个简单的phpinfo文件进行测试:
vim /usr/share/nginx/html/index.php

<?php
phpinfo();
?>

打开服务器的地址http://192.168.42.15/进行测试,如果看到下面类似的界面,证明php环境搭建成功了。

phpinfo

安装、启动mysql,并加入开机启动

安装:

[root@template ~]# yum install mysql-server -y

启动:

[root@template ~]# service mysqld start

初始化设置:

[root@template ~]# mysql_secure_installation

Enter current password for root (enter for none):
OK, successfully used password, moving on…

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!

Remove anonymous users? [Y/n] Y
… Success!

Disallow root login remotely? [Y/n] Y
… Success!

Remove test database and access to it? [Y/n] Y
– Dropping test database…
… Success!
– Removing privileges on test database…
… Success!

Reload privilege tables now? [Y/n] Y
… Success!

需要注意的是,mysql5.1版本root用户是没有初始密码的,建议按照提示设置下。

加入开机启动:

[root@template ~]# chkconfig mysqld on

好了,本教程的第一部分,关于LEMP环境的安装,在此告一段落了,下一部分我们就开始进行zabbix3.2.1的安装了。

CentOS6安装nginx,php7,zabbix3.2教程(1)
Tagged on:             

One thought on “CentOS6安装nginx,php7,zabbix3.2教程(1)

发表评论

电子邮件地址不会被公开。 必填项已用*标注