在Ubuntu中部署JavaScript应用程序通常涉及以下几个步骤:
准备服务器:
sudo apt update && sudo apt upgrade
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
上传你的JavaScript应用程序:
scp -r /path/to/your/local/app username@your_server_ip:/path/to/remote/directory
安装依赖:
cd /path/to/remote/directory
npm install
配置环境变量(如果有必要):
~/.bashrc或~/.profile文件中设置,或者使用.env文件结合dotenv包。启动应用程序:
npm start
sudo npm install pm2 -g
pm2 start app.js --name "your-app-name"
配置反向代理(可选):
sudo apt install nginx
sudo nano /etc/nginx/sites-available/your-app
server {
listen 80;
server_name your_server_ip_or_domain;
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;
}
}
sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
设置防火墙规则(可选):
sudo ufw allow 'Nginx Full'
通过以上步骤,你应该能够在Ubuntu服务器上成功部署你的JavaScript应用程序。