如何在CentOS 7上部署Let\'sChat服务器
Anne655
・8 分钟阅读
let's Chat是一个开源聊天应用程序,为小型团队提供消息传递服务。
let's Chat基于NodeJS和MongoDB,在本文中,我们将了解如何在Centos7服务器上部署Let'sChat。
前提条件
- 至少2G RAM的CentOS 7服务器实例,推荐4G RAM。
- 一个sudo用户 ,
步骤1:更新系统
当第一次登录系统时,必须按如下方式执行系统更新:
sudo yum install epel-release -y
sudo yum clean all && sudo yum update -y && sudo shutdown -r now
重新引导后,以相同的sudo用户身份登录。
步骤2安装NodeJS
按照下面的步骤安装最新的NodeJS 6.x,在编写本文时是6.9.5 :
cd
curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -
sudo yum install -y nodejs
步骤3:安装MongoDB
按照下面的步骤安装最新的MongoDB,本文撰写时是3.4 。
3.1创建MongoDB 3.4 YUM repo,如下所示:
cat <<EOF | sudo tee -a /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
EOF
3.2安装,并且启动最新的MongoDB服务器版本,本文编写时是3.4.1 :
sudo yum install -y mongodb-org
sudo systemctl start mongod.service
sudo systemctl enable mongod.service
sudo yum install git -y
cd /opt
sudo git clone https://github.com/sdelements/lets-chat.git
cd lets-chat
sudo npm install
注意:在安装过程中看到几个npm WARN ...
消息是正常的,忽略他们。
步骤5(可选)创建settings.yml
文件
如果你想要自定义let's Chat,你可以创建一个名为/opt/lets-chat/settings.yml的文件,然后将你的定制设置,
sudo cp settings.yml.sample settings.yml
本教程中,我们将使用示例设置文件中的默认设置。
步骤6:安装Forever
你可以从/opt/lets-chat目录中启动 let's Chat:
cd /opt/lets-chat
npm start
输出应类似于:
> lets-chat@0.4.8 start /opt/lets-chat
> node app.js
██╗ ███████╗████████╗███████╗ ██████╗██╗ ██╗ █████╗ ████████╗
██║ ██╔════╝╚══██╔══╝██╔════╝ ██╔════╝██║ ██║██╔══██╗╚══██╔══╝
██║ █████╗ ██║ ███████╗ ██║ ███████║███████║ ██║
██║ ██╔══╝ ██║ ╚════██║ ██║ ██╔══██║██╔══██║ ██║
███████╗███████╗ ██║ ███████║ ╚██████╗██║ ██║██║ ██║ ██║
╚══════╝╚══════╝ ╚═╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
Release 0.4.8
为了让let's Chat保持运行,让我们先按Ctrl-C退出,然后安装一个名为forever的应用程序:
sudo npm install forever -g
使用forever应用程序启动let's Chat:
cd /opt/lets-chat
forever start app.js
let's Chat运行后,你可以通过以下方式在本地访问它:
http://localhost:5000
你可以使用下面的命令来测试你的安装:
curl -I http://localhost:5000
输出应类似于:
HTTP/1.1 302 Found
X-Frame-Options: SAMEORIGIN
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Security-Policy:
X-Content-Security-Policy:
X-WebKit-CSP:
X-UA-Compatible: IE=Edge,chrome=1
Location: /login
Vary: Accept, Accept-Encoding
Content-Type: text/plain; charset=utf-8
Content-Length: 28
set-cookie: connect.sid=s%3A0YTFL6Un5G7iMc3zt8i-vlIh2YDQqTZ3.1dVZFG3VWmwd%2FXXXJiuyWSQ4k432MVvxm7xrgJGIej4; Path=/; HttpOnly
Date: Wed, 01 Feb 2017 11:30:03 GMT
Connection: keep-alive
步骤7:将Nginx安装为反向代理
为了启用外部Web访问你需要设置一个反向代理,例如,nginx来重定向流量。
7.1安装Nginx :
sudo yum install nginx -y
7.2修改Nginx的设置:
sudo vi /etc/nginx/nginx.conf
查找http {}
段中的location/{}
段:
http {
location / {
}
}
将下面的行插入到location/{}
段中:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_pass http://127.0.0.1:5000;
最后的结果应该是:
http {
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
}
保存和退出:
$1
!
7.3启动Nginx服务:
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
7.4修改防火墙规则以允许网络访问:
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload
步骤8:网络访问
将你的网络浏览器指向http://203.0.113.1访问let's Chat,然后单击,I need an account
链接,注册登录的用户名。
就是这样,感谢阅读。