Ubuntu 22.04 中的 PHP、Python 和 Node.js 开发环境
一、PHP 开发环境
1. 安装 LAMP 平台(Linux + Apache + MySQL + PHP)
LAMP 是在 Linux 上搭建 Web 应用的经典组合。
安装步骤:
bash
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装 Apache
sudo apt install apache2 -y
# 安装 MySQL
sudo apt install mysql-server -y
sudo mysql_secure_installation # 按提示设置 root 密码等安全选项
# 安装 PHP 及常用扩展
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
# 重启 Apache
sudo systemctl restart apache2
# 验证 PHP 是否正常工作
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
访问 http://localhost/info.php 查看 PHP 信息页面。
2. PHP 集成开发工具简介
常见工具包括:
- Eclipse PDT(PHP Development Tools)
- VS Code + PHP Intelephense
- PhpStorm(商业)
3. 安装 Eclipse IDE for PHP
步骤:
bash
# 安装 Java(Eclipse 依赖)
sudo apt install default-jre -y
# 下载 Eclipse for PHP Developers
wget https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/2024-09/R/eclipse-php-2024-09-R-linux-gtk-x86_64.tar.gz -O eclipse-php.tar.gz
# 解压
tar -xvzf eclipse-php.tar.gz
sudo mv eclipse /opt/
# 创建桌面快捷方式(可选)
cat <<EOF | sudo tee /usr/share/applications/eclipse.desktop
[Desktop Entry]
Name=Eclipse PHP
Exec=/opt/eclipse/eclipse
Icon=/opt/eclipse/icon.xpm
Type=Application
Categories=Development;IDE;
EOF
启动:/opt/eclipse/eclipse
4. 使用 Eclipse IDE for PHP 开发 PHP 程序
示例:创建一个简单的用户注册表单处理程序
php
<?php
// register.php
// 接收 POST 请求并验证用户名与邮箱格式
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
// 基本验证
if (empty($username) || empty($email)) {
echo "用户名和邮箱不能为空!";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "邮箱格式不正确!";
exit;
}
// 模拟保存到数据库(此处仅打印)
echo "注册成功!用户名: $username, 邮箱: $email";
} else {
// 显示注册表单
?>
<!DOCTYPE html>
<html>
<head><title>用户注册</title></head>
<body>
<form method="post" action="">
用户名: <input type="text" name="username" required><br>
邮箱: <input type="email" name="email" required><br>
<button type="submit">注册</button>
</form>
</body>
</html>
<?php
}
?>
将文件保存为
/var/www/html/register.php,通过浏览器访问测试。
5. 部署 PHP 调试环境(Xdebug)
bash
# 安装 Xdebug
sudo apt install php-xdebug -y
# 编辑配置文件
sudo nano /etc/php/8.1/apache2/conf.d/20-xdebug.ini
添加以下内容(根据 PHP 版本调整路径):
ini
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
重启 Apache:
bash
sudo systemctl restart apache2
在 Eclipse 中配置 Xdebug 监听器(端口 9003),即可进行断点调试。
二、Python 集成开发环境
1. Python 简介
Python 是一种高级、解释型、通用编程语言,支持面向对象、函数式和过程式编程范式。
2. 安装 Python
Ubuntu 22.04 默认已安装 Python 3.10,但建议确认并升级:
bash
python3 --version
sudo apt update
sudo apt install python3 python3-pip python3-venv -y
3. 虚拟环境和包管理
创建虚拟环境:
bash
mkdir myproject && cd myproject
python3 -m venv venv
source venv/bin/activate # 激活
pip install --upgrade pip
安装包示例:
bash
pip install flask requests
pip freeze > requirements.txt # 导出依赖
退出虚拟环境:deactivate
4. 安装 Python 集成开发环境(以 PyCharm Community 为例)
bash
# 安装 snap(若未安装)
sudo apt install snapd -y
# 安装 PyCharm Community
sudo snap install pycharm-community --classic
启动:pycharm-community
5. 使用 PyCharm 开发 Python 应用程序
案例:Flask Web 应用
python
# app.py
from flask import Flask, request, jsonify
app = Flask(__name__)
# 路由:返回欢迎信息
@app.route('/')
def home():
return "<h1>欢迎使用 Flask!</h1>"
# API 示例:接收 JSON 数据并返回处理结果
@app.route('/api/greet', methods=['POST'])
def greet():
data = request.get_json() # 获取 JSON 请求体
name = data.get('name', '匿名用户')
message = f"你好,{name}!"
return jsonify({"message": message})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
在 PyCharm 中运行此脚本,访问
http://localhost:5000或发送 POST 请求测试 API:
bash
curl -X POST http://localhost:5000/api/greet -H "Content-Type: application/json" -d '{"name":"张三"}'
三、Node.js 开发环境
1. Node.js 简介
Node.js 是基于 Chrome V8 引擎的 JavaScript 运行时,用于构建高性能网络应用,支持非阻塞 I/O。
2. 在 Ubuntu 系统上安装 Node.js
推荐使用 NodeSource 仓库安装 LTS 版本:
bash
# 添加 NodeSource 仓库(以 20.x LTS 为例)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# 安装 Node.js 和 npm
sudo apt install -y nodejs
# 验证安装
node -v # 输出如 v20.13.1
npm -v # 输出如 10.7.0
3. 安装 Node.js 集成开发环境
推荐使用 VS Code(轻量且插件丰富):
bash
sudo snap install code --classic
安装插件:
- ESLint
- Prettier
- Node.js Extension Pack
4. 开发 Node.js 应用程序
案例:使用 Express 构建 RESTful API
javascript
// server.js
const express = require('express');
const app = express();
// 中间件:解析 JSON 请求体
app.use(express.json());
// 根路由
app.get('/', (req, res) => {
res.send('<h1>欢迎使用 Node.js + Express!</h1>');
});
// 模拟用户数据
let users = [
{ id: 1, name: '李四' },
{ id: 2, name: '王五' }
];
// GET /api/users --- 获取所有用户
app.get('/api/users', (req, res) => {
res.json(users);
});
// POST /api/users --- 创建新用户
app.post('/api/users', (req, res) => {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: '姓名不能为空' });
}
const newUser = {
id: users.length + 1,
name: name
};
users.push(newUser);
res.status(201).json(newUser);
});
// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`服务器运行在 http://localhost:${PORT}`);
});
初始化项目并安装依赖:
bash
mkdir node-app && cd node-app
npm init -y
npm install express
node server.js
测试 API:
bash
# 获取用户列表
curl http://localhost:3000/api/users
# 创建新用户
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"name":"赵六"}'
5. 调试 Node.js 应用程序
方法一:使用 VS Code 内置调试器
- 在项目根目录创建
.vscode/launch.json:
json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动 server.js",
"program": "${workspaceFolder}/server.js",
"console": "integratedTerminal"
}
]
}
- 在代码中设置断点,按 F5 启动调试。
方法二:使用 --inspect 模式
bash
node --inspect=0.0.0.0:9229 server.js
然后在 Chrome 访问 chrome://inspect,点击"Open dedicated DevTools for Node"进行调试。
总结
| 技术栈 | 关键组件 | 开发工具 | 调试方式 |
|---|---|---|---|
| PHP | Apache + PHP + MySQL | Eclipse PDT / VS Code | Xdebug |
| Python | Python + Flask | PyCharm / VS Code | pdb / PyCharm Debugger |
| Node.js | Node.js + Express | VS Code | Chrome DevTools / VS Code Debugger |
以上内容覆盖了 Ubuntu 22.04 下三种主流后端语言的完整开发环境搭建、基础语法应用及调试方法,并提供可直接运行的案例代码,适合教学或实际项目参考。