- A+
这两年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
- 下载
1 2 |
cd /usr/local/src/ wget http://www.php.net/get/php-5.5.0.tar.bz2/from/jp1.php.net/mirror |
# 如果以上PHP不存在了,大家可以直接到官方下载. 如果还是找不到可以留言,我将会通过邮箱发送.
- 安装依赖包
确保安装之前有安装gd,png,curl,xml等等lib开发库。如果不确定,执行以下命令:
1 |
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,如果不是可以跳过。
1 2 3 4 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来选择你的安装参数,如果你不是的话,我建议你直接复制黏贴我的配置参数.这样可以少走一些弯路.
- 配置php
1 2 |
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
1 |
/usr/local/php-5.5.0/sbin/php-fpm |
执行以上命令,如果没报错一般情况下表示启动正常,如果不放心,也可以通过端口判断是PHP否启动
1 2 |
# netstat -lnt | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN |
2、安装配置nginx
- 安装nginx
请看<ttlsa教程系列之nginx - nginx安装>
- 配置测试站点test.ttlsa.com
1 2 3 4 5 6 |
mkdir /data/logs/nginx/ # 用于存放nginx日志.请看2.3的配置文件 mkdir -p /data/site/test.ttlsa.com/ # 站点根目录 vim /data/site/test.ttlsa.com/info.php <?php phpinfo(); ?> |
保存退出
- nginx配置
在nginx.conf的http断中加上如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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;
- 启动nginx
1 |
/usr/local/nginx-1.4.1/sbin/nginx |
3. 访问测试
1 2 |
# curl http://test.ttlsa.com/info.php test php |
出现如上内容,说明PHP安装完成。

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