腾讯云主机linux(centos7)服务器基本操作和安装日志

前言

我们使用应用程序、web网页等数据,都来源于云端服务器。
世界上90%的服务器使用的都是linux操作系统,最早由一个名叫linux的学生开发的,后来用他的名字命名。
linux流行的主要原因是因为该操作系统开源、免费、稳定、强大。
我们租用阿里云、腾讯云主机,运行的一般也是linux操作系统。因此,需要掌握一些技能,来使用该服务器。
该篇文章主要讲解如何登陆(远程)服务器、基本安装操作的命令、node安装管理、redis缓存服务安装、mongodb数据库安装、nginx安装、nginx简单的配置。

1. 标准登录方式

服务器都在云端机房,我们操作服务器只能通过网络连接,在自己计算机上远程操作服务器。
如果使用的苹果或linux系统,不需要安装其他软件,就可以通过网络连接到服务器。
如果使用windows,则需要安装专门的客户端软件才能连接到服务器。

1.1 使用 Windows 系统的电脑

  • 下载安装 Windows SSH 和 Telnet 客户端工具 Putty。下载Putty:http://www.putty.org/
  • 用户名:root,Host:<主机ip>。
  • 按照 Putty 使用帮助进行登录。 Putty 密码方式使用帮助。

1.2 使用 Linux/Mac OS X 系统的电脑(使用密码登录)

  • 1、打开 SSH 客户端(Mac可使用系统自带的终端)。
  • 2、输入:ssh -q -l root -p 22 <主机ip>。
  • 3、输入 CVM 实例密码进行登录。

1.3 使用 Linux/Mac OS X 系统的电脑(使用密钥登录)

  • 打开 SSH 客户端(Mac可使用系统自带的终端)。
  • 查找您实例关联的 SSH 密钥文件本地存放地址。
  • 您的密钥必须不公开可见,SSH 才能工作。请使用此命令: chmod 400 [密钥文件路径]。
  • 输入命令: ssh [-i 密钥文件路径] root@<主机ip>。

1.4 比较流行的软件有

  • Putty
    Putty 是最有名的 SSH 和 telnet 客户端,简单易用。最初由 Simon Tatham 为 Windows 平台开发。Putty 是一款开源软件,有可用的源代码,和一群志愿者的开发和支持。
    Putty图片
    Putty 非常易于安装和使用,通常大部分的配置选项你都不需要修改。你只需要输入少量基本的参数,就可以开始很简单地建立连接会话。点此下载Putty:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
    相对其他客户端软件而言,Putty功能比较单一。

  • Bitvise SSH Client【推荐】
    支持 SSH 和 SFTP 的 Windows 免费客户端。登录后自动打开一个终端窗口,一个SFTP窗口,特别适合需要终端及文件传递工作。
    终端可设置高宽,在终端窗口菜单,可设置 Ctl+C 拷贝,Ctl+V粘贴,使用非常方便。
    由 Bitvise 开发和提供专业支持。这款 SSH 客户端性能强悍,拥有功能丰富的图形界面,通过一个有自动重连功能的内置代理进行动态端口转发。
    Bitvise SSH Client图片
    对个人用户使用是免费的,同时对于在组织内部的个人商业使用也一样。你可以点击这里:http://www.bitvise.com/download-area下载 Bitvise SSH 客户端。

    注意:使用时需要更改编码方式,推荐使用UTF-8

  • MobaXterm
    是你的远程计算的终极工具箱。在一个 Windows 应用里,它为程序员、网管、IT 管理员及其它用户提供了精心裁剪的一揽子功能,让他们的远程操作变得简约时尚。

    提供了所有重要的远程网络工具 (如SSH、 X11、 RDP、 VNC、 FTP、 MOSH 等等),以及 Windows 桌面上的Unix 命令(bash、 ls、 cat、sed、 grep、 awk、 rsync等等),而这些都是由一个开箱即用的单一的便携程序所提供。MobaXterm 对个人使用免费,你可以点击这里http://mobaxterm.mobatek.net/download.html下载 MobaXterm。
    在使用中,一会没有发指令,就中断了,需要重连,非常麻烦,不知道是配置问题,还是什么?。

  • Cygwin
    是一款 GNU 和开源工具的大杂烩,提供的功能近似于一个 Windows 平台下的 Linux。

    Cygwin 包括了一个称为模拟库的 Unix 系统:cygwin.dll,集成了大量的 GNU 和其它的免费软件,以大量的可选包方式组织而成。在这些安装包中,有高质量的编译器和其他软件开发工具、一个X11服务器、一套完整的X11开发套件、GNU emacs 编辑器、Tex 和 LaTeX、openSSH(客户端和服务器),除此之外还有很多,包括在微软 Windows 下需要编译和使用 PhysioToolkit 软件的每一样东西。

  • windows10 新的版本,好像可以安装 linux内核,可以实现类似功能。

2. 基本操作指令

2.1 查看服务器状态

查看系统版本信息 lsb_release -a
如果提示错误 command not found
需要安装lsb yum install lsb –y

[root@VM_0_4_centos ~]$ ylsb_release -a
LSB Version::core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID:	CentOS
Description   :	CentOS Linux release 7.5.1804 (Core) 
Release        :	7.5.1804
Codename   :	Core
  • 查看内存
[root@VM_0_4_centos ~]$ sed -n '/MemTotal\|MemFree/p' /proc/meminfo
MemTotal:        1015564 kB
MemFree:          223076 kB
  • 查看磁盘空间 df -h

3. 安装软件

连上服务器之后,一般都需要安装软件,linux上安装软件,一般有如下几种方法。

3.1 yum

yum(Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器。
基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。
yum提供了查找、安装、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。

3.2 yum自动选择最快的源

由于yum中有的mirror速度是非常慢的,如果yum选择了这个mirror,这个时候yum就会非常慢,对此,可以下载fastestmirror插件,它会自动选择最快的mirror。
yum install yum-fastestmirror

3.3 CentOS配置网易163yum源

首先备份系统内置源,保证出错后可以恢复
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

下载对应版本repo文件, 放入/etc/yum.repos.d/

cd /etc/yum.repos.d/
##注意,CentOS7、CentOS6、CentOS5 对应的库不一样
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

运行以下命令生成缓存

yum clean all
yum makecache

查看yum源 yum repolist

[root@VM_0_4_centos ~]$ yum repolist
repo id                               repo name                                               status
base/7/x86_64                         CentOS-7 - Base - 163.com                                9,591
epel/7/x86_64                         EPEL for redhat/centos 7 - x86_64                       12,219
extras/7/x86_64                       CentOS-7 - Extras - 163.com                                329
updates/7/x86_64                      CentOS-7 - Updates - 163.com                             1,698
repolist: 23,837

3.4 yum常用命令

  • 列出所有可更新的软件清单命令:yum check-update
  • 更新所有软件命令:yum update
  • 仅安装指定的软件命令:yum install <package_name>
  • 仅更新指定的软件命令:yum update <package_name>
  • 列出所有可安装的软件清单命令:yum list
  • 删除软件包命令:yum remove <package_name>
  • 查找软件包 命令:yum search <keyword>
  • 查看程序包简要信息 命令:yum info <package_name>
  • 清除缓存命令:
    yum clean packages:清除缓存目录下的软件包。
    yum clean headers:清除缓存目录下的 headers
    yum clean oldheaders: 清除缓存目录下旧的 headers
    yum clean, yum clean all (等于 yum clean packages或者yum clean oldheaders)` :清除缓存目录下的软件包及旧的headers

3.5 安装常用软件

yum install lsb -y
yum install git -y
yum install gcc -y
yum install make -y
yum install tcl -y
yum install gcc-c++ -y
yum install perl –y

4. 安装JS服务端运行环境

4.1 安装node.js,使用nvm安装node,方便切换版本

  • 安装nvm

    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
    
  • 使环境变量生效
    source ~/.bashrc

  • 查看nvm是否安装成功

nvm --version
0.33.8
  • 修改 .bash_profile ,将如下脚本写入,如果采用了**source ~/.bashrc**,该方法可忽略

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
    
  • 常用命令
    nvm ls-rmote 查看服务器上的有效版本 。

    nvm ls-rmote
    ## 输出结果,省略部分
    	...
    	v10.15.0   (Latest LTS: Dubnium)
    	v11.0.0
    	v11.1.0
    	v11.2.0
    	v11.3.0
    	v11.4.0
    	v11.5.0
    	v11.6.0
    
  • 安装指定版本
    nvm install v9.4.0

  • 查看已经安装到本地的node版本

    [root@VM_0_4_centos ~]$ nvm ls
    ->      v10.0.0
    default -> v10.0.0
    node -> stable (-> v10.0.0) (default)
    stable -> 10.0 (-> v10.0.0) (default)
    iojs -> N/A (default)
    lts/* -> lts/dubnium (-> N/A)
    lts/argon -> v4.9.1 (-> N/A)
    lts/boron -> v6.16.0 (-> N/A)
    lts/carbon -> v8.15.0 (-> N/A)
    lts/dubnium -> v10.15.0 (-> N/A)
    
  • 切换指定node版本
    nvm use v9.4.0

  • 查看当前使用node的版本
    nvm current

  • 查看node是否安装成功

    [root@VM_0_4_centos ~]$ nvm use v10.0.0
    Now using node v10.0.0 (npm v5.6.0)
    [root@VM_0_4_centos ~]$ node -v
    v10.0.0
    [root@VM_0_4_centos ~]$ npm -v
    5.6.0
    

4.2 设置开机启动缺省版本及项目指定node版本

系统缺省:.bash_profile 中设置 nvm alias default 9.4.0
如果不设置缺省版本,每次重启计算机,都需要按项目设置不同的版本。
如果项目要用到不同版本的 node,在项目目录下用 .nvmrc 设置缺省的 nvm use 版本号。
也可以在 package.json 的各个 script 入口代码加上 nvm use。
这项执行 start/test 等脚本前就会先 use 一下。

Example: 
nvm run 0.10.24 myApp.js #Run myApp.js using node v0.10.24  
Note: to remove, delete or uninstall nvm - just remove ~/.nvm, ~/.npm and ~/.bower folders

4.3 安装启动程序pm2

node.js 程序虽然可以通过 node 来启动,但是一旦出错,程序就死掉了。
pm2 相当于一个守护程序,node.js程序退出时,能自动重启。并且能控制同时启动多个程序进程,并发工作,不过,如果服务程序使用了 websocket 服务端状态,多个进程会出问题。
nginx 会将同一个用户转发到不同进程,会导致状态在有的进程不存在。需要将状态放入redis,多进程共享。
传统的 web server,解决了这个问题,node.js 服务,需要我们自己手动解决多进程的状态共享问题。

  • 常用命令
##全局安装
	npm install pm2 -g
##查看版本
	pm2 --version
##启动方法1
	pm2 start app.js --name="app"
##启动方法2
	pm2 start id|name

5. 安装缓存服务 redis

Redis命令参考:http://redisdoc.com/index.html

  • 安装
    yum -y install redis

  • 启动方式1,使用systemctl控制启动服务redis.service
    systemctl start redis.service或者service redis start

    [root@VM_0_4_centos ~]$ service redis start
    Redirecting to /bin/systemctl start redis.service
    
  • 启动方式2,需要配置文件 /etc/redis.conf
    redis-server /etc/redis.conf

  • 如果需要对外网开放,需打开防火墙端口6379
    iptables -I INPUT -p tcp --dport 6379 -j accept

  • 设置redis密码
    打开配置文件,找到其中的# requirepass foobared,去掉前面的#,并把foobared改成你的密码。
    redis.conf文件默认在/etc目录下,你可以更改它的位置和名字,更改后,注意在文件 /usr/lib/systemd/system/redis.service 中,把 ExecStart=/usr/bin/redis-server /etc/redis/6379.conf --daemonize no 中的redis.conf的路径改成的新的路径。

  • 查看redis是否开启

    [root@VM_0_4_centos ~]$ ps -ef | grep redis
    redis    25256     1  0 13:36 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379
    root     25267 14623  0 13:36 pts/1    00:00:00 grep --color=auto redis
    
  • 进入redis服务

    [root@VM_0_4_centos ~]$ redis-cli
    127.0.0.1:6379> ?
    redis-cli 3.2.12
    To get help about Redis commands type:
    	  "help @<group>" to get a list of commands in <group>
    	  "help <command>" for help on <command>
    	  "help <tab>" to get a list of possible help topics
    	  "quit" to exit
    
    To set redis-cli perferences:
    	  ":set hints" enable online hints
    	  ":set nohints" disable online hints
    Set your preferences in ~/.redisclirc
    127.0.0.1:6379> get
    (error) ERR wrong number of arguments for 'get' command
    127.0.0.1:6379> get key
    (nil)
    127.0.0.1:6379> get key abc
    (error) ERR wrong number of arguments for 'get' command
    127.0.0.1:6379> set key abc
    OK
    127.0.0.1:6379> get key
    "abc"
    127.0.0.1:6379> quit
    ```sehll
    
    
  • 关闭服务

    [root@VM_0_4_centos ~]$ systemctl start redis.service
    [root@VM_0_4_centos ~]$ ps -ef | grep redis
    redis    26169     1  0 13:46 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379
    root     26174 14623  0 13:46 pts/1    00:00:00 grep --color=auto redis
    [root@VM_0_4_centos ~]$ redis-cli
    127.0.0.1:6379> PING
    PONG
    127.0.0.1:6379> SHUTDOWN
    not connected> 
    
    [root@VM_0_4_centos ~]$ redis-cli
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    not connected> 
    
    [root@VM_0_4_centos ~]$ ps -ef | grep redis
    root     26274 14623  0 13:47 pts/1    00:00:00 grep --color=auto redis
    
  • 开放端口6379、6380的防火墙

    ##开启6379
    	/sbin/iptables -I INPUT -p tcp --dport 6379  -j ACCEPT
    ##开启6380
    	/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
    ##保存
    	/etc/rc.d/init.d/iptables save
    

6. 安装服务 MongoDB 数据库

  • 创建仓库
    vi /etc/yum.repos.d/mongodb-org-3.6.repo

  • 同时复制如下内容,粘贴至上面的文件内,然后保存,保存命令:
    1、按Esc
    2、输入英文状态下的冒号:
    3、输入保存退出wq回车。

    [mongodb-org-3.6]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
    
  • 安装
    yum install -y mongodb-org

  • 启动
    systemctl start mongod.service

  • 查看进程

    [root@VM_0_4_centos yum.repos.d]$ ps -ef | grep mongod
    mongod   28396     1  1 14:12 ?        00:00:00 /usr/bin/mongod -f /etc/mongod.conf
    root     28513 14623  0 14:13 pts/1    00:00:00 grep --color=auto mongod
    
  • 重启
    systemctl restart mongod.service

  • 停止
    systemctl stop mongod.service

  • 连接mongoDB

    [root@VM_0_4_centos ~]$ mongo 127.0.0.1:27017
    MongoDB shell version v3.6.9
    connecting to: mongodb://127.0.0.1:27017/test
    Implicit session: session { "id" : UUID("8389582d-a160-4bfa-af8e-04afd8599503") }
    MongoDB server version: 3.6.9
    Server has startup warnings: 
    2018-12-29T14:12:42.238+0800 I STORAGE  [initandlisten] 
    2018-12-29T14:12:42.238+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2018-12-29T14:12:42.238+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2018-12-29T14:12:42.955+0800 I CONTROL  [initandlisten] 
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] 
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] 
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] 
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2018-12-29T14:12:42.956+0800 I CONTROL  [initandlisten] 
    > 
    > use test
    switched to db test
    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB
    > 
    

7. 安装 nginx 服务

7.1安装nginx

  • 建立服务文件
    vi /usr/lib/systemd/system/nginx.service

  • 服务文件内容如下

    [Unit]
    Description=nginx - high performance web server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    ExecStart=/opt/nginx/sbin/nginx
    ExecReload=/opt/nginx/sbin/nginx -s reload
    ExecStop=/opt/nginx/sbin/nginx -s stop
    
    [Install]
    WantedBy=multi-user.target
    
  • 上述服务文件内容解释说明:

    ##服务的说明
    	[Unit]
    ##描述服务
    	Description=nginx - high performance web server
    ##描述服务类别
    	After=network.target remote-fs.target nss-lookup.target
    
    ##服务运行参数的设置
    ##注意:启动、重启、停止命令全部要求使用绝对路径
    	[Service]
    ##后台运行的类型
    	Type=forking
    ##服务的具体运行命令
    ##开始
    	ExecStart=/opt/nginx/sbin/nginx
    ##重启
    	ExecReload=/opt/nginx/sbin/nginx -s reload
    ##停止
    	ExecStop=/opt/nginx/sbin/nginx -s stop
    ##表示给服务分配独立的临时空间
    	#PrivateTmp=True
    
    ##服务安装的相关设置,可设置为多用户
    	[Install]
    	WantedBy=multi-user.target
    
  • 使文件生效
    systemctl daemon-reload

  • 安装
    yum install -y nginx

  • 启动和查看进程

    [root@VM_0_4_centos KoaService]$ systemctl start nginx.service
    [root@VM_0_4_centos KoaService]$  ps -ef | grep nginx
    root      1056     1  0 15:02 ?        00:00:00 nginx: master process /usr/sbin/nginx
    nginx     1057  1056  0 15:02 ?        00:00:00 nginx: worker process
    root      1123 14623  0 15:03 pts/1    00:00:00 grep --color=auto nginx
    

7.2 配置nginx

  • 浏览器访问查看是否成功:http://<主机ip>,ngnix自动监听80端口,访问成功如下截图:
    浏览器访问截图

  • 设置开机自启动

    [root@VM_0_4_centos KoaService]$ systemctl enable nginx.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
  • 操作命令

    ##启动nginx服务
    	systemctl start nginx.service
    ##停止开机自启动
    	systemctl disable nginx.service
    ##查看服务当前状态
    	systemctl status nginx.service
    ##重新启动服务
    	systemctl restart nginx.service
    
  • nginx配置文件

    • 默认需要我们自己配置文件 /etc/nginx/conf.d

    • /etc/nginx/conf.d 下新建配置文件my.conf

    • 创建文件夹和文件命令:https://jingyan.baidu.com/article/ca00d56c47578be99febcf5c.html

      [root@VM_0_4_centos conf.d]$ touch my.conf
      [root@VM_0_4_centos conf.d]$ ls
      my.conf
      
    • my.conf 内容如下

      upstream myapp {
      	server 127.0.0.1:3031;
      }
      server {
      	listen      80;
      	server_name www.xxx.com;
      
      	location / {
      		proxy_pass http://myapp;
      		proxy_http_version 1.1;
      		proxy_set_header Upgrade $http_upgrade;
      		proxy_set_header Connection 'upgrade';
      	}
      }
      
    • 重启服务 systemctl restart nginx.service

    • 浏览器访问地址验证是否成功:http://www.xxx.com

8. 启动后台服务 systemctl

  • systemctl替代 chkconfig和service,系统服务在 /usr/lib/systemd/system 目录,可以通过 ls 查看。

    ##查询服务是否开机启动。得到的值可以是enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。
    	systemctl is-enabled servicename.service
    ##开机运行服务。说明:启用服务就是在当前“runlevel”的配置文件目录`/etc/systemd/system/multi-user.target.wants/`,建立`/usr/lib/systemd/system` 里面对应服务配置文件的软链接。
    	systemctl enable *.service
    ##取消开机运行
    	systemctl disable *.service
    ##启动服务
    	systemctl start *.service
    ##停止服务
    	systemctl stop *.service
    ##重启服务
    	systemctl restart *.service
    ##重新加载服务配置文件
    	systemctl reload *.service
    ##查询服务运行状态
    	systemctl status *.service
    ##查看所有已启动的服务
    	systemctl list-units --type=service	
    	systemctl list-unit-files|grep enabled	
    ##显示启动失败的服务
    	systemctl --failed 
    

9. 启动一个 Node 程序(案例)

  • 程序放置路径 /home/node
  • 操作流程
    cd /
    cd home/node
    git clone https://gitee.com/hpmogul/KoaService.git
    cd KoaService
    npm i 
    npm run build
    npm run pm2start ## 安装了 pm2 的启动方式,在项目根目录启动,否则提示找不到"app"
    
  • pm2 启动
    pm2 start process.prod.json
  • pm2 配置文件 process.prod.json
    {
        "name": "app", //启动的应用名称
        "script": "lib/index.js", //项目启动文件路径
        "cwd": "./", //项目根目录
        "exec_mode": "fork", //单机模式,
        "watch": ["config","lib","view"], //需要监听的文件夹,有改变自动重启
        "ignore_watch": ["node_modules","log","public","upload","test"], //不需要监听
        "env": {"NODE_ENV": "production"}, //正式环境
        "engines": {"node": ">=6.0"} //node版本,大于等于6.x
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章