LNMP环境搭建(三:PHP)
1.获取php源码
# cd /usr/local/src/
# wget http://cn2.php.net/get/php-7.0.15.tar.gz/from/this/mirror
2.解压源码包
# tar -xf mirror
# cd php-7.0.15
3.根据自己需要写配置
# ./configure --prefix=/usr/local/php --with-mysqli --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl --with-pdo_mysql
注:此过程会报错,和nginx一样缺少什么软件就安装对应的dev包,我在执行时报的错误安装了一下包,仅供参考 yum install libxml2-devel.x86_64 bzip2-devel.x86_64 libcurl-devel.x86_64 -y
4.# make
5.# make test
6.# make install
7.为php提供主配置文件
# cp php.ini-production /etc/php.ini
8.为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
#chkconfig php-fpm on
9.为php-fpm提供配置文件
#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
10.修改配置文件
修改主配置文件:
# vim /etc/php.ini
cgi.fix_pathinfo=0
修改php-fpm配置文件:
# vim /usr/local/php/etc/php-fpm.conf
开启php-fpn.conf的pid pid=run/php-fpm.pid
修改www的配置文件:
# vim /usr/local/php/etc/php-fpm.d/www.conf
将用户与组都修改为www-data用户
user=www-data
group=www-data
11.修改nginx配置文件,使其将收到的php请求转发给php-fpm处理,取消PHP模块之前的注释,并修改为
#vim /etc/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
12.修改php.ini指定mysql的socket文件地址
# vim /etc/my.cnf | grep socket
会显示当前的sock文件地址 格式:socket=/var/lib/mysql/mysql.sock
将mysql的socket地址填入php.ini
# vim /etc/php.ini
找到并修改为mysqli.default_socket = /var/lib/mysql/mysql.sock
13.启动php-fpm,重新启动nginx
# /usr/local/php/sbin/php-fpm
# /usr/local/nginx/sbin/nginx -t
# killall nginx
# /usr/local/nginx/sbin/nginx
14.创建测试页
#rm /usr/local/nginx/html/index.html
# vim /usr/local/nginx/html/index.php
<?php $conn = new mysqli('localhost','root','此处填写MySQL-root用户的密码','mysql'); var_dump($conn);
15.测试
#curl http://localhost