目錄
前言
我們使用應用程序、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:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html 。
相對其他客戶端軟件而言,Putty功能比較單一。 -
Bitvise SSH Client【推薦】
支持 SSH 和 SFTP 的 Windows 免費客戶端。登錄後自動打開一個終端窗口,一個SFTP窗口,特別適合需要終端及文件傳遞工作。
終端可設置高寬,在終端窗口菜單,可設置 Ctl+C 拷貝,Ctl+V粘貼,使用非常方便。
由 Bitvise 開發和提供專業支持。這款 SSH 客戶端性能強悍,擁有功能豐富的圖形界面,通過一個有自動重連功能的內置代理進行動態端口轉發。
對個人用戶使用是免費的,同時對於在組織內部的個人商業使用也一樣。你可以點擊這裏: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 }