ubuntu: 安装 nginx
在 ubuntu 下安装 nginx
01 更新系统
# 安装 Nginx(自动注册 systemd 服务)
sudo apt update
sudo apt install nginx -y
02 安装 nginx 并不要提示
sudo apt install nginx -y
# 检查服务是否存在
systemctl status nginx
03 添加自启动
使用 systemctl 管理
# 启动 Nginx 服务
sudo systemctl start nginx
# 停止 Nginx 服务
sudo systemctl stop nginx
# 重启 Nginx 服务(先停止再启动)
sudo systemctl restart nginx
# 重新加载配置文件(不中断服务,推荐用于配置变更后)
sudo systemctl reload nginx
# 重载或重启:如果只是配置变化则 reload,否则 restart
sudo systemctl reload-or-restart nginx
# 查看 Nginx 服务状态(是否运行、PID、错误信息等)
sudo systemctl status nginx
# 查看 Nginx 是否开机自启
sudo systemctl is-enabled nginx
# 设置 Nginx 开机自启
sudo systemctl enable nginx
# 取消 Nginx 开机自启
sudo systemctl disable nginx
04 查持状态
sudo systemctl enable nginx # 开机自启
sudo systemctl start nginx # 立即启动
sudo systemctl status nginx # 查看状态
05 直接kill nginx
直接中止 nginx
# 先杀死旧进程
sudo pkill nginx
# 或更强制:
sudo killall nginx