这两年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/
25F
baocuo.好多错误
24F
楼主大大,我tar -xjf php-5.5.0.tar.bz2的时候提示没有那个文件和目录,是不是因为没有php-5.5.0啊? :cry: :cry: 可以把这个发给我吗?谢谢楼主大大啦 :mrgreen:
B1
@ F君 自己下啊
23F
–with-jpeg-dir=/usr/local –with-png-dir=/usr/local,看到好多类似这样,存放路径为/usr/loacl/下面的。不是应该/usr/local/php/jpeg、这样形式的吗
B1
@ 逗伴夫君 没多大区别
22F
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;
}
}
21F
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
实在抱歉,想知道这个是什么意思。
20F
:?大
19F
php-5.5.0.tar.bz2和nginx-1.5.1在官网都未找到
B1
@ JMY 你可以更换其他版本
18F
cp /usr/local/php-5.5.0/etc/php-fpm.conf.default /usr/local/php-5.5.0/etc/php-fpm.conf 出现问题,没有那个default文件?
B1
@ 璐璐 同样提示缺少文件【./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 就可以了
B2
@ jungiewolf 您好,重构了还是没有default呀。
B2
@ jungiewolf 您好,重构之后还是没有default呀。
B3
@ 匿名 编译安装后直接用find / -name php-fpm.conf.default 试试
17F
搞不懂 你这个对不对 我做一只出错
16F
/usr/local/nginx/sbin/nginx
nginx: [emerg] unknown log format “main” in /usr/local/nginx/conf/nginx.conf:39
这个怎么解决啊
B1
@ jigger233 上面解决了,但是最后
# curl http://test.ttlsa.com/info.php
curl: (7) Failed to connect to ff03::c1: 网络不可达
B2
@ jigger233 我也是这个问题,请问怎么解决的?
B3
@ solo 你这样访问的 不是访问你配置的域名了 而是访问www.ttlsa.com了
你要这样访问 curl localhost/info.php 或者curl -x127.0.0.1:80 http://www.ttlsa.com/info.php
B2
@ jigger233 你这样访问的 不是访问你配置的域名了 而是访问www.ttlsa.com了
你要这样访问 curl localhost/info.php 或者curl -x127.0.0.1:80 http://www.ttlsa.com/info.php
B2
@ jigger233 怎么解决
B1
@ jigger233 请问这个怎么解决的啊
15F
./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
复制好麻烦
14F
按照上面的源码编译,php版本是5.6.16,在Linux上执行“php -version”报错,“php command not found”,检查端口显示是启用的。这个是没配置好吗?
B1
@ Garfield -v
13F
我在配置nginx的conf的时候出现问题,1提示access_log后面的main语法不正确,2你的index root是写在location外面的,而默认的conf规范里面有个案例注释的,它要求写在location里面,是怎么回事呢
12F
最后一步无响应 so 是nginx没有配好?
11F
有好多不熟悉的人呢。
10F
修改php.ini配置文件后,重启php-fpm和重启nginx,配置文件还是不生效怎么回事啊?求教
B1
@ huan冰 修改什么内容了
9F
请问,php是源码安装,MySQL是yum安装,是不是php不能连上MySQL啊?
B1
@ vistor 两者没有必然的联系,可以连上的
8F
为什么我的php.ini修改了重启php-fpm ,没作用?
B1
@ phper 请详细说明你的问题
B1
@ phper 我按照你上面的方法安装php,访问phpinfo,配置文件路径是/usr/local/php/etc/,可是修改了配置文件的date.timezone,重启php-fpm,却没改变。还有就是,为什么配置文件的;extension=curl.dll前面的分号都没去掉,phpinfo里面却能显示curl的信息,是不是还要配置其他的信息?
B2
@ phper extension=curl.dll这个是Windows的,请不要混淆了
B2
@ phper php-fpm的重启方式除了kill -hup 还有别的方式吗
B3
@ vistor 有php-fpm service脚本
B3
@ vistor php-fpm有service脚本
7F
请问info.php 文件的内容应该怎么写???
B1
@ vikey <?php
phpinfo();
?>
B2
@ 运维生存时间 之前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没有配置正确么?还是其他原因?
B3
@ vikey 你为什么要访问http://test.ttlsa.com/info.php ?
B4
@ 默北 本章中的第三步,访问测试呀
B4
@ vikey 这个域名要改成你自己配置的域名,如果你也是用tets.ttlsa.com,那么请在hosts里面绑定。
B4
@ 运维生存时间 嗯嗯,谢谢
6F
请教个问题:在上面看见 你用# 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启动这个步骤,难道是自启动 问题有点多,希望能给与指导,谢谢
B1
@ nginx-help LAMP不需要判断php是否启动,LNMP判断9000端口,实际上最简单的就是看php文件能不能访问
B2
@ 运维生存时间 right~
5F
请教个问题:在上面看见 你用# netstat -lnt | grep 9000 来查看php是不是已经启动, 之前我用过LAMP的架构,但是在LAMP下通过 netstat -lnt 是不能看见php是不是已经启动(或者说我根本没有办法去判断php是不是启动,除了写一个<?php phpinfo(); ?>,望指点),请问1.这个端口号9000,为什么能在nginx下能看见,而在LAMP的环境下看不见,对nginx是特殊对待吗 2.在LNMP和LAMP下我们怎么判断php是不已经启动 问题有点多,希望能给与指导,谢谢
B1
@ nginx-help LAMP环境下php是Apache的一模块,所以php没有独立的端口。在lnmp环境下php是独立的,也就是独立运行的php-fpm.希望我的回答,你能理解!
B2
@ 运维生存时间 那能不能这么理解:在LAMP架构下apache和php必须在同一台服务器上,而在LNMP架构下nginx和php可以安装在同一台服务器上,也可以安装在不通服务器上,希望能给与指导,谢谢
4F
用浏览器访问info.php 直接变成下载了 ,这是什么情况?
” />
B1
@ Clouds 说明没有解析到php,看看nginx配置是否正确
3F
这里我补充一下验证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;
}
2F
php-fpm已经整合到了php5.5.12里面,只是不需要再去打php-cgi的补丁,但是在使用的时候还是需要启动php-fpm的。
1F
现在已经是php 5.5.12请问是不是php-fpm已经集合在里面不用再去重启php-fpm服务了?
B1
@ eason php-fpm已经整合到了php5.5.12里面,只是不需要再去打php-cgi的补丁,但是在使用的时候还是需要启动php-fpm的。
来自外部的引用