nginx怎样配置php环境,安装nginx并配置php环境

环境:

Linux  ubuntu 3.2.0-23-generic-pae

Ubuntu下安装nginx+php+FastCGI

1. 安装nginx

使用命令安装:

#sudo  apt-get  install nginx

Nginx的启动和停止:

/etc/init.d/nginx  start/stop/restart

Service  nginx  start/stop

2. 修改nginx的配置

#vi  /etc/nginx/sites-availavle/default

内容修改如下:

server {

listen 80;

server_name  localhost;

root  /var/www;

index  index.php index.html index.htm;

access_log /var/log/nginx/localhost.access.log;

location / {

root  /var/www;

index  index.php index.html index.htm;

}

location /doc {

root /usr/share;

autoindex  on;

allow  127.0.0.1;

deny  all;

}

#location /images {

#root  /usr/share;

#autoindex  on;

#}

#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  /var/www;

}

# 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$ {

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME /var/www$fastcgi_script_name;

include  /etc/nginx/fastcgi_params;

}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

location ~ /\.ht {

deny all;

}

}

配置完成,重新启动nginx

测试:

在/var/www下编辑index.html文件:

Vi  index.html

加入如下内容:

Hello world!!!

保存,关闭

3. 安装php环境

PHP5通过FastCGI在nginx下运行。ubuntu提供一个FastCGI-enabled PHP5 安装包,可以这样安装。

apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

打开 /etc/php5/cgi/php.ini 配置文件,修改cgi.fix_pathinfo=1

4. 安装FastCGI

Ubuntu没有独立的FastCGI安装包,所以用lighttpd里面的spawn-fcgi,运行下面命令:

#apt-get install lighttpd

安装完成时会出现lighttpd无法启动的错误,因为nginx占用了80端口。运行

#update-rc.d -f lighttpd remove

使lighttpd开机不启动。

我们安装lighttpd只需要其中的/usr/bin/spawn-fcgi,来运行FastCGI进程。运行

spawn-fcgi  --help 查看它的命令。

以用户www-data在本机localhost的9000端口下运行一个PHP FastCGI进程,输入以下命 令:

#/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P  /var/run/fastcgi-php.pid

加入开机运行,以免每次开机运行此命令:

#vi  /etc/rc.local

在最后一行加入下面语句(在exit前面):

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P  /var/run/fastcgi-php.pid

5. 测试

在/var/www中添加index.php文件

Vi  index.php

加入如下内容:

phpinfo();

?>

在访问php文件前,确保spawn-fcgi已经运行。

6.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值