centos 下编译安装nginx + php 配置nginx+php-fpm

             最近由于工作需要,要在centos下编译安装nginx 和php,让php .在网上查了很多资料发现要编译这两个还是比较麻烦的,特此写此篇博客,希望对其他网友有所帮助。已下命令都在root用户下执行。关于在CentOS下php对权限为777的文件进行操作时会出现 failed to open stream: Permission denied 的错误提示,在文章结尾会给出可能出现此情形的原因。

            首先说明下我编译环境的版本:操作系统:CentOS-6.3-x86_64 ,这是64位的。nginx为nginx-1.3.7 ,php为php-5.4。

第一需要更新CentOS的源,具体操作请参看http://mirrors.163.com/.help/centos.html ,本文就不做详细说明了。还需要到http://repoforge.org/use/ 安装一个外部源,这个外部源会在安装 libmcrypt 等依赖包时需要用到的,否则用yum 安装时会提示找不到此安装包的错误提示。 

下面给出具体安装一些必要编译软件和依赖包的安装命令和解释:

          yum groupinstall Development Tools              #安装编译环境和相关软件

          yum install libxml2-devel openssl-devel bzip2-devel curl-devel libjpeg-devel libpng-devel freetype-devel gmp-devel mysql-devel ncurses-devel unixODBC-devel pspell-devel net-snmp-devel libXpm-devel libmcrypt libmcrypyt-devel libxslt-devel pcre-devel                               #安装编译时相关依赖包

         比如你在编译时出现configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution 这样的错误提示你就需要安装libslt-devel 包 ,在出现 configure: error: mcrypt.h not found. Please reinstall libmcrypt 错误提示时就需要安装libmcrypt-devel 包,这个包是在外部源中,安装上文给出 http://repoforge.org/use/ 这个页面 提示添加外部源即可。 pcre-devel这个包是编译nginx需要的。

         准备工作做完了我们只需进行编辑即可。首先编译php,进入php的源码包执行如下编译命令(一下是我个人设置的参数,根据需要可做调整):

          ./configure --prefix=/usr/local/php-5.4.7  --with-config-file-path=/usr/local/php-5.4.7/etc  --with-curl --with-pear --with-gd --with-jpeg-dir --with-png-dir --with-zlib --with-xpm-dir --with-freetype-dir --with-t1lib --with-mcrypt --with-mhash --with-mysql --with-mysqli --with-pdo-mysql --with-openssl --with-xmlrpc --with-xsl --with-bz2 --with-gettext --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data  --enable-mbstring  

          若无错误执行 make && make install 即可。这样php 就编译安装完成了。安装好了后不要忘记将源码包的php.ini-development文件复制到php的配置文件目录下,如若按我上述编译的话,则复制到/usr/local/php-5.4.7/etc/目录下并改名为php.ini,如果当前在php源码包中则使用如下命令实现即可:cp php.ini-development  /usr/local/php-5.4.7/etc/php.ini

           接下来编译安装nginx ,进入nginx源码包按下面命令编译安装nginx:

            ./configure --prefix=/usr/local/nginx-1.3.7 --user=www-data --group=www-data

            make 

    make install 

            --prefix是指定安装目录,--user指定进程以某个用户启动,--group指定以某个用户组启动,我这边是设为www-data ,在此之前需要创建www-data用户组和用户www-data ,这两个参数可不用设置。

          若顺利安装,则让我们启动nginx 看下我们的成果,以我上面编译参数为例使用一下命令启动nginx

          /usr/local/nginx-1.3.7/sbin/nginx  (其中/usr/local/nginx-1.3.7 是nginx的安装目录,即在configure 是prefix指定的)

          在浏览者中输入127.0.0.1查看,是不是看到欢迎页面了啊。接下来我们需要修改nginx配置文件使其能执行php文件。

          首先我们启动php-fpm ,使用如下命令启动php-fpm

          /usr/local/php-5.4.7/sbin/php-fpm            (/usr/local/php-5.4.7为php安装目录,在configure是由prefix指定的)

          在/usr/local/nginx-1.3.7/conf/nginx.conf中找到

         # 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;
        #  } 

         这段代码,去掉其前面的#号并将 fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 改为

          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

         即这段代码变为:

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        修改完成后保存,使用如下命令重启nginx:

        /usr/local/nginx-1.3.7/sbin/nginx -s reload 

        之后我们在/usr/local/nginx-1.3.7/html/ 目录下新建phpinfo.php文件,文件内容如下:

          <?php

           echo phpinfo() ;


        我们在浏览器中输入 127.0.0.1/phpinfo.php

        好了,尽情享受你的工作成果吧大笑

       题外话

       1.对于php对一个拥有777权限的文件进行读写时却会有 failed to open stream: Permission denied这样的错误提示时,可能有两种情况,一是selinux引起的(这种情况大多发生在redhat系列的系统和fedora系统中),使用setenforce 0 ,关闭selinux功能即可.还有一种可能就是使用 lsattr  查看文件是否有i或a属性 使用chattr -i 或 chattr -a 去掉 i或a属性即可(这种情况比较少见)。

    2.若系统安装在虚拟机中,在使用本机浏览器使用192.168.31.131(虚拟机ip)服务时打不开页面时,90%的原因是防火墙没有开放80端口,对于CentOS系统 使用如下命令关闭防火墙即可:

    service iptables stop

    之后刷新下你的浏览器,是不是看到了你想看到的页面了大笑

        

          

           

        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值