`

centos(64位) 安装PHP5.6,配置LNMP

阅读更多

下定决心自己整一个LNPM,第一次在Linux上搞开发环境,比win难多了。虚拟机centos6.5,网上搜了一些教程,撸起袖子开干,没想到坑一个接一个,时不时的挂起vpn google一下。好在我终于搞定了。下面整理一下我遇到的坑,以及整个解决过程。

 

第一坑:nginx 下 php  file not found 。

原因:nginx web执行php是通过php-fpm开执行,PHP5.3.3以前的PHP需要单独安装php-fpm补丁包,以后的版本不需要了。正确编译PHP并启动php-fpm即可,同时nginx.conf中配置好就行,重点是SCRIPT_FILENAME  $document_root$fastcgi_script_name;  这个不能写死了,如下:

location ~ \.php$ 

      {     

          #fastcgi_pass  unix:/tmp/php-cgi.sock;

          fastcgi_pass   127.0.0.1:9000;  #fastcgi监听端口,监听php-fpm*/

          fastcgi_index  index.php;

          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

          include        fcgi.conf;       #fastcgi配置文件,修改为以下内容*/

 

      }

 

第二坑: 连接数据库。Fatal error: Call to undefined function mysql_connect() 

由于不知道Linux下PHP各个模块都是需要编译的,第一次安装PHP 根本没在乎./configure 后的参数,从网上拷贝一份直接执行,导致数据库功能一直不成功,折腾了一礼拜。据我能看懂的部分,./configure 参数意义如下:

--prefix=/usr/local/php # 这是php的安装路径

 

--with-mysql=/usr   #这是mysql相关编译路径,这里网上很多都是--with-mysql=/usr/share/mysql,但我这样做,编译完提示一个error,Cannot find MySQL header files under /usr/share/mysql ,经查,有的说是因为64位电脑的原因,此处正确写法是 先 用 find / -name mysql.h  找到mysql.h的路径,然后写该路径的前缀。比如我电脑搜出来路径/usr/include/mysql/mysql.h ,所以前缀是 /usr

 

--with-mysql-sock --with-mysqli=/usr/lib64/mysql/mysql_config  # 这应该是mysqli 编译配置,后面的参数可以 先  find / -name mysql_config,然后填写

 

 

--enable-fpm # 开启php-fpm 功能

 

第三坑:找不到 mysql.h

我是采用 yum install mysql mysql-server 安装mysql,但找不到mysql.h,经查,

还需要 yum install mysql-devel

 

在我解决Fatal error: Call to undefined function mysql_connect()的过程中,想当然的以为php.ini中没开启mysql.so ,于是在php源码下进入cd ext/mysql/,

执行/usr/local/php/bin/phpize

然后编译,

./configure --with-php-config=/usr/local/php/bin/php-config --with-mysql=/usr/local/mysql/ --with-zlib-dir=/usr/local/lib

这里我试过--with-mysql=/usr/local/mysql/ 和 --with-mysql=/usr

最后在/usr/local/lib/php/extensions/no-debug-non-zts-20090626/中也添加了编译生成的mysql.so,修改php.ini,添加extension=mysql.so,重启php-fpm(备注:修改php.ini后Linux下重启nginx是没有用的,需要重启php-fpm),还是没有解决问题。

 

也许是我方法不对。 最后还是决定重头再来,我删除了mysql、php全部重装。终于好了。

 

下面是安装过程,因为编译PHP需要用到mysql路径,所以必须先安装mysql,有博客写可以通过/usr/local/php/bin/phpize后编译mysql.so,但是我没有成功。为了减少不必要的麻烦,还是先安装mysql,再安装PHP,nginx什么时候安装都可以。

 

 

第一步: mysql

   

 yum install mysql mysql-server   #询问是否要安装,输入Y即可自动安装,直到安装完成

       /etc/init.d/mysqld start   #启动MySQL

       chkconfig mysqld on   #设为开机启动

       cp /usr/share/mysql/my-medium.cnf   /etc/my.cnf  #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

       shutdown -r now  #重启系统

 

2、为root账户设置密码

       mysql_secure_installation

       回车,根据提示输入Y

       输入2次密码,回车

       根据提示一路输入Y

 

       最后出现:Thanks for using MySQL!

 

第二步,安装nginx 

       yum install nginx      #安装nginx,根据提示,输入Y安装即可成功安装

       service nginx start     #启动

       chkconfig  nginx on    #设为开机启动

       /etc/init.d/nginx  restart  #重启

       rm -rf /usr/share/nginx/html/*  #删除ngin默认测试页

 

 备注: 以上两个步骤出自 http://www.osyunwei.com/archives/2353.html 

 

 

如果觉得yum安装的版本不是最新的,可以在nginx官网下载安装包,然后手动安装。方法如下:

在目录 /usr/local/src/nginx-1.7.5.tar.gz

 tar zxvf nginx-1.7.5.tar.gz

cd nginx-1.7.5
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --without-http_rewrite_module --with-http_ssl_module --with-pcre

 

注意: --user=nginx --group=nginx 这是运行nginx的组和用户,没有通过add添加


make && make install


#启动nginx,nginx启动成功。
/usr/local/nginx/sbin/nginx -s reload
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx  //建立nginx命令软连接

//接下来修改nginx配置文件,根据需要修改对应文件
vim nginx.conf

 

注意修改user nginx;  去掉;

 

第三步:安装 PHP

 

1.安装php需要用的组件,包括图片、邮件、xml、mbstring等等,最主要的包括php-fpm

我参考了两篇博客,写的组件内容不一样,我把两篇博客的组件内容做了并集,也不一定完整,但更详细,用yum安装,如下:

 

yum install -y gcc gcc-c++ autoconf php-mysql php-gd libjpeg libjpeg-devel php-imap php-ldap php-odbc php-pear php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel php-fpm

 

2.安装php 同nginx安装,可以用 yum install php ,也可以自己下载安装包,我下载的是php-5.6.5.tar.gz,放在/usr/local/src/ 下

 

tar zxvf php-5.6.5.tar.gz

cd php-5.6.5

 

./configure --prefix=/usr/local/php --with-mysql=/usr --with-mysql-sock --with-mysqli=/usr/lib64/mysql/mysql_config --enable-fpm --with-ncurses --enable-soap --with-libxml-dir --with-XMLrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --disable-mbregex --disable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-pdo-mysql --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sqlite-utf8 --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear

 

注意: 如果编译出现了error,就不要在执行make了,先把error解决了。 我在这一步就出现了mysql相关的error信息,然后我把 --with-mysql=/usr/local/mysql改为 --with-mysql=/usr(即mysql.h目录前缀,没有mysql.h参考前文安装)

 

make && make install

 

Build complete.

Don't forget to run 'make test'.

 

cp /usr/local/php/etc/php-fpm.conf.default php-fpm.conf

//复制一份并重命名

 

/usr/local/php/sbin/php-fpm

//启动php-fpm

 

修改FPM 配置文件php-fpm.conf  

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

去掉分号,参数值根据自己的需要设置,这只是推荐值

 

ln -s /usr/local/php/sbin/php-fpm /bin/php-fpm  

添加php-fpm 命令

 

cp /usr/local/src/php/php.ini-producsion /usr/local/php/lib/php.ini

注意:cp这一步非必须。在我第二次安装php的时候,make命令后等待时间非常长,大概是我前一次安装时间长度的4、5倍,所以我怀疑第一次安装php好多模块啥的都没安装上,这次安装完之后查看phpinfo(),显示的信息也多了不少,其中php.ini显示在/etc/目录下,并不需要从php目录中拷贝,是安装生成的,而且,php.ini也没有mysql.so,但mysql功能是开启的,所以也是编译、安装自动的,只需要在安装php的时候把编译参数、组件安装全即可。

 

 

至此,配置nginx.conf 的 local / php 参数,重启nginx、重启mysql、重启php-fpm 

 

大功告成

 

 

 参考博客:

 

http://www.aichengxu.com/view/35376 

 

http://www.osyunwei.com/archives/2353.html

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics