如何在Debian 11上安装Git、Node.js、MySQL和Nginx

2023-05-05大约10分钟

在 Debian 11 Bullseye 上安装 Git,可以按照下列的步骤。

前置准备: 更新并升级系统的包。

在我们安装任何软件之前,请务必通过运行以下命令来确保您的系统是最新的 apt 终端中的命令:

sudo apt update
sudo apt upgrade

安装Git

步骤1. 在 Debian 11 上安装 Git。

默认情况下,Git 在 Debian Bullseye 存储库中可用,运行以​​下命令进行安装:

sudo apt install git

通过打印 Git 版本来验证安装:

$ git --version
git version 2.30.2

步骤2. 配置 Git。

完成安装后,您将需要设置常用设置,例如名称和电子邮件,主要围绕 git commit 消息。 这非常简单,因为本教程将在下面解释:

git config --global user.name "Lema"
git config --global user.email "example@lema.fun"

您可以使用以下命令验证更改:

$ git config --list
user.name=Lema
user.email=example@lema.fun

安装Node.js

Debian 11 存储库包含的预构建的Node.js包的版本是v12的,比较老,所以这里把PPA存储库加到系统里,可以安装新的Node.js版本。

步骤1. 使用以下命令将 PPA 存储库添加到您的系统。

NodeJS 18.x 是目前最新的稳定版本,比Debian 11 repo (12.x) 中的版本要新。 将setup_18.x 替换为安装时可用的最新版本的NodeJS。

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

输出的信息如下:

## Installing the NodeSource Node.js 18.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 http://mirrors.tencentyun.com/debian bullseye InRelease
Hit:2 http://mirrors.tencentyun.com/debian bullseye-updates InRelease
Get:3 http://mirrors.tencentyun.com/debian-security bullseye-security InRelease [48.4 kB]
Get:4 http://mirrors.tencentyun.com/debian-security bullseye-security/main amd64 Packages [239 kB]
Get:5 http://mirrors.tencentyun.com/debian-security bullseye-security/main Translation-en [157 kB]
Fetched 444 kB in 0s (967 kB/s)                            
Reading package lists... Done

## Confirming "bullseye" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_18.x/dists/bullseye/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 18.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x bullseye main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x bullseye main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 http://mirrors.tencentyun.com/debian bullseye InRelease
Hit:2 http://mirrors.tencentyun.com/debian bullseye-updates InRelease
Hit:3 http://mirrors.tencentyun.com/debian-security bullseye-security InRelease
Get:4 https://deb.nodesource.com/node_18.x bullseye InRelease [4,586 B] 
Get:5 https://deb.nodesource.com/node_18.x bullseye/main amd64 Packages [776 B]
Fetched 5,362 B in 1s (4,182 B/s) 
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 18.x and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
     echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     sudo apt-get update && sudo apt-get install yarn

步骤2. 使用以下命令安装 NodeJS 和 NPM

您无需在此处指定 NPM 包,因为该包已经是 NodeJS 包的一部分。

sudo apt-get install -y nodejs

步骤3. 验证 NodeJS 和 NPM 是否已正确安装

$ node -v
v18.16.0
$ npm -v
9.5.1

安装MySQL

安装 Mysql 的源。可以从这个网页获取最新版本的链接:https://dev.mysql.com/downloads/repo/apt/

步骤1. 配置源

在第二步需要选择需要安装的版本。

wget https://dev.mysql.com/get/mysql-apt-config_0.8.25-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.25-1_all.deb
rm mysql-apt-config_0.8.25-1_all.deb

步骤2. 安装

sudo apt update
sudo apt-get install mysql-server

步骤3. 验证安装

$ mysql --version
mysql  Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

安装Nginx

步骤1. 安装依赖

sudo apt install curl gnupg2 ca-certificates lsb-release

步骤2. 安装

sudo apt install nginx -y

步骤3. 验证安装

查看Nginx版本:

$ sudo nginx -v
nginx version: nginx/1.18.0

查看Nginx的运行状态:

sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-05-06 10:23:14 CST; 31s ago
       Docs: man:nginx(8)
    Process: 14645 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 14646 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 14728 (nginx)
      Tasks: 3 (limit: 2333)
     Memory: 9.0M
        CPU: 37ms
     CGroup: /system.slice/nginx.service
             ├─14728 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─14730 nginx: worker process
             └─14731 nginx: worker process

步骤4. 配置

设置Nginx在系统启动时候自动启动:

sudo systemctl enable nginx

常用的命令:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

配置生成工具