const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello Worldn');
});
server.listen(port, hostname, () => {
console.log(`服务器运行在 http://${hostname}:${port}/`);
});
// 启动
node home.js
// 如果报错'Unhandled 'error' event',可能是端口被占用了,先查看端口占用情况
ps -ef|grep node
// 如果有占用,删除占用,'xxxx'为'root'后的数字
skill -9 xxxxx
出现:服务器运行在 http://0.0.0.0:3000/ 即表示node运行成功,运行成功后,登录阿里云后台配置安全组规则
配置成功如下显示:
允许 自定义 TCP 3000/3000 IPv4地址段访问 0.0.0.0/0 node后台端口
然后就可以在浏览器地址栏输入你的服务器公网ip地址加上:3000,成功出现Hello World即表示安全组配置成功
6、配置nginx
// 进入 '/etc/nginx' 文件夹,查看下 'nginx.conf' 配置文件
cd /etc/nginx
ls
vim nginx.conf
// 低版本的nginx 'nginx.conf' 文件夹里有以下内容
// # include /etc/nginx/conf.d/*.conf;
// # include /etc/nginx/sites-enabled/*;
// 去掉 '#' 号
// 创建nginx配置文件,文件名随意,我一般喜欢用项目名加端口号,比如 'wxServer-3000'
vim /etc/nginx/conf.d/wxServer-3000.conf
// 编写配置文件代码
# 项目名字
upstream wxServer {
# 需要代理的node端口号,也就是你写的端口号
server 0.0.0.0:3000;
# nginx最大连接数
keepalive 8;
}
# nginx服务器实例
server {
# 代理出去的端口号,默认Http协议的80端口,如果配置其它端口需要更改 SELinux 的设置
listen 0.0.0.0:80;
# 别人访问的域名或者ip地址,多个用空格隔开
server_name lzf.fun www.lzf.fun;
# 错误日志存放地址
access_log /var/log/nginx/wxServer-3000.log;
location / {
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_pass 设置反向代理用服务器域名,不使用反向代理,直接用上面upstream的名字就可以了
proxy_pass http://wxServer/;
proxy_redirect off;
}
}
// 保存配置文件后,检查是否编写错误
nginx -t
// 出现以下内容为正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
// 重新加载nginx服务器
systemctl reload nginx
// 在阿里云后台开启80端口的安全组,然后在浏览器输入域名,可以看到 'Hello World' 就表示nginx配置成功了









