部署web服务器
Jonnzer Lv4

前言

部署Node应用和web应用,都需要部署到云服务器。

那么,今天来记录下怎么一个云服务器如何构建,以满足web开发的基本使用。


准备工作

服务器选购

首先得买台云服务器,我选的是腾讯的 centos系统,配置是 cpu 2核内存4G的云服务器。
如果喜欢命令行方式,推荐选 centos
更喜欢图形化界面,也可以选 windowServer 或者其他系统。
img

安装功能(可使用 yum wget
  • Node(自带Npm)
  • Nvm: Node版本管理工具
  • Nginx: 映射多页面多端口,可以部署多个web应用
  • pm2: Node进程管理工具 (npm i -g pm2)

可选:

  • lsb_release: 查看Linux发行版本 (yum install lsb_release)

其中,
NodeJs安装 这块,可以参考文档部署NodeJs环境

PM2 需要用到的基本命令:
(1) pm2 start index.jsNode Application entry file):启动Node服务
(2) pm2 list : 查看所有正在使用pm2的服务列表
(3) pm2 monit: 查看 cpu和内存占用情况

Nginx安装这块,可以参考文档Nginx安装
Nginx 简单部署多页面,并且用域名访问教程

Nginx 一些常用命令:
(1) nginx -t: 修改配置后,检测语法是否正确
(2) nginx -s reload: 重启 Nginx服务器

Nginx 的基本配置,是在 /etc/nginx/nginx.conf,可以看到 里面有一句: include /etc/nginx/conf.d/*.conf;
也就是我们的自定义Nginx配置 conf文件,应该在 /etc/nginx/conf.d 文件夹里放置
所以,接下来的Node.conffront.conf 的位置都是在这个文件夹里。

Nginx 部署 Node项目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Node.conf
upstream my_nodejs_upstream {
server 127.0.0.1:3002;
keepalive 64;
}
server {
listen 80;
server_name 42.193.248.156;

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://my_nodejs_upstream/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}

Nginx部署 web 项目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# front.conf
server {
listen 8080; # 需要云服务器新增一个8080端口,此处会有更优解
server_name 42.193.248.156; #此处使用测试域名。实际配置中使用您的服务器域名。
access_log /var/log/Nginx/front.access.log main;

location / {
root /usr/local/web/front; #测试站点路径。即您的项目代码路径。
index index.html index.htm;
try_files $uri /index.html; # 解决刷新404问题
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/Nginx/html;
}
}

常见错误

(1) XX command not found(拿 pm2 举例子:):
a. find / -name pm2: 寻找该包的安装地址 命令行会输出pm2的安装位置 比如 pm2路径 /usr/local/nodejs/node-v10.16.3-linux-x64/bin/pm2
b. ln -s pm2路径 /usr/local/bin: 关联软链接到环境变量下

可用命令记录

(1) lsb_release -a: 查看系统版本信息

参考链接;
yum: Linux软件包安装 命令
wget: Linux下载文件 命令

  • 本文标题:部署web服务器
  • 本文作者:Jonnzer
  • 创建时间:2023-02-06 22:39:31
  • 本文链接:https://jonnzer.github.io/2023/02/06/开发环境/部署web服务器/index/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论