让php来搞定一切!,ubuntu安装和配置php5

安装php5

在linux下安装PHP简直太容易了,一行命令搞定一切:

sudo apt-get -y install php5-common php5-cli php5-fpm

之后我们可以运行一下命令检查一下是否已经安装成功了:

php -v

43337b82590927328f6135c1a8d1c0b6.png

安装nginx

如果你还米有安装nginx的话,请参考我的另外一篇博文在ubuntu下安装nginx

配置php5

我们需要修改一下fpm的配置文件:

sudo gedit /etc/php5/fpm/php.ini

找到cgi.fix_pathinfo=1这一行,然后把1改成0:

cgi.fix_pathinfo=0

cgi.fix_pathinfo=0 表示关闭 PHP 的自动 PATH_INFO 检测。cgi.fix_pathinfo=1的时候,举个例子,当nginx传给 PHP 的值为 /var/www/lrenwang/test.png/xxx.php的时候,$_SERVER 中 SCRIPT_FILENAME 却是 /var/www/test/test.png。

因为/var/www/lrenwang/test.png/xxx.php 并不存在,/var/www/lrenwang/test.png 被 PHP 解析为 SCRIPT_FILENAME,/xxx.php 被 PHP 解析为 PATH_INFO 后被丢弃,因此并没有在 $_SERVER 中出现。当cgi.fix_pathinfo设置为0的时候则不会有此问题。

另外,我们需要检查一下php5-fpm的配置,可能还需要一点小修改。打卡www.conf这个文件:

sudo gedit /etc/php5/fpm/pool.d/www.conf

找到listen = 127.0.0.1:9000, 把127.0.0.1:9000改成/var/run/php5-fpm.sock。

listen = /var/run/php5-fpm.sock

之后重启:

sudo service php5-fpm restart

NOTE:我这边安装完php-fpm后,这个listen地址就是正确的了。

另外,这里稍微提一下FastCGI的运作原理。Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程,如图所示:

a73dd84b4b0e041e933a72536e9fda37.png

配置nginx

打开默认站点配置文件:

sudo gedit /etc/nginx/sites-available/default

修改如下所示,你可以参开:

server {

listen 80;

root /usr/share/nginx/www;

index index.php index.html index.htm;

server_name example.com;

location / {

try_files $uri $uri/ /index.html;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/www;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {

#fastcgi_pass 127.0.0.1:9000;

# With php5-fpm:

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

修改如下:

在index这一行添加index.php

修改server_name为你的域名或者IP

修改 “location ~ .php$ {“ 配置节

测试php页面

要测试nginx是否配置成功,我们可以新建一个PHP页面:

sudo touch /usr/share/nginx/www/info.php

sudo gedit /usr/share/nginx/www/info.php

输入以下PHP源代码:

phpinfo();

?>

重启nginx:

sudo service nginx restart

然后访问http://youripaddress/info.php这个页面查看结果

7ce75f978eb23297309d8f25e8e1aaab.png

PS:其实后来我又在centos上安装了nginx和php,过程其实大同小异。LINUX果然是一法通万法通。

yum install php-common php-cli php-fpm

wget http://nginx.org/keys/nginx_signing.key

rpm --import nginx_signing.key

yum update

yum install nginx

之后的php配置其实和上文是一样的,只不过在centos下不会写php5-fpm.sock,而是写php-fpm.sock,即前缀是php而不是php5。

PS:这边其实还要注意一个问题,就是防火墙的问题。默认情况下,centos的防火墙并没有开放80端口,因此我们需要手工开放端口。

开放80端口:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

保存防火墙配置:

/etc/rc.d/init.d/iptables save

这样重启计算机后,CentOS防火墙默认已经开放了80端口。不过我们也可以在不重启系统的情况下重新加载防火墙配置:

/etc/init.d/iptables restart

我们可以通过以下命令查看CentOS防火墙信息:

/etc/init.d/iptables status

参考文档

项目中要用到zend Optimizer,但是目前只支持php5.2,Ubuntu默认安装php版本为5.3,找了好多 方法,终于借助下面这篇文章的方法成功的安装5.2及5.3两个版本的php Although Drupals 7+ run smoothly on PHP 5.3, Drupal 6 still feels much better with PHP 5.2. Even though D6 core is compatible with PHP 5.3 for quite some time now, a lot of contributes modules still get nasty hiccup when asked to run on the newer version. Therefore developing for both D7 and D6 at the same time becomes much less painful when running both versions of PHP in parallel. One way of doing it is using mod_php5 Apache module to serve PHP 5.3 applications, while running PHP 5.2 applications using fastcgi module. Under Ubuntu 12.04 this can be achieved by installing PHP 5.3 from the repositories and manually compiling and installing PHP 5.2 afterwards. Installing PHP 5.3 from repositories is fairly easy process, which you most probably already have under your belt, so let's just say that it looks more or less like this: sudo apt-get install php5 php5-common php5-cli php5-dev php5-mysql phpmyadmin php5-pgsql phppgadmin php5-gd php5-mcrypt php5-curl php-pear libapache2-mod-php5 php5-xdebug php5-codesniffer What is much more interesting though, and what this post will focus on, is how to add PHP 5.2 to the whole picture and make both those versions work nicely together. Please note that this tutorial is for Apache's name- based virtual hosts, and essentially leaves PHP 5.3 enabled globally while allowing to use PHP 5.2 on specific, selected virtual hosts only.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值