Nginx
安装
更新软件包列表
打开终端,首先更新本地的软件包列表,以确保获取最新的软件版本信息。
安装Nginx
使用apt命令直接安装Nginx。
系统会提示你确认安装,输入 y并按回车即可。
启动并设置开机自启
安装完成后,Nginx服务通常会自动启动。为确保万无一失,可以手动启动并设置为开机自启。
1
2
sudo systemctl start nginx # 启动Nginx
sudo systemctl enable nginx # 设置开机自启
验证安装
在浏览器中输入你的服务器IP地址或 http://localhost,如果看到Nginx的默认欢迎页面,说明安装成功。
你也可以通过检查服务状态来确认:
1
sudo systemctl status nginx
通过 apt方式安装后,Nginx 的相关文件会按标准规则存放在系统的特定目录下。
文件类型
所在目录
主程序
/usr/sbin/nginx
配置文件
/etc/nginx/
默认网站根目录
/usr/share/nginx/html/
日志文件
/var/log/nginx/
修改默认端口
在 /etc/nginx/nginx.conf 可以看到关键的两行是:
1
2
include /etc/nginx/conf.d/*.conf ;
include /etc/nginx/sites-enabled/* ;
这表示 Nginx 会加载 /etc/nginx/conf.d/ 目录下所有 .conf 文件以及 /etc/nginx/sites-enabled/ 目录下的所有文件。默认的 80 端口监听配置通常就定义在这些被引入的文件里。
在 /etc/nginx/sites-enabled/default 中内容如下:
1
2
3
4
5
6
server {
listen 80 default_server ;
listen [::]:80 default_server ;
...
}
修改监听端口:
1
2
3
4
5
6
7
# 修改前
listen 80 default_server ;
listen [::]:80 default_server ;
# 修改后
listen 8080 default_server ;
listen [::]:8080 default_server ;
修改后,必须执行以下命令来使更改生效:
1
2
sudo systemctl stop nginx
sudo systemctl start nginx
Docker和Docker Compose
安装 Docker
推荐通过 Docker 官方仓库安装,能确保获得最新的稳定版本。
更新软件包索引并安装依赖工具
首先确保系统软件包列表是最新的,然后安装一些必要的工具,这些工具允许 apt通过 HTTPS 使用仓库。
1
2
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
添加 Docker 的官方 GPG 密钥
此步骤用于验证所下载软件包的完整性。
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
添加 Docker 软件仓库
将 Docker 的稳定版仓库地址添加到系统的软件源列表中。
1
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $( lsb_release -cs) stable"
安装 Docker Engine
再次更新软件包列表(这次包含了 Docker 仓库的信息),然后安装 Docker。
1
2
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
启动 Docker 服务并设置开机自启
安装完成后,启动 Docker 服务,并让它在你下次重启服务器时自动启动。
1
2
sudo systemctl start docker
sudo systemctl enable docker
清理下载缓存:之前失败的下载可能会留下损坏的缓存,需要清理后重新拉取。
docker system prune -a -f
注意:此命令会清除所有未使用的镜像、容器、网络和构建缓存。如果系统提示权限不足,请在命令前加 sudo。
安装 Docker Compose
Docker Compose 是一个用于定义和运行多容器 Docker 应用程序的工具。
通过 apt 安装
1
sudo apt install docker-compose
配置国内镜像加速器
创建或编辑 Docker 的配置文件。
1
sudo vim /etc/docker/daemon.json
在文件中添加以下内容。
保存文件后,重新加载配置并重启 Docker。
1
2
sudo systemctl daemon-reload
sudo systemctl restart docker
Go
换用国内镜像源
备份原文件:安全起见,先执行 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 备份。
编辑源文件:执行 sudo nano /etc/apt/sources.list,将文件内容全部删除,并替换为下方的阿里云镜像源地址。
1
2
3
4
5
6
# 阿里云镜像源 (Ubuntu 22.04)
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
保存并更新:按 Ctrl+X,然后按 Y,最后按 Enter 保存文件。接着运行 sudo apt update 更新软件列表。
安装
1
2
3
4
5
sudo apt update
sudo apt install golang-go -y
# 验证安装
go version
配置Go模块代理:即使安装了 Go,后续拉取依赖包时可能仍需要加速。建议安装后立即执行:
1
go env -w GOPROXY = https://goproxy.cn,direct
Nodejs
1
2
3
4
5
6
7
# 更新软件包索引并安装
sudo apt update
sudo apt install nodejs npm -y
# 验证版本
node -v
npm -v
Cpolar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# cpolar 安装(国内使用)
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
# cpolar 短链接安装方式:(国外使用)
curl -sL https://git.io/cpolar | sudo bash
# 查看版本号,有正常显示版本号即为安装成功
cpolar version
# token 认证,登录 cpolar 官网后台,点击左侧的验证,查看自己的认证 token,之后将token贴在命令行里
cpolar authtoken xxxxxxx
# 简单穿透测试,按ctrl+c退出
cpolar http 8080
# 向系统添加服务
sudo systemctl enable cpolar
# 启动 cpolar 服务
sudo systemctl start cpolar
# 查看服务状态
sudo systemctl status cpolar
# 查看 Web 面板
http://Ubuntu_IP:9200