活动公告

系统通知
05-18 21:22
系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

Kubuntu系统开发环境搭建完全指南从系统安装基础配置到高级优化详解各类编程语言环境搭建数据库服务器部署IDE配置版本控制集成及团队协作工具设置助你打造高效开发平台

SunJu_FaceMall

3万

主题

2860

科技点

3万

积分

白金月票

碾压王

积分
32872

塔罗立华奏

<font color=白金月票" /> 发表于 2025-9-12 01:40:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1. Kubuntu系统安装

1.1 下载与准备

Kubuntu是Ubuntu的KDE Plasma桌面环境版本,它提供了美观且功能丰富的用户界面,非常适合开发者使用。首先,我们需要从官方网站获取Kubuntu的最新稳定版本。

访问Kubuntu官网下载适合你系统的版本。目前,Kubuntu 22.04 LTS是最新的长期支持版本,推荐开发者使用,因为它提供五年的安全更新支持。

下载完成后,你需要创建一个启动U盘:
  1. # 确认U盘设备名称(假设为/dev/sdb)
  2. lsblk
  3. # 使用dd命令将ISO写入U盘(注意:替换为实际的ISO文件路径和U盘设备名)
  4. sudo dd if=kubuntu-22.04-desktop-amd64.iso of=/dev/sdb bs=4M status=progress
  5. sync
复制代码

或者,你可以使用图形化工具如Etcher或Ventoy来创建启动U盘,这些工具提供了更友好的用户界面。

1.2 安装过程详解

启动U盘准备就绪后,按照以下步骤进行安装:

1. 将U盘插入电脑,重启并进入BIOS/UEFI设置,将启动顺序设置为优先从U盘启动。
2. 保存设置并重启,你会看到Kubuntu的启动菜单。选择”Try Kubuntu”进入试用模式或直接选择”Install Kubuntu”进行安装。
3. 在安装向导中,选择语言、键盘布局。
4. 在”安装类型”界面,你可以选择:清除整个磁盘并安装Kubuntu(适合新手)与其他操作系统共存(双系统)手动分区(适合高级用户)
5. 清除整个磁盘并安装Kubuntu(适合新手)
6. 与其他操作系统共存(双系统)
7. 手动分区(适合高级用户)

• 清除整个磁盘并安装Kubuntu(适合新手)
• 与其他操作系统共存(双系统)
• 手动分区(适合高级用户)

对于开发者,建议使用手动分区,这样可以更好地管理磁盘空间。推荐的分区方案:
  1. / (根分区)      50-100 GB  ext4
  2. /home (家目录)  剩余空间   ext4
  3. swap (交换空间)  内存大小   swap
复制代码

1. 设置时区、用户名和密码。建议创建一个具有管理员权限的用户账户。
2. 等待安装完成,重启系统。

1.3 安装后初次配置

系统首次启动后,进行以下基本配置:
  1. # 更新系统
  2. sudo apt update
  3. sudo apt upgrade -y
  4. # 安装常用软件
  5. sudo apt install -y curl wget git htop vim neofetch
  6. # 检查系统信息
  7. neofetch
复制代码

接下来,配置系统的一些基本设置:

1. 系统设置:打开”系统设置”,调整显示、主题、桌面行为等选项。配置网络连接,确保可以访问互联网。
2. 打开”系统设置”,调整显示、主题、桌面行为等选项。
3. 配置网络连接,确保可以访问互联网。
4. 软件源配置:
“`bash备份原始源列表sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

系统设置:

• 打开”系统设置”,调整显示、主题、桌面行为等选项。
• 配置网络连接,确保可以访问互联网。

软件源配置:
“`bash

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 使用文本编辑器修改源列表(使用国内镜像可提高下载速度)
   sudo nano /etc/apt/sources.list
  1. 将源列表替换为国内镜像,例如阿里云镜像:
复制代码

debhttp://mirrors.aliyun.com/ubuntu/jammy main restricted universe multiverse
   debhttp://mirrors.aliyun.com/ubuntu/jammy-security main restricted universe multiverse
   debhttp://mirrors.aliyun.com/ubuntu/jammy-updates main restricted universe multiverse
   debhttp://mirrors.aliyun.com/ubuntu/jammy-proposed main restricted universe multiverse
   debhttp://mirrors.aliyun.com/ubuntu/jammy-backports main restricted universe multiverse
   deb-srchttp://mirrors.aliyun.com/ubuntu/jammy main restricted universe multiverse
   deb-srchttp://mirrors.aliyun.com/ubuntu/jammy-security main restricted universe multiverse
   deb-srchttp://mirrors.aliyun.com/ubuntu/jammy-updates main restricted universe multiverse
   deb-srchttp://mirrors.aliyun.com/ubuntu/jammy-proposed main restricted universe multiverse
   deb-srchttp://mirrors.aliyun.com/ubuntu/jammy-backports main restricted universe multiverse
  1. 更新软件包列表:
  2.    ```bash
  3.    sudo apt update
复制代码

2. 系统基础配置

2.1 系统更新与源配置

保持系统更新是确保安全性和稳定性的关键步骤。除了前面提到的基本更新外,还可以配置自动更新:
  1. # 安装自动更新工具
  2. sudo apt install -y unattended-upgrades
  3. # 配置自动更新
  4. sudo dpkg-reconfigure unattended-upgrades
复制代码

在配置过程中,你可以选择自动下载并安装安全更新。

此外,添加一些有用的PPA(Personal Package Archive)可以让你获得更多最新软件:
  1. # 添加常用PPA
  2. sudo add-apt-repository ppa:kubuntu-ppa/backports -y
  3. sudo add-apt-repository ppa:deadsnakes/ppa -y  # Python PPA
  4. sudo add-apt-repository ppa:ondrej/php -y     # PHP PPA
  5. sudo add-apt-repository ppa:ondrej/nodejs -y  # Node.js PPA
  6. # 更新软件包列表
  7. sudo apt update
复制代码

2.2 驱动安装与硬件优化

Kubuntu通常能够自动检测并安装大多数硬件的驱动程序,但对于某些特定的硬件(尤其是NVIDIA显卡),可能需要手动安装驱动:
  1. # 检查可用驱动
  2. ubuntu-drivers list
  3. # 安装推荐的驱动(例如NVIDIA显卡)
  4. sudo ubuntu-drivers autoinstall
  5. # 或者安装特定版本的驱动
  6. sudo apt install nvidia-driver-530
复制代码

对于笔记本电脑,还可以安装一些工具来优化电池使用和硬件控制:
  1. # 安装TLP(电源管理工具)
  2. sudo apt install -y tlp tlp-rdw
  3. sudo tlp start
  4. # 安装CPU频率调节工具
  5. sudo apt install -y indicator-cpufreq
复制代码

2.3 基本工具安装

开发者需要一些基本工具来提高工作效率:
  1. # 安装基本开发工具
  2. sudo apt install -y build-essential cmake
  3. # 安装压缩工具
  4. sudo apt install -y rar unrar zip unzip p7zip-full
  5. # 安装网络工具
  6. sudo apt install -y net-tools openssh-server sshpass nmap
  7. # 安装系统监控工具
  8. sudo apt install -y htop iotop iftop nmon
  9. # 安装文件同步工具
  10. sudo apt install -y rsync grsync
  11. # 安装终端复用器
  12. sudo apt install -y tmux byobu screen
  13. # 安装Docker(后续会详细配置)
  14. sudo apt install -y docker.io
复制代码

2.4 系统美化与个性化

Kubuntu的KDE Plasma桌面环境本身就非常美观,但你可以进一步个性化它:
  1. # 安装额外的主题和图标
  2. sudo apt install -y plasma-widgets-addons kde-plasma-wallpapers
  3. sudo apt install -y papirus-icon-theme
  4. # 安装系统美化工具
  5. sudo apt install -y kde-config-gtk-style-preview
复制代码

此外,你还可以从KDE Store获取更多的小部件、主题和插件。访问KDE Store浏览并下载你喜欢的美化资源。

3. 系统高级优化

3.1 性能优化设置

针对开发工作负载,可以进行一些性能优化:
  1. # 创建新的sysctl配置文件
  2. sudo nano /etc/sysctl.d/99-sysctl.conf
  3. # 添加以下内容以优化系统性能
  4. vm.swappiness=10
  5. vm.vfs_cache_pressure=50
  6. net.core.rmem_max = 16777216
  7. net.core.wmem_max = 16777216
  8. net.ipv4.tcp_rmem = 4096 87380 16777216
  9. net.ipv4.tcp_wmem = 4096 65536 16777216
复制代码

应用这些设置:
  1. sudo sysctl -p /etc/sysctl.d/99-sysctl.conf
复制代码

对于SSD用户,可以进行以下优化:
  1. # 检查是否为SSD
  2. lsblk -d -o name,rota
  3. # 如果是SSD,创建fstrim定时任务
  4. sudo systemctl enable fstrim.timer
  5. sudo systemctl start fstrim.timer
复制代码

3.2 内核参数调优

对于需要更高性能的开发者,可以考虑使用低延迟内核或进行内核参数调优:
  1. # 安装低延迟内核(可选)
  2. sudo apt install -y linux-lowlatency
  3. # 查看当前内核参数
  4. sysctl -a
  5. # 修改内核参数(例如增加文件描述符限制)
  6. echo "fs.file-max = 100000" | sudo tee -a /etc/sysctl.conf
  7. echo "* soft nofile 100000" | sudo tee -a /etc/security/limits.conf
  8. echo "* hard nofile 100000" | sudo tee -a /etc/security/limits.conf
复制代码

3.3 系统安全加固

开发环境也需要适当的安全措施:
  1. # 安装防火墙并配置
  2. sudo apt install -y ufw
  3. sudo ufw default deny incoming
  4. sudo ufw default allow outgoing
  5. sudo ufw allow ssh
  6. sudo ufw enable
  7. # 安装fail2ban防止暴力破解
  8. sudo apt install -y fail2ban
  9. sudo systemctl enable fail2ban
  10. sudo systemctl start fail2ban
  11. # 配置自动安全更新
  12. sudo dpkg-reconfigure -plow unattended-upgrades
复制代码

3.4 系统监控与维护工具

安装一些系统监控工具,帮助你了解系统状态:
  1. # 安装系统监控工具
  2. sudo apt install -y glances ksysguard
  3. # 安装日志分析工具
  4. sudo apt install -y goaccess multitail
  5. # 安装系统清理工具
  6. sudo apt install -y bleachbit
复制代码

Glances是一个功能强大的系统监控工具,可以通过以下方式安装和使用:
  1. # 安装Glances
  2. sudo apt install -y glances
  3. # 启动Glances
  4. glances
复制代码

4. 编程语言环境搭建

4.1 Python环境配置

Python是开发中最常用的语言之一,Kubuntu默认安装了Python 3,但我们需要配置完整的开发环境:
  1. # 安装Python和pip
  2. sudo apt install -y python3 python3-pip python3-venv python3-dev
  3. # 安装pyenv(Python版本管理工具)
  4. curl https://pyenv.run | bash
  5. # 配置环境变量
  6. echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
  7. echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
  8. echo 'eval "$(pyenv init -)"' >> ~/.bashrc
  9. # 重启shell或执行
  10. source ~/.bashrc
  11. # 安装特定Python版本
  12. pyenv install 3.11.4
  13. pyenv global 3.11.4
  14. # 验证Python版本
  15. python --version
  16. # 升级pip
  17. python -m pip install --upgrade pip
  18. # 安装常用Python包
  19. python -m pip install --user numpy pandas matplotlib requests jupyterlab
复制代码

创建虚拟环境:
  1. # 创建虚拟环境
  2. python -m venv myproject_env
  3. # 激活虚拟环境
  4. source myproject_env/bin/activate
  5. # 在虚拟环境中安装包
  6. pip install django flask
  7. # 退出虚拟环境
  8. deactivate
复制代码

4.2 Java开发环境

Java在企业级开发中仍然占据重要地位,以下是Java开发环境的配置:
  1. # 安装默认JDK(OpenJDK 11)
  2. sudo apt install -y default-jdk
  3. # 或者安装特定版本的OpenJDK
  4. sudo apt install -y openjdk-8-jdk
  5. sudo apt install -y openjdk-11-jdk
  6. sudo apt install -y openjdk-17-jdk
  7. # 安装SDKMAN(Java开发工具管理器)
  8. curl -s "https://get.sdkman.io" | bash
  9. source "$HOME/.sdkman/bin/sdkman-init.sh"
  10. # 使用SDKMAN安装Java
  11. sdk install java 11.0.17-tem
  12. sdk install java 17.0.5-tem
  13. # 切换Java版本
  14. sdk use java 11.0.17-tem
  15. # 安装Maven和Gradle
  16. sudo apt install -y maven gradle
  17. # 或使用SDKMAN安装
  18. sdk install maven
  19. sdk install gradle
  20. # 验证安装
  21. java -version
  22. mvn -version
  23. gradle -version
复制代码

配置环境变量:
  1. # 设置JAVA_HOME
  2. echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
  3. source ~/.bashrc
复制代码

4.3 C/C++开发环境

C/C++是系统编程和性能敏感应用的首选语言:
  1. # 安装GCC/G++编译器和相关工具
  2. sudo apt install -y build-essential gdb
  3. # 安装CMake(跨平台构建工具)
  4. sudo apt install -y cmake cmake-curses-gui
  5. # 安装额外的编译器和工具
  6. sudo apt install -y clang clang-format clang-tidy
  7. sudo apt install -y ninja-build
  8. # 安装Boost库
  9. sudo apt install -y libboost-all-dev
  10. # 安装其他常用库
  11. sudo apt install -y libssl-dev zlib1g-dev libcurl4-openssl-dev
复制代码

创建一个简单的C++项目示例:
  1. # 创建项目目录
  2. mkdir hello_cpp && cd hello_cpp
  3. # 创建主程序文件
  4. cat > main.cpp << EOF
  5. #include <iostream>
  6. int main() {
  7.     std::cout << "Hello, C++ World!" << std::endl;
  8.     return 0;
  9. }
  10. EOF
  11. # 创建CMakeLists.txt
  12. cat > CMakeLists.txt << EOF
  13. cmake_minimum_required(VERSION 3.10)
  14. project(HelloCpp)
  15. set(CMAKE_CXX_STANDARD 17)
  16. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  17. add_executable(hello_cpp main.cpp)
  18. EOF
  19. # 构建项目
  20. mkdir build && cd build
  21. cmake ..
  22. make
  23. # 运行程序
  24. ./hello_cpp
复制代码

4.4 JavaScript/Node.js环境

JavaScript/Node.js是Web开发的核心技术:
  1. # 使用NodeSource仓库安装最新的Node.js
  2. curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
  3. sudo apt-get install -y nodejs
  4. # 或者使用NVM(Node版本管理器)
  5. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  6. source ~/.bashrc
  7. # 安装特定版本的Node.js
  8. nvm install 18
  9. nvm use 18
  10. # 验证安装
  11. node -v
  12. npm -v
  13. # 安装Yarn包管理器
  14. npm install -g yarn
  15. # 安装全局常用包
  16. npm install -g npm-check-updates nodemon typescript ts-node @angular/cli create-react-app vue-cli
复制代码

创建一个简单的Node.js项目:
  1. # 创建项目目录
  2. mkdir hello_node && cd hello_node
  3. # 初始化项目
  4. npm init -y
  5. # 安装Express框架
  6. npm install express
  7. # 创建主程序文件
  8. cat > app.js << EOF
  9. const express = require('express');
  10. const app = express();
  11. const port = 3000;
  12. app.get('/', (req, res) => {
  13.   res.send('Hello, Node.js World!');
  14. });
  15. app.listen(port, () => {
  16.   console.log(\`App listening at http://localhost:\${port}\`);
  17. });
  18. EOF
  19. # 运行程序
  20. node app.js
复制代码

4.5 Go语言环境

Go是现代云原生开发的首选语言之一:
  1. # 下载并安装Go
  2. wget https://golang.org/dl/go1.20.5.linux-amd64.tar.gz
  3. sudo tar -C /usr/local -xzf go1.20.5.linux-amd64.tar.gz
  4. # 设置环境变量
  5. echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
  6. echo 'export GOPATH=$HOME/go' >> ~/.bashrc
  7. echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
  8. source ~/.bashrc
  9. # 验证安装
  10. go version
  11. # 创建工作目录
  12. mkdir -p $GOPATH/src/hello_go
  13. cd $GOPATH/src/hello_go
  14. # 创建Go程序
  15. cat > main.go << EOF
  16. package main
  17. import "fmt"
  18. func main() {
  19.     fmt.Println("Hello, Go World!")
  20. }
  21. EOF
  22. # 运行程序
  23. go run main.go
  24. # 构建程序
  25. go build
  26. ./hello_go
复制代码

4.6 Rust环境

Rust是系统编程的新兴选择,提供内存安全保证:
  1. # 安装Rust
  2. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  3. source ~/.cargo/env
  4. # 验证安装
  5. rustc --version
  6. cargo --version
  7. # 创建新项目
  8. cargo new hello_rust
  9. cd hello_rust
  10. # 运行项目
  11. cargo run
复制代码

4.7 PHP环境

PHP仍然是Web后端开发的重要语言:
  1. # 添加PPA
  2. sudo add-apt-repository ppa:ondrej/php -y
  3. sudo apt update
  4. # 安装PHP和常用扩展
  5. sudo apt install -y php8.2 php8.2-cli php8.2-common php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip
  6. sudo apt install -y php8.2-mysql php8.2-pgsql php8.2-sqlite3 php8.2-gd
  7. # 安装Composer(PHP包管理器)
  8. curl -sS https://getcomposer.org/installer | php
  9. sudo mv composer.phar /usr/local/bin/composer
  10. sudo chmod +x /usr/local/bin/composer
  11. # 验证安装
  12. php --version
  13. composer --version
  14. # 创建PHP项目
  15. mkdir hello_php && cd hello_php
  16. # 初始化Composer项目
  17. composer init
  18. # 创建index.php
  19. cat > index.php << EOF
  20. <?php
  21. echo "Hello, PHP World!\n";
  22. ?>
  23. EOF
  24. # 运行PHP内置服务器
  25. php -S localhost:8000
复制代码

4.8 Ruby环境

Ruby以其优雅的语法和强大的Rails框架而闻名:
  1. # 安装RVM(Ruby版本管理器)
  2. gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
  3. curl -sSL https://get.rvm.io | bash -s stable
  4. source ~/.rvm/scripts/rvm
  5. # 安装Ruby
  6. rvm install 3.2.2
  7. rvm use 3.2.2 --default
  8. # 验证安装
  9. ruby -v
  10. # 安装Bundler和Rails
  11. gem install bundler rails
  12. # 创建Rails应用
  13. rails new hello_rails
  14. cd hello_rails
  15. # 启动Rails服务器
  16. rails server
复制代码

4.9 .NET Core环境

.NET Core是微软的跨平台开发框架:
  1. # 添加Microsoft包仓库
  2. wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
  3. sudo dpkg -i packages-microsoft-prod.deb
  4. rm packages-microsoft-prod.deb
  5. # 安装.NET SDK
  6. sudo apt-get update
  7. sudo apt-get install -y dotnet-sdk-7.0
  8. # 验证安装
  9. dotnet --version
  10. # 创建新的控制台应用
  11. dotnet new console -o hello_dotnet
  12. cd hello_dotnet
  13. # 运行应用
  14. dotnet run
复制代码

5. 数据库服务器部署

5.1 MySQL/MariaDB安装与配置

MySQL和MariaDB是最流行的开源关系型数据库:
  1. # 安装MariaDB
  2. sudo apt install -y mariadb-server mariadb-client
  3. # 安全配置
  4. sudo mysql_secure_installation
  5. # 启动并启用服务
  6. sudo systemctl start mariadb
  7. sudo systemctl enable mariadb
  8. # 登录MariaDB
  9. sudo mysql -u root -p
  10. # 创建数据库和用户
  11. CREATE DATABASE myapp;
  12. CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
  13. GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost';
  14. FLUSH PRIVILEGES;
  15. EXIT;
  16. # 或者安装MySQL
  17. sudo apt install -y mysql-server mysql-client
  18. # 启动并启用服务
  19. sudo systemctl start mysql
  20. sudo systemctl enable mysql
  21. # 安全配置
  22. sudo mysql_secure_installation
复制代码

5.2 PostgreSQL安装与配置

PostgreSQL是功能强大的开源对象关系型数据库:
  1. # 安装PostgreSQL
  2. sudo apt install -y postgresql postgresql-contrib
  3. # 启动并启用服务
  4. sudo systemctl start postgresql
  5. sudo systemctl enable postgresql
  6. # 切换到postgres用户
  7. sudo -u postgres psql
  8. # 创建数据库和用户
  9. CREATE DATABASE myapp;
  10. CREATE USER myuser WITH ENCRYPTED PASSWORD 'password';
  11. GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
  12. \q
  13. # 配置远程访问(可选)
  14. sudo nano /etc/postgresql/14/main/postgresql.conf
  15. # 修改: listen_addresses = '*'
  16. sudo nano /etc/postgresql/14/main/pg_hba.conf
  17. # 添加: host all all 0.0.0.0/0 md5
  18. # 重启服务
  19. sudo systemctl restart postgresql
复制代码

5.3 MongoDB安装与配置

MongoDB是流行的NoSQL文档数据库:
  1. # 导入MongoDB公钥
  2. wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
  3. # 添加MongoDB仓库
  4. echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  5. # 安装MongoDB
  6. sudo apt update
  7. sudo apt install -y mongodb-org
  8. # 启动并启用服务
  9. sudo systemctl start mongod
  10. sudo systemctl enable mongod
  11. # 验证安装
  12. mongo --eval 'db.runCommand({ connectionStatus: 1 })'
  13. # 创建管理员用户
  14. mongo
  15. use admin
  16. db.createUser({
  17.   user: "admin",
  18.   pwd: "password",
  19.   roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
  20. })
  21. exit
复制代码

5.4 Redis安装与配置

Redis是高性能的键值存储系统:
  1. # 安装Redis
  2. sudo apt install -y redis-server
  3. # 启动并启用服务
  4. sudo systemctl start redis-server
  5. sudo systemctl enable redis-server
  6. # 验证安装
  7. redis-cli ping
  8. # 配置Redis(可选)
  9. sudo nano /etc/redis/redis.conf
  10. # 修改: requirepass yourpassword
  11. # 重启服务
  12. sudo systemctl restart redis-server
复制代码

5.5 SQLite配置

SQLite是轻量级的嵌入式数据库:
  1. # 安装SQLite
  2. sudo apt install -y sqlite3
  3. # 验证安装
  4. sqlite3 --version
  5. # 创建并使用数据库
  6. sqlite3 myapp.db
  7. CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
  8. INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
  9. SELECT * FROM users;
  10. .exit
复制代码

6. IDE配置

6.1 Visual Studio Code配置

VS Code是轻量级但功能强大的代码编辑器:
  1. # 下载并安装VS Code
  2. wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
  3. sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
  4. echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list
  5. sudo apt update
  6. sudo apt install -y code
  7. # 安装常用扩展
  8. code --install-extension ms-python.python
  9. code --install-extension ms-vscode.cpptools
  10. code --install-extension ms-vscode.vscode-typescript-next
  11. code --install-extension ms-azuretools.vscode-docker
  12. code --install-extension eamodio.gitlens
  13. code --install-extension ms-vscode-remote.remote-ssh
  14. code --install-extension ms-vscode-remote.remote-containers
  15. code --install-extension dbaeumer.vscode-eslint
  16. code --install-extension esbenp.prettier-vscode
复制代码

配置VS Code设置:
  1. # 创建设置目录
  2. mkdir -p ~/.config/Code/User/
  3. # 创建settings.json
  4. cat > ~/.config/Code/User/settings.json << EOF
  5. {
  6.     "editor.fontFamily": "'Fira Code', 'Droid Sans Mono', 'monospace', monospace",
  7.     "editor.fontSize": 14,
  8.     "editor.tabSize": 4,
  9.     "editor.insertSpaces": true,
  10.     "editor.renderWhitespace": "all",
  11.     "files.autoSave": "afterDelay",
  12.     "files.trimTrailingWhitespace": true,
  13.     "terminal.integrated.shell.linux": "/bin/bash",
  14.     "workbench.colorTheme": "Visual Studio Dark",
  15.     "workbench.iconTheme": "material-icon-theme",
  16.     "git.enableSmartCommit": true,
  17.     "git.confirmSync": false,
  18.     "[python]": {
  19.         "editor.formatOnSave": true,
  20.         "editor.codeActionsOnSave": {
  21.             "source.organizeImports": true
  22.         },
  23.         "python.linting.enabled": true,
  24.         "python.linting.pylintEnabled": true
  25.     },
  26.     "[javascript]": {
  27.         "editor.formatOnSave": true,
  28.         "editor.codeActionsOnSave": {
  29.             "source.fixAll.eslint": true
  30.         }
  31.     }
  32. }
  33. EOF
复制代码

6.2 JetBrains系列IDE配置

JetBrains提供了多种专业IDE,如IntelliJ IDEA、PyCharm和WebStorm:
  1. # 安装Snap(如果尚未安装)
  2. sudo apt install -y snapd
  3. # 安装JetBrains Toolbox
  4. sudo snap install jetbrains-toolbox --classic
  5. # 或者直接安装特定IDE
  6. sudo snap install pycharm-professional --classic
  7. sudo snap install intellij-idea-ultimate --classic
  8. sudo snap install webstorm --classic
  9. sudo snap install datagrip --classic
  10. # 或者使用Flatpak
  11. sudo flatpak install flathub com.jetbrains.PyCharm-Professional
  12. sudo flatpak install flathub com.jetbrains.IntelliJ-IDEA-Ultimate
  13. sudo flatpak install flathub com.jetbrains.WebStorm
复制代码

6.3 Eclipse配置

Eclipse是老牌的Java开发IDE:
  1. # 安装Eclipse
  2. sudo snap install eclipse --classic
  3. # 或者从官方网站下载安装包
  4. wget https://download.eclipse.org/eclipse/downloads/drops4/R-4.24-202206070700/eclipse-platform-4.24-linux-gtk-x86_64.tar.gz
  5. tar -xzf eclipse-platform-4.24-linux-gtk-x86_64.tar.gz
  6. sudo mv eclipse /opt/
  7. # 创建桌面快捷方式
  8. cat > ~/.local/share/applications/eclipse.desktop << EOF
  9. [Desktop Entry]
  10. Name=Eclipse
  11. Type=Application
  12. Exec=/opt/eclipse/eclipse
  13. Terminal=false
  14. Icon=/opt/eclipse/icon.xpm
  15. Comment=Integrated Development Environment
  16. NoDisplay=false
  17. Categories=Development;IDE;
  18. EOF
复制代码

6.4 Vim/Neovim配置

Vim是强大的文本编辑器,适合终端环境:
  1. # 安装Vim
  2. sudo apt install -y vim
  3. # 安装Neovim
  4. sudo apt install -y neovim
  5. # 安装Vim插件管理器vim-plug
  6. curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
  7.     https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  8. # 创建Vim配置文件
  9. cat > ~/.vimrc << EOF
  10. set nocompatible              " be iMproved, required
  11. filetype off                  " required
  12. " set the runtime path to include Vundle and initialize
  13. set rtp+=~/.vim/bundle/Vundle.vim
  14. call vundle#begin()
  15. " alternatively, pass a path where Vundle should install plugins
  16. "call vundle#begin('~/some/path/here')
  17. " let Vundle manage Vundle, required
  18. Plugin 'VundleVim/Vundle.vim'
  19. " The following are examples of different formats supported.
  20. " Keep Plugin commands between vundle#begin/end.
  21. " plugin on GitHub repo
  22. Plugin 'tpope/vim-fugitive'
  23. " plugin from http://vim-scripts.org/vim/scripts.html
  24. Plugin 'L9'
  25. " Git plugin not hosted on GitHub
  26. Plugin 'git://git.wincent.com/command-t.git'
  27. " git repos on your local machine (i.e. when working on your own plugin)
  28. Plugin 'file:///home/gmarik/path/to/plugin'
  29. " The sparkup vim script is in a subdirectory of this repo called vim.
  30. " Pass the path to set the runtimepath properly.
  31. Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
  32. " Install L9 and avoid a Naming conflict if you've already installed a
  33. " different version somewhere else.
  34. " Plugin 'ascenator/L9', {'name': 'newL9'}
  35. " All of your Plugins must be added before the following line
  36. call vundle#end()            " required
  37. filetype plugin indent on    " required
  38. " To ignore plugin indent changes, instead use:
  39. "filetype plugin on
  40. "
  41. " Brief help
  42. " :PluginList       - lists configured plugins
  43. " :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
  44. " :PluginSearch foo - searches for foo; append `!` to refresh local cache
  45. " :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
  46. "
  47. " see :h vundle for more details or wiki for FAQ
  48. " Put your non-Plugin stuff after this line
  49. " Basic settings
  50. set number
  51. set relativenumber
  52. set autoindent
  53. set expandtab
  54. set tabstop=4
  55. set shiftwidth=4
  56. set smarttab
  57. set softtabstop=4
  58. set mouse=a
  59. set encoding=utf-8
  60. set fileencoding=utf-8
  61. set ruler
  62. syntax on
  63. set hlsearch
  64. set incsearch
  65. set showmatch
  66. set laststatus=2
  67. set nobackup
  68. set noswapfile
  69. set wildmenu
  70. set wildmode=list:longest,full
  71. set backspace=indent,eol,start
  72. set clipboard=unnamedplus
  73. set cursorline
  74. set ttyfast
  75. set lazyredraw
  76. set showmatch
  77. set matchtime=2
  78. " Color scheme
  79. colorscheme desert
  80. EOF
  81. # 安装Neovim配置
  82. mkdir -p ~/.config/nvim
  83. cat > ~/.config/nvim/init.vim << EOF
  84. set runtimepath^=~/.vim runtimepath+=~/.vim/after
  85. let &packpath = &runtimepath
  86. source ~/.vimrc
  87. EOF
  88. # 安装插件
  89. vim +PluginInstall +qall
复制代码

6.5 其他编辑器配置

除了上述编辑器,还有其他一些流行的选择:
  1. # 安装Sublime Text
  2. wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
  3. echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
  4. sudo apt update
  5. sudo apt install -y sublime-text
  6. # 安装Atom
  7. sudo snap install atom --classic
  8. # 安装Notepad++
  9. sudo snap install notepad-plus-plus
复制代码

7. 版本控制集成

7.1 Git安装与配置

Git是最流行的分布式版本控制系统:
  1. # 安装Git
  2. sudo apt install -y git
  3. # 配置Git
  4. git config --global user.name "Your Name"
  5. git config --global user.email "your.email@example.com"
  6. git config --global core.editor vim
  7. git config --global color.ui true
  8. git config --global init.defaultBranch main
  9. # 生成SSH密钥
  10. ssh-keygen -t ed25519 -C "your.email@example.com"
  11. # 启动ssh-agent并添加密钥
  12. eval "$(ssh-agent -s)"
  13. ssh-add ~/.ssh/id_ed25519
  14. # 显示公钥(添加到GitHub/GitLab等)
  15. cat ~/.ssh/id_ed25519.pub
  16. # 安装Git LFS(大文件存储)
  17. sudo apt install -y git-lfs
  18. git lfs install
  19. # 安装Git Flow扩展
  20. sudo apt install -y git-flow
  21. # 创建Git仓库示例
  22. mkdir myproject && cd myproject
  23. git init
  24. echo "# My Project" > README.md
  25. git add README.md
  26. git commit -m "Initial commit"
复制代码

7.2 图形化Git客户端

虽然命令行Git功能强大,但有时图形界面更直观:
  1. # 安装GitKraken(Snap)
  2. sudo snap install gitkraken --classic
  3. # 安装Git Cola
  4. sudo apt install -y git-cola
  5. # 安装GitG(GNOME Git客户端)
  6. sudo apt install -y gitg
  7. # 安装QGit
  8. sudo apt install -y qgit
复制代码

7.3 SVN环境配置

虽然Git更流行,但有些项目仍在使用SVN:
  1. # 安装SVN
  2. sudo apt install -y subversion
  3. # 验证安装
  4. svn --version
  5. # 检出仓库示例
  6. svn checkout https://svn.example.com/repo/trunk myproject
  7. # 常用SVN命令
  8. # svn update
  9. # svn commit -m "Commit message"
  10. # svn add file.txt
  11. # svn delete file.txt
  12. # svn log
  13. # svn diff
复制代码

7.4 Mercurial配置

Mercurial是另一个分布式版本控制系统:
  1. # 安装Mercurial
  2. sudo apt install -y mercurial
  3. # 配置Mercurial
  4. cat > ~/.hgrc << EOF
  5. [ui]
  6. username = Your Name <your.email@example.com>
  7. editor = vim
  8. [extensions]
  9. hgext.extdiff =
  10. EOF
  11. # 验证安装
  12. hg --version
  13. # 创建仓库示例
  14. mkdir myproject && cd myproject
  15. hg init
  16. echo "# My Project" > README.md
  17. hg add README.md
  18. hg commit -m "Initial commit"
复制代码

8. 团队协作工具设置

8.1 Docker环境配置

Docker是容器化技术的领导者,简化了环境配置和部署:
  1. # 安装Docker
  2. sudo apt install -y docker.io docker-compose
  3. # 将用户添加到docker组
  4. sudo usermod -aG docker $USER
  5. # 重新登录以应用组更改
  6. # 验证Docker安装
  7. docker --version
  8. docker-compose --version
  9. # 运行Hello World容器
  10. docker run hello-world
  11. # 创建Dockerfile示例
  12. cat > Dockerfile << EOF
  13. FROM ubuntu:22.04
  14. RUN apt-get update && apt-get install -y python3 python3-pip
  15. COPY . /app
  16. WORKDIR /app
  17. RUN pip3 install -r requirements.txt
  18. CMD ["python3", "app.py"]
  19. EOF
  20. # 创建docker-compose.yml示例
  21. cat > docker-compose.yml << EOF
  22. version: '3'
  23. services:
  24.   web:
  25.     build: .
  26.     ports:
  27.       - "5000:5000"
  28.     volumes:
  29.       - .:/app
  30.   redis:
  31.     image: "redis:alpine"
  32. EOF
  33. # 启动服务
  34. docker-compose up -d
复制代码

8.2 虚拟环境管理

除了Docker,虚拟环境也是隔离项目依赖的重要工具:
  1. # Python虚拟环境
  2. sudo apt install -y python3-venv python3-virtualenv
  3. # 创建虚拟环境
  4. python3 -m venv myenv
  5. source myenv/bin/activate
  6. pip install django flask
  7. deactivate
  8. # 使用virtualenvwrapper
  9. pip install virtualenvwrapper
  10. echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
  11. echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
  12. echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
  13. source ~/.bashrc
  14. # 创建虚拟环境
  15. mkvirtualenv myproject
  16. workon myproject
  17. deactivate
  18. # Node.js版本管理(nvm)
  19. # 已在前面章节安装
  20. # Ruby版本管理(RVM)
  21. # 已在前面章节安装
  22. # Java版本管理(SDKMAN)
  23. # 已在前面章节安装
复制代码

8.3 CI/CD工具配置

持续集成和持续部署是现代开发流程的核心:
  1. # 安装Jenkins
  2. wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
  3. sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
  4. sudo apt update
  5. sudo apt install -y jenkins
  6. # 启动Jenkins服务
  7. sudo systemctl start jenkins
  8. sudo systemctl enable jenkins
  9. # 访问Jenkins(http://localhost:8080)并按照向导完成设置
  10. # 安装GitLab Runner
  11. curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
  12. sudo apt install -y gitlab-runner
  13. # 注册Runner
  14. sudo gitlab-runner register
  15. # 安装GitHub Actions Runner(需要从GitHub项目设置页面获取配置令牌)
  16. mkdir actions-runner && cd actions-runner
  17. curl -o actions-runner-linux-x64-2.299.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.299.1/actions-runner-linux-x64-2.299.1.tar.gz
  18. tar xzf ./actions-runner-linux-x64-2.299.1.tar.gz
  19. ./config.sh --url https://github.com/yourusername/yourrepo --token YOUR_TOKEN
  20. sudo ./svc.sh install
  21. sudo ./svc.sh start
复制代码

8.4 项目管理工具集成

项目管理工具帮助团队协作和跟踪进度:
  1. # 安装Taiga(项目管理工具)
  2. sudo apt install -y build-essential python3-dev python3-venv libpq-dev libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
  3. # 创建虚拟环境
  4. python3 -m venv taiga-env
  5. source taiga-env/bin/activate
  6. # 安装Taiga依赖
  7. pip install -r https://raw.githubusercontent.com/taigaio/taiga-back/master/requirements.txt
  8. # 或者使用Docker运行Taiga
  9. docker run -itd --name taiga-back -e TAIGA_SECRET_KEY="yoursecretkey" -p 8000:8000 taigaio/taiga-back:latest
  10. docker run -itd --name taiga-front -p 9000:80 --link taiga-back taigaio/taiga-front-dist:latest
  11. # 安装Redmine(项目管理工具)
  12. sudo apt install -y redmine redmine-mysql
  13. # 配置Redmine数据库
  14. sudo mysql -u root -p
  15. CREATE DATABASE redmine CHARACTER SET utf8mb4;
  16. CREATE USER 'redmine'@'localhost' IDENTIFIED BY 'password';
  17. GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';
  18. FLUSH PRIVILEGES;
  19. EXIT;
  20. # 配置Redmine
  21. sudo nano /etc/redmine/default/database.yml
  22. # 修改数据库配置
  23. # 运行Redmine
  24. sudo bundle exec rails server webrick -e production
复制代码

8.5 即时通讯工具设置

团队沟通是项目成功的关键:
  1. # 安装Slack(Snap)
  2. sudo snap install slack --classic
  3. # 安装Microsoft Teams(Snap)
  4. sudo snap install teams-for-linux
  5. # 安装Discord(Snap)
  6. sudo snap install discord
  7. # 安装Telegram Desktop
  8. sudo apt install -y telegram-desktop
  9. # 安装Rocket.Chat(自托管团队聊天)
  10. sudo snap install rocketchat-server
  11. # 配置Rocket.Chat
  12. sudo nano /var/snap/rocketchat-server/current/rocketchat-server.env
  13. # 添加配置,如:ROOT_URL=http://your-hostname.com
  14. # 重启服务
  15. sudo systemctl restart snap.rocketchat-server.rocketchat-server.service
复制代码

9. 开发工作流整合

9.1 创建高效开发环境

整合所有工具,创建一个高效的开发环境:
  1. # 创建项目目录结构
  2. mkdir -p ~/projects/{active,archive,templates}
  3. cd ~/projects/active
  4. # 创建新项目模板
  5. mkdir -p ~/projects/templates/python-webapp
  6. cd ~/projects/templates/python-webapp
  7. # 创建基本项目结构
  8. mkdir -p {src,tests,docs,scripts,data}
  9. touch requirements.txt
  10. touch README.md
  11. touch .gitignore
  12. touch Dockerfile
  13. touch docker-compose.yml
  14. # 创建.gitignore
  15. cat > .gitignore << EOF
  16. # Byte-compiled / optimized / DLL files
  17. __pycache__/
  18. *.py[cod]
  19. *\$py.class
  20. # C extensions
  21. *.so
  22. # Distribution / packaging
  23. .Python
  24. build/
  25. develop-eggs/
  26. dist/
  27. downloads/
  28. eggs/
  29. .eggs/
  30. lib/
  31. lib64/
  32. parts/
  33. sdist/
  34. var/
  35. wheels/
  36. *.egg-info/
  37. .installed.cfg
  38. *.egg
  39. MANIFEST
  40. # PyInstaller
  41. *.manifest
  42. *.spec
  43. # Installer logs
  44. pip-log.txt
  45. pip-delete-this-directory.txt
  46. # Unit test / coverage reports
  47. htmlcov/
  48. .tox/
  49. .nox/
  50. .coverage
  51. .coverage.*
  52. .cache
  53. nosetests.xml
  54. coverage.xml
  55. *.cover
  56. .hypothesis/
  57. .pytest_cache/
  58. # Translations
  59. *.mo
  60. *.pot
  61. # Django stuff:
  62. *.log
  63. local_settings.py
  64. db.sqlite3
  65. # Flask stuff:
  66. instance/
  67. .webassets-cache
  68. # Scrapy stuff:
  69. .scrapy
  70. # Sphinx documentation
  71. docs/_build/
  72. # PyBuilder
  73. target/
  74. # Jupyter Notebook
  75. .ipynb_checkpoints
  76. # IPython
  77. profile_default/
  78. ipython_config.py
  79. # pyenv
  80. .python-version
  81. # celery beat schedule file
  82. celerybeat-schedule
  83. # SageMath parsed files
  84. *.sage.py
  85. # Environments
  86. .env
  87. .venv
  88. env/
  89. venv/
  90. ENV/
  91. env.bak/
  92. venv.bak/
  93. # Spyder project settings
  94. .spyderproject
  95. .spyproject
  96. # Rope project settings
  97. .ropeproject
  98. # mkdocs documentation
  99. /site
  100. # mypy
  101. .mypy_cache/
  102. .dmypy.json
  103. dmypy.json
  104. # Pyre type checker
  105. .pyre/
  106. # IDE
  107. .vscode/
  108. .idea/
  109. *.swp
  110. *.swo
  111. *~
  112. # OS
  113. .DS_Store
  114. Thumbs.db
  115. EOF
  116. # 创建README.md模板
  117. cat > README.md << EOF
  118. # Project Name
  119. Brief description of the project.
  120. ## Features
  121. - Feature 1
  122. - Feature 2
  123. - Feature 3
  124. ## Installation
  125. \`\`\`bash
  126. git clone https://github.com/yourusername/projectname.git
  127. cd projectname
  128. pip install -r requirements.txt
  129. \`\`\`
  130. ## Usage
  131. \`\`\`bash
  132. python src/main.py
  133. \`\`\`
  134. ## Contributing
  135. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
  136. Please make sure to update tests as appropriate.
  137. ## License
  138. [MIT](https://choosealicense.com/licenses/mit/)
  139. EOF
  140. # 创建Dockerfile模板
  141. cat > Dockerfile << EOF
  142. FROM python:3.11-slim
  143. WORKDIR /app
  144. COPY requirements.txt .
  145. RUN pip install --no-cache-dir -r requirements.txt
  146. COPY . .
  147. CMD ["python", "src/main.py"]
  148. EOF
  149. # 创建docker-compose.yml模板
  150. cat > docker-compose.yml << EOF
  151. version: '3'
  152. services:
  153.   app:
  154.     build: .
  155.     ports:
  156.       - "5000:5000"
  157.     volumes:
  158.       - .:/app
  159.     environment:
  160.       - FLASK_ENV=development
  161.     depends_on:
  162.       - db
  163.   db:
  164.     image: postgres:13
  165.     environment:
  166.       - POSTGRES_USER=user
  167.       - POSTGRES_PASSWORD=password
  168.       - POSTGRES_DB=myapp
  169.     volumes:
  170.       - postgres_data:/var/lib/postgresql/data
  171. volumes:
  172.   postgres_data:
  173. EOF
  174. # 创建requirements.txt模板
  175. cat > requirements.txt << EOF
  176. flask==2.2.3
  177. psycopg2-binary==2.9.5
  178. python-dotenv==0.21.1
  179. gunicorn==20.1.0
  180. EOF
  181. # 创建基本Flask应用
  182. mkdir -p src
  183. cat > src/main.py << EOF
  184. from flask import Flask
  185. app = Flask(__name__)
  186. @app.route('/')
  187. def hello():
  188.     return "Hello, World!"
  189. if __name__ == '__main__':
  190.     app.run(debug=True, host='0.0.0.0')
  191. EOF
  192. # 创建基本测试
  193. mkdir -p tests
  194. cat > tests/test_app.py << EOF
  195. import unittest
  196. from src.main import app
  197. class AppTestCase(unittest.TestCase):
  198.     def setUp(self):
  199.         self.app = app.test_client()
  200.     def test_hello(self):
  201.         response = self.app.get('/')
  202.         self.assertEqual(response.status_code, 200)
  203.         self.assertEqual(response.data.decode('utf-8'), 'Hello, World!')
  204. if __name__ == '__main__':
  205.     unittest.main()
  206. EOF
  207. # 创建.gitattributes
  208. cat > .gitattributes << EOF
  209. # Auto detect text files and perform LF normalization
  210. * text=auto
  211. # Custom for Visual Studio
  212. *.cs     diff=csharp
  213. *.sln    merge=union
  214. *.csproj merge=union
  215. *.vbproj merge=union
  216. *.fsproj merge=union
  217. *.dbproj merge=union
  218. # Standard to msysgit
  219. *.doc         diff=astextplain
  220. *.DOC         diff=astextplain
  221. *.docx diff=astextplain
  222. *.DOCX diff=astextplain
  223. *.dot  diff=astextplain
  224. *.DOT  diff=astextplain
  225. *.pdf  diff=astextplain
  226. *.PDF         diff=astextplain
  227. *.rtf         diff=astextplain
  228. *.RTF         diff=astextplain
  229. EOF
复制代码

9.2 自动化工作流设置

创建自动化脚本来简化日常开发任务:
  1. # 创建项目初始化脚本
  2. mkdir -p ~/bin
  3. cat > ~/bin/newproject << 'EOF'
  4. #!/bin/bash
  5. # 项目初始化脚本
  6. if [ -z "$1" ]; then
  7.     echo "Usage: newproject <project-name>"
  8.     exit 1
  9. fi
  10. PROJECT_NAME=$1
  11. PROJECT_DIR=~/projects/active/$PROJECT_NAME
  12. TEMPLATE_DIR=~/projects/templates/python-webapp
  13. # 创建项目目录
  14. mkdir -p $PROJECT_DIR
  15. cd $PROJECT_DIR
  16. # 复制模板文件
  17. cp -r $TEMPLATE_DIR/* .
  18. # 初始化Git仓库
  19. git init
  20. git add .
  21. git commit -m "Initial commit"
  22. # 创建虚拟环境
  23. python3 -m venv venv
  24. source venv/bin/activate
  25. pip install -r requirements.txt
  26. # 打开项目
  27. code .
  28. echo "Project $PROJECT_NAME created successfully!"
  29. EOF
  30. chmod +x ~/bin/newproject
  31. # 创建数据库备份脚本
  32. cat > ~/bin/backup-db << 'EOF'
  33. #!/bin/bash
  34. # 数据库备份脚本
  35. BACKUP_DIR=~/backups
  36. DATE=$(date +%Y%m%d_%H%M%S)
  37. DB_NAME="myapp"
  38. DB_USER="user"
  39. DB_PASS="password"
  40. # 创建备份目录
  41. mkdir -p $BACKUP_DIR
  42. # 备份数据库
  43. mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_NAME_$DATE.sql
  44. # 压缩备份文件
  45. gzip $BACKUP_DIR/$DB_NAME_$DATE.sql
  46. # 删除30天前的备份
  47. find $BACKUP_DIR -name "*.gz" -type f -mtime +30 -delete
  48. echo "Database backup completed: $BACKUP_DIR/$DB_NAME_$DATE.sql.gz"
  49. EOF
  50. chmod +x ~/bin/backup-db
  51. # 创建系统更新脚本
  52. cat > ~/bin/update-system << 'EOF'
  53. #!/bin/bash
  54. # 系统更新脚本
  55. echo "Updating package lists..."
  56. sudo apt update
  57. echo "Upgrading packages..."
  58. sudo apt upgrade -y
  59. echo "Removing unnecessary packages..."
  60. sudo apt autoremove -y
  61. echo "Cleaning up package cache..."
  62. sudo apt clean
  63. echo "Updating Python packages..."
  64. pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
  65. echo "Updating global npm packages..."
  66. npm update -g
  67. echo "System update completed!"
  68. EOF
  69. chmod +x ~/bin/update-system
  70. # 创建项目部署脚本
  71. cat > ~/bin/deploy << 'EOF'
  72. #!/bin/bash
  73. # 项目部署脚本
  74. if [ -z "$1" ]; then
  75.     echo "Usage: deploy <project-directory>"
  76.     exit 1
  77. fi
  78. PROJECT_DIR=~/projects/active/$1
  79. cd $PROJECT_DIR
  80. # 拉取最新代码
  81. git pull origin main
  82. # 安装/更新依赖
  83. source venv/bin/activate
  84. pip install -r requirements.txt
  85. # 运行测试
  86. python -m pytest tests/
  87. # 构建Docker镜像
  88. docker build -t $1 .
  89. # 停止并删除旧容器
  90. docker stop $1 || true
  91. docker rm $1 || true
  92. # 运行新容器
  93. docker run -d --name $1 -p 5000:5000 $1
  94. echo "Project $1 deployed successfully!"
  95. EOF
  96. chmod +x ~/bin/deploy
复制代码

9.3 常见问题解决方案

开发过程中可能会遇到一些常见问题,以下是解决方案:
  1. # 创建问题诊断脚本
  2. cat > ~/bin/diagnose << 'EOF'
  3. #!/bin/bash
  4. # 系统诊断脚本
  5. echo "===== System Information ====="
  6. uname -a
  7. echo
  8. echo "===== Disk Usage ====="
  9. df -h
  10. echo
  11. echo "===== Memory Usage ====="
  12. free -h
  13. echo
  14. echo "===== CPU Usage ====="
  15. top -bn1 | head -20
  16. echo
  17. echo "===== Network Connections ====="
  18. netstat -tuln
  19. echo
  20. echo "===== Running Services ====="
  21. systemctl list-units --type=service --state=running
  22. echo
  23. echo "===== Docker Status ====="
  24. docker ps -a
  25. echo
  26. echo "===== Python Environment ====="
  27. which python
  28. python --version
  29. pip list
  30. echo
  31. echo "===== Node.js Environment ====="
  32. which node
  33. node --version
  34. npm list -g --depth=0
  35. echo
  36. echo "===== Git Status ====="
  37. git status
  38. echo
  39. EOF
  40. chmod +x ~/bin/diagnose
  41. # 创建权限修复脚本
  42. cat > ~/bin/fix-permissions << 'EOF'
  43. #!/bin/bash
  44. # 修复文件权限脚本
  45. echo "Fixing home directory permissions..."
  46. chown -R $USER:$USER $HOME
  47. echo "Fixing SSH key permissions..."
  48. chmod 700 ~/.ssh
  49. chmod 600 ~/.ssh/id_*
  50. chmod 644 ~/.ssh/*.pub
  51. echo "Fixing Docker socket permissions..."
  52. sudo usermod -aG docker $USER
  53. echo "Permissions fixed! Please log out and log back in for changes to take effect."
  54. EOF
  55. chmod +x ~/bin/fix-permissions
  56. # 创建网络诊断脚本
  57. cat > ~/bin/network-test << 'EOF'
  58. #!/bin/bash
  59. # 网络诊断脚本
  60. echo "===== Testing DNS Resolution ====="
  61. nslookup google.com
  62. echo
  63. echo "===== Testing Internet Connectivity ====="
  64. ping -c 4 google.com
  65. echo
  66. echo "===== Testing Local Network ====="
  67. ping -c 4 8.8.8.8
  68. echo
  69. echo "===== Checking Listening Ports ====="
  70. sudo netstat -tuln
  71. echo
  72. echo "===== Checking Firewall Rules ====="
  73. sudo ufw status
  74. echo
  75. echo "===== Checking Network Interfaces ====="
  76. ip addr show
  77. echo
  78. echo "===== Checking Routing Table ====="
  79. ip route show
  80. echo
  81. EOF
  82. chmod +x ~/bin/network-test
复制代码

通过以上步骤,你已经成功搭建了一个功能完善的Kubuntu开发环境,包括系统安装、基础配置、高级优化、各类编程语言环境、数据库服务器、IDE配置、版本控制和团队协作工具。这套环境将极大地提高你的开发效率,并支持各种类型的项目开发。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则