1、首先安装nginx
sudo apt-get install nginx
2、安装php和php5-fpm模块
sudo apt-get install nginx php5-fpm
启动nginx,并访问127.0.0.1:
/etc/init.d/nginx start
如下图:
3、编辑配置文件
sudo vim /etc/nginx/site-available/default
找到并编辑相应位置,如下:
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
nginx默认的网页目录在:/usr/share/nginx/html ;可以在/etc/nginx/site-available/default配置文件里查看到
创建一个index.php,键入如下代码:
<?php
echo "hello world !";
?>
访问该页面:127.0.0.1/index.php ,如果出现一下错误(这是我安装完nginx后启动出现的错误):
查看log文件(/var/log/nginx/error.log),如果类似报如下错误:
unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.2.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:"
则重新启动php-fpm模块即可:
/usr/sbin/php5-fpm重启nginx:
/etc/init.d/nginx restart再次访问:127.0.0.1/index.php,则正常访问:
到此已经完成nginx对php的支持
参考地址:
http://blog.csdn.net/hursing/article/details/21527159
http://www.cnblogs.com/zdz8207/p/3765579.html