ubuntu18.04 安装PHP7.2并且配置Nginx支持php-fpm模块

PHP7.2安装

1.首先在PHP官网下载

// 下载
wget -c https://www.php.net/distributions/php-7.2.20.tar.gz
// 解压
tar -zxvf php-7.2.20.tar.gz
// 进入
cd php-7.2.20

2.安装需要的配置

sudo apt-get install libxml2
sudo apt-get install libxml2-devel
sudo apt-get install openssl
sudo apt-get install openssl-devel
sudo apt-get install curl
sudo apt-get install curl-devel
sudo apt-get install libjpeg
sudo apt-get install libjpeg-devel
sudo apt-get install libpng
sudo apt-get install libpng-devel
sudo apt-get install freetype 
sudo apt-get install freetype-devel
sudo apt-get install pcre
sudo apt-get install pcre-devel
sudo apt-get install libxslt
sudo apt-get install libxslt-devel
sudo apt-get install bzip2
sudo apt-get install bzip2-devel

3.编译配置

./configure --prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip

可以使用./configure --help命令查看所有选项,这里注意在php7中--with-mysql原生支持已经不存在了,操作都变成mysqli或者pdo了。以上这些选项在正常的php开发中完全够用了,后期如果需要,可以选择手动开启相应的模块。

编译成功显示:

| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: main/php_config.h is unchanged
config.status: executing default commands

编译中遇到的问题:

err1:
configure: error: Cannot find OpenSSL's <evp.h>
# 解决办法:
sudo apt-get install libssl-dev
# 安装libssl-dev时如果出现错误:
The following packages have unmet dependencies:
 libssl-dev : Depends: libssl1.0.0 (= 1.0.1f-1ubuntu2) but 1.0.1f-1ubuntu2.19 is to be installed
              Recommends: libssl-doc but it is not going to be installed
# 解决办法:使用aptitude进行安装
sudo apt-get install aptitude
# 然后使用aptitude再次安装libssl-dev,并选择降级的版本。
sudo aptitude install libssl-dev

# 这里提示时,一定要选n,选Y跟apt-get install操作一样
Accept this solution? [Y/n/q/?] n
The following actions will resolve these dependencies:

     Downgrade the following packages:                                   
1)     libssl1.0.0 [1.0.1f-1ubuntu2.19 (now) -> 1.0.1f-1ubuntu2 (trusty)]

# 接受这里的降级处理,成功安装
Accept this solution? [Y/n/q/?] y
The following packages will be DOWNGRADED:
  libssl1.0.0 
The following NEW packages will be installed:

err2:
configure: error: jpeglib.h not found
# 解决办法:
sudo apt-get install libjpeg-dev
如果上面命令失败的话,使用降级安装 sudo aptitude install libjpeg-dev 第一次选n,之后选择y

err3:
configure: error: png.h not found.
# 解决办法:
sudo apt-get install libpng-dev
如果上面命令失败的话,使用降级安装 sudo aptitude install libpng-dev  第一次选n,之后选择y

err4:
configure: error: freetype-config not found.
# 解决办法:
sudo apt-get install libfreetype6-dev
如果上面命令失败的话,使用降级安装 sudo aptitude install libfreetype6-dev 第一次选n,之后选择y

err5:
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决办法:
sudo apt-get install libxslt1-dev

详细可见:https://phperzh.com/articles/1789

4.编译并且安装,需要较长的时间,请耐心等待
make && make install

5.配置相应文件

cp php.ini-development /usr/local/php/lib/php.ini 
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

然后设置php.ini,使用:vim /usr/local/php/lib/php.ini打开php配置文件找到cgi.fix_pathinfo配置项,这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0,设置完毕保存并且退出。

6.创建web用户

groupadd www-data
useradd-g www-data www-data

// 打开www.conf文件,配置用户权限
vim /usr/local/php/etc/php-fpm.d/www.conf
默认user和group的设置为nobody,将其改为www-data
  1. 启动php-fpm服务
/usr/local/bin/php-fpm

php-fpm服务默认使用9000端口,使用 netstat -tln |grep 9000 可以查看端口使用情况,9000端口正常使用,说明php-fpm服务启动成功。

8.配置mginx
打开nginx.conf文件
首先找到user 设置为:user www-data www-data;
然后在service{}层中 添加php location段

       location / {
              root  /var/www/test;
              index index.php index.html; // 添加index.php
        }


        location ~ \.php$ {
           root           /var/www/test; // 项目文件根目录
           fastcgi_pass   127.0.0.1:9000; // 对应的php-fpm地址和端口
           fastcgi_index  index.php; // 默认访问文件
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; // 将 /scripts改为$document_root
           include        fastcgi_params;
        }

修改完之后保存nginx.conf文件 并且重启nginx

/usr/local/nginx/sbin/nginx -s reload

然后在项目路径中创建一个index.php文件,内容可以如下:

<?php 
    echo phpinfo(); 
?>

然后打开浏览器输入对应的地址进行访问,看到输出页面,说明nginx和php都配置成功。

本文参考:https://www.jianshu.com/p/19650dfcb961

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容