centos7离线安装PHP7.4.30+nginx1.23

环境

centos7.9

PHP7.4.30

nginx1.23.2

准备工作

在编译PHP时会提示一些包版本不够或者缺少某些包,一般选择yum来安装缺少的包,但因为是离线安装,所以可以手动配置本地yum源。先看一下系统版本

[root@xcc ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

下载系统对应的iso镜像文件CentOS-7.9-x86_64-Everything-2009.iso,传到服务器上并配置本地yum源。配置方法可以参考:https://www.cnblogs.com/haolb123/p/16553119.html

安装完下面这些,可以减少在编译PHP时提示的缺包问题。

yum install -y gcc gcc-c++ make cmake bison autoconf wget lrzsz
yum install -y libtool libtool-ltdl-devel 
yum install -y freetype-devel libjpeg.x86_64 libjpeg-devel libpng-devel gd-devel
yum install -y python-devel  patch  sudo 
yum install -y openssl* openssl openssl-devel ncurses-devel
yum install -y bzip* bzip2 unzip zlib-devel
yum install -y pcre pcre-devel
yum install -y libevent*
yum install -y libxml* libxml2-devel
yum install -y libcurl* curl-devel 
yum install -y readline-devel
yum install -y sqlite-devel.x86_64

附1:如果没有系统iso镜像文件,可以直接编译安装,提示缺少什么,就从https://pkgs.org/下载系统对应的包,然后手动rpm -i安装即可。有些包可能需要编译安装。

附2:单独下载rpm包安装时,会提示缺少依赖项等。而yum安装时会自动安装这个包的依赖项,所以还是建议配置本地yum源来装。

附3:安装完上面这些,如果是最小化安装PHP7.4.30则可以正常通过编译,可以直接make && make install安装PHP。

PHP

下载地址:https://www.php.net/downloads.php

安装

安装时,如果此时不指定./configure后面的扩展,后期也可以手动加上。

附:指定的扩展越多,编译时可能会遇到的问题越多,不要盲目的添加很多用不上的扩展,如果可以,建议选择默认(最小化)安装。

附:根据实际的web服务器开启适当的扩展,Nginx使用--enable-fpm,Apache使用--with-apxs2

tar zxvf php-7.4.30.tar.gz
cd php-7.4.30
./configure --prefix=/usr/local/php --with-config-file-scan-dir=/usr/local/php/etc/ --enable-inline-optimization --enable-opcache --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbregex --enable-mbstring --with-iconv --with-mhash --with-openssl --enable-bcmath --enable-soap --with-xmlrpc --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zip --with-bz2 --enable-gd --with-jpeg --with-readline --with-freetype --disable-fileinfo
#可选1:最小化安装-适合nginx平台
#./configure --prefix=/usr/local/php --with-config-file-scan-dir=/usr/local/php/etc/ --enable-fpm 
#可选2:最小化安装-适合apache平台(先安装httpd,--with-apxs2指定httpd安装位置)
#./configure --prefix=/usr/local/php --with-config-file-scan-dir=/usr/local/php/etc/ --with-apxs2=/usr/sbin/apxs

PHP7.4.30的编译扩展有些更新,参考:https://www.php.net/manual/zh/migration74.other-changes.php

编译时遇到的一些问题

oniguruma

因为扩展指定了--enable-mbstring需要用到oniguruma包,如果用不到可以禁用这个扩展--disable-mbregex

下载地址:https://pkgs.org/download/oniguruma  和  https://pkgs.org/download/oniguruma-devel

安装

rpm -i oniguruma-devel-6.8.2-2.el7.x86_64.rpm
rpm -i oniguruma-6.8.2-2.el7.x86_64.rpm

libzip

继续编译PHP,提示libzip >= 0.11因为系统镜像里的版本是0.10,需要单独安装,这里下载1.2.0,因为高版本需要cmake。

下载地址:https://libzip.org/download/libzip-1.2.0.tar.gz

安装

tar xvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

 执行完后并没有被系统识别,相当于Windows系统的环境变量一样,让系统识别还需要配置PKG_CONFIG_PATH,用pkg-config查看libzip.pc,发现没有输出,find查找一下,默认位置在/usr/local/lib/pkgconfig/libzip.pc然后建立链接。

[root@xcc ~]# pkg-config --list-all|grep libzip
[root@xcc ~]# find / -name libzip.pc
/root/libzip-1.2.0/libzip.pc
/usr/local/lib/pkgconfig/libzip.pc
[root@xcc ~]# ln -sf /usr/local/lib/pkgconfig/libzip.pc /usr/lib64/pkgconfig/
[root@xcc ~]# pkg-config --list-all|grep libzip
libzip            libzip - library for handling zip archives

 继续编译PHP,这里已经可以正常编译通过。

继续安装

make 

make install

配置文件

从安装包里把配置文件拷贝到安装目录并重命名,配置文件有两个:生产环境php.ini-production和开发环境php.ini-development

[root@xcc php-7.4.30]# cp php.ini-* /usr/local/php/etc/
[root@xcc php-7.4.30]# cd /usr/local/php/etc/
[root@xcc etc]# cp php.ini-development php.ini
[root@xcc etc]# ls
php-fpm.conf.default  php-fpm.d  php.ini  php.ini-development  php.ini-production

配置系统环境变量

编辑文件vi /etc/profile在末尾添加

PATH=/usr/local/php/bin:$PATH
export PATH

使其立即生效source /etc/profile

查看版本

[root@xcc ~]# php -v
PHP 7.4.30 (cli) (built: Nov  2 2022 17:32:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

查看扩展

[root@xcc ~]# php -m
[PHP Modules]
bcmath
bz2
Core
ctype
curl
date
dom
filter
gd
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
zip

[Zend Modules]

Nginx

下载地址:http://nginx.org/en/download.html

安装

tar zxvf nginx-1.23.2.tar.gz
cd nginx-1.23.2
./configure
make && make install

查看版本

cd /usr/local/nginx/sbin
./nginx -v
nginx version: nginx/1.23.2
./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动

cd /usr/local/nginx/sbin      #进入目录
./nginx                       #启动
./nginx -s stop               #关闭
./nginx -s reload             #重启

防火墙

#防火墙开放端口
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload             #防火墙随系统开启启动
#附:
systemctl status firewalld        #查看防火墙状态
systemctl stop firewalld          #关闭防火墙
systemctl start firewalld         #开启防火墙
firewall-cmd --reload             #防火墙重启
systemctl disable firewalld       #禁止防火墙自启动
systemctl enable firewalld        #防火墙随系统开启启动

浏览器访问

配置PHP

启动php-fpm,PHP默认安装在/usr/local/php

/usr/local/php/sbin/php-fpm

 修改nginx支持PHP,vi /usr/local/nginx/conf/nginx.conf把server里这几行注释去掉,同时需要修改fastcgi_param

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
修改为:
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

重启nginx:/usr/local/nginx/sbin/nginx -s reload

/usr/local/nginx/html目录下创建phpinfo.php文件,输入

<?php 
 phpinfo();
?>

访问  http://ip地址/phpinfo

配置nginx服务

进入目录/usr/lib/systemd/system创建新服务文件,名称为:nginx.service,内容如下

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存后,重新加载一下daemon,就可以用systemctl启动或停止nginx服务了。

#重新加载daemon
systemctl daemon-reload
#启动nginx
systemctl start nginx.service
#查看nginx状态
systemctl status nginx.service
#停止nginx
systemctl stop nginx.service
#加入开机启动
systemctl enable nginx.service
#查看nginx是否为开机启动
systemctl is-enabled nginx

end。

posted @ 2022-11-02 18:27  xjournal  阅读(3185)  评论(0编辑  收藏  举报