簡體   English   中英

如何設置 Linux 環境變量,以便 pm2 可以在 node.js 應用程序中看到它們?

[英]How do I set Linux environment variables so that pm2 can see them for node.js apps?

當我使用node server.js在 Ubuntu Linux 服務器上運行我的 nodejs 應用程序時,它可以正常工作,並使用process.env.db輸出環境變量$db的值。

但是,應用程序在運行sudo pm2 start server.js時中斷,將環境變量視為undefined

我嘗試在以下文件中添加變量:

  • 等/環境: db="hello"
  • ~/.ssh/environment: db="hello"
  • ~/.bashrc: export db="hello"

我還重新啟動並運行source ~/.bashrc以確保變量可用。

我想我已經嘗試了這里提到的一切,我不知道還能做什么:

請注意,在source ~/.bashrc中說你正在加載當前用戶的變量。 但是,當你說sudo ...你正在使用root用戶運行時,所以這不會改變。

你可以做的是使用sudo -E

sudo -E pm2 start server.js

來自man sudo

-E, --preserve-env
             Indicates to the security policy that the user wishes to reserve their
             existing environment variables.  The security policy may eturn an error
             if the user does not have permission to preserve the environment.

請參閱此主題https://github.com/Unitech/pm2/issues/204

好像你的環境變量被緩存了。

我刪除了 pm2 進程並像往常一樣重新啟動它。 只是簡單地重新啟動該過程並不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM