1.磁盤掛載
許多朋友不清楚自己是否有數據盤需要掛載(雖然購買時自己買沒買你應該知道的,但還是有人不知道啊),那麼你應該在linux服務器上使用使用“fdisk -l”命令查看。如下圖:若您執行fdisk -l命令,發現沒有 /dev/xvdb 標明您的雲服務無數據盤,那麼您無需進行掛載,此時該教程對您不適用
-
如果上一步你看到了/dev/xvdb,那麼就執行“fdisk -S 56 /dev/xvdb”命令,對數據盤進行分區;根據提示,依次輸入“n”,“p”,“1”,兩次回車,人後輸入“wq”退出。分區就開始了,很快就會完成。
-
此時再使用“fdisk -l”命令可以看到,新的分區xvdb1已經建立完成了。
-
這時候需要注意,有兩種情況。第一種,你這個數據盤是新購買的,以前沒有用過(沒有存儲過數據),那麼就要對其進行格式化,使用“mkfs.ext3 /dev/xvdb1”命令。第二種,如果你之前就有數據盤,只是你把系統盤重置了,需要將數據盤再次掛載到重置後的系統上,那麼就不要對其格式化(跳過此步驟),直接執行後面的步驟掛載就可以了。
-
在繼續下面的命令前,你需要知道的是,下面那行命令的“/mnt”就是你要把數據盤掛載到的文件夾,如果你想掛載到別的文件夾,比如你自己建立的/mydata那麼就把/mnt改成/mydata,前提是你已經創建了這個目錄(創建目錄的命令是mkdir)。使用“echo '/dev/xvdb1 /mnt ext3 defaults 0 0' >> /etc/fstab”(不含引號)命令寫入新分區信息。然後使用“cat /etc/fstab”命令查看,出現以下信息就表示寫入成功。
-
最後,使用“mount -a”命令掛載新分區,然後用“df -h”命令查看,出現以下信息就說明掛載成功,可以開始使用新的分區了。
2.安裝Centos相關 openresty
yum -y update更新相關安裝包
2.1 mnt目錄新建相關文件夾
- 1、logs (訪問日誌文件夾)
- 2、project(項目文件夾)
- 3、ssl443 (https證書文件夾)
- 4、openresty (nginx文件夾)
- 5、tools (工具包文件夾)
2.2 安裝依賴
- 1、爲編譯程序做準備,執行如下命令
yum install pcre-devel openssl-devel gcc curl postgresql-dev
2.3安裝openresty
- 1.openresty-1.15.8.3.tar.gz放到tools文件夾下
- 2.解壓源碼包openresty-1.15.8.3.tar.gz 在tools目錄下
tar -zxvf openresty-1.15.8.3.tar.gz
- 3.cd 到openresty-1.15.8.3目錄下
cd /mnt/tools/openresty-1.15.8.3
- 4.配置openresty選項
./configure --prefix=/mnt/openresty --with-luajit --with-http_iconv_module --with-http_postgres_module
說明:/mnt/openresty 是openresty安裝目錄
- 5.接下來編譯並安裝源碼(都是在tools目錄openresty-1.15.8.3目錄下)
make && make install
- 6.配置nginx環境變量,打開/etc/profile文件加入如下信息 export PATH=$PATH:/mnt/openresty/nginx/sbin 然後執行命令
source /etc/profile (使修改的環境變量生效)
2.4 Centos 開機啓動Openresty之中的Nginx
- 1.在系統服務目錄(/usr/lib/systemd/system/)裏創建nginx.service文件。nginx.service內容如下
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/mnt/openresty/nginx/sbin/nginx
ExecReload=/mnt/openresty/nginx/sbin/nginx -s reload
ExecStop=/mnt/openresty/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
參數說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3
- 2.設置開機啓動,執行命令
systemctl enable nginx.service(開機啓動)
systemctl disable nginx.service (開機禁用)
2.5 nginx 命令
檢查配置文件nginx.conf文件的正確性命令 nginx -t檢測正確性
- 1.nginx -s reload
- 2.nginx -s stop
3.linux其他配置
3.1 安裝node.js和pm2
yum -y update
1.curl -sL https://rpm.nodesource.com/setup_10.x | bash -
2.yum install -y nodejs
3.npm install pm2 -g
3.2 安裝git
yum install -y git
3.3 本地生成ssh並且寫入gitee爲了服務器上每次git pull重複輸入密碼
1.ssh-keygen -t rsa -C "[email protected]"
2.三次回車直接使用默認值
3.cd ~/.ssh
4.cat id_rsa.pub 複製內容到gitee上
3.4 部署項目
1.在項目的package.json中script加入 "pm2": "pm2 start npm --name 'pc_hhb' -- run start"
2.npm run pm2
3.5 修改hosts
- 1.vi /etc/hosts進入host添加自己的api
- 2./etc/init.d/network restart 重新啓動
4.nginx開發部署
4.1新建conf文件
以hhb的前端項目爲例子,進入/mnt/openresty/nginx/conf/ 新加入 vhost文件夾,所有的新的配置都放在這裏。
## www.cbi360.net_hhb.conf內容
server {
listen 80;
listen 443 ssl;
server_name www.cbi360.net;
#添加https配置
include vhost/ssl.conf;
#lua腳本可以不使用
access_by_lua_file /mnt/openresty/nginx/conf/lua/access_by_mns.lua;
#自定義頭部這裏一定要加上
underscores_in_headers on;
#自定義日誌位置和格式
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log /mnt/logs/www.cbi360.net_hhb/$year-$month-$day-access.log;
error_log /mnt/logs/www.cbi360.net_hhb/error.log error;
include vhost/ua.conf;
#location ~* \.(png|jpg|jpeg|gif|js|css|bmp|flv|ico)$ {
# access_log off;
# proxy_pass http://127.0.0.1:3002;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection keep-alive;
# proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
#}
location ~* ^/hhb/sg_([0-9]+)/gs/? {
if ($http_user_agent ~* "(baiduspider|baiduspider/2.0)") {
return 404;
}
proxy_pass http://127.0.0.1:9527;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
#添加蜘蛛重定向
location ^~ /hhb {
proxy_pass http://127.0.0.1:9527;
include vhost/www.cbi360.net_hhb_spider_proxy.conf;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass http://127.0.0.1:9527;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
#添加健康檢測
include vhost/www.cbi360.net_hhb_monitor.conf;
}
4.2再nginx.conf中引入相關
進入/mnt/openresty/nginx/conf/ 找到nginx.conf
#這裏一定要加root 不然自定義日誌不會生效
user root;
worker_processes 4;
events {
worker_connections 10240;
}
http {
# 隱藏Nginx版本信息
server_tokens off;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#代理的相關參數設置
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
# 啓用gzip壓縮,提高用戶訪問速度
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].";
server_names_hash_bucket_size 128;
client_max_body_size 100m;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
resolver 8.8.8.8;
#增加虛擬主機
include vhost/www.cbi360.net_hhb.conf;
}
防火牆方面
- 1.設置開機啓用防火牆:systemctl enable firewalld.service
- 2.設置開機禁用防火牆:systemctl disable firewalld.service
- 3.啓動防火牆:systemctl start firewalld
- 4.關閉防火牆:systemctl stop firewalld
- 5.檢查防火牆狀態:systemctl status firewalld
1.開放指定端口
- 1.查看防火牆狀態:firewall-cmd --state
- 2.重新加載配置:firewall-cmd --reload
- 3.查看開放的端口:firewall-cmd --list-ports
- 4.開啓防火牆端口:firewall-cmd --zone=public --add-port=9200/tcp --permanent
- 5.關閉防火牆端口:firewall-cmd --zone=public --remove-port=9200/tcp --permanent
2.開啓對外端口號
添加端口 返回 success 代表成功(--permanent永久生效,沒有此參數重啓後失效)
//單個開放 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-port=443/tcp --permanent //批量開放 firewall-cmd --zone=public --add-port=80-85/tcp --permanent //關閉端口 firewall-cmd --zone=public --remove-port=443/tcp --permanent //重載 firewall-cmd --reload