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
相关推荐
htzyl2061 小时前
前端样式-弹窗淡入淡出,为什么你的 `transition` 一直不生效
前端·css·html
Brookty1 小时前
CSS基础知识(二):盒模型、内外边距、外边距折叠与父子元素的布局关系
前端·css
十有八七1 小时前
手搓 Agent(四):长点记性——上下文满了怎么办,怎么记住上一次
前端·人工智能·后端
叫个啥呢3 小时前
第一次写 DSH 插件:跟着自己的喜好实现个简单的代码管理插件
前端
掘金酱3 小时前
AI 带来的新生意|稀土掘金 X-Lab 年度大会招募开启
前端·后端·vibecoding
某哈压力大3 小时前
移动端 rem 适配
前端·vue.js·面试
瑞雪兆丰年兮3 小时前
Linux零基础快速入门-第二章 Linux基础命令
linux·运维·服务器
泯泷3 小时前
Vercel AI SDK 6 完整教程系列 - 第一部分:基础入门篇
前端·javascript
lcy_243 小时前
10 万条数据滚动不卡?聊聊我写的虚拟列表组件 vue-virtualize
前端