[实战AWS EC2]五、基于Ubuntu,Tengine服务器构建Git管理Nodejs,Redis,MongoDB的网站
[实战AWS EC2]五、基于Ubuntu,Tengine服务器构建Git管理Nodejs,Redis,MongoDB的网站
### [实战AWS EC2]五、基于Ubuntu,Tengine服务器构建Git管理Nodejs,Redis,MongoDB的网站
在本教程中,我们将使用AWS EC2实例部署一个包含Node.js后端、Redis缓存和MongoDB数据库的Web应用。整个应用将通过Tengine(Nginx的一个分支)进行反向代理,并且我们将使用Git来管理代码。
1. 准备工作
首先,确保你已经在AWS上创建了一个EC2实例,并且已经安装了Ubuntu操作系统。然后,我们需要配置安全组以允许HTTP (80) 和 SSH (22) 流量。
# 更新系统包
sudo apt-get update && sudo apt-get upgrade -y
# 安装必要的软件包
sudo apt-get install git nginx tengine -y
2. 安装Node.js和npm
我们可以通过NodeSource的APT仓库来安装Node.js:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
3. 配置MongoDB
接下来,添加MongoDB的官方APT仓库并安装MongoDB:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
启动MongoDB服务:
sudo systemctl start mongod
sudo systemctl enable mongod
4. 安装Redis
sudo apt-get install redis-server
启动Redis服务:
sudo systemctl start redis
sudo systemctl enable redis
5. 使用Git管理代码
假设你的GitHub仓库地址为https://github.com/username/myapp.git
,则可以执行以下命令:
cd /var/www/html
git clone https://github.com/username/myapp.git
cd myapp
6. 配置Tengine
编辑Tengine配置文件以设置反向代理:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
保存并重启Tengine服务:
sudo systemctl restart tengine
7. 运行Node.js应用
在项目目录中,安装依赖并运行应用:
cd /var/www/html/myapp
npm install
node app.js
这样,你就完成了一个简单的Node.js应用的部署,该应用使用了MongoDB作为数据库,Redis作为缓存,并通过Tengine进行了反向代理。
[实战AWS EC2]五、基于Ubuntu,Tengine服务器构建Git管理Nodejs,Redis,MongoDB的网站
在这个教程中,我们将详细说明如何在AWS EC2实例上使用Ubuntu系统搭建一个包含Node.js后端、Redis缓存、MongoDB数据库的Web应用。应用将通过Tengine(Nginx的一个分支)进行反向代理,并且我们将使用Git来管理代码。
1. 准备工作
首先,在AWS上创建一个EC2实例并安装Ubuntu系统。配置安全组以允许HTTP (80) 和 SSH (22) 流量。
# 更新系统包
sudo apt-get update && sudo apt-get upgrade -y
# 安装必要的软件包
sudo apt-get install git nginx tengine -y
2. 安装Node.js和npm
通过NodeSource的APT仓库安装Node.js:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
3. 配置MongoDB
添加MongoDB的官方APT仓库并安装MongoDB:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
启动MongoDB服务:
sudo systemctl start mongod
sudo systemctl enable mongod
4. 安装Redis
sudo apt-get install redis-server
启动Redis服务:
sudo systemctl start redis
sudo systemctl enable redis
5. 使用Git管理代码
假设你的GitHub仓库地址为https://github.com/username/myapp.git
,则可以执行以下命令:
cd /var/www/html
git clone https://github.com/username/myapp.git
cd myapp
6. 配置Tengine
编辑Tengine配置文件以设置反向代理:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
保存并重启Tengine服务:
sudo systemctl restart tengine
7. 运行Node.js应用
在项目目录中,安装依赖并运行应用:
cd /var/www/html/myapp
npm install
node app.js
这样,你就完成了一个简单的Node.js应用的部署,该应用使用了MongoDB作为数据库,Redis作为缓存,并通过Tengine进行了反向代理。