本页目录

  • 前置条件
  • 开始之前
  • 安装 MongoDB
  • 安装 Node.js
  • 下载并安装 MEAN Stack
  • 创建 MEAN 应用
  • 为 MEAN 应用安装并配置 Nginx
  • 访问 MEAN 应用
  • 结论

MEAN 技术栈是一个免费且开源的、基于 JavaScript 的框架,用于开发 Web 应用。MEAN 由四种关键技术 MongoDB、Express、Angular 和 Node 组成。该技术栈旨在让使用 JavaScript 构建 Web 应用和处理 JSON 变得极其简单。你可以以多种方式使用 MEAN 应用。不过,它尤其适用于实时应用,特别是那些原生运行在云端的、以及使用 Angular.js 构建的单页(动态)Web 应用。

在本文中,我们将向你展示如何在 Ubuntu 22.04 服务器上安装 MEAN 技术栈。

前置条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 已指向你服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

开始之前

在开始之前,建议将所有系统软件包更新到最新版本。你可以使用以下命令更新所有软件包:

apt update -y
apt upgrade -y

一旦所有软件包都已更新,请使用以下命令安装所需的依赖项:

apt install python3 dirmngr gnupg apt-transport-https ca-certificates software-properties-common -y

一旦所有依赖项都已安装,你就可以继续下一步。

安装 MongoDB

默认情况下,MongoDB 软件包不包含在 Ubuntu 的默认仓库中。因此你需要将 MongoDB 仓库添加到你的服务器。

首先,使用以下命令下载并添加 MongoDB 密钥:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -

接下来,使用以下命令将 MongoDB 仓库添加到 APT:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

接下来,使用以下命令下载并安装 libssl 依赖项:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

接下来,使用以下命令更新仓库并安装 MongoDB 软件包:

apt-get update -y
apt-get install mongodb-org -y

安装成功后,使用以下命令启动并设置开机自启 MongoDB 服务:

systemctl start mongod
systemctl enable mongod

接下来,使用以下命令检查 MongoDB 服务的状态:

systemctl status mongod

你将看到以下输出:

? mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-30 07:30:10 UTC; 5s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 2995 (mongod)
     Memory: 60.5M
        CPU: 1.050s
     CGroup: /system.slice/mongod.service
             ??2995 /usr/bin/mongod --config /etc/mongod.conf

Oct 30 07:30:10 ubuntu2204 systemd[1]: Started MongoDB Database Server.

完成后,你可以继续安装 Node.js。

安装 Node.js

默认情况下,Node.js 软件包不包含在 Ubuntu 的默认仓库中。因此你需要从 Node 源仓库安装它。

首先,使用以下命令添加 Node 源仓库:

curl -sL https://deb.nodesource.com/setup_18.x | bash -

接下来,使用以下命令安装 Node.js 软件包:

apt-get install nodejs -y

一旦 Node.js 软件包安装完成,你可以使用以下命令验证 Node.js 版本:

node -v

你将看到以下输出:

v18.12.0

接下来,使用 NPM 安装 Yarn、Gulp 和 PM2 软件包:

npm install -g yarn
npm install -g gulp
npm install pm2 -g

完成后,你可以继续下一步。

下载并安装 MEAN Stack

首先,你需要从 Git 仓库下载最新版本的 MEAN 技术栈。你可以使用以下命令下载它:

git clone https://github.com/meanjs/mean

下载完成后,进入下载的目录并使用以下命令安装所有必需的依赖项:

cd mean
yarn install

一旦所有依赖项都已安装,你就可以继续下一步。

创建 MEAN 应用

在本节中,我们将创建一个简单的 MEAN 应用。

为此,使用以下命令在 mean 目录中创建一个 MEAN 应用:

nano server.js

添加以下代码:

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();

app.use('/', (req, res) => {
MongoClient.connect("mongodb://localhost:27017/test", function(err, db){
db.collection('Example', function(err, collection){
collection.insert({ pageHits: 'pageHits' });
db.collection('Example').count(function(err, count){
if(err) throw err;
res.status(200).send('Page Hits: ' + Math.floor(count/2));
});
});
});
});

app.listen(3000);
console.log('Server running at http://localhost:3000/');

module.exports = app;

保存并关闭文件,然后使用 PM2 启动该应用:

pm2 start server.js

你将看到以下输出:

                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/mean/server.js in fork_mode (1 instance)
[PM2] Done.
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? id  ? name      ? namespace   ? version ? mode    ? pid      ? uptime ? ?    ? status    ? cpu      ? mem      ? user     ? watching ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? 0   ? server    ? default     ? 0.6.0   ? fork    ? 5644     ? 0s     ? 0    ? online    ? 0%       ? 45.2mb   ? root     ? disabled ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

接下来,设置 MEAN 应用开机自启:

pm2 startup

你将看到以下输出:

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

默认情况下,MEAN 应用监听 3000 端口。你可以使用以下命令检查:

ss -antpl | grep 3000

你将看到以下输出:

LISTEN 0      511                *:3000             *:*    users:(("node /root/mean",pid=5644,fd=20))

完成后,你可以继续下一步。

为 MEAN 应用安装并配置 Nginx

首先,使用以下命令安装 Nginx Web 服务器软件包:

apt-get install nginx -y

一旦 Nginx 安装完成,使用以下命令创建一个 Nginx 虚拟主机配置文件:

nano /etc/nginx/conf.d/mean.conf

添加以下配置:

server {
listen 80;

server_name mean.example.com;
access_log /var/log/nginx/mean-access.log;
error_log /var/log/nginx/mean-error.log;

location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3000/;
}
}

保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:

nginx -t

你应该看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 服务以使更改生效。

systemctl restart nginx

你也可以使用以下命令检查 Nginx 状态:

systemctl status nginx

你应该看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-10-30 07:37:24 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 6416 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 6417 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 6418 (nginx)
      Tasks: 3 (limit: 4579)
     Memory: 3.3M
        CPU: 49ms
     CGroup: /system.slice/nginx.service
             ??6418 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??6419 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ??6420 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Oct 30 07:37:24 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 30 07:37:24 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

完成后,你可以继续下一步。

访问 MEAN 应用

此时,Nginx 已安装并配置为 MEAN 应用的反向代理。你现在可以使用 URL http://mean.example.com 访问 MEAN 应用。你应该在以下屏幕中看到 MEAN Web 界面:

在 Ubuntu 22.04 系统上从零安装 MEAN 技术栈完整教程

结论

恭喜!你已成功在 Ubuntu 22.04 上安装并配置了 MEAN 技术栈。你现在可以开始使用 MEAN 技术栈开发应用并将其部署到生产服务器上。如果你有任何问题,随时问我。