参数配置
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-apxs2=/usr/local/apache2/bin/apxs \ 指定apache的apxs,会在其modules中生成libphp7.so
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--with-curl \
--with-mcrypt \
--with-curlwrappers \
--with-mhash \
--with-mcrypt \
--without-pear \
--with-gd \
--with-xmlrpc \
--enable-mysqlnd \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--enable-zip \
--enable-soap \
如果报错,直接安装需要的扩展包
sudo apt-get install libopenssl-dev libcurl4-openssl-dev libjpeg-dev ...
配置通过了,开始编译安装 make && sudo make install
拷贝配置文件 sudo cp php.ini-development /usr/local/php7/etc/php.ini
配置Apache解析php脚本,因为已经在其modules目录生成了libphp7.so,只需在配置文件中导入即可:
LoadModule php7_module modules/libphp7.so
配置php.ini,使其可以通过mysqli连接MySQL数据库
因为是通过mysqlnd驱动的,不需要在extension=mysqli,直接找到[MySQLi]部分进行配置即可:
mysqli.default_socket=/tmp/mysql.sock
启动mysql(service mysql start)后,如果只有/var/run/mysqld/mysqld.sock没有/tmp/mysql.sock,
则需要创建个软连接:ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
mysqli.default_host='localhost' mysqli.default_user='root'
如果要使用Pdo_mysql,同样 pdo_mysql.default_socket=/tmp/mysql.sock
测试一下,在Apache文档根目录创建info.php 和 test.php
info.php <?php
phpinfor();
?>
浏览器访问 .../info.php可以看到
test.php <?php
// 面向对象方法
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); if ($mysqli->connect_error) {
die('Connect Error:'. $mysqli->connect_error);
}
echo “Connect Successfully”; //面向过程方法
$conn = mysqli_connect($host, $servername, $username, $password); // 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
浏览器访问 .../test.php
Warning: mysqli::__construct(): (HY000/2002): Permission denied in /usr/local/apache2/htdocs/test.php on line 3
Connection Error: Permission denied
将上面的localhost 改成 127.0.0.1 结果:Connected successfully 。。。
//PDO方法
<?php
$servername = "127.0.0.1";
$username = "username";
$password = "password"; try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo $e->getMessage();
}