NodeJS+Express+Vue+MongoDB+Nginx环境搭建

这一套环境算是目前(2021年)主流的网站建设框架了吧,最近刚好需要部署学院里的一套网站(前端&后端),所以就简要记录一下配置安装的步骤防止自己后面遗忘😘

Attention:服务器系统为CentOS7

安装nodeJS

打开官网下载地址,选择想要的版本并copy链接,在终端使用wget下载

1
$ wget https://nodejs.org/dist/v14.15.3/node-v14.15.3-linux-x64.tar.xz

解压文件并移动到安装目录

1
2
3
$ tar -zxvf node-v14.15.3-linux-x64.tar.xz
$ mv node-v14.15.3-linux-x64 /usr/local/
$ mv node-v14.15.3-linux-x64 nodejs

创建程序引导链接(Method.1)

1
2
$ ln -s /usr/local/nodejs/bin/node /usr/local/bin
$ ln -s /usr/local/nodejs/bin/npm /usr/local/bin

创建链接的方法可能略有繁琐,也可以把整个bin可执行文件目录添加到系统环境变量(Method.2)

1
$ vim /etc/profile

在文末添加

1
2
PATH = $PATH:/usr/local/nodejs/bin
export PATH

查看版本号测试一下

1
2
$ node -v
$ npm -v

安装cnpm

1
$ npm install cnpm -g

基本所有用npm的时候都可以用cnpm来替代(速度更快噢)

安装PM2

1
$ npm install pm2 -g

如果安装后没有自动添加pm2的启动索引,就手动加一下吧

1
$ ln -s /usr/local/nodejs/bin/pm2 /usr/local/bin

安装express

1
$ npm install express -g

安装Vue

1
$ npm install --global vue-cli

创建第一个项目DEMO

1
2
3
4
$ vue init webpack myProjectName    # 创建vue工程
$ cd myProjectName # 进入工程目录
$ npm install # 安装依赖环境
$ npm run dev # 跑起来咯

安装MongoDB

推荐使用清华源安装

  1. 新建yum源配置文件

    1
    $ vim /etc/yum.repos.d/mongodb.repo

    填入内容

    1
    2
    3
    4
    5
    [mongodb-org]
    name=MongoDB Repository
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el$releasever/
    gpgcheck=0
    enabled=1
  2. 刷新缓存

    1
    $ sudo yum makecache
  3. 一步安装到位

    1
    $ sudo yum install mongodb-org
  4. 创建数据存放的文件目录

    1
    2
    $ mkdir -p /var/database/mongo
    $ mkdir -p /var/log/mongodb

    这一步没啥讲究,你喜欢放哪就放哪

  5. 启动数据库服务端

    1
    $ mongod --dbpath /var/database/mongo --logpath /var/log/mongodb/mongod.log --fork
  6. 本地客户端测试

    1
    $ shell

MongoDB基本语法

  • 创建、更换数据库

    1
    use DATABASE
  • 显示所有存在的数据库

    1
    show dbs
  • 删除数据库

    1
    db.dropDatabase()
  • 创建集合(表)

    1
    db.createCollection(name,options)

    详细的options可选参数内容见官网

    当向不存在的集合插入数据时,MongoDB会自动创建集合

    1
    db.COLLECTION.insert(DATA)
  • 删除集合(表)

    1
    db.COLLECTION.drop()
  • 显示所有集合(表)

    1
    show collections
  • 插入数据

    1
    2
    3
    4
    db.COLLECTION.insert(DATA)
    db.COLLECTION.insertOne(DATA)
    db.COLLECTION.replaceOne(DATA)
    db.COLLECTION.insertMany([DATA1,DATA2,...])

    用insert插入时,若主键重复,将会抛出异常;而且其他的会更新(覆盖)相同主键的数据

  • 更新数据

    1
    db.COLLECTION.update(<QUERY>,<UPDATEPROPERTY>)

    QUERY是匹配数据的规则;UPDATEPROPERTY是更新的新数据,详细格式见官网

  • 删除数据

    1
    db.COLLECTION.remove(<QUERY>)
  • 查询数据

    1
    db.COLLECTION.find(<QUERY>).pretty()

    pretty方法以格式化的方式显示结果

MongoDB工具

工具说明官网传送门

  • 导入数据

    可用下格式导入CSV文件:地址为URL:27017MongoDB服务器上,并且使用USRNAME用户(密码PSWD)登陆,导入到DATABASE数据库下的COLLECTION表。

    大写表示需要适当替换为正确的参数

    使用–headerline表示使用CSV文件的第一行作为键

    1
    $ mongoimport --url URL --port 27017 --usrname USRNAME --password PSWD -d DATABASE -c COLLECTION --headerline --file FILENAME --type csv

    用mongoimport工具也可以导入其他格式的数据文件,详细见官网说明

  • 导出数据

    好困呀🙃有空再写吧

Nginx

Why Nginx

Nginx(engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的http://Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

安装Nginx

  1. 安装依赖环境

    1
    $ sudo yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
  2. 安装PCRE,让Nginx支持Rewrite功能

    1
    2
    3
    4
    5
    6
    7
    $ wget https://sourceforge.net/projects/pcre/files/pcre2/10.36/pcre2-10.36.tar.gz #下载安装包
    $ tar -zxvf pcre2-10.36.tar.gz #解压
    $ cd pcre2-10.36
    $ ./configure
    $ sudo make # 编译
    $ sudo make install # 安装
    $ pcre2-config --version # 若安装成功,检验版本

    根据提示,pcre库被安装在了/usr/local/lib

  3. 下载Nginx并安装

    官网下载页面传送门

    1
    2
    3
    4
    5
    6
    7
    $  wget http://nginx.org/download/nginx-1.18.0.tar.gz
    $ tar -zxvf nginx-1.18.0.tar.gz
    $ cd nginx-1.18.0
    $ ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre2-10.36
    $ sudo make
    $ sudo make install
    $ sudo ln -s /usr/local/webserver/nginx/sbin/nginx /usr/sbin

    上面的–with-pcre一项内容改为你的下载资源目录,若报错可不要该项。–prefix=为安装目录

  4. 启动

    1
    $ sudo nginx
  5. 修改配置后相关命令

    • 测试新配置文件是否正确

      1
      $ sudo nginx -t
    • 重新载入配置文件

      1
      $ sudo nginx -s reload
    • 重启Nginx

      1
      $ sudo nginx -s reopen
    • 停止Nginx

      1
      $ sudo nginx -s stop
    • 修改总配置文件”./conf/nginx.conf”

      1
      $ sudo vi /usr/local/webserver/nginx/conf/nginx.conf

Nginx配置文件说明

先把Nginx安装目录下”conf/nginx.conf”文件打印出来看看

1
sudo cat /usr/local/webserver/nginx/conf/nginx.conf

内容如下:

为了方便阅读,在原基础上添加了俺的中文说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#user  nobody;  # nginx启动的账户
worker_processes 1; # 线程量

#error_log logs/error.log; # 错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid; # 线程文件,退出nginx后会自动删除


events {
worker_connections 1024; # 最大并发量
}


http { # 配置HTTP服务器
include mime.types;
default_type application/octet-stream;

#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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server { # 配置一个服务器属性
listen 8022; # 监听端口号
server_name localhost; # 服务器名称(域名、IP等)

#charset koi8-r;

#access_log logs/host.access.log main;

location / { # 匹配路由
root husteic; # 映射的本地静态文件路径
index index.html; # 页面
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; # 错误页面配置
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS 服务器 (433端口号)
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2023 RY.J
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信