使用到技术Node.js、Mongodb、Socket.io和React等
WEB服务器使用Nginx反向代理
准备工作:
所需环境:Nodejs >= 8.9.0、Mongodb
操作系统是Linux的CentOS 7
如果购买的VPS服务器的内存太小,会导致很多进程被杀掉,所以需要添加Swap虚拟内存,不然构建过程会失败。
运行命令:
wget https://www.moerats.com/usr/shell/swap.sh && bash swap.sh
然后根据选项进行操作,记得添加swap的时候填写纯数字,默认单位为M。
1、安装Nodejs
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install nodejs git -y
2、安装Mongodb
cat <<EOF > /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF
yum -y install mongodb-org
3、启动Mongodb并设置开机自启
systemctl start mongod
systemctl enable mongod
4、安装web应用
拉取源码并存放于/opt文件夹
git clone https://github.com/yinxin630/fiora.git -b master /opt/fiora
cd /opt/fiora
安装依赖,这里不能用npm,需要用yarn来安装
npm i -g yarn
yarn
构建
npm run build
转移产物
npm run move-dist
启动
npm start
5、端口开放
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload
6、新建Systemd配置文件
先修改你的userId和运行端口后复制到SSH运行
Administrator=5d329dd354b9
Port=9200
新建fiora用户并授权在这里插入代码片
useradd -M fiora && usermod -L fiora
chown -R fiora:fiora /opt/fiora
新建systemd配置文件,将以下代码一起复制到SSH运行
cat > /etc/systemd/system/fiora.service <<EOF
[Unit]
Description=fiora
After=network.target
Wants=network.target
[Service]
Type=simple
PIDFile=/var/run/fiora.pid
ExecStart=$(command -v npm) start
WorkingDirectory=/opt/fiora
Environment=Administrator=$Administrator Port=$Port
User=fiora
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
EOF
7、开始启动并设置开机自启
systemctl start fiora
systemctl enable fiora
使用宝塔面板的,需要给node做个软连接,不然后面会启动失败
ln -sf $(which node) /usr/bin/node