CTF-WEB: 配置一个Ubuntu 多版本php服务器

今天复现题windows的php /tmp缓存一直是空的,直接配一个新虚拟机

开始

准备一个全新的虚拟机,可以在这里下载镜像

Verifying - USTC Mirrors

走完常规安装流程继续

设置中文(可选)

复制代码
sudo apt install language-pack-zh-hans language-pack-gnome-zh-hans

然后在设置->语言中选择chinese

安装 zsh(可选)

复制代码
sudo apt install zsh -y

chsh -s $(which zsh)

exec zsh

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses --- 2

安装Apache Web服务器

bash 复制代码
sudo apt install apache2

添加PHP的PPA源

为安装多个PHP版本,添加Ondřej Surý的PPA仓库:

bash 复制代码
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

安装PHP 7和PHP 8

安装所需的PHP版本及其模块:

bash 复制代码
sudo apt install php7.4 libapache2-mod-php7.4
sudo apt install php8.0 libapache2-mod-php8.0

如果需要其他扩展,例如MySQL支持,可以安装:

bash 复制代码
sudo apt install php7.4-mysql php8.0-mysql

配置Apache以切换PHP版本

Apache使用模块来处理PHP。要在不同的PHP版本之间切换,需要启用或禁用相应的模块。

禁用所有PHP模块(确保干净的开始):

bash 复制代码
sudo a2dismod php7.4
sudo a2dismod php8.0

启用所需的PHP版本模块:

  • 切换到PHP 7.4:

    bash 复制代码
    sudo a2enmod php7.4
    sudo a2dismod php8.0
    sudo systemctl restart apache2
  • 切换到PHP 8.0:

    bash 复制代码
    sudo a2enmod php8.0
    sudo a2dismod php7.4
    sudo systemctl restart apache2

每次更改后,重启Apache以应用更改。

验证当前的PHP版本

创建一个info.php文件以查看当前正在运行的PHP版本:

bash 复制代码
sudo nano /var/www/html/info.php

在文件中添加:

php 复制代码
<?php
phpinfo();
?>

保存并退出,然后在浏览器中访问:

复制代码
http://你的服务器IP地址/info.php

您将看到PHP的信息页面,其中显示了当前的PHP版本。

切换CLI中的PHP版本(可选)

如果需要在命令行界面(CLI)中也切换PHP版本,可以使用update-alternatives

bash 复制代码
sudo update-alternatives --set php /usr/bin/php7.4

bash 复制代码
sudo update-alternatives --set php /usr/bin/php8.0

或者使用交互式命令进行切换:

bash 复制代码
sudo update-alternatives --config php

然后按照提示选择所需的PHP版本。

使用PHP-FPM(可选)

如果您希望使用PHP-FPM,可以安装并配置:

安装PHP-FPM:

bash 复制代码
sudo apt install php7.4-fpm php8.0-fpm

配置Apache使用PHP-FPM:

启用必要的模块:

bash 复制代码
sudo a2enmod proxy_fcgi setenvif

禁用传统的PHP模块:

bash 复制代码
sudo a2dismod php7.4 php8.0

切换到PHP 7.4 FPM:

bash 复制代码
sudo a2enconf php7.4-fpm
sudo a2disconf php8.0-fpm
sudo systemctl reload apache2

切换到PHP 8.0 FPM:

bash 复制代码
sudo a2enconf php8.0-fpm
sudo a2disconf php7.4-fpm
sudo systemctl reload apache2

踩坑

如果info.php文件无回显,这是因为你使用了root创建了文件,这会导致阿帕奇无权限读取

复制代码
sudo chmod 644 /var/www/html/info.php
相关推荐
ℋᙚᵐⁱᒻᵉ鲸落3 小时前
移动端滑动手势冲突:overflow: auto导致外层横向滑动失效
前端·javascript·css·vue.js·html
Brilliantwxx3 小时前
【Linux】 进程(9)程序与进程地址空间(基础+进阶+面试题)
linux·运维·服务器·开发语言·c++
不怕犯错,就怕不做4 小时前
git prune 自动删除本地记录中那些远程已经不存在的分支引用
linux·服务器·git
默_笙4 小时前
🎃 前端学了 Next.js,后端该学啥?NestJS 就是 Node 版的蜜雪冰城
前端·javascript
weixin_416667965 小时前
【无标题】
运维·服务器·网络
前端snow5 小时前
ai agent --- 实现 openclaw 定时间效果
前端
码云之上6 小时前
换模型之后,Chatbot 为什么要自己做 compact?
前端·agent·前端工程化
星栈6 小时前
自定义 UI-UX-Pro-Max 的 CSV 知识库,把 AI 生成的后台拉回行业该有的样子
前端·agent·weui
研☆香6 小时前
简单的图片上传 删除 预览
前端
ITresearchGuest6 小时前
我用 AI 一小时写了一个世界杯数据可视化平台|前端 VibeCoding 初体验
前端·人工智能·信息可视化