建议安装一个 xshell。这样的话,文章当中的命令,就可以直接复制粘贴上去了。
使用git将源码克隆到本地的~/.nvm目录下,并检查最新版本
yum install git
git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
激活NVM
echo ". ~/.nvm/nvm.sh" >> /etc/profile
source /etc/profile
列出Node.js的所有版本
nvm list-remote
安装多个Node.js版本
nvm install v7.4.0
nvm install v12.21.0
运行nvm ls
查看已安装的Node.js版本。本示例使用的版本为v12.21.0。返回结果如下所示。
[root@iZXXXXZ .nvm]# nvm ls
v12.21.0
-> v7.4.0
system
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 12.21 (-> v12.21.0) (default)
运行nvm use <版本号>
可以切换Node.js版本。
例如,切换Node.js版本至v12.21.0。返回结果如下所示。
[root@iZXXXXZ .nvm]# nvm use v12.21.0
Now using node v12.21.0
yum安装
// 添加CentOS 7 Nginx yum资源库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
// 安装
yum install -y nginx
检查nginx是否安装成功
nginx -t // 有下面提示代表安装成功
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
systemctl start nginx.service // 启动nginx
systemctl stop nginx.service // 停止nginx
systemctl restart nginx.service // 重启nginx
systemctl enable nginx.service // 设置开机启动
如果报错:
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
解决办法:杀死 nginx 的所有进程,重新启动nginx
查看进程
netstat -tpln
杀死进程
kill -9 PID
nginx 配置
nginx配置文件为 /etc/nginx/nginx.conf
(如果你发现的 nginx.conf 文件没有 server 字段。那这个字段应该添加到 http 中,才会生效)
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000; # 本地node启动的端口为3000
}
}
重启 nginx
systemctl restart nginx.service
如果报错:Failed to restart nginx.servic.service: Unit not found.
原因:是因为nginx没有将添加到系统服务,手动添加这个服务即可解决
vim /etc/init.d/nginx
插入以下代码
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
cd /etc/init.d
依此执行以下命令
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
开启nginx
service nginx start
如果一切顺利,你会看到
[root@iZuf6gk9a5p415wnahjxc4Z init.d]# service nginx start
Starting nginx (via systemctl): [ OK ]
服务器防火墙
查看所有信息,看添加的端口是否存在(注:ports后面的就是开放的对应端口)
systemctl start firewalld
firewall-cmd --list-all
可以看到如下信息:
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
若是ports为空,则自行添加所需要开放的端口( 例如添加3000端口 )
// 添加需要开放的端口号
firewall-cmd --zone=public --add-port=3000/tcp --permanent
重启防火墙
firewall-cmd --reload
再次检查检查端口是否打开
firewall-cmd --list-all
配置安全组
不出意外的话,访问 ip+端口,可以看到 Hello World
,两个大字。那么恭喜你,你成功了!!!
总结一下✔️:
想要放开一个端口,必须要去阿里云的官网上进行安全组配置,放开防火墙端口,nginx 中写入端口号。
安装 git
yum install git
查看版本
git --version
生成 ssh 公钥
ssh-keygen -t rsa -C "1*********[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:m0OxHwUdbdxkFRRfyLCX2uqrzfIixoidrK0RO1v7Pe8 1514535562@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| ..o*.BO|
| ...*o+|
| . o.o .|
| o . + |
| . S . . . |
| o . + . . |
| ++.++ . . |
| .*=.+o++ |
| +ooo..oOE. |
+----[SHA256]-----+
进入 .ssh 文件夹
cd .ssh
查看 id_rsa.pub
ls
==> authorized_keys id_rsa id_rsa.pub known_hosts
输入命令 more id id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjzz+Qr35mu94Hlse9Ml89yzkPzw4A1gP2ibulEpLPGR1ojuQJgp7HhOB/2TkZ
4F8jWwjehbwekdwkdqkwbd31293u1291bkwabcscgas7q76hjsbckasc asc0ascsauc82yge32ebqwbsabxadgqROONG
Gt2ZTX8g4VSlceFyqr5c3248y12ihdnqkwkdqdwqdnwqidRj3kUDyI+Q+K4IocNZ1kf/E3u5h4RSvVBgM9ZMc4c+vPA2eHhxQiQik+/La
F7RqzuMQVDxTs/RR6gzZTmFw6OkHsZCIhIwm8RSNzQv3wb7PcpM8DEBOZYNUkXl+Ja4Rcpmxjz7x7BI5 1365125855562@qq.com
复制粘贴到你的 gitee 的 ssh 中。这样就可以在服务器端使用 git 了。
pm2
npm install pm2 -g
找一个文件夹,存放项目代码
cd /home
git clone git@gitee.com:suiboyu/readbook-express.git
像之前一样 cd readbook-express
,npm install
使用 pm2 开启服务
pm2 start app.js
看到这个,证明你成功开启了。
不出意外的话,访问 ip+端口,根据项目中的接口路由,在浏览器中进行访问。
万一要是失败,那就祝你好运了
参考文章:
https://www.jianshu.com/p/7aad651bdbb4
https://help.aliyun.com/document_detail/50775.html?spm=5176.11065259.1996646101.searchclickresult.73aa4729mcloGI
https://www.cnblogs.com/ansibee/p/8087476.html
https://blog.csdn.net/weixin_43322048/article/details/106749954