记录安装 Laravel 项目遇到多版本 PHP 的坑
介绍
由于服务器上运行着一些老项目php
版本使用的是 5.4 的,而我的新项目用的php7.0
以上的。由于不敢随意更新php
版本,只能安装一个php7.4
的版本
php7.4.0 安装
系统:
centos7
下载并解压
$ wget http://php.net/distributions/php-7.4.0.tar.gz
$ tar -zxvf php-7.4.0.tar.gz
$ cd php-7.4.0
编译并安装
编译前,新增用户组,用户,用于编译使用
$ groupadd www
$ useradd -g www www
开始编译(根据自己需要增减)
$ ./configure \
--prefix=/usr/local/php7.4.0 \
--with-config-file-path=/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-freetype-dir \
--enable-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 \
--enable-fpm
这里需要注意的是在php7.4 编译参数 –with-gd 要改成了 –enable-gd
安装
$ make && make install
设置配置文件
$ cp -R ./sapi/fpm/php-fpm.conf /usr/local/php7.4.0/etc/php-fpm.conf
$ cp php.ini-development /usr/local/php7.4.0/lib/php.ini
$ cp -R ./sapi/fpm/php-fpm /etc/init.d/php-fpm7.4.0
安装完成!
修改监听端口
修改php-fpm.conf的侦听端口为9074,因为主版本5.4是侦听9000。
; Note: This value is mandatory.
listen = 127.0.0.1:9074
启动php-fpm
$ /etc/init.d/php-fpm7.4.0 -c /usr/local/php7.4.0/lib/php.ini -y /usr/local/php7.4.0/etc/php-fpm.conf
通过 netstat -anp | grep 9074
可以看到 php7.4
已启动
配置项目
从git
服务器下载项目后。
这时需要 composer install
。出错了,composer
默认使用 系统默认的php5.4
版本
解决方式
$ which php
/usr/bin/php
将 php7.4.0
复制一份到环境目录里
$ cp /usr/local/php7.4.0/bin/php /usr/bin/php74
$ php74 -v
PHP 7.4.0 (cli) (built: Jun 30 2020 10:49:08) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
进入项目目录并下载 composer.phar
$ cd /var/www/tempweb
$ wget https://getcomposer.org/download/1.6.5/composer.phar
执行php74
安装命令
$ php74 composer.phar install
出错了,说是没有fileinfo
文件
解决方法
删除掉原来的 composer.lock
文件并重新执行命令php74 composer.phar install
即可成功!
配置nginx
web
站点
server {
listen 9090;
# server_name 不设置 server_name 默认 通过ip访问
root /var/www/TempWeb/public;
client_max_body_size 20m;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
# 指定 php7.4 的 9074 端口
fastcgi_pass 127.0.0.1:9074;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
保存后重载配置文件
$ ./usr/sbin/nginx -s reload
搞定!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: