nginx连接PHP 5.5 – ttlsa教程系列之nginx

凉白开 Nginx6499,82064字数 2533阅读8分26秒阅读模式
摘要

PHP从一开始的php 5.2到现在的5.3/5.4/5.5.起草这篇文章的时候发现官方已经停止了5.3的开发,最后的版本定格到了5.3.27,只会修复一些bug,宣告了5.3寿终正寝了.并且建议大家升级到5.4或者5.5, 鉴于版本更新得如此之快,决定写一片nginx连接php5.5的文章.于是乎,本草文写完了. 1. 安装PHP 5.5.0 下载

这两年IT更新换代的速度太快了,nginx从2年前的1.0版本到现在的1.5版本,各个版本共同开发。PHP从一开始的php 5.2到现在的5.3/5.4/5.5.起草这篇文章的时候发现官方已经停止了5.3的开发,最后的版本定格到了5.3.27,只会修复一些bug,宣告了5.3寿终正寝了.并且建议大家升级到5.4或者5.5, 鉴于版本更新得如此之快,决定写一片nginx连接php5.5的文章.于是乎,本草文写完了.

1. 安装PHP 5.5.0

  •  下载
cd /usr/local/src/
wget http://www.php.net/get/php-5.5.0.tar.bz2/from/jp1.php.net/mirror

# 如果以上PHP不存在了,大家可以直接到官方下载. 如果还是找不到可以留言,我将会通过邮箱发送.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

  • 安装依赖包

确保安装之前有安装gd,png,curl,xml等等lib开发库。如果不确定,执行以下命令:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y
  • 编译安装PHP 5.5.0

以下参数支持,ftp,图片函数,pdo等支持,因为使用了php自带的mysqlnd,所以不需要额外安装mysql的lib库了.如果你是64位系统,参数后面加上--with-libdir=lib64,如果不是可以跳过。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

tar -xjf php-5.5.0.tar.bz2
cd php-5.5.0
./configure --prefix=/usr/local/php-5.5.0 --with-config-file-path=/usr/local/php-5.5.0/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64
make
make install

备注:如果PHP不需要curl和ftp的支持,可以将以上的--with-curl --enable-ftp去掉. 如果你是专业的linux从业人员,你完全可以看着help来选择你的安装参数,如果你不是的话,我建议你直接复制黏贴我的配置参数.这样可以少走一些弯路.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

  • 配置php
cp php.ini-production /usr/local/php-5.5.0/etc/php.ini
cp /usr/local/php-5.5.0/etc/php-fpm.conf.default /usr/local/php-5.5.0/etc/php-fpm.conf
  • 启动php-fpm
/usr/local/php-5.5.0/sbin/php-fpm

执行以上命令,如果没报错一般情况下表示启动正常,如果不放心,也可以通过端口判断是PHP否启动文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

# netstat -lnt | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

2、安装配置nginx

  • 安装nginx

请看<ttlsa教程系列之nginx - nginx安装>文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

  •  配置测试站点test.ttlsa.com
mkdir /data/logs/nginx/ # 用于存放nginx日志.请看2.3的配置文件
mkdir -p /data/site/test.ttlsa.com/ # 站点根目录
vim /data/site/test.ttlsa.com/info.php
<?php
phpinfo();
?>


保存退出文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

  • nginx配置

在nginx.conf的http断中加上如下内容:文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

server {
listen 80;
server_name test.ttlsa.com;
access_log /data/logs/nginx/test.ttlsa.com.access.log main;

index index.php index.html index.html;
root /data/site/test.ttlsa.com;

location /
{
try_files $uri $uri/ /index.php?$args;
}

location ~ .*\.(php)?$
{
expires -1s;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;

}
}
  • 配置讲解

nginx将会连接回环地址9000端口执行PHP文件,需要使用tcp/ip协议,速度比较慢.建议大家换成使用socket方式连接。将fastcgi_pass 127.0.0.1:9000;改成fastcgi_pass unix:/var/run/phpfpm.sock;文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

  • 启动nginx
/usr/local/nginx-1.4.1/sbin/nginx

3. 访问测试

# curl http://test.ttlsa.com/info.php
test php

出现如上内容,说明PHP安装完成。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/

文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-php-5_5/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 15/07/2013 15:26:29
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-php-5_5/
  • nginx连接php
  • php 5.5
  • php 5.5安装
  • php5.5
评论  64  访客  59
    • 151
      151 0

      baocuo.好多错误

      • F君
        F君 0

        楼主大大,我tar -xjf php-5.5.0.tar.bz2的时候提示没有那个文件和目录,是不是因为没有php-5.5.0啊? :cry: :cry: 可以把这个发给我吗?谢谢楼主大大啦 :mrgreen:

          • 匿名
            匿名 9

            @ F君 自己下啊

          • 逗伴夫君
            逗伴夫君 1

            –with-jpeg-dir=/usr/local –with-png-dir=/usr/local,看到好多类似这样,存放路径为/usr/loacl/下面的。不是应该/usr/local/php/jpeg、这样形式的吗

            • 匿名
              匿名 0

              nginx启动报错,我是按照教程来的,提示的行没有这个双引号,求大神帮忙看下:
              [root@localhost conf]# /usr/local/nginx-1.5.1/sbin/nginx
              nginx: [emerg] unknown directive ” ” in /usr/local/nginx-1.5.1/conf/nginx.conf:89

              server {
              listen 80;
              server_name test.ttlsa.com;
              access_log /data/logs/nginx/test.ttlsa.com.access.log main;
              index index.php index.html index.html;
              root /data/site/test.ttlsa.com;
               
              location /
              {
              try_files $uri $uri/ /index.php?$args;
              }
               
              location ~ .*\.(php)?$
              {
              expires -1s;
              try_files $uri =404;
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              include fastcgi_params;
              fastcgi_param PATH_INFO $fastcgi_path_info;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              #fastcgi_pass 127.0.0.1:9000;
              fastcgi_pass unix:/var/run/phpfpm.sock;

              }
              }

              • 匿名
                匿名 9

                cp php.ini-production /usr/local/php-5.5.0/etc/php.ini
                cp /usr/local/php-5.5.0/etc/php-fpm.conf.default /usr/local/php-5.5.0/etc/php-fpm.conf
                实在抱歉,想知道这个是什么意思。

                • 匿名
                  匿名 9

                  :?大

                  • JMY
                    JMY 0

                    php-5.5.0.tar.bz2和nginx-1.5.1在官网都未找到

                      • 管理员
                        管理员 5

                        @ JMY 你可以更换其他版本

                      • 璐璐
                        璐璐 1

                        cp /usr/local/php-5.5.0/etc/php-fpm.conf.default /usr/local/php-5.5.0/etc/php-fpm.conf 出现问题,没有那个default文件?

                          • jungiewolf
                            jungiewolf 0

                            @ 璐璐 同样提示缺少文件【./configure –prefix=/usr/local/php-7.1.3 –with-config-file-path=/usr/local/php-7.1.3/etc –with-bz2 –with-curl –enable-ftp –enable-sockets –disable-ipv6 –with-gd –with-jpeg-dir=/usr/local –with-png-dir=/usr/local –with-freetype-dir=/usr/local –enable-gd-native-ttf –with-iconv-dir=/usr/local –enable-mbstring –enable-calendar –with-gettext –with-libxml-dir=/usr/local –with-zlib –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-mysql=mysqlnd –enable-dom –enable-xml –enable-fpm –with-libdir=lib64】后重构
                            再执行CP 就可以了

                              • 匿名
                                匿名 9

                                @ jungiewolf 您好,重构了还是没有default呀。

                                • 匿名
                                  匿名 9

                                  @ jungiewolf 您好,重构之后还是没有default呀。

                                    • abiuoo
                                      abiuoo 0

                                      @ 匿名 编译安装后直接用find / -name php-fpm.conf.default 试试

                                • zlf123
                                  zlf123 0

                                  搞不懂 你这个对不对 我做一只出错

                                  • jigger233
                                    jigger233 1

                                    /usr/local/nginx/sbin/nginx
                                    nginx: [emerg] unknown log format “main” in /usr/local/nginx/conf/nginx.conf:39

                                    这个怎么解决啊

                                      • jigger233
                                        jigger233 1

                                        @ jigger233 上面解决了,但是最后
                                        # curl http://test.ttlsa.com/info.php
                                        curl: (7) Failed to connect to ff03::c1: 网络不可达

                                          • solo
                                            solo 0

                                            @ jigger233 我也是这个问题,请问怎么解决的?

                                              • eason
                                                eason 1

                                                @ solo 你这样访问的 不是访问你配置的域名了 而是访问www.ttlsa.com了
                                                你要这样访问 curl localhost/info.php 或者curl -x127.0.0.1:80 http://www.ttlsa.com/info.php

                                              • eason
                                                eason 1

                                                @ jigger233 你这样访问的 不是访问你配置的域名了 而是访问www.ttlsa.com了
                                                你要这样访问 curl localhost/info.php 或者curl -x127.0.0.1:80 http://www.ttlsa.com/info.php

                                                • samba
                                                  samba 1

                                                  @ jigger233 怎么解决

                                                • 匿名
                                                  匿名 9

                                                  @ jigger233 请问这个怎么解决的啊

                                                • 编译配置php选项
                                                  编译配置php选项 9

                                                  ./configure –prefix=/usr/local/php-5.6.23 –with-config-file-path=/usr/local/php-5.6.23/etc –with-bz2 –with-curl –enable-ftp –enable-sockets –disable-ipv6 –with-gd –with-jpeg-dir=/usr/local –with-png-dir=/usr/local –with-freetype-dir=/usr/local –enable-gd-native-ttf –with-iconv-dir=/usr/local –enable-mbstring –enable-calendar –with-gettext –with-libxml-dir=/usr/local –with-zlib –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-mysql=mysqlnd –enable-dom –enable-xml –enable-fpm –with-libdir=lib64
                                                  复制好麻烦

                                                  • Garfield
                                                    Garfield 1

                                                    按照上面的源码编译,php版本是5.6.16,在Linux上执行“php -version”报错,“php command not found”,检查端口显示是启用的。这个是没配置好吗?

                                                    • 风衣
                                                      风衣 0

                                                      我在配置nginx的conf的时候出现问题,1提示access_log后面的main语法不正确,2你的index root是写在location外面的,而默认的conf规范里面有个案例注释的,它要求写在location里面,是怎么回事呢

                                                      • lmj
                                                        lmj 1

                                                        最后一步无响应 so 是nginx没有配好?

                                                        • Aceslup
                                                          Aceslup 9

                                                          有好多不熟悉的人呢。

                                                          • huan冰
                                                            huan冰 9

                                                            修改php.ini配置文件后,重启php-fpm和重启nginx,配置文件还是不生效怎么回事啊?求教

                                                            • vistor
                                                              vistor 9

                                                              请问,php是源码安装,MySQL是yum安装,是不是php不能连上MySQL啊?

                                                                • TTLSA
                                                                  TTLSA 9

                                                                  @ vistor 两者没有必然的联系,可以连上的

                                                                • phper
                                                                  phper 9

                                                                  为什么我的php.ini修改了重启php-fpm ,没作用?

                                                                    • 运维生存时间
                                                                      运维生存时间 7

                                                                      @ phper 请详细说明你的问题

                                                                      • phper
                                                                        phper 9

                                                                        @ phper 我按照你上面的方法安装php,访问phpinfo,配置文件路径是/usr/local/php/etc/,可是修改了配置文件的date.timezone,重启php-fpm,却没改变。还有就是,为什么配置文件的;extension=curl.dll前面的分号都没去掉,phpinfo里面却能显示curl的信息,是不是还要配置其他的信息?

                                                                          • TTLSA
                                                                            TTLSA 9

                                                                            @ phper extension=curl.dll这个是Windows的,请不要混淆了

                                                                            • vistor
                                                                              vistor 9

                                                                              @ phper php-fpm的重启方式除了kill -hup 还有别的方式吗

                                                                          • vikey
                                                                            vikey 9

                                                                            请问info.php 文件的内容应该怎么写???

                                                                              • 运维生存时间
                                                                                运维生存时间 7

                                                                                @ vikey <?php
                                                                                phpinfo();
                                                                                ?>

                                                                                  • vikey
                                                                                    vikey 9

                                                                                    @ 运维生存时间 之前info.php中什么内容都没添加,运行 curl http://test.ttlsa.com/info.php 输出为:
                                                                                    <html>
                                                                                    <head>
                                                                                    <meta http-equiv="refresh" content="0; url=http://m1.souluo.net" />
                                                                                    </head
                                                                                    <body>
                                                                                    </body>
                                                                                    </html>
                                                                                    info.php加上
                                                                                    <?php
                                                                                    phpinfo();
                                                                                    ?>
                                                                                    之后
                                                                                    [root@master html]# curl http://test.ttlsa.com/info.php
                                                                                    curl: (6) Couldn’t resolve host ‘test.ttlsa.com’
                                                                                    请问这是nginx没有配置正确么?还是其他原因?

                                                                                      • 默北
                                                                                        默北 6

                                                                                        @ vikey 你为什么要访问http://test.ttlsa.com/info.php ?

                                                                                          • vikey
                                                                                            vikey 9

                                                                                            @ 默北 本章中的第三步,访问测试呀

                                                                                            • 运维生存时间
                                                                                              运维生存时间 7

                                                                                              @ vikey 这个域名要改成你自己配置的域名,如果你也是用tets.ttlsa.com,那么请在hosts里面绑定。

                                                                                              • vikey
                                                                                                vikey 9

                                                                                                @ 运维生存时间 嗯嗯,谢谢

                                                                                        • nginx-help
                                                                                          nginx-help 9

                                                                                          请教个问题:在上面看见 你用# netstat -lnt | grep 9000 来查看php是不是已经启动, 之前我用过LAMP的架构,但是在LAMP下通过 netstat -lnt 是不能看见php是不是已经启动(或者说我根本没有办法去判断php是不是启动,除了写一个<?php phpinfo(); ?>,望指点),请问1.这个端口号9000,为什么能在nginx下能看见,而在LAMP的环境下看不见,对nginx是特殊对待吗 2.在LNMP和LAMP下我们怎么判断php是不已经启动 3.为什么在LAMP架构下就没有php-fpm启动这个步骤,难道是自启动 问题有点多,希望能给与指导,谢谢

                                                                                          • nginx-help
                                                                                            nginx-help 9

                                                                                            请教个问题:在上面看见 你用# netstat -lnt | grep 9000 来查看php是不是已经启动, 之前我用过LAMP的架构,但是在LAMP下通过 netstat -lnt 是不能看见php是不是已经启动(或者说我根本没有办法去判断php是不是启动,除了写一个<?php phpinfo(); ?>,望指点),请问1.这个端口号9000,为什么能在nginx下能看见,而在LAMP的环境下看不见,对nginx是特殊对待吗 2.在LNMP和LAMP下我们怎么判断php是不已经启动 问题有点多,希望能给与指导,谢谢

                                                                                              • 运维生存时间
                                                                                                运维生存时间 7

                                                                                                @ nginx-help LAMP环境下php是Apache的一模块,所以php没有独立的端口。在lnmp环境下php是独立的,也就是独立运行的php-fpm.希望我的回答,你能理解!

                                                                                                  • nginx-help
                                                                                                    nginx-help 9

                                                                                                    @ 运维生存时间 那能不能这么理解:在LAMP架构下apache和php必须在同一台服务器上,而在LNMP架构下nginx和php可以安装在同一台服务器上,也可以安装在不通服务器上,希望能给与指导,谢谢

                                                                                                • Clouds
                                                                                                  Clouds 9

                                                                                                  用浏览器访问info.php 直接变成下载了 ,这是什么情况? comment” />

                                                                                                    • TSA
                                                                                                      TSA 9

                                                                                                      @ Clouds 说明没有解析到php,看看nginx配置是否正确

                                                                                                    • " 梦 想 粺
                                                                                                      " 梦 想 粺 9

                                                                                                      这里我补充一下验证PHP 只需要在默认文档中添加index.php 可去除php那块的注释即可。
                                                                                                      注意更改fastcgi的目录~

                                                                                                      location / {
                                                                                                      root html;
                                                                                                      index index.php index.html index.htm;
                                                                                                      }

                                                                                                      location ~ .php$ {
                                                                                                      root html;
                                                                                                      fastcgi_pass 127.0.0.1:9000;
                                                                                                      fastcgi_index index.php;
                                                                                                      fastcgi_param SCRIPT_FILENAME /usr/local/nginx-1.6.0/html$fastcgi_script_name;
                                                                                                      include fastcgi_params;
                                                                                                      }

                                                                                                      • 博主
                                                                                                        博主 9

                                                                                                        php-fpm已经整合到了php5.5.12里面,只是不需要再去打php-cgi的补丁,但是在使用的时候还是需要启动php-fpm的。

                                                                                                        • eason
                                                                                                          eason 9

                                                                                                          现在已经是php 5.5.12请问是不是php-fpm已经集合在里面不用再去重启php-fpm服务了?

                                                                                                            • 博主
                                                                                                              博主 9

                                                                                                              @ eason php-fpm已经整合到了php5.5.12里面,只是不需要再去打php-cgi的补丁,但是在使用的时候还是需要启动php-fpm的。

                                                                                                          评论已关闭!