12.3/12.4 PHP安装

时间:2022-04-27
本文章向大家介绍12.3/12.4 PHP安装,主要内容包括PHP安装目录概要、PHP安装、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

PHP安装目录概要

  • 和LAMP安装PHP方法有差别,需要开启php-fpm服务
  • cd /usr/local/src/
  • wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
  • tar zxf php-5.6.30.tar.gz
  • useradd -s /sbin/nologin php-fpm
  • cd php-5.6.30
  • ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl
  • make && make install
  • cp php.ini-production /usr/local/php-fpm/etc/php.ini
  • vi /usr/local/php/etc/php-fpm.conf //写入如下内容
  • cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  • chmod 755 /etc/init.d/php-fpm
  • chkconfig --add php-fpm
  • chkconfig php-fpm on
  • service php-fpm start
  • ps aux |grep php-fpm

PHP安装

  • LAMP架构和LNMP架构中安装PHP方法是不同的
    • 在LAMP中,php是作为Apache的一个模块存在的,需要用apxs2 指定Apache的路径,使用apxs的工具进行自动配置模块的加载
    • 在LNMP里,需要指定mysql的路径,但是不需要制动Nginx的路径,因为是LNMP中,PHP是作为一个独立的服务在运行的,和Nginx没有直接的关系,所以它也不需要依赖Nginx
  • 若是在之前LAMP架构中,编译过PHP,所以只需要进入PHP的目录下执行make clean 把之前编译过的那些文件,全部删掉
  1. 首先进入到/usr/local/src/
[root@hanfeng ~]# cd /usr/local/src/
[root@hanfeng src]# 
  1. 下载php安装包
[root@hanfeng src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
  1. 解压php安装包
[root@hanfeng src]# tar zxf php-5.6.30.tar.gz
[root@hanfeng src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  php-5.6.30  php-5.6.30.tar.gz
[root@hanfeng src]# 
  1. 增加用户并指定shell
  • useradd -s 指定用户的shell
[root@hanfeng src]# useradd -s /sbin/nologin php-fpm
[root@hanfeng src]# 
  1. 切换到/usr/local/src/php-5.6.30
[root@hanfeng ~]# cd /usr/local/src/php-5.6.30
[root@hanfeng php-5.6.30]# 
  1. 初始化./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl
  • ./configure –prefix=/usr/local/php-fpm //指定路径
  • --with-config-file-path=/usr/local/php-fpm/etc //指定配置文件所在路径
  • --enable-fpm //必须加上,否则无法启动该服务
  • --with-fpm-user=php-fpm //指定用户
  • --with-fpm-group=php-fpm //指定组
  • --with-mysql=/usr/local/mysql //指定 mysql 的路径
  • --with-mysqli=/usr/local/mysql/bin/mysql_config //指定 mysqli 的路径
  • --with-pdo-mysql=/usr/local/mysql //指定 pdo-mysql 的路径
[root@hanfeng php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl  --with-openssl
  • 错误
checking for cc... no
checking for gcc... no
configure: error: in `/usr/local/src/php-5.6.30':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
  • 解决方法
yum install -y gcc
  • 错误
configure: error: xml2-config not found. Please check your libxml2 installation.
  • 解决方法
[root@hanfeng php-5.6.30]# yum install -y libxml2-devel
  • 错误
configure: error: Cannot find OpenSSL's <evp.h>
  • 解决方法
yum install -y openssl-devel
  • 错误
configure: error: Please reinstall the libcurl distribution -
  easy.h should be in <curl-dir>/include/curl/
  • 解决方法
yum install -y libcurl-devel
或
yum install -y curl curl-devel
  • 错误
configure: error: jpeglib.h not found.
  • 解决方法
yum install -y libjpeg-devel
  • 错误
configure: error: png.h not found.
  • 解决方法
yum install -y libpng

yum install -y libpng-devel
  • 错误
configure: error: freetype-config not found.
  • 解决方法
yum install -y freetype-devel
  • 错误
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
  • 解决方法
yum install -y epel-release

yum install -y libmcrypt-devel
  1. 可以echo $?检查是否初始化成功
[root@hanfeng php-5.6.30]# echo $?
0
[root@hanfeng php-5.6.30]# 
  1. 然后make && make install
[root@hanfeng php-5.6.30]# make && make instal
  1. 用echo $?检查是否成功
[root@hanfeng php-5.6.30]# echo $?
0
[root@hanfeng php-5.6.30]#
  1. 查看/usr/local/php-fpm/目录——>于/usr/local/php/目录项比较多了 sbin 和 var 目录
[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var
[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/sbin/    //在sbin目录下有一个php-fpm文件,就是用来启动php-fpm的
php-fpm
[root@hanfeng php-5.6.30]# ls /usr/local/php-fpm/var/      //在var目录下有 log 和 run 文件,log文件存放日志的,run存放pid的,日志和pid路径都是可以指定的
log  run
[root@hanfeng php-5.6.30]# 
  1. /usr/local/php-fpm/sbin/php-fpm 和 /usr/local/php-fpm/bin/php 的用法是基本一致的
/usr/local/php-fpm/sbin/php-fpm -m       查看模块
/usr/local/php-fpm/sbin/php-fpm -i
/usr/local/php-fpm/sbin/php-fpm -t          测试配置文件文件语法
  1. 用/usr/local/php-fpm/sbin/php-fpm -t 测试配置文件文件语法是否错误
[root@hanfeng php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[03-Jan-2018 00:10:44] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[03-Jan-2018 00:10:44] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[03-Jan-2018 00:10:44] ERROR: FPM initialization failed
[root@hanfeng php-5.6.30]# 
  1. 这里会提示配置文件并不存在,若想要启动php-fpm服务,首先需要去配置配置文件
  2. 拷贝cp php.ini-production /usr/local/php-fpm/etc/php.ini文件
  • 查看目录发现/usr/local/src/php-5.6.30 有2个配置文件
    • php.ini-development 适合开发环境使用
    • php.ini-production 适合生产环境使用
      • 两者区别在于,错误日志不同
[root@hanfeng php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@hanfeng php-5.6.30]# 
  1. 进入到/usr/local/php-fpm/etc/目录下
[root@hanfeng php-5.6.30]# cd /usr/local/php-fpm/etc/
[root@hanfeng etc]# ls
pear.conf  php-fpm.conf.default  php.ini
[root@hanfeng etc]# 
  1. 编辑配置文件vim php-fpm.conf,或者直接把php-fpm.conf.default文件改名字为php-fpm.cnf——>这里直接新建写入的
[root@hanfeng etc]# vim php-fpm.conf

去https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/php-fpm.conf拷贝样例

拷贝到文件中
[global]                //全局配置
pid = /usr/local/php-fpm/var/run/php-fpm.pid                //定义PID
error_log = /usr/local/php-fpm/var/log/php-fpm.log        //错误日志
[www]                    //模块名字,启动服务的时候就能看到
listen = /tmp/php-fcgi.sock        //监听的地址
listen.mode = 666        //当监听的是sock的时候,这个语句才会生效,这个语句用来定义sock文件权限的
user = php-fpm        //用户,定义是哪个用户启动的
group = php-fpm             // 组,定义是哪个组启动的
pm = dynamic                    //进程相关信息
pm.max_children = 50                        //进程相关信息
pm.start_servers = 20                        //进程相关信息
pm.min_spare_servers = 5             //进程相关信息
pm.max_spare_servers = 35        //进程相关信息
pm.max_requests = 500         //进程相关信息
rlimit_files = 1024

然后保存退出

linsten 可以监听sock、tcp 可以写成 linsten = 127.0.0.1:9000(默认端口就是9000,可自定义),一般监听的到是“127.0.0.1 ”IP,因为php-fpm这个服务是针对内部使用的、在本机上用的,也就是说Nginx和php通常是在一台机器上,两者之间通信直接使用内部网络就可以了;在这块选择的linsten配置不同,到之后的配置Nginx配置,也就是告诉Nginx到哪里找php的配置语句也是不同

  1. 进入到/usr/local/src/php-5.6.30/下,拷贝启动的脚本
[root@hanfeng etc]# cd /usr/local/src/php-5.6.30/
[root@hanfeng php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@hanfeng php-5.6.30]#
  1. 加入到服务列表中,先更改权限,然后在chkconfig,最后在开机启动
[root@hanfeng php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@hanfeng php-5.6.30]# chkconfig --add php-fpm
[root@hanfeng php-5.6.30]# chkconfig php-fpm on
[root@hanfeng php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@hanfeng php-5.6.30]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
php-fpm        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@hf-01 php-5.6.30]# 
  1. 查看php-fpm服务是否启动
[root@hanfeng php-5.6.30]# ps aux |grep php-fpm
root      2386  0.0  0.4 124184  4936 ?        Ss   17:04   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   2387  0.0  0.4 124184  4700 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2388  0.0  0.4 124184  4700 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2389  0.0  0.4 124184  4700 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2390  0.0  0.4 124184  4700 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2391  0.0  0.4 124184  4704 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2392  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2393  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2394  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2395  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2396  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2397  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2398  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2399  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2400  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2401  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2402  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2403  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2404  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2405  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
php-fpm   2406  0.0  0.4 124184  4708 ?        S    17:04   0:00 php-fpm: pool www
root      2408  0.0  0.0 112676   988 pts/0    R+   17:04   0:00 grep --color=auto php-fpm
[root@hanfeng php-5.6.30]#