06-01-编译安装php7
https://www.php.net/downloads
安装必要依赖包
sudo yum install epel-release gcc gcc-c++ bison openssl openssl-devel zlib-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel mhash-devel readline readline-devel libedit llibxslt libxslt-devel libzip pcre-devel autoconf libevent libevent-devel oniguruma xz-devel sqlite-devel automake libtool m4 oniguruma-devel libzip-devel c-ares
安装sqlite
wget https://www.sqlite.org/2022/sqlite-autoconf-3390400.tar.gz --no-check-certificate
tar -xvf sqlite-autoconf-3390400.tar.gz
cd sqlite-autoconf-3390400/
./configure --prefix=/usr/local/sqlite
sudo make -j 4 && sudo make install -j 4
sudo mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
sudo ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
设置PKG_CONFIG_PATH
sudo vim /etc/profile
添加如下内容
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH Path
source /etc/profile
sudo yum install sqlite sqlite-devel
cmake版本过低,需新版本
sudo yum remove cmake
sudo yum install -y cmake3
sudo cmake -version
编译安装libzip-1.9.2
sudo rpm -q libzip
sudo rpm -q libzip-devel
sudo yum remove libzip libzip-devel
wget https://libzip.org/download/libzip-1.9.2.tar.gz
tar -zxvf libzip-1.9.2.tar.gz
cd libzip-1.9.2
sudo yum install -y cmake3
mkdir build && cd build
//在刚刚建立的build目录中
cmake -DCMAKE_INSTALL_PREFIX=/usr ../
// 这里如果执行失败,请直接用cmake的绝对路径执行
//编译软件
sudo make -j 4
//安装软件
sudo make -j 4 install
echo $PKG_CONFIG_PATH
//发现没有返回东西,或者返回的东西没有libzip相关的信息所以我们需要添加path
配置生效:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig
//执行下面代码检查下有没有成功
pkg-config --libs libzip
//得到的回复是有libzip 信息的就代表成功了
方式1:查单个(推荐) 有的话会提示 -lzip
pkg-config --libs libzip
方式2:列出所有看看有没有libzip
pkg-config --list-all
或:
//编辑bashrc
vim ~/.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/local/lib64/pkgconfig
export PKG_CONFIG_PATH
//使path 生效
source ~/.bashrc
//重启或执行source ~/.bashrc 都可以使path生效
//再次进入系统我们执行下列操作检查一下有没有成功
pkg-config --libs libzip
//得到的回复是有libzip 信息的就代表成功了,重启的话也不会丢失
-L/usr/local/lib64 -lzip
手动安装oniguruma
wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
tar -zxf oniguruma-6.9.4.tar.gz
cd oniguruma-6.9.4
备用下载: http://down.24kplus.com/linux/oniguruma/oniguruma-6.9.4.tar.gz
./autogen.sh && ./configure --prefix=/usr --libdir=/lib64
sudo make -j 4 && sudo make -j 4 install
php安装
.下载
wgethttps://www.php.net/distributions/php-8.1.12.tar.gz --no-check-certificate 安装php8
wgethttps://www.php.net/distributions/php-7.4.33.tar.gz --no-check-certificate 安装php7
.解压
tar -xvf php-7.4.33.tar.gz
tar -xvf php-8.2.4.tar.gz
.进入php目录
cd php-8.2.4
cd php-7.4.33
. 编译
php7
sudo ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-pcntl \
--enable-soap \
--with-openssl \
--with-mhash \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--enable-xml \
-enable-mbstring \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-mbregex \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--with-zip \
--with-zlib \
--enable-phar \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-gd \
--with-jpeg \
--with-freetype
sudo make -j 4 && sudo make install -j 4
php8
sudo ./configure \
--prefix=/usr/local/php8 \
--with-config-file-path=/usr/local/php8/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-pcntl \
--enable-soap \
--with-openssl \
--with-mhash \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--enable-xml \
-enable-mbstring \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-mbregex \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--with-zip \
--with-zlib \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-gd \
--with-jpeg \
--with-freetype
sudo make -j 4 && sudo make install -j 4
环境变量[如果php多个版本共存只能设置其中一个版本的
# sudo vim /etc/profile
#######php#########
PHP=/usr/local/php
PATH=$PATH:$PHP/bin
export PHP PATH
# source /etc/profile
8.配置php-fpm
php7
sudo cp php.ini-development /usr/local/php/etc/php.ini
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
sudo vim /etc/init.d/php7-fpm ## 如果要安装多版本请修改php路径变量prefix=/usr/local/php即可
sudo chmod +x /etc/init.d/php7-fpm
sudo vim /usr/local/php/etc/php-fpm.d/www.conf
[php7-fpm]
user = liwu
group = liwu
;listen = 127.0.0.1:9000
listen = /var/run/php7-fpm.socket
listen.owner = liwu
listen.group = liwu
listen.mode = 0665
加入开机启动
sudo systemctl daemon-reload
sudo service php7-fpm start
socket方式可不用以下步骤
netstat -anlt | grep 9000*
sudo ufw allow 9000/tcp
php8
sudo cp php.ini-development /usr/local/php8/etc/php.ini
sudo cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
sudo cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php8-fpm
sudo vim /etc/init.d/php8-fpm ## 如果要安装多版本请修改php路径变量prefix=/usr/local/php8即可
sudo chmod +x /etc/init.d/php8-fpm
sudo vim /usr/local/php8/etc/php-fpm.d/www.conf
[php8-fpm]
user = liwu
group = liwu
;listen = 127.0.0.1:9001
listen = /var/run/php8-fpm.socket
listen.owner = liwu
listen.group = liwu
listen.mode = 0665
加入开机启动
sudo systemctl daemon-reload
sudo service php8-fpm start
socket方式可不用以下步骤
netstat -anlt | grep 9000*
sudo ufw allow 9001/tcp
独立安装GD扩展
进入php源码文件的gd库
cd ext/gd
php7
sudo /usr/local/php/bin/phpize
sudo ./configure \
--with-php-config=/usr/local/php/bin/php-config \
--with-jpeg \
--with-png \
--with-freetype \
--with-zlib
sudo make -j 4 && sudo make install -j 4
php8
sudo /usr/local/php8/bin/phpize
sudo ./configure \
--with-php-config=/usr/local/php8/bin/php-config \
--with-jpeg \
--with-png \
--with-freetype \
--with-zlib
sudo make -j 4 && sudo make install -j 4
sudo vim /usr/local/php/etc/php.ini
sudo vim /usr/local/php8/etc/php.ini
在底部加入
[GD]
extension=gd.so
$ php --ri gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.12.1
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 6b
PNG Support => enabled
libPNG Version => 1.6.36
WBMP Support => enabled
XBM Support => enabled
BMP Support => enabled
TGA Read Support => enabled
Directive => Local Value => Master Value
gd.jpeg_ignore_warning => 1 => 1
redis扩展
wget https://pecl.php.net/get/redis-5.3.7.tgz --no-check-certificate
tar -xvf redis-5.3.7-php-extension.tgz
cd redis-5.3.7/
------------php7---------
sudo /usr/local/php/bin/phpize
sudo ./configure --with-php-config=/usr/local/php/bin/php-config
sudo make -j 4 && sudo make install -j 4
------------php8---------
sudo /usr/local/php8/bin/phpize
sudo ./configure --with-php-config=/usr/local/php8/bin/php-config
sudo make -j 4 && sudo make install -j 4
php.ini最后添加
sudo vim /usr/local/php/etc/php.ini安装
sudo vim /usr/local/php8/etc/php.ini
[Redis]
extension=redis.so
liwu@lw-deepin:~/soft/redis-5.3.7$ php --ri redis 或 /usr/local/php8/bin/php --ri redis
redis
Redis Support => enabled
Redis Version => 5.3.7
Redis Sentinel Version => 0.1
Available serializers => php, json
Directive => Local Value => Master Value
redis.arrays.algorithm => no value => no value
redis.arrays.auth => no value => no
sudo service php7-fpm restart
sudo service php-fpm restart
06-04-安装PECL
swoole 扩展安装
php高版本[推荐]
> sudo apt-get install libc-ares-dev
centos: sudo yum install c-ares
> 解决 No releases available for package "pecl.php.net/swoole"
sudo /usr/local/php8/bin/pear clear-cache
sudo /usr/local/php8/bin/pear update-channels
sudo /usr/local/php8/bin/pear upgrade
https://codeload.github.com/swoole/swoole-src/zip/refs/heads/master
Swoole 项目已收录到 PHP 官方扩展库,除了手动下载编译外,还可以通过 PHP 官方提供的 pecl 命令,
sudo /usr/local/php8/bin/pecl install -D 'enable-sockets="yes" enable-openssl="yes" enable-http2="yes" enable-mysqlnd="yes" enable-swoole-json="yes" enable-swoole-curl="yes" enable-cares="yes"' swoole
sudo vim /usr/local/php8/etc/php.ini
[Swoole]
extension=swoole.so
> 方法2(低版本推荐)
https://www.pimspeak.com/ubuntu-2004-php7-4-swoole.html
wget https://github.com/swoole/swoole-src/archive/refs/tags/v4.8.11.zip --no-check-certificate ---一定要下这个版本
unzip v4.8.11.zip
unzip swoole-4.8.11.zip
cd swoole-src-4.8.11/
sudo /usr/local/php/bin/phpize
sudo ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl --enable-sockets --enable-mysqlnd --enable-swoole-curl --enable-cares(最后这个可以去掉)
sudo make -j 4 && sudo make install -j 4
sudo vim /usr/local/php/etc/php.ini
[Swoole]
extension=swoole.so
liwu@lw-deepin:~/soft/swoole-src-4.8.11$ /usr/local/php/bin/php --ri swoole
swoole
Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.8.11
Built => Nov 10 2022 00:02:23
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1d 10 Sep 2019
dtls => enabled
curl-native => enabled
错误: Enable c-ares support, require c-ares library
wget --no-check-certificate https://c-ares.org/download/c-ares-1.18.1.tar.gz
tar -xvf c-ares-1.18.1.tar.gz
./configure
sudo make -j 4
sudo make -j 4 install
php zlib扩展安装(finall) ----php7.34及以后已集成
php zip扩展安装----php7.34 已集成及以后已集成
php和nginx接收数据大小和时间的一些设置:06-05-php内存上传设置
06-02-composer安装
php和nginx的结合
1. 修改nginx配置
sudo vim /usr/local/nginx/conf/nginx.conf
user liwu;
worker_processes 2;
error_log /var/log/nginx/error.log;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 300m;
sendfile on;
keepalive_timeout 65;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
include /usr/local/nginx/conf/conf.d/*.conf;
}
cd /usr/local/nginx/conf/conf.d/
sudo vim enbale_php7.ini -----注意这里的文件的格式不能是.conf格式的文件
sudo vim enable_php8.ini
add_header X-Frame-Options SAMEORIGIN;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location /
{
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ ^.+\.php
{
try_files $uri =404;
# 注意这里要和/usr/local/php/etc/php-fpm.d/www.conf 配置的 listen 一致
# 1 . listen = 127.0.0.1:9000
# 2. listen = unix://var/run/php7-fpm.socket php8修改这一行
fastcgi_pass unix://var/run/php7-fpm.socket;
fastcgi_index index.php;
#定义变量 $path_info
set $path_info "";
#定义变量 $real_script_name,用于存放真实地址
set $real_script_name $fastcgi_script_name;
#如果地址与引号内的正则表达式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#将文件地址赋值给变量 $real_script_name
set $real_script_name $1;
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log /dev/null;
}
#禁止访问的文件或目录
location ~ ^/(\.env|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
虚拟站点配置
sudo vim brace.conf
server {
listen 80;
server_name localhost.brace.com;
root /home/liwu/project/php/baoxian/braceapi/public;
index index.php index.html index.htm;
include /usr/local/nginx/conf/conf.d/enbale_php7.ini;
}
添加域名
sudo vim /etc/hosts
127.0.0.1 localhost.brace.com
重启php和nginx
sudo service php7-fpm stop
sudo service php7-fpm start
sudo service nginx stop
sudo service nginx start
大坑一: 验证码不显示!
咸鱼君项目采用的是Laravel5.6 + captcha做的验证码展示, 结果升级后发现,验证码显示不出来, 图片请求全是500错误, 打开Laravel Debug模式,看到报错
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function Intervention\Image\Gd\imagettfbbox()
一看, php的gd扩展有问题啊!!!
赶紧去ssh生产,查看gd扩展是不是正常安装的
php -m | grep gd
发现, gd扩展是有的!
下面是重点了, 解决方案如下
问题原因
其实这个问题的确和 gd 扩展有关系, 主要是和gd扩展的子扩展freetype有关系!
你需要先判断生产环境是不是安装了freetype
//查看freetype安装目录
find / -name freetype
要是安装了, 则编写一个小php脚本来判断有没有正确加载gdtest.php
<?php
if(extension_loaded('gd')){
echo "can use gd",PHP_EOL;
foreach(gd_info() as $cate=>$value)
echo "$cate: $value",PHP_EOL;
}else
echo "can not use gd";
?>
执行 php test.php
can use gd
GD Version: bundled (2.0.34 compatible)
FreeType Support: 1 #如果这里冒号后面没有值,说明gd不支持freetype
T1Lib Support:
GIF Read Support: 1
GIF Create Support: 1
JPEG Support: 1
PNG Support: 1
WBMP Support: 1
XPM Support:
XBM Support: 1
JIS-mapped Japanese Font Support:
gd不支持freetype 这里就要重新手动安装GD了 PHP7,4.33之后已经集成 请在编译的时候加上GD库以及GD依赖的扩展库
进入php源码 ext/gd扩展
sudo /usr/local/php/bin/phpize
sudo ./configure \
--with-php-config=/usr/local/php/bin/php-config \
--with-jpeg \
--with-png \
--with-freetype \
--with-zlib
sudo make -j 4 && sudo make install -j 4
liwu@debian:~/soft/php-8.1.12/ext/gd$ php --ri gd
gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.10.4
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 6b
PNG Support => enabled
libPNG Version => 1.6.37
WBMP Support => enabled
XBM Support => enabled
BMP Support => enabled
TGA Read Support => enabled
重启php-fpm和nginx
报错:
RSA_SSLV23_PADDING undeclared (first use in this function); did you mean ‘RSA_NO_PADDING’?
这个错误是因为openssl版本太高 加密算法已不支持 如果是安装PHP7 我至今没解决 建议安装PHP8吧