天天看点

nginx与php编译安装

#解决依赖

[[email protected] ~]# yum install gcc gcc-c++ autoconf automake -y 
 [[email protected] ~]# yum install zlib zlib-devel openssl openssl-devel -y
     Zlib:Nginx提供gzip模块,需要zlib的支持
     Openssl:Nginx提供SSL的功能
 [[email protected] ~]# tar xvf pcre-8.37.tar.bz2 -C /usr/local/src/  //解压此安装包即可,不需要安装,Nginx 需要指定 pcre 的源码不是安装后的路径(类似于 httpd 的安装 apr 时候的方式),此包的功能是支持地址重写 rewrite 功能  pcre的依赖可以yum安装pcre和pcre-devel解决! 
#解压编译安装 
 [[email protected] ~]# tar xvf nginx-1.8.0.tar.gz -C /usr/local/src/ ; cd /usr/local/src/nginx-1.8.0 
 [[email protected] nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --withhttp_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --withhttp_mp4_module --with-pcre=/usr/local/src/pcre-8.37 
 ************************
  --with-http_dav_module    #启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)                                     默认关闭,需要编译开启
   --with-http_stub_status_module  #启用支持(获取Nginx上次启动以来的工作状态)
   --with-http_addition_module   #启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
   --with-http_sub_module     #启用支持(允许一些其他文本替换Nginx相应中的一些文本)
   --with-http_flv_module     #启用支持(提供支持flv视频文件支持) 
   --with-http_mp4_module     #启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持) 
   --with-pcre=/usr/local/src/pcre-8.37   #需要注意,这里指的是源码,用#./configure 
   --help |grep pcre查看帮助  
   
 [[email protected]er63 nginx-1.8.0]# make ; make install ; cd 
 [[email protected] ~]# useradd -M -u 8001 -s /sbin/nologin nginx   //用于运行Nginx的用户 
           

#Nginx 目录结构

[[email protected] ~]# ll /usr/local/nginx/ 
 drwxr-xr-x 2 root root 4096 Sep 15 13:44 conf           #Nginx相关配置文件 
 drwxr-xr-x 2 root root 4096 Sep 15 13:44 html           #网站根目录 
 drwxr-xr-x 2 root root 4096 Sep 15 13:44 logs           #日志文件 
 drwxr-xr-x 2 root root 4096 Sep 15 13:44 sbin           #Nginx启动脚本 
 #配置 Nginx 支持 php 文件 
 [[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf   //Nginx主配置文件 
 #user  nobody;                   
 user nginx nginx;                   #添加此行 
 ……
          #location ~ \.php$ {
                   #    root           html;
                   #    fastcgi_pass   127.0.0.1:9000;         
                   #    fastcgi_index  index.php;         
                   #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;         
                   #    include        fastcgi_params;         
                   #}                      #找到上面这段内容,将这段内容复制,去掉#且修改为如下 
location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;     
     fastcgi_index  index.php;     
     fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;      #将scripts修改为 nginx的html,即Nginx页面目录,因为要处理的php文件也在这个目录下     
     include        fastcgi_params; 
     } 
           

启动 Nginx

[[email protected] ~]# /usr/local/nginx/sbin/nginx 
[[email protected] ~]# netstat -tlnp | grep nginx tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7083/nginx           
使用浏览器测试   http://192.168.1.63 
           

#编译安装PHP

提前菜踩的坑http://xtblinux.com/thread-29-1-2.html

在Nginx中年,我们使用的是php-fpm来对php页面解析,PHP-FPM其实是PHP源代码的一个补丁,指在将FastCGI 进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,再编译安装PHP后才可以使用

从PHP5.3.3开始,PHP中直接整合了PHP-FPM,所以从PHP5.3.3版本以后,不需要下载PHP-FPM补丁包了,下面 是PHP-FPM官方发出来的通知: http://php-fpm.org/download

##PHP 添加 libmcrypt 拓展

libmcrypt加密算法扩展库,支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法

官方网站:http://mcrypt.hellug.gr/index.html 或者 http://mcrypt.sourceforge.net/

在页面上点击下图连接,可以下载最新的2.5.8版本(页面上有个Browse All Files→→Libmcrypt)

[[email protected] ~]# tar xvf libmcrypt-2.5.8.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/libmcrypt-2.5.8/
[[email protected] libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt ; make ; make install ; cd 
           

需要添加到库文件路径,由于系统默认规定只在/lib、/lib64、/lib/lib64下面找库文件,所以我们需要手动添加进去。

[[email protected] ~]# vim /etc/ld.so.conf 
include ld.so.conf.d/*.conf         #此行原有 
/usr/local/libmcrypt/lib      #此行添加 若不添加,则会编译安装报错: 
…… 
checking for sysvipc shared memory support... no 
checking for mmap() using MAP_ANON shared memory support... no 
checking for mmap() using /dev/zero shared memory support... no 
checking for mmap() using shm_open() shared memory support... no 
checking for mmap() using regular file shared memory support... no 
checking "whether flock struct is linux ordered"... "no" 
checking "whether flock struct is BSD ordered"... "no" 
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no 
           

#解决依赖

[[email protected] ~]# yum install php-pear    //pear按照一定的分类来管理pear应用代码库,你 的pear代码可以组织到其中适当的目录中,其他人可以方便的检索并分享到你的成果;pear不仅仅是一个代码仓库,它同时也 是一个标准,使用这个标准来书写你的php代码,将会增强你的程序的可读性,复用性,减少出错的几率;Pear通过两个类为 你搭建了一个框架,实现了诸如析构函数,错误捕获功能,你通过继承就可以使用这些功能 
 
如果不添加,编译安装的时候将会看到下面的错误: 
lxml2 -lz -lm -lxml2 -lz -lm -lcrypt  -o sapi/cgi/php-cgi Generating phar.php
   /usr/local/src/php-5.4.14/sapi/cli/php: error while loading shared libraries:  libmysqlclient.so.18: cannot open shared object file: No such file or directory make: *** [ext/phar/phar.php] Error 127   
 
[[email protected] ~]# vim /etc/ld.so.conf 
include ld.so.conf.d/*.conf         #此行原有 
/usr/local/libmcrypt/lib      #此行在libmcrypt已添加 
/usr/local/mysql/lib                    #此行添加  
[[email protected] ~]# ldconfig 
[[email protected] ~]# echo 'ldconfig' >> /etc/rc.local 
 
除开上面的依赖解决之外,还需要安装图片,xml,字体支持基本库,使用yum去安装,安装的时候,这些软件包自 身也有依赖! 
[[email protected]  ~]# yum install -y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetypedevel 
           

#编译安装 php

[[email protected] ~]# tar xvf php-5.6.13.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/php-5.6.13  
[[email protected] php-5.6.13]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php -with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir --withfreetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath -enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex -enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl -enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt 

 --with-config-file-path        #设置 php.ini 的搜索路径。默认为 PREFIX/lib --with-mysql                     #mysql安装目录,对mysql的支持 
 --with-mysqli                    #mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还 可以使访问数据库工作变得更加稳定。是一个数据库驱动 --with-iconv-dir                 #种字符集间的转换 
 --with-freetype-dir              #打开对freetype字体库的支持  
 --with-jpeg-dir                  #打开对jpeg图片的支持  
 --with-png-dir                #打开对png图片的支持 
 --with-zlib                        #打开zlib库的支持,实现GZIP压缩输出       
 --with-libxml-dir=/usr     #打开libxml2库的支持,libxml是一个用来解析XML文档的函数库
 --enable-xml                     #支持xml文档 
 --disable-rpath               #关闭额外的运行库文件 
 --enable-bcmath               #打开图片大小调整,用到zabbix监控的时候用到了这个模块 
 --enable-shmop                #shmop共享内存操作函数,可以与c/c++通讯 
 --enable-sysvsem  #加上上面shmop,这样就使得你的PHP系统可以处理相关的IPC函数(活动在内核级别)。 
 --enable-inline-optimization       #优化线程 
 --with-curl                      #打开curl浏览工具的支持   
 --with-curlwrappers           #运用curl工具打开url流 ,新版PHP5.6已弃用 
 --enable-mbregex              #支持多字节正则表达式 
 --enable-fpm                     #CGI方式安装的启动程序,PHP-FPM服务 
 --enable-mbstring             #多字节,字符串的支持 
 --with-gd                        #打开gd库的支持,是php处理图形的扩展库,GD库提供了一系列用来处理图片 的API,使用GD库可以处理图片,或者生成图片。 
 --enable-gd-native-ttf     #支持TrueType字符串函数库 
 --with-openssl                #打开ssl支持 
 --with-mhash                     #支持mhash算法扩展 
 --enable-pcntl                #freeTDS需要用到的,pcntl扩展可以支持php的多线程操作 
 --enable-sockets              #打开 sockets 支持 
 --with-xmlrpc                    #打开xml-rpc的c语言 
 --enable-zip                      #打开对zip的支持 
 --enable-soap                 #扩展库通过soap协议实现了客服端与服务器端的数据交互操作 
 --with-mcrypt                    #mcrypt算法扩展 

 
 [[email protected] php-5.6.13]# make -j 4 ; make install ; cd 
           

配置 php 和 php-fpm

PHP配置文件:
  [[email protected] ~]# cp /usr/local/src/php-5.6.13/php.ini-production /usr/local/php/php.ini 
PHP-FPM配置文件:
  [[email protected] ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf PHP-FPM启动脚本
[[email protected] ~]# cp /usr/local/src/php-5.6.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  
[[email protected] ~]# chmod +x /etc/init.d/php-fpm 
[[email protected] ~]# chkconfig php-fpm on 
[[email protected] ~]# /etc/init.d/php-fpm start 检测PHP-FPM启动成功与否 
[[email protected] ~]# netstat -antup | grep php-fpm tcp       0      0 127.0.0.1:9000              
0.0.0.0:*                   LISTEN      122801/php-fpm   
测试 LNMP 的 PHP 支持 
[[email protected] ~]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php 浏览器访问:http://192.168.1.63