Linux 使用Nginx搭建PHP7服务环境 (过程中问题总结)

 

很早时间就已经转nginx并虚拟机搭建使用过,此处新建Linux服务器(centOs6 购于aliyun此处有专属优惠码 rrs9h6 有效期至2017-05-31,支付页面输入打折哦)再次操作并记录一下~~

 

PHP官网下载地址,下载自己使用的版本

http://cn2.php.net/downloads.php 

Nginx 此处使用OpenResty版本(扩展Nginx和Lua更方便使用)

http://openresty.org/cn/download.html

==================================================================

tar -xvf openresty-1.9.15.1.tar.gz

tar -xvf php-7.0.8.tar.gz

进入目录,执行配置(配置时有需要指定路径的自己添加即可)

./configure --prefix=/usr/local/nginx 

make && make install 

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc/ --enable-fpm

同样 make && make install

-----------------------------------------------------------------------------------------------

假设没有问题(遇到的问题集 见后文罗列并说明解决方案),则依次验证服务即可:

cp nginx.conf之后启动服务:

/usr/local/nginx/nginx/sbin/nginx -c /usr/local/nginx/nginx/conf/nginx.conf

并访问(默认端口80),显示nginx欢迎页面即 nginx启动成功!

 

主要在结合配置PHP:

使用默认模板

cd /usr/local/php/etc/php-fpm.d/

cp www.conf.default www.conf

cd /usr/local/php/etc/

cp php-fpm.conf.default php-fpm.conf

启用php服务

/usr/local/php/sbin/php-fpm -c start 

在nginx下的html站点目录下新建测试php文件

vim /wwwroot/htdoc/index.php

输入: <? phpinfo() ?>

chmod -R 755 /wwwroot  #赋予站点目录及以下文件权限为读取和执行

vim /usr/local/nginx/conf/nginx.conf  #更改nginx配置文件使其可以访问php文件

  server

  {

      listen 80;

      server_name www.test.com test.com;

      root   /wwwroot/htdoc; 

 

      location /{

        index index.html index.htm index.html index.php default.html Default.html;

      }

      location ~ \.php${

         fastcgi_pass   127.0.0.1:9000;

         fastcgi_index  index.php;

         fastcgi_param  SCRIPT_FILENAME /$document_root$fastcgi_script_name;    

         include      fastcgi_params;

      }

   }

/usr/local/nginx/nginx/sbin/nginx -s reload  #重载nginx配置就可以访问测试

页面展示: PHP Version 7.0.8 等即成功!

 

-----------------------------------------------------------------------------------------------

问题一:

安装nginx时候就会出现 PCRE OpenSSL zlib 库的依赖找不到 NOT FOUND

不要紧张,这都是nginx模块需要使用到的,依次安装上即可

yum -y install pcre openssl zlib

然后使用 rpm -qa 查看是否都安装成功,其中一个注意点:

------------------------------ nginx编译openssl要注意啦! --------------------------------

使用安装好的openssl目录

./configure --prefix=/usr/local/nginx --with-openssl=/usr/local/ssl(此处为已经安装好的目录)

配置没问题,但是make就会会报错如下:

。。。。。。

make[1]: Leaving directory `/home/zhang/nginx/build/lua-redis-parser-0.12'

cd /home/zhang/nginx/build/lua-rds-parser-0.06 && make DESTDIR= LUA_INCLUDE_DIR=/home/zhang/nginx/build/luajit-root/usr/local/nginx/luajit/include/luajit-2.1 LUA_LIB_DIR=/usr/local/nginx/lualib CC=cc

make[1]: Entering directory `/home/zhang/nginx/build/lua-rds-parser-0.06'

make[1]: Nothing to be done for `all'.

make[1]: Leaving directory `/home/zhang/nginx/build/lua-rds-parser-0.06'

cd /home/zhang/nginx/build/nginx-1.9.15 && make

make[1]: Entering directory `/home/zhang/nginx/build/nginx-1.9.15'

make -f objs/Makefile

make[2]: Entering directory `/home/zhang/nginx/build/nginx-1.9.15'

cd /usr/local/ssl \

&& if [ -f Makefile ]; then make clean; fi \

&& ./config --prefix=/usr/local/ssl/.openssl no-shared  \

&& make \

&& make install_sw LIBDIR=lib

/bin/sh: line 2: ./config: No such file or directory

make[2]: *** [/usr/local/ssl/.openssl/include/openssl/ssl.h] Error 127

make[2]: Leaving directory `/home/zhang/nginx/build/nginx-1.9.15'

make[1]: *** [build] Error 2

make[1]: Leaving directory `/home/zhang/nginx/build/nginx-1.9.15'

make: *** [all] Error 2

 

对于&& ./config --prefix=/usr/local/ssl/.openssl no-shared  \

 

解决方案:使用源码openssl目录

./configure --prefix=/usr/local/nginx --with-openssl=/home/zhang/openssl

配置好如下:

。。。。。。。

adding module in ../rds-csv-nginx-module-0.07

 + ngx_http_rds_csv_filter_module was configured

checking for PCRE library ... found

checking for PCRE JIT support ... found

checking for zlib library ... found

creating objs/Makefile

 

Configuration summary (如下即配置成功)

  + using system PCRE library

  + using OpenSSL library: /home/zhang/openssl

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library

 

  nginx path prefix: "/usr/local/nginx/nginx"

  nginx binary file: "/usr/local/nginx/nginx/sbin/nginx"

  nginx modules path: "/usr/local/nginx/nginx/modules"

  nginx configuration prefix: "/usr/local/nginx/nginx/conf"

  nginx configuration file: "/usr/local/nginx/nginx/conf/nginx.conf"

  nginx pid file: "/usr/local/nginx/nginx/logs/nginx.pid"

  nginx error log file: "/usr/local/nginx/nginx/logs/error.log"

  nginx http access log file: "/usr/local/nginx/nginx/logs/access.log"

  nginx http client request body temporary files: "client_body_temp"

  nginx http proxy temporary files: "proxy_temp"

  nginx http fastcgi temporary files: "fastcgi_temp"

  nginx http uwsgi temporary files: "uwsgi_temp"

  nginx http scgi temporary files: "scgi_temp"

 

问题二:

编译php-7.0.2出错,xml2-config not found

cd php7

./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --enable-fpm

返回错误:

checking for strftime... (cached) yes

checking whether to enable LIBXML support... yes

checking libxml2 install dir... no

checking for xml2-config path... 

configure: error: xml2-config not found. Please check your libxml2 installation.

 

yum -y install libxml2

之后依然如上错误,libxml2没装成功,但是yum安装检验是成功的

rpm -qa libxml2  ==》 libxml2-2.9.1-6.el7_2.3.x86_64

查资料后发现原来还需要安装libxml2-devel,再次执行安装命令,

 

解决方案:  yum -y install libxml2-devel  

再重新编译php,可以编译成功。

 

问题三:

装php7,make报错:...is not a valid libtool object

 

libtool: link: `ext/opcache/ZendAccelerator.lo' is not a valid libtool object

make: *** [ext/opcache/opcache.la] Error 1

[1]+  Exit 2                  make

查看libtool(由ltmain.sh生成)该错误是由于:

if (${SED} -e ‘2q’ $arg | grep “^# Generated by .*$PACKAGE”) >/dev/null 2>&1; then 语句不成立而后续引起的。

正确的lo文件应该如下:

# ext/date/php_date.lo – a libtool object file

# Generated by ltmain.sh – GNU libtool 1.5.26 (1.1220.2.492 2008/01/30 06:40:56)

#

# Please DO NOT delete this file!

# It is necessary for linking the library.

# Name of the PIC object.

pic_object=none

# Name of the non-PIC object.

non_pic_object=’php_date.o’

初始的5行,是有libtool文件中compile时添加的。

在用安装脚本运行configure和make时,经常发现未添加成功,这就导致了后面的错误。而手动

解决办法:make clean 后再 make

# make clean

find . -name \*.gcno -o -name \*.gcda | xargs rm -f

find . -name \*.lo -o -name \*.o | xargs rm -f

find . -name \*.la -o -name \*.a | xargs rm -f 

find . -name \*.so | xargs rm -f

find . -name .libs -a -type d|xargs rm -rf

rm -f libphp7.la sapi/cli/php sapi/cgi/php-cgi   sapi/fpm/php-fpm  modules/* libs/*

make 即显示

Build complete.

Don't forget to run 'make test'.

 

问题四:

Nginx请求index.php返回:File not found 并在日志显示FastCGI错误Primary script unknown

 

在成功编译安装nginx php 并成功启动nginx和php-fpm后,访问php提示 "File not found." ,同时在错误log中看到:

2016/06/24 16:51:04 [error] 25191#0: *97 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, 

 

解决方法:

在Nginx配置文件中找到定义调用脚本文件的地方:

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

修改成如下方式($document_root)此处 $document_root 代表当前请求在root指令中指定的值:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

配置中的$document_root就是针对/wwwroot/htdoc(自己建的站点根目录)下的php文件进行解析。

 

系统环境各不相同,自然问题还有很多不同的,操作中随手解决了也就忽略记录,问题嘛总能解决滴;

还是那句话:内事不决问百度,外事难决找google ~~

 

随记

 

【转载请标明出处 -- 转自】

http://zl378837964.iteye.com/blog/2307174

 

猜你喜欢

转载自zl378837964.iteye.com/blog/2307174