離婚協(xié)議書模板 完整版海南seo代理加盟供應(yīng)商
nextjs項目有多種部署方式,本文介紹最簡單的一種方式,將源碼上傳到云服務(wù)器,編譯后使用pm2后臺運(yùn)行nextjs工程。
檢查node、npm是否安裝
查看npm版本,如果版本較低先升級npm版本
npm -v
卸載
yum remove nodejs npm -y
安裝新版本
sudo yum install https://rpm.nodesource.com/pub_21.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y
sudo yum install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1
部署和編譯
-
拷貝工程源碼目錄到服務(wù)器,其中node_modules、.git不需要上傳
-
編譯
npm run build
使用pm2后臺運(yùn)行nextjs程序
- 安裝pm2
npm install -g pm2
- 運(yùn)行
#注意當(dāng)前是在工程目錄下,命令中的名稱可以任意命名,pm2管理中起到標(biāo)識作用,實(shí)際運(yùn)行的程序是當(dāng)前目錄下
pm2 start --name 自定義名稱 npm -- start
- 查看端口占用,如工程中使用的3000,端口列表中也出現(xiàn)了,說明后臺運(yùn)行成功了
netstat -nultp
- 如果服務(wù)器開放了對應(yīng)端口,可以加端口瀏覽器訪問項目,如果只能開放80,繼續(xù)nginx
安裝配置nginx
- 安裝nginx
yum install nginx -y
- nginx配置文件(/etc/nginx/etc/nginx.conf)
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;autoindex on;autoindex_exact_size off;autoindex_localtime on;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;server {listen 80 default_server;listen [::]:80 default_server;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}include /etc/nginx/vhosts/*.conf;
}
- 多工程配置
創(chuàng)建 /etc/nginx/vhosts/工程名.conf,自行修改域名,確保域名映射已設(shè)置好
server {listen 80;#443;server_name xxx.xxxx.com;root html;index index.html index.htm;location / {proxy_pass http://127.0.0.1:3000;#設(shè)置主機(jī)頭和客戶端真實(shí)地址,以便服務(wù)器獲取客戶端真實(shí)IPproxy_set_header Host $host;proxy_set_header X-Real-Ip $remote_addr;proxy_set_header X-Forwarded-For $remote_addr;}
}
- 啟動nginx
# 啟動
systemctl start nginx
# 停止
systemctl stop nginx
# 重啟
systemctl restart nginx
# 重新加載配置文件
nginx -s reload
- 使用域名訪問測試
pm2命令介紹
# 查看任務(wù)
pm2 list/# 重啟
pm2 restart app_name# 停止
pm2 stop app_name|app_id# 停止所有
pm2 stop all# 刪除
pm2 delete app_name|app_id# 刪除所有
pm2 delete all# 日志
pm2 logs app_name|app_id# 查看所有日志
pm2 logs