当前位置首页 > Nginx知识

ccentos7下安装php5.6并使用nginx+php-fpm部署多个不同端口网站

阅读次数:245 次  来源:admin  发布时间:

作为一个的勤杂工,近期因公司内部信息化的需求,给新进员工提供基础的知识培训和介绍,也为了给公司内部建立一个沟通交流的平台,百度找了开源的百科系统HDwiki和开源的问答系统Tipask问答系统,蛋痛的这两套系统均是php+mysql开发,作为一个有多年.net开发经验的老鸟,面对着这些无法下一步解决的系统部署,心里一遍又一遍地感叹微软的好。

在windows server + IIS + php 7环境内部署Tipask时出现了各种问题,面对着php.ini的配置一时也不知道如何入手,然后切换到centos 7 + nginx + php5.6上。

在centos上安装php,我这边采用的是Webtatic源,Webtatic上最新php版本为7.2,因HDwiki不支持最新的php 7.2,所以选择了5.6版。使用webtatic提供的源进行php的安装非常简单,可参见官方安装步骤。

#安装Webtati的yum仓库
yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

#安装php及插件
yum install php56w php56w-fpm php56w-opcache

#查询php插件并安装
yum search php56w
yum install 插件名称

hp安装完成后,配置nginx进行测试。

[root@localhost conf.d]# vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        # 默认页增加index.php
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #   fastcgi_index  index.php;
    #   fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
    #     include        fastcgi_params;
    #}
    #去掉location ~ \.php$配置节前面的#
    location ~ \.php$ {
        root           html;
        #php-fpm默认的监听端口为9000
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        # 文件位置修改为/usr/share/nginx/html
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

在/usr/share/nginx/html文件夹下增加index.php文件。文件内容如下:

<?php
 phpinfo() 
?>

重启nginx服务,启动php-fpm服务。

#重新载入nginx
nginx -s reload

#将php-fpm设置为开启启动
systemctl enable php-fpm.service
#启动php-fpm服务
systemctl start php-fpm.service

在浏览器中访问index.php页面,出现下图界面说明配置成功。ccentos7下安装php5.6并使用nginx+php-fpm部署多个不同端口网站

hp-fpm默认监听的端口号为9000,如果我们想修改端口号或部署多个系统在不同的端口号时应该如何做呢?

1. 修改监听的端口

通过查看php-fpm的配置文件/etc/php-fpm.conf可以看到include=/etc/php-fpm.d/*.conf的配置,在/etc/php-fpm.d/文件夹中存在www.conf配置文件,打开文件编辑listen=127.0.0.1:9000,将端口号改为其他端口号,然后重启php-fpm.service服务。重启完后,修改nginx配置并重启,即可生效。

2. 部署多个系统在不同的端口号

经检查php-fpm的相关配置文件有:

/etc/php-fpm.conf /etc/php-fpm.d/www.conf /var/run/php-fpm/php-fpm.pid /usr/lib/systemd/system/php-fpm.service

当需要部署多个系统在不同的端口时,可以复制上述4个文件,修改2中的监听端口号,修改4中的启动项,使用-y 制定php-fpm启动的配置文件即可。

上一篇:Nginx安装
下一篇:美女同事的烦恼:如何配置ApacheSkyWalking告警?