You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

全局安装Node.js Forever遇权限问题,sudo安装后致网站HTTP故障求助

Fixing Permission Issues After Installing Forever with Sudo

Let’s walk through resolving this step by step—you’ve hit two linked problems here: messed-up system directory permissions from using sudo with npm, and a broken HTTP service as a direct result.

1. First, Fix the /usr/local Directory Ownership

The immediate issue is that /usr/local is owned by sshuser instead of the system’s root user, which is blocking your HTTP service from accessing necessary files. Run this command to reset the ownership back to the correct system defaults:

sudo chown -R root:root /usr/local

(Note: On some systems, the group might be staff instead of root—if you run into odd issues later, check the ownership of other directories in /usr to match that setup.)

2. Install Global npm Packages Without Sudo (Prevent This Problem Long-Term)

Using sudo for npm installs is a common source of permission headaches. Instead, configure npm to use a user-specific directory for global packages so you never need sudo again:

  • Create a dedicated global npm directory for your user:
    mkdir -p ~/.npm-global
    
  • Tell npm to use this directory as its global prefix:
    npm config set prefix '~/.npm-global'
    
  • Add this directory to your shell’s PATH so it can find the installed tools. Edit your ~/.bashrc (or ~/.zshrc if you use Zsh) and add this line:
    export PATH=~/.npm-global/bin:$PATH
    
  • Refresh your shell configuration to apply the change:
    source ~/.bashrc
    
  • Now install Forever without sudo—it’ll live in your user’s directory instead of touching system folders:
    npm install forever -g
    

3. Verify and Restart Your Services

After fixing permissions, restart your HTTP service (e.g., sudo systemctl restart nginx or whichever service you’re using) to ensure it can access files properly. Then start your Node.js app with Forever again—since you installed it without sudo this time, it’ll run with your user’s permissions, which won’t conflict with the HTTP service as long as your app’s files have correct read/write permissions for the service user.

Quick Breakdown of What Happened

When you ran sudo npm install, npm sometimes sets the ownership of system directories (like /usr/local) to the user who invoked sudo (in your case, sshuser) instead of keeping it as root. This breaks system services that rely on standard permissions for those directories. Using a user-specific global npm directory avoids this conflict entirely.

内容的提问来源于stack exchange,提问作者Juliver Galleto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:52:11