新 Ubuntu 服务器的开发环境配置
以下是我在入手新的服务器后,常用的开发环境配置。
安装ohmyzsh
- 安装zsh和必要工具
sudo apt update
sudo apt install zsh git curl wget -y
- 将zsh设置为默认shell
chsh -s $(which zsh)
- 使用curl安装Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
或者使用wget安装:
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
- 安装插件
安装 syntax-highlighting 插件
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
安装 zsh-autosuggestions 插件:
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
- 编辑 ~/.zshrc 配置文件启用插件
vim ~/.zshrc
# 找到 plugins 行,添加这两个插件(保留原有的插件,比如git)
plugins=(git z npm zsh-autosuggestions zsh-syntax-highlighting)
- 使配置生效
source ~/.zshrc
安装MongoDB 7.x
- 首先导入MongoDB公钥
# 导入公钥
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
- 创建MongoDB源列表文件
# 创建源文件
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
- 更新包列表并安装MongoDB
# 更新包列表
sudo apt update
# 安装MongoDB
sudo apt install mongodb-org -y
- 启动MongoDB服务并设置开机自启
# 启动服务
sudo systemctl start mongod
# 设置开机自启
sudo systemctl enable mongod
# 检查服务状态
sudo systemctl status mongod
验证安装
# 检查MongoDB版本
mongod --version
# 连接MongoDB
mongosh
常用管理命令
# 停止服务
sudo systemctl stop mongod
# 重启服务
sudo systemctl restart mongod
# 查看日志
sudo tail -f /var/log/mongodb/mongod.log
mongsh常用命令
# 切换到某个表中
use dbName
# 创建admin表root账号
db.createUser({user:"",pwd:"",roles:[{role:"root",db:"admin"}]})
# 创建其他表管理员账号
db.createUser({user:"",pwd:"",roles:[{role:"dbOwner",db:""}]})
# 为admin表创建管理员账号后,最好开启MongoDB配置中的安全选项
# /etc/mongod.conf内的内容如下:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
mongorestore还原备份
mongorestore -uuser -ppwd dbname bakpath
安装nodejs
- 安装nvm,仓库地址:https://github.com/nvm-sh/nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# 注意环境配置
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
- 使用nvm安装nodejs
nvm install node
开启root远程访问
- 首先设置root密码
sudo passwd root
- 编辑SSH配置文件
sudo nano /etc/ssh/sshd_config
- 找到并修改以下设置(如果没有就添加):
PermitRootLogin yes
- 重启SSH服务:
sudo systemctl restart ssh
安全建议:
建议使用密钥登录而不是密码:
# 在你的本地机器上生成密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"
# 将公钥复制到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@your_server_ip
添加以下安全配置到 sshd_config:
# 禁用密码认证(如果使用密钥登录)
PasswordAuthentication no
# 限制最大认证尝试次数
MaxAuthTries 3
# 使用强加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com