如何在 Ubuntu 22.04 上安装 MEAN Stack如何在 Ubuntu 22.04 上安装 MEAN Stack如何在 Ubuntu 22.04 上安装 MEAN Stack如何在 Ubuntu 22.04 上安装 MEAN Stack
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Ubuntu 22.04 上安装 MEAN Stack

在此页

  1. 先决条件
  2. 开始
  3. 安装 MongoDB
  4. 安装 Node.js
  5. 下载并安装 MEAN Stack
  6. 创建 MEAN 应用程序
  7. 为 MEAN 应用程序安装和配置 Nginx
  8. 访问 MEAN 应用程序
  9. 结论

MEAN 堆栈是一个免费的开源基于 JavaScript 的框架,用于开发 Web 应用程序。 MEAN 由四项关键技术 MongoDB、Express、Angular 和 Node 组成。该堆栈旨在使使用 JavaScript 构建 Web 应用程序和处理 JSON 变得异常容易。您可以通过多种方式使用 MEAN 应用程序。但是,它特别适合实时应用程序,尤其是那些在云中本地运行的应用程序和在 Angular.js 中构建的单页(动态)Web 应用程序。

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

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 用您的服务器 IP 指向的有效域名。
  • 在服务器上配置了根密码。

入门

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

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 网络界面:

结论

恭喜!您已经在 Ubuntu 22.04 上成功安装并配置了 MEAN 堆栈。您现在可以开始使用 MEAN 堆栈开发应用程序并将其托管在生产服务器上。如果您有任何问题,请随时问我。

©2015-2025 艾丽卡 support@alaica.com