如何在宝塔面板中部署thinkjs或node.js程序?
最近要把一个node js程序部署到宝塔面板上,遇到了一些坑。记录下面,方便他人。
安装nvm、pm2
安装完成,把代码上传,按照thinkjs文档部署
这一步有几点说明一下:
- pm2.json里的路径一定要写对
- 我是使用命令启动的:pm2 start pm2.json
thinkjs程序在宝塔里的配置
- 首先添加站点
- 然后再站点那一行后面有个『操作』将【网站目录】中的【运行目录】改为【/www】,如图:
- 然后把thinkjs项目中nginx.conf的内容复制到配置文件中,如图:
附上nginx配置文件模板:
server {
listen 80;
server_name abc.com;
root /www/wwwroot/abc.com/www;
set $node_port 9999;
index index.js index.html index.htm;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_http_version 1.1;
proxy_hide_header X-Powered-By;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:$node_port$request_uri;
proxy_redirect off;
}
location ~ /static/ {
etag on;
expires max;
}
}
最后
这种部署方法同样适用于将thinkjs项目部署在二级域名下面。如果是二级域名,比如sub.abc.com,nginx这么写即可:
server {
listen 80;
server_name sub.abc.com;
root /www/wwwroot/abc.com/sub/www;
set $node_port 9999;
index index.js index.html index.htm;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_http_version 1.1;
proxy_hide_header X-Powered-By;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:$node_port$request_uri;
proxy_redirect off;
}
location ~ /static/ {
etag on;
expires max;
}
}