参考
Centos8(Liunx) 中安装PHP7.4 的三种方法和删除它的三种方法
Linux下使用 autoconf 命令提示command not found
CentOS 7 编译安装 PHP 7.4 提示 No package oniguruma found
LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)
centos7版本选择
一般用minimal版本,比较干净
http://isoredirect.centos.org/centos/7/isos/x86_64/
下载最新版本iso
选择文件名形如:CentOS-7-x86_64-Minimal-{小版本号,目前是2009}.iso
/usr/local下建立services目录放服务软件,如nginx, php, mysql, redis ...
cd /usr/local/
mkdir services
安装nginx
先安装依赖环境
gcc环境:基本运行环境
pcre:用于nginx的http模块解析正则表达式
zlib:用户进行gzip压缩
openssl:用于nginx https协议的传输
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
http://nginx.org/en/download.html
下载最新版本nginx源码文件(tar.gz格式)
目前是1.19.9
安装包一般放在/opt目录
cd /opt/ wget http://nginx.org/download/nginx-1.19.9.tar.gz
解压编译安装
–prefix=/usr/local/services/nginx 的意思是把nginx安装到/usr/local/services/nginx
cd /opt/
tar -xzvf nginx-1.19.9.tar.gz
cd nginx-1.19.9
./configure --prefix=/usr/local/services/nginx
make && make install
cd /usr/local/services/nginx/
启动Nginx并测试
/usr/local/services/nginx/sbin/nginx curl http://127.0.0.1
也可以通过浏览器输入网址测试
如果需要其他电脑访问,需要开启防火墙的80端口:
firewall-cmd --zone=public --add-port=80/tcp
firewall-cmd --zone=public --add-port=80/tcp --permanent
--permanent表示永久生效
查看配置结果,验证配置
firewall-cmd --list-all
安装php
https://www.php.net/downloads.php
下载最新php版本的tar.gz格式(目前是8.0.3)并解压
cd /opt/ wget https://www.php.net/distributions/php-8.0.3.tar.gz
tar -xzvf php-8.0.3.tar.gz
cd /opt/php-8.0.3
安装php的依赖库和安装编译的工具,无论有没有装都可以执行一下
yum -y install gcc gcc-c++ yum -y install libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel openldap-devel openldap openldap-devel
yum -y install libtool
创建用户组和用户
groupadd www useradd -g www www
配置
这里因为准备装多个php所以设置
--prefix=/usr/local/services/php803 区分不同版本目录
./configure --prefix=/usr/local/services/php803 --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --with-mcrypt --with-libmbfl --enable-ftp --with-gd --enable-gd-jis-conv --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm
报错解决参考:
https://www.cnblogs.com/alliancehacker/p/12255445.html
oniguruma装不上参考(上面一篇文章对oniguruma的解决方案似乎不行)
https://cyhour.com/1627/
autoconf 命令提示command not found参考
https://blog.csdn.net/qq_42324086/article/details/100055434
安装
make make install
验证
/usr/local/services/php803/bin/php -v PHP 8.0.3 (cli) (built: Apr 9 2021 05:20:23) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies
连接nginx和php-fpm
先复制php-fpm的两个配置文件
cd /usr/local/services/php803/etc/ cp php-fpm.conf.default php-fpm.conf cd /usr/local/services/php803/etc/php-fpm.d cp www.conf.default www.conf
启动php-fpm
/usr/local/services/php803/sbin/php-fpm
默认监听9000端口,如果装一个新版本的php要改这个端口号
配置nginx站点
vi /usr/local/services/nginx/conf/nginx.conf
最后个大括号前添加:
include /usr/local/services/nginx/conf/vhosts/*.conf;
创建vhosts目录
cd /usr/local/services/nginx/conf/ mkdir vhosts cd vhosts vi php803.conf
输入
server { listen 80; server_name php803.140.dev; charset utf-8; root /var/www/test/php803; location ~ .php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_connect_timeout 10; fastcgi_send_timeout 60; fastcgi_read_timeout 60; fastcgi_buffers 256 4k; fastcgi_param SCRIPT_FILENAME /var/www/test/php803$fastcgi_script_name; include fastcgi_params; } }
创建/var/www/test/php803目录,测试phpinfo
mkdir -p /var/www/test/php803
cd /var/www/test/php803
vi info.php
输入
<?php phpinfo();
配置hosts文件
192.168.56.140 php803.140.dev
重启nginx
/usr/local/services/nginx/sbin/nginx -s reload
浏览器访问
http://php803.140.dev/info.php
安装php扩展
试着装个redis扩展
查找最新扩展安装包,注意兼容的php版本。
比如redis扩展就在
http://pecl.php.net/package/redis
下面(现在最新版本为5.3.4)
cd /opt/ wget http://pecl.php.net/get/redis-5.3.4.tgz tar -xzvf redis-5.3.4.tgz cd redis-5.3.4 /usr/local/services/php803/bin/phpize ./configure --with-php-config=/usr/local/services/php803/bin/php-config make && make install
注意:如果要升级php扩展重复上述过程即可。
显示
Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/services/php803/lib/php/extensions/no-debug-non-zts-20200930/
注意这个目标目录也在/usr/local/services/php803/下面
从安装目录复制开发版php.ini并修改
cp /opt/php-8.0.3/php.ini-development /usr/local/services/php803/lib/php.ini vi /usr/local/services/php803/lib/php.ini
查找extension=
该模块最后添加
extension=redis
重启php-fpm
刷新phpinfo
可以看到redis扩展信息
安装一个新版本的php并配置nginx
重复上述所有过程
注意
新版本php安装目录名称要和原版本不一样
新版本php-fpm监听端口要和原版本不一样(在php安装目录/etc/php-fpm.dwww.conf里)
nginx调用新版本php-fpm端口要在配置里修改对应php-fpm端口
新版本php扩展安装要选对新版本的php路径,如果安装文件目录phpize且configure过别的版本,需要删除原安装目录重新解压,否则会冲突。
其他
建立符号链接快捷方式
ln -s /usr/local/services/php803/bin/php /usr/local/bin/php ln -s /usr/local/services/nginx/sbin/nginx /usr/local/bin/nginx ln -s /usr/local/services/php803/bin/php /usr/local/bin/php803 ln -s /usr/local/services/php7416/bin/php /usr/local/bin/php7416 ln -s /usr/local/services/php803/sbin/php-fpm /usr/local/bin/php-fpm ln -s /usr/local/services/php803/sbin/php-fpm /usr/local/bin/php803-fpm ln -s /usr/local/services/php7416/sbin/php-fpm /usr/local/bin/php7416-fpm
测试
[root@localhost bin]# php -v PHP 8.0.3 (cli) (built: Apr 9 2021 05:20:23) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies [root@localhost bin]# php803 -v PHP 8.0.3 (cli) (built: Apr 9 2021 05:20:23) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies [root@localhost bin]# php7416 -v PHP 7.4.16 (cli) (built: Apr 9 2021 06:53:03) ( ZTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies [root@localhost bin]# php-fpm [root@localhost bin]# php803-fpm [13-Apr-2021 01:58:03] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98) [13-Apr-2021 01:58:03] ERROR: FPM initialization failed [root@localhost bin]# php7416-fpm [root@localhost bin]# php7416-fpm [13-Apr-2021 01:59:29] ERROR: unable to bind listening socket for address '127.0.0.1:9001': Address already in use (98) [13-Apr-2021 01:59:29] ERROR: FPM initialization failed
设置开机自启动
vi /etc/rc.local
添加
php-fpm php7416-fpm nginx
不要忘了给rc.local添加执行权限
chmod +x /etc/rc.d/rc.local
重启测试成功
over~~~