aliyun服务器配置

操作系统:Ubuntu 14.04 64位

ssh远程登入
1
ssh root@47.97.127.190 (购买服务器的公有ip地址)
命令
  • fdisk -l:查看数据盘
  • df -h:查看硬盘使用情况
增加新用户
1
2
3
4
5
6
7
8
9
# adduser daodao (并增加密码等信息)
给用户设置权限 # gpasswd -a daodao sudo
# sudo visudo
在打开的文件中#User privilege specification
root ALl(ALL:ALL)ALL
daodao ALl(ALL:ALL)ALL
用刚才新增的用户登入:ssh daodao@47.97.127.190 (并验证密码)
!!如果登入失败,通过ssh restart命令重启一下再试
ssh无密码登入(通过github指示创建SSH key)
  • 创建本地SSH key 以及 远程服务器的SSH key

  • 在远程.ssh目录下新建authorized_keys,把本地的公钥放在这个文件中

  • 授权命令:chmod 600 authorized_keys

    ​ sudo service ssh restart (重启服务)

修改服务器默认端口
1
2
3
4
5
6
7
8
##在用户叨叨下输入命令:sudo vim /etc/ssh/sshd-config (并输入daodao用户的密码)
修改Port 2888 (范围在1024以上~65536, 0~1024最好别用,这些端口号系统会占用)
在最后新增一行AllowUsers daodao
然后重启服务器 sudo service ssh restart
!!!注意,需要在阿里云中设置安全组,添加2888端口为允许访问端口,否则无法登入
在这之后登入叨叨这个用户,使用加有-p参数的命令ssh -p 2888 daodao@47.97.127.190
禁用root登入
1
2
3
4
5
6
7
8
#dadao用户下
sudo vim /etc/ssh/sshd_config
----
PermitRootLogin no
也可以把密码登入给禁止PasswordAuthentication no
允许空密码登入 PermitEmptyPasswords no
-----
重启服务 sudo service ssh restart
ubuntu更新命令

sudo apt-get update && sudo apt-get upgrade

1
2
3
update 是更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的地址,这样才能获取到最新的软件包
upgrade 是升级已安装的所有软件包,升级之后的版本就是本地地址里的,因此,在执行 upgrade 之前一定要执行 update, 这样才能更新到最新的
安装Node.js环境及其他模块
1
sudo apt-get install vim openssl build-essential libssl-dev wget curl git
  • 安装nvm
1
2
3
#安装nvm,即node版本管理工具(地址:https://github.com/creationix/nvm)
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
或者: wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
1
2
3
4
5
6
7
## 通过nvm来安装node版本
nvm install v8.11.3
nvm use v8.11.3
nvm alias default v8.11.3
其他命令: node -v / npm -v 查看版本
nvm current 查看当前使用版本
nvm ls 查看安装的所有node版本
  • 设置淘宝镜像

    1
    2
    3
    4
    5
    6
    添加配置:
    npm config set registry https://registry.npm.taobao.org --global
    npm config set disturl https://npm.taobao.org/dist --global
    删除配置:
    npm config delete registry
    npm config delete disturl
  • ubuntu增加系统文件监控数目命令

    1
    echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
  • 通过npm安装包
    1
    npm i pm2 webpack -g
  • 测试vim app.js

    1
    2
    3
    4
    5
    6
    const http = require('http')
    http.createServer((req,res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'})
    res.end('welcome!')
    }).listen(8080)
    console.log('running on http://47.97.127.190:8080')

    注意: 要再阿里云的安全组中设置8080端口

借助pm2让nodejs服务常驻
1
2
3
4
5
6
7
8
9
#直接运行命令pm2 start xxx.js
#其他命令
- pm2 list:查看服务器列表
- pm2 show name:查看详细信息展示
- pm2 logs:查看所有应用程序的日志
- pm2 logs[app-name]:查看指定的日志
- pm2 monit:查看每个应用程序cpu和内存的占用情况
- pm2 stop:停止应用
- pm2 stop 0:停止id为0的应用程序
配置Nginx反向代理Nodejs端口

daodao用户下是没有root权限的,无法访问root用户下的0~1024端口

  • 安装nginx: sudo apt-get install nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    1. cd /etc/nginx/conf.d
    2. 新建配置文件 sudo vi daodao-com-2888.conf //配置文件命名写清楚摘要信息
    upstream daodao {
    server 127.0.0.1:8080;
    }
    server {
    listen 80;
    server_name 47.97.127.190;
    location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }
    3. sudo nginx -t 检测是否正确
    4. 重启nginx: sudo nginx -s reload