1.前言
昨天看到网易云音乐node.js版API,但是因为对node.js的认识只在一些语句上面,苦于不知咋用.结果发现原来就直接node app.js
就能在本地服务器localhost
中运行.于是就想把他挂到我的vps上面.弄了一下午弄好了.把这次的踩坑经验发上来
我的vps是centos 6版本的
具体步骤:
1. node.js npm 环境配置
2. git配置(我的vps在做gitpage的时候已经配置了,所以就不写了)
3. pm2
4. Nginx
2. 配置过程
1. node.js配置
由于linux上安装文件是真的有很多方式,没具体学过linux的我真的晕晕了,由于我对node的版本没有要求,就直接安装了具体版本号.
推荐以下操作在 /opt 目录下进行
下载压缩包
wget http://developer.jpanj.com/node-v10.15.3-linux-x64.tar.xz

解压为 tar 包
xz -d node-v10.15.3-linux-x64.tar.xz
如果xz命令不存在的话,可以查看博客或者根据一下步骤一步步敲
wget http://tukaani.org/xz/xz-5.2.2.tar.gz`
`tar -zxf xz-5.2.2.tar.gz`
./configure
make && make install
解压
tar -xvf node-v10.15.3-linux-x64.tar
当前目录下软链一个 node
目录出来
这样做的好处是,未来升级版本非常方便,只需要更新这个软链就行
ln -s ./node-v10.15.3-linux-x64 ./node
通过软链接,将可执行程序放入系统环境变量的路径中
- 查看当前系统中都有哪些环境变量路径
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
可以看到我的列表中有:
/usr/local/bin
/usr/bin
大家约定成俗逻辑是:
/usr/bin
下面的都是系统预装的可执行程序,会随着系统升级而改变。/usr/local/bin
目录是给用户放置自己的可执行程序的地方
所以我推荐将软链放在 /usr/local/bin
目录下:
ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
但是在这一步我遇到了-bash: cd: src: Too many levels of symbolic links
的问题,后续解决方案是我cd ~
之后 ln -s /opt/node/bin/node /usr/local/bin/node
来完成的。
如果遇到file exists
的问题,就到你软链到的那个地址的命令文件删掉
检查是否安装成功
[root@dc8 ~]# node -v
v10.15.3
[root@dc8 ~]# npm -v
6.4.1
2. pm2
pm2的作用是能够将你的node后台项目持久得挂载在后台并且能够自动地帮你负载均衡之类的,适合我这种傻瓜嘻嘻
安装pm2
npm i -g pm2
通过软链接,将可执行程序放入系统环境变量的路径中
因为不像windows,npm下载之后不能够直接在全局环境之中使用
所以也要像之前一样
ln -s /opt/node/bin/pm2 /usr/local/bin/pm2
接下来就能将node项目一直挂载在后台了
使用
pm2 start app.js //启动pm2项目
pm2 stop all //停止所有项目
PORT=4000 pm2 start app.js //使用的这个网易云后台程序,在项目目录下启动,更多命令内容建议看参考
3. Nginx
1.第一步,在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo:
cd /etc/yum.repos.d/
vim nginx.repo
填写如下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
保存,则会产生一个/etc/yum.repos.d/nginx.repo文件。
下面直接执行如下指令即可自动安装好Nginx:
yum install nginx -y
安装完成,下面直接就可以启动Nginx了:
/etc/init.d/nginx start
现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。
如果还无法访问,则需配置一下Linux防火墙。
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
这里注意 之后Nginx要监听哪个端口就必须开放哪个端口,我在这里踩了一下坑 现在我们释放的是默认的80端口
service iptables save
service iptables restart
Nginx的命令以及配置文件位置:
/etc/init.d/nginx start # 启动Nginx服务
/etc/init.d/nginx stop # 停止Nginx服务
/etc/nginx/nginx.reload # 重启Nginx服务
/etc/nginx/nginx.conf # Nginx配置文件位置
chkconfig nginx on #设为开机启动
至此,Nginx已经全部配置安装完成。
2.一台主机上适应多个服务
在你的nginx通过代理的方式转发请求:进入nginx文件夹下的conf.d文件夹
cd conf.d
新建一个.conf后缀文件(在这个文件夹下面其实已经有一个默认的 default.conf配置文件了)参考:wiki.nginx.org/FullExample
//taotao.config 我的新建的配置文件
server {
listen 4001;#你希望在外网访问的端口,记得一定要关掉防火墙
location / {
proxy_pass http://127.0.0.1:4030;#你在本机上运行的端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
配置好之后重启一下nginx服务器就行啦
到这里,一个简单的能在外网访问的node项目就完成了,希望会对大家有用