LNMP环境搭建

        nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx;


版本

php-5.5.19、nginx/1.6.2

一、编译安装php-fpm


PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里


安装前准备:

yum -y install gcc automake autoconf libtool make

yum -y install gcc gcc-c++ glibc

yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-devel

新版php-fpm安装(推荐安装方式)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir

make all install

配置php-fpm:

# cd /usr/local/php
# cp etc/php-fpm.conf.default etc/php-fpm.conf
# vim etc/php-fpm.conf
修改为:

user = nginx
group = nginx

二、安装nginx
参考:http://nginx.org/en/linux_packages.html

三、修改nginx配置文件以支持php-fpm

nginx安装完成后,修改nginx配置文件为,nginx.conf

    server{
	  listen 80;
	  server_name localhost;
	  index index.html index.htm index.php;
	  root /usr/share/nginx/html;
	  
	  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	  location ~ .*\.(php|php5)?$ {
	   fastcgi_pass 127.0.0.1:9000;
	   fastcgi_index index.php;
	   include fastcgi.conf;
	  }

	  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
	   expires 30d;
	  }
	  location ~ .*\.(js|css)?$ {
	   expires 1h;
	  }

	  access_log off;
    }

在nginx配置文件夹下新增fastcgi.conf文件

fastcgi_param  GATEWAY_INTERFACE  CGI/1.3.3;
fastcgi_param  SERVER_SOFTWARE    b43boo;

#防止webshell跨站
fastcgi_param  PHP_ADMIN_VALUE    open_basedir=$document_root:/tmp/:/proc/;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

四、创建index.php测试文件

<?php
    phpinfo();
?>

五、 启动php-fpm和nginx服务

# /usr/local/php/sbin/php-fpm 
# systemctl start nginx.service


最后访问应用

WAMP环境:http://blog.csdn.net/hz_blog/article/details/41958287


更多:http://www.nginx.cn/231.html


版权声明:本文为hanzhou4519原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。