远程服务器连接
关于服务器的连接,windows下可以选择Xshell、SecureCRT、Putty 等,Mac下可以选择SecureCRT 、FinallShell等。在这里我通过mac终端连接阿里云的Linux服务器(CentOS)。
1. 切换到root权限下
- sudo -I
2. 通过ssh连接linux服务器,根据提示输入远程服务器密码
- ssh root@iptest.com
bogon:~ xuqingbiao$ sudo -I
Password:
bogon:~ root# ssh root@iptest.com
root@iptest.com's password:
Last failed login: Thu Mar 21 09:49:14 CST 2019 from 114.113.64.173 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Mar 21 09:07:38 2019 from 114.113.64.173
Welcome to Alibaba Cloud Elastic Compute Service !
至此,连接远程服务器成功。
3. 介绍几个常用的命令
1 mac上传文件到Linux服务器
scp /Users/xuqingbiao/demo root@iptest.com:/work
2 mac上传文件夹到Linux服务器
scp -r /Users/xuqingbiao/demo root@iptest.com:/work
3 Linux服务器下载文件到mac
scp root@iptest.com:/work/demo /Users/xuqingbiao/
4 Linux服务器下载文件夹到mac
scp -r root@iptest.com:/work/demo /Users/xuqingbiao/
远程连接失败时:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:febs6WhV+vyqocDpgParTocBLO92LCM4DgTAiHIQn7w.
Please contact your system administrator.
Add correct host key in /Users/xuqingbiao/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/xuqingbiao/.ssh/known_hosts:2
RSA host key for 47.105.181.201 has changed and you have requested strict checking.
Host key verification failed.
lost connection
删除/Users/xuqingbiao/.ssh/known_host目录下的缓存
或者
ssh-keygen -R IP地址
node环境搭建
1. 配置npm的源
- curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
[root@iZm5e33l0ge76uqi8nq87aZ ~]# curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
## Installing the NodeSource Node.js 8.x LTS Carbon repo...
## Inspecting system...
+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m
## Confirming "el7-x86_64" is supported...
+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
## Downloading release setup RPM...
+ mktemp
+ curl -sL -o '/tmp/tmp.AIyIQvYadg' 'https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
## Installing release setup RPM...
+ rpm -i --nosignature --force '/tmp/tmp.AIyIQvYadg'
## Cleaning up...
+ rm -f '/tmp/tmp.AIyIQvYadg'
## Checking for existing installations...
+ rpm -qa 'node|npm' | grep -v nodesource
## Run `sudo yum install -y nodejs` to install Node.js 8.x LTS Carbon and npm.
## You may also need development tools to build native addons:
sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn
2. 根据提示运行 yum install -y nodejs
- yum install -y nodejs
已安装:
nodejs.x86_64 2:8.15.1-1nodesource
完毕!
[root@iZm5e33l0ge76uqi8nq87aZ ~]#
Installed:
nodejs.x86_64 2:8.15.1-1nodesource Complete!
[root@iZm5e33l0ge76uqi8nq87aZ ~]#
3. 出现以上其一,node环境已经安装完成,检查node版本
[root@iZm5e33l0ge76uqi8nq87aZ ~]# node -v
v8.15.1
[root@iZm5e33l0ge76uqi8nq87aZ ~]#
4. 安装淘宝镜像
- npm install -g cnpm --registry=https://registry.npm.taobao.org
[root@iZm5e33l0ge76uqi8nq87aZ ~]# npm install -g cnpm --registry=https://registry.npm.taobao.org
npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
/usr/bin/cnpm -> /usr/lib/node_modules/cnpm/bin/cnpm
+ cnpm@6.0.0
added 679 packages from 898 contributors in 15.191s
5. 安装express生成器工具
- cnpm install express-generator -g
[root@iZm5e33l0ge76uqi8nq87aZ ~]# cnpm install express-generator -g
Downloading express-generator to /usr/lib/node_modules/express-generator_tmp
Copying /usr/lib/node_modules/express-generator_tmp/_express-generator@4.16.0@express-generator to /usr/lib/node_modules/express-generator
Installing express-generator's dependencies to /usr/lib/node_modules/express-generator/node_modules
[1/5] commander@2.13.0 installed at node_modules/_commander@2.13.0@commander
[2/5] sorted-object@2.0.1 installed at node_modules/_sorted-object@2.0.1@sorted-object
[3/5] ejs@2.5.7 installed at node_modules/_ejs@2.5.7@ejs
[4/5] mkdirp@0.5.1 installed at node_modules/_mkdirp@0.5.1@mkdirp
[5/5] minimatch@3.0.4 installed at node_modules/_minimatch@3.0.4@minimatch
All packages installed (9 packages installed from npm registry, used 412ms(network 409ms), speed 240.39kB/s, json 9(16.66kB), tarball 81.66kB)
[express-generator@4.16.0] link /usr/bin/express@ -> /usr/lib/node_modules/express-generator/bin/express-cli.js
6. 服务器下新建一个work文件夹,并在该文件夹下初始化一个express
- cd ..
- mkdir work
- express demo
create : demo/
create : demo/public/
create : demo/public/javascripts/
create : demo/public/images/
create : demo/public/stylesheets/
create : demo/public/stylesheets/style.css
create : demo/routes/
create : demo/routes/index.js
create : demo/routes/users.js
create : demo/views/
create : demo/views/error.jade
create : demo/views/index.jade
create : demo/views/layout.jade
create : demo/app.js
create : demo/package.json
create : demo/bin/
create : demo/bin/www
change directory:
$ cd demo
install dependencies:
$ npm install
run the app:
$ DEBUG=demo:* npm start
[root@iZm5e33l0ge76uqi8nq87aZ work]# ls
7.安装对应的依赖
- cd demo
- cpm install
[root@iZm5e33l0ge76uqi8nq87aZ work]# cd demo
[root@iZm5e33l0ge76uqi8nq87aZ demo]# cnpm install
✔ Installed 6 packages
✔ Linked 85 latest versions
✔ Run 0 scripts
deprecate jade@~1.11.0 Jade has been renamed to pug, please install the latest version of pug instead of jade
deprecate jade@1.11.0 › constantinople@~3.0.1 Please update to at least constantinople 3.1.1
deprecate jade@1.11.0 › transformers@2.1.0 Deprecated, use jstransformer
✔ All packages installed (100 packages installed from npm registry, used 2s(network 2s), speed 1.08MB/s, json 91(213.77kB), tarball 1.59MB)
8. 启动应用
- DEBUG=demo:* npm start
[root@iZm5e33l0ge76uqi8nq87aZ demo]# DEBUG=demo:* npm start
> demo@0.0.0 start /work/demo
> node ./bin/www
demo:server Listening on port 3000 +0ms
9.打开浏览器,输入 Ip地址:3000 即可查看效果
express.png10. vim编辑器编辑users文件添加用户接口
var express = require('express');
var router = express.Router();
var User = require('./user')
var URL = require('url')
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/getUserInfo', function(req, res, next) {
var user = new User();
var params = URL.parse(req.url, true).query;
if(params.id == '1') {
user.name = "徐文俊";
user.age = "18";
user.city = "北京";
} else {
user.name = "Ms.xu";
user.age = "2";
user.city = "广东市";
}
var response = {status:1,data:user};
res.send(JSON.stringify(response))
});
module.exports = router;
11. 启动项目,打开浏览器,输入 Ip地址:3000/users/getUserInfo 即可查看效果
user.png12. 安装pm2依赖
- cnpm install pm2 -g
Recently updated (since 2019-03-14): 1 packages (detail see file /usr/lib/node_modules/pm2/node_modules/.recently_updates.txt)
2019-03-20
→ @pm2/io@4.1.2 › @pm2/agent-node@1.1.8 › proxy-agent@^3.0.3(3.1.0) (12:11:51)
All packages installed (273 packages installed from npm registry, 1 packages installed from remote url, used 4s(network 4s), speed 1.46MB/s, json 235(427.95kB), tarball 5.9MB)
[pm2@3.4.0] link /usr/bin/pm2@ -> /usr/lib/node_modules/pm2/bin/pm2
[pm2@3.4.0] link /usr/bin/pm2-dev@ -> /usr/lib/node_modules/pm2/bin/pm2-dev
[pm2@3.4.0] link /usr/bin/pm2-docker@ -> /usr/lib/node_modules/pm2/bin/pm2-docker
[pm2@3.4.0] link /usr/bin/pm2-runtime@ -> /usr/lib/node_modules/pm2/bin/pm2-runtime
13. 执行相关任务
- pm2 startup systemd
14. 保存配置
pm2 save
15. cd到项目,pm2启动node项目
启动 pm2 start ./bin/www
停止 pm2 stop all
重启 pm2 start -f ./bin/www
[root@iZm5e33l0ge76uqi8nq87aZ 0321node]# pm2 start ./bin/www
[PM2] Starting /work/0321node/bin/www in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ www │ 0 │ 0.0.0 │ fork │ 21200 │ online │ 0 │ 0s │ 0% │ 13.8 MB │ root │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
16. 查看应用运行情况
- pm2 show www
[root@iZm5e33l0ge76uqi8nq87aZ 0321node]# pm2 show www
Describing process with id 0 - name www
┌───────────────────┬───────────────────────────────┐
│ status │ online │
│ name │ www │
│ version │ 0.0.0 │
│ restarts │ 0 │
│ uptime │ 3m │
│ script path │ /work/0321node/bin/www │
│ script args │ N/A │
│ error log path │ /root/.pm2/logs/www-error.log │
│ out log path │ /root/.pm2/logs/www-out.log │
│ pid path │ /root/.pm2/pids/www-0.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ /work/0321node │
│ exec mode │ fork_mode │
│ node.js version │ 8.15.1 │
│ node env │ N/A │
│ watch & reload │ ✘ │
│ unstable restarts │ 0 │
│ created at │ 2019-03-21T07:31:36.690Z │
└───────────────────┴───────────────────────────────┘
Code metrics value
┌────────────────────────┬───────┐
│ Heap Size │ 13.83 │
│ Heap Usage │ 71.93 │
│ Used Heap Size │ 9.95 │
│ Active requests │ 0 │
│ Active handles │ 4 │
│ Event Loop Latency │ 0.62 │
│ Event Loop Latency p95 │ 1.69 │
└────────────────────────┴───────┘
Add your own code metrics: http://bit.ly/code-metrics
Use `pm2 logs www [--lines 1000]` to display logs
Use `pm2 env 0` to display environement variables
Use `pm2 monit` to monitor CPU and Memory usage www
Nginx配置,承载对应的静态文件请求
1.安装epel库
- yum install epel-release -y
[root@iZm5e33l0ge76uqi8nq87aZ /]# yum install epel-release -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 epel-release.noarch.0.7-11 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================
Package 架构 版本 源 大小
=======================================================================================================
正在安装:
epel-release noarch 7-11 epel 15 k
事务概要
=======================================================================================================
安装 1 软件包
总下载量:15 k
安装大小:24 k
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : epel-release-7-11.noarch 1/1
警告:/etc/yum.repos.d/epel.repo 已建立为 /etc/yum.repos.d/epel.repo.rpmnew
验证中 : epel-release-7-11.noarch 1/1
已安装:
epel-release.noarch 0:7-11
完毕!
2.安装nginx
- yum -y install nginx
验证中 : gperftools-libs-2.6.1-1.el7.x86_64 20/25
验证中 : libX11-common-1.6.5-2.el7.noarch 21/25
验证中 : freetype-2.8-12.el7_6.1.x86_64 22/25
验证中 : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 23/25
验证中 : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 24/25
验证中 : freetype-2.4.11-15.el7.x86_64 25/25
已安装:
nginx.x86_64 1:1.12.2-2.el7
作为依赖被安装:
dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7
fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7
gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7
libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7
libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7
libjpeg-turbo.x86_64 0:1.2.90-6.el7 libpng.x86_64 2:1.5.13-7.el7_2
libxcb.x86_64 0:1.13-1.el7 libxslt.x86_64 0:1.1.28-5.el7
nginx-all-modules.noarch 1:1.12.2-2.el7 nginx-filesystem.noarch 1:1.12.2-2.el7
nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7
nginx-mod-mail.x86_64 1:1.12.2-2.el7 nginx-mod-stream.x86_64 1:1.12.2-2.el7
作为依赖被升级:
freetype.x86_64 0:2.8-12.el7_6.1
完毕!
3.启动nginx
- systemctl start nginx.service
4. 查看nginx状态 nginx -t
[root@iZm5e33l0ge76uqi8nq87aZ conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5. 配置反向代理
- vim /etc/nginx/conf.d/demo.conf
upstream cloud-app {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 3389;
server_name 47.105.181.201;
root /root/cloud;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://cloud-app/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
6. 检查nginx状态,没问题的话 重启nginx
- nginx -t
- nginx -s reload
[root@iZm5e33l0ge76uqi8nq87aZ conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@iZm5e33l0ge76uqi8nq87aZ conf.d]# nginx -s reload
网友评论