centos7安装php7
安装php7前需要安装php运行所需要的库
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel epel-release
yum -y install libmcrypt-devel 这个得先yum安装epel-release后才能安装
解压PHP
tar xf php-7.1.26.tar.gz
cd php-7.1.26
编译选项
./configure \
--prefix=/usr/local/php \ #这是编译安装的路径
--with-mysql=/usr/local/mysql/\ #这是mysql的安装目录,由于php跟mysql是在同一台机器上面的,所以最好带上
--with-config-file-path=/etc \
--enable-fpm \ #这是为了fast-cgi接口能够调用php,即是nginx/apache这类软件通过fast-cgi调用php程序解析网页文件
--with-fpm-user=www \ #这里的用户名最好跟启动nginx服务的用户一致
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--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 \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache
注意以上编译选项不能有空格
make && make install
安装完成配置相应文件
cp php.ini-production /etc
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //复制启动脚本
chmod +x /etc/init.d/php-fpm //给予脚本执行的权限
设置环境变量
echo '/usr/local/php/bin:$PATH'>>/etc/profile
. /etc/profile
设置开机启动
chkconfig --add php-fpm
chkconfig php-fpm on
验证nginx是否成功通过fast-cgi调用php解析
在nginx.conf里面加入
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php mysql-test.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
在nginx的html下面新建文件以.php结尾
<?php
phpinfo();
?>
启动php-fpm,然后重启nginx
输入页面能够看见页面输出php信息就成功
验证php与本地mysql连接
新建文件以.php结尾
<?php
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'user1'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo '数据库连接成功!';
mysqli_close($conn);
?>
注意:以上用的连接方式主机地址写着localhost,这时默认会调用sock去连接数据库,而不是IP:端口方式连接,所以要求主机有sock文件,需要
修改my.cnf文件,告诉mysql,mysqldump,mysqladmin等mysql服务的socket位置在哪里就可以用localhost方式连接
参考地址:https://www.cnblogs.com/Lam7/p/6090989.html
输入页面能够看见数据库连接成功即可