首页 > 技术文章 > Ubuntu下配置PHP和CakePHP记录

hubery 2015-05-22 21:00 原文

目前在完成一个PayPal的支付页面,需要有PHP的开发环境,同时,在开发时使用了CakePHP的框架,于是就有了下面的情景。

操作环境:

  OS:ubuntu-14.04.2-desktop-amd64

  mysql --version:mysql  Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3

  whereis mysql :mysql: /usr/bin/mysql /etc/mysql /usr/lib/mysql /usr/bin/X11/ mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz

一、Apache与PHP的安装

PayPal需要使用Json、OpenSSL、SimpleXML、Curl,所以在安装PHP时需要安装和加入相关库,于是apache和php的配置如下:

./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite

./configure --prefix=/usr/local/php --with-openssl-dir=/usr/bin/openssl --with-openssl --with-mysqli=/usr/bin/mysql_config --with-gd=shared --with-curl=/usr/bin/curl --with-imap=shared --with-mysql=/usr/ --with-kerberos --with-imap-ssl --with-apxs2=/usr/local/apache2/bin/apxs

1.在重装apache时发现80端口被占用,导致新安装的apache无法启动,使用lsof -i:80和ps aux | grep 80发现80端口被aolserver4占用,搜索知道aolserver其实是apache的一部分,怀疑直接rm -Rf /usr/apache2是不正确的。

使用以下命令卸载相关服务,即可重新启动apache。

sudo apt-get autoremove aolserver4-daemon

2.

configure: error: ...No recognized SSL/TLS toolkit detected

 

sudo apt-get install openssl 
sudo apt-get install libssl-dev 

 3.

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

4.others:

sudo apt-get install libxml2-dev 
sudo apt-get install libcurl4-openssl-dev
configure: error: png.h not found.
sudo apt-get install libpng-dev
configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.
sudo apt-get install libc-client-dev
configure: error: Cannot find MySQL header files under /usr/.
sudo apt-get install libmysqld-dev
configure: error: Kerberos libraries not found. 
sudo apt-get install libkrb5-dev
configure: error: Cannot find OpenSSL’s libraries
sudo apt-get install pkg-config
PHP Warning:  mysqli::mysqli(): Headers and client library minor version mismatch. Headers:50543 Library:50717

此错误可能导致apache内存错误并退出,安装 apache2.2php5 的最新版本可以解决,参考Headers and client library minor version mismatch

5.undefined mb_convert_encoding(后续)

在后来使用中,出现 mb_convert_encoding 未定义,是由于扩展 mbstring 没有安装,在PHP安装完成添加 mbstring 扩展的方法:

#cd ~/Downloads/LAMP/php-5.5.23/ext/mbstring
#/usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212
#./configure --with-php-config=/usr/local/php/bin/php-config --enable-mbstring
...
#make
...
#make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
Installing header files:          /usr/local/php/include/php/
#sudo echo "extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/mbstring.so" >> /usr/local/php/lib/php.ini
#sudo /usr/local/apache2/bin/apachectl restart

我最开始配置PHP是参考PHP圣经(PHP and MySQL Web Development(Fourth Edition)),后来发现作为圣经它也不一定是完全正确的,第一,其在安装MySQL后就去安装PHP,这是不合逻辑的,PHP安装需要/usr/local/apache2/bin/apxs,并会把libphp5.so放到特定的目录;第二,关于其对httpd.conf和php.ini的配置也是不够而且致错的,最严重的问题是apache的执行者,直接导致curl 127.0.0.1/index.php时得到的是php源文件;第三,我们需要从php源文件/home/hubery/LAMP/php-5.5.23/php.ini-development拷贝php.ini到安装目录/usr/local/php/lib/php.ini。httpd.conf要修改的地方:

我们使用不可登陆系统的用户apache来运行apache,使用用户www来管理/var/www,对于特定目录比如upload我们给予o+r的权限或使其归属于apache用户。

 php.ini要修改的地方:

data.timezone = Aisa/Shanghai

二、CakePHP的配置

此时在参考CakePHP博客教程配置完毕后遇到多个问题:

1、Notice (1024): Please change the value of 'Security.salt' in app/config/core.php;

2、URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don't / can't use URL rewriting;
3、Cake is NOT able to connect to the database. Database connection "Mysql" is missing, or could not be created.

解决:

1、只需要修改以下两个地方为非默认值就可以了。

2、我一直以为是我的PHP没有安装或配置正确,其实最后发现是因为我在复制CakePHP时弄丢了根目录下的.htaccess文件,这个文件在整体copy是copy不了的,这是在摸索CakePHP文档时发现的。

据说同时要把httpd.conf中的AllowOverride none,改成AllowOverride all。

3、这个问题也花了我不少时间,CakePHP连接数据库不是用的mysql或mysqli,而是pdo_mysql,以下是在安装好了的PHP中添加pdo_mysql模块的步骤

cd /home/hubery/LAMP/php-5.5.23/ext/pdo_mysql

sudo /usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/

make && make install

生成了 /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/pdo_mysql.so

于是在php.ini加入extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/pdo_mysql.so",重启apache生效。

 

推荐阅读