部署一个简单的node服务
登陆kim账号
cd ~
创建index.js文件
编辑内容:
const http = require('http');
const hostname = '0.0.0.0';
const port = 7500;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
复制代码
在上面的代码中我们创建了一个node项目,主机名0.0.0.0,端口7500,启动了一个http服务,当访问这个主机的时候返回'hello world'。
运行node index.js
启动项目
kim@VM-0-12-ubuntu:~$ node index.js
Server running at http://0.0.0.0:7500/
复制代码
这时候可以另外打开一个terminal,用crul
指令看看返回什么
kim@VM-0-12-ubuntu:~$ curl http://0.0.0.0:7500/
Hello World
复制代码
也可以在本地浏览器中访问以下地址,查看项目是否正常运行
http://云服务器实例的公网IP:7500
复制代码
用pm2启动服务
当退出index项目,再用浏览器访问同一个地址的时候,得到的是This site can’t be reached
因为云服务器上的服务已经停止了,这就是为什么要用pm2,他可以帮我们管理node线程。
执行pm2 start index.js
kim@VM-0-12-ubuntu:~$ pm2 start index.js
[PM2] Starting /home/kim/index.js in fork_mode (1 instance)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0 │ index │ fork │ 0 │ online │ 0% │ 24.9mb │
复制代码
这时候又能看到hello world
了。
用nginx代理端口
浏览器默认是80端口(http)和443端口(https),因为我们项目的端口是7500,所以在使用浏览访问的时候必须指定7500端口,才能正常访问到我们需要的资源。如果要省略这一步,我们需要nginx来做一下反向代理。
安装nginx
# 更新
sudo apt-get update
# 安装 nginx
sudo apt-get install nginx
# 根据业务编写nginx.conf
cd /etc/nginx/conf.d/
vi test.conf
# 以下为nginx常用命令
# 检测nginx文件
nginx -t
# 启动nginx服务
systemctl start nginx.service
# 停止nginx服务
systemctl stop nginx.service
# 重启nginx服务
systemctl restart nginx.service
# 重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service
复制代码
安装完成后访问http://公网IP
,看到如下页面,This site can’t be reached
不见了
看一下/etc/nginx
目录下的文件
kim@VM-0-12-ubuntu:/etc/nginx$ ls
conf.d fastcgi_params koi-win nginx.conf scgi_params sites-enabled uwsgi_params
fastcgi.conf koi-utf mime.types proxy_params sites-available snippets win-utf
复制代码
主要看sites-available
,sites-enabled
这两个文件夹和nginx.conf
这个配置文件。sites-enabled是sites-available的一个软链接,先不用管它,nginx.conf包含了sites-enabled的配置。
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
复制代码
所以可以通过修改/etc/nginx/sites-available/default
的参数,然后执行sudo nginx -s reload
重载nginx配置。也可以直接改nginx.conf
但是结构如下
http {
server {}
}
复制代码
我们编辑/etc/nginx/sites-available/default
在location模块里面新增一行proxy_pass http://0.0.0.0:7500;
,编辑完成退出。
sudo nginx -t
测试是否配置正确
systemctl restart nginx
重启nginx
kim@VM-0-12-ubuntu:/etc/nginx/sites-available$ sudo vi default
[sudo] password for kim:
kim@VM-0-12-ubuntu:/etc/nginx/sites-available$ systemctl restart nginx
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'nginx.service'.
Multiple identities can be used for authentication:
1. ubuntu,,, (ubuntu)
2. ,,, (kim)
Choose identity to authenticate as (1-2): 2
Password:
==== AUTHENTICATION COMPLETE ===
kim@VM-0-12-ubuntu:/etc/nginx/sites-available$
复制代码
再访问公网IP,已经不需要加端口号了
一般除了proxy_pass,还会增加一些配置
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_set_header Cookie $http_cookie;
proxy_pass http://0.0.0.0:7500;
}
复制代码
部署本地node项目
正常我们是要把本地项目部署到云服务器,而不是在云服务上写一个项目。这就需要把代码push到云服务器上。可以用pm2实现自动部署,但是这里我们先手动实现一下。
现在服务器的/var/www
目录下新建一个api
目录。修改一下权限sudo chmod 777 api
我本地有一个项目node_api
,这个项目是node+mongoDB的学习项目,MDN的express+mongodb实践,可以拿来学习。
终端切换到项目的根目录,然后执行sudo scp -r -vvv * kim@49.235.172.41:/var/www/api
如果设置了禁止密码登陆会上传不成功,得在
/etc/ssh/sshd_config
里把PasswordAuthentication改为true
等待上传完看下现在的api目录
kim@VM-0-12-ubuntu:/var/www/api$ ll
total 112
drwxrwxrwx 9 root root 4096 Feb 2 17:49 ./
drwxr-xr-x 4 root root 4096 Feb 2 16:12 ../
-rw-r--r-- 1 kim kim 0 Feb 2 17:43 access.log
-rw-r--r-- 1 kim kim 2189 Feb 2 17:43 app.js
drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 bin/
drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 controllers/
drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 models/
drwxr-xr-x 159 kim kim 4096 Feb 2 17:49 node_modules/
-rw-r--r-- 1 kim kim 127 Feb 2 17:49 nodemon.json
-rw-r--r-- 1 kim kim 52728 Feb 2 17:49 package-lock.json
-rw-r--r-- 1 kim kim 578 Feb 2 17:49 package.json
-rw-r--r-- 1 kim kim 8262 Feb 2 17:49 populatedb.js
drwxr-xr-x 5 kim kim 4096 Feb 2 17:49 public/
drwxr-xr-x 2 kim kim 4096 Feb 2 17:49 routes/
drwxr-xr-x 2 kim kim 4096 Feb 2 17:49 views/
kim@VM-0-12-ubuntu:/var/www/api$
复制代码
用pm2启动这个项目pm2 start bin/www
kim@VM-0-12-ubuntu:/var/www/api$ pm2 start bin/www
[PM2] Starting /var/www/api/bin/www in fork_mode (1 instance)
[PM2] Done.
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ index │ default │ N/A │ fork │ 26826 │ 2h │ 0 │ online │ 0% │ 40.0mb │ kim │ disabled │
│ 1 │ www │ default │ 0.0.0 │ fork │ 10733 │ 0s │ 0 │ online │ 0% │ 10.0mb │ kim │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
kim@VM-0-12-ubuntu:/var/www/api$
复制代码
可以看到除了之前的index项目,现在多了个www项目。
浏览器访问http://49.235.172.41:3000/catalog
继续修改一下nginx配置sudo vi /etc/nginx/sites-available/default
增加一个location配置:
location /catalog {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_set_header Cookie $http_cookie;
proxy_pass http://127.0.0.1:3000;
}
复制代码
重启nginx使配置生效systemctl restart nginx
,现在不加端口号也可以访问了。
后续
后续可以做的事
- 部署一个静态页面
- 实现pm2自动部署
- 参考express部署最佳实践优化部署
参考
Ubuntu部署nodejs应用-Docker
Vue项目打包部署总结
pm2 使用教程
How To Set Up a Node.js Application for Production on Ubuntu 16.04