在Linux系统下,现在越来越多的LNMP(Linux + Nginx +MySQL +PHP)代替LAMP(Linux + Apache+MySQL +PHP)。主要是Nginx处理并发会更优越,也比较轻量级。记录了Linux(centos)lnmp环境搭建,lnmp编译安装nginx+php步骤。
1、安装
1 2 3 4 5 6 7 8 9
| [root@Linux ~]# yum -y install gcc gcc-c++ autoconf automake openssl openssl-devel pcre-devel zlib-devel #安装编译软件库 [root@Linux ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz #下载nginx 1.6.2源码包 [root@Linux ~]# tar zxvf nginx-1.6.2.tar.gz [root@Linux ~]# cd nginx-1.6.2 [root@Linux nginx-1.6.2]# ./configure --with-http_stub_status_mocule --prefix=/opt/nginx #编译设置模块 [root@Linux nginx-1.6.2]# make && make install安装 [root@Linux ~]# /opt/nginx/sbin/nginx -s reload #nginx重新启动 [root@Linux ~]# /opt/nginx/sbin/nginx #nginx启动 [root@Linux ~]# /opt/nginx/sbin/nginx -t #nginx检测配置 |
2、的安装和配置
1 2 3 4 5 6 7 8 9
| [root@Linux ~]# yum install -y gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel mysql mysql-devel #安装php依赖软件库 [root@Linux ~]# wget http://am1.php.net/distributions/php-5.6.3.tar.gz #下载php5.6源码包 [root@Linux ~]# tar -zxvf php-5.6.3.tar.gz [root@Linux ~]# cd php-5.6.3 [root@Linux php-5.6.3]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --enable-fpm --with-pear --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv --with-mhash --with-zlib --enable-mbstring --disable-debug #编译设置模块 [root@Linux php-5.6.3]# make && make install #编译安装 [root@Linux php-5.6.3]# cp php.ini-production /opt/php/lib/php.ini #复制php配置文件 [root@Linux php-5.6.3]# /opt/php/sbin/php-fpm #启动php-fpm [root@Linux php-5.6.3]# /opt/php/sbin/php-fpm -t #检测配置是否正确 |
在centos上成功编译安装nginx 1.6、php 5.6并成功启动nginx和php-fpm后,访问php提示”File not found.”,同时在错误日志中看到:
2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 192.168.168.1, server: localhost, request: “GET / HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “192.168.168.133”:
在Nginx配置文件中找到定义调用脚本文件的地方,如:
1 2 3
| fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改成如下方式($document_root): fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
nginx gzip设置
1 2 3 4 5 6 7
| gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css applocation/xml; gzip_vary on; |