linux环境下安装php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 安装依赖
[root@shmily ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libzip-devel pcre-devel
# 进入/usr/local目录并下载php安装包
[root@shmily ~]# cd /usr/local
[root@shmily local]# wget http://cn2.php.net/distributions/php-7.3.3.tar.gz
# 解压并进入php目录
[root@shmily local]# tar -zxzf php-7.3.3.tar.gz
[root@shmily local]# cd php-7.3.3
# 配置
[root@shmily php-7.3.3]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --with-mcrypt=/usr/include --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-gd --with-iconv --with-zlib --enable-xml --enable-shmop --enable-sysvsem
# 此处运行报错:configure: error: no acceptable C compiler found in $PATH(执行完成后重新运行上一步命令)
[root@shmily php-7.3.3]# yum install gcc
# 编译并安装
[root@shmily php-7.3.3]# make && make install
# 复制配置文件并修改文件名
[root@shmily php-7.3.3]# cp php.ini-production /usr/local/php/bin/php.ini
[root@shmily php-7.3.3]# cd /usr/loacl/php/etc/
[root@shmily etc]# cp php-fpm.conf.default php-fpm.conf
# 创建软连接并启动
[root@shmily etc]# ln -s /usr/local/php/sbin/php-fpm
[root@shmily ~]# ./php-fpm
# 此处运行报错:No pool defined(执行完成后重新启动)
[root@shmily ~]# cd /usr/local/php/etc/php-fpm.d
[root@shmily php-fpm.d]# cp www.conf.default www.conf
# 报错:ERROR: [pool www] cannot get uid for user 'nginx'
#       ERROR: FPM initialization failed
[root@shmily php-fpm.d]# vim /usr/local/php/etc/php-fpm.d/www.conf
# 修改:
user = 当前用户
group = 当前用户组
# 创建index.php
[root@shmily php-fpm.d]# echo "<?php echo phpinfo(); ?>" > /usr/local/nginx/html/index.php
# 浏览器访问xx.xx.xx.xx/index.php(需在安装nginx后的环境下运行)

看到此页面表示配置完成
在这里插入图片描述
如果报错:file not fonud,则修改nginx配置文件中的 user 要和php-fpm的user用户一致

访问结果如果是下载文件,则nginx配置中添加:

location ~ .php$ {
root /usr/local/nginx/html;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME

documentrootdocument_root

documentr?ootfastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

至此,php安装完成