Ubuntu服务器 Nginx+Node+nvm+pm2+Yarn环境搭建,以及域名解析
2019-08-16 03:06
911 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_37746009/article/details/99669993
如果使用了
然后,查看使用下面命令行查看当前服务器安装Nodejs的版本
可通过下面命令行 查看端口占用情况
添加域名 并在
Ubuntu服务器 Nginx+Node+nvm+pm2+Yarn环境搭建,以及域名解析
准备工作
> sudo apt-get update # 升级服务器的软件包 > sudo apt-get install git vim openssl build-essential libssh-dev wget curl # 安装必要的软件
安装nvm
> curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash # 下载 nvm
完成后,
nvm已经安装在
~/.nvm下,接下来是
配置环境变量
如果使用了
zsh的话,需要在
~/.zshrc这个配置文件中配置,否则就找
~/.bash_profile或
~/.profile。
然后,查看使用下面命令行查看当前服务器安装Nodejs的版本
> nvm ls
安装 Node JS
> nvm install 16.04 # Node 8.0 以前 使用如 nvm install v7.x.x > nvm use v8.1.2 # 切换NodeJS版本 > nvm alias default v8.1.2 # 将某个版本设置为默认版本
测试是否安装成功
> vi server.js
// fileName: server.js const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; 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 server.js
如果报错检查是否
端口被占用或者
防火墙没有开放端口
使用终端访问
> curl http://127.0.0.1:3000
安装 Yarn
打开 Yarn官网
找到对应的安装方法(本次使用Ubuntu),下载
> curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
下载完之后,通过下面命令安装
> sudo apt-get update && sudo apt-get install yarn # 安装 Yarn > yarn config set registry https://registry.npm.taobao.org # 修改 Yarn 的软件源
继续安装
pm2 Vue-cli
> npm install vue-cli pm2 -g # 使用 npm 全局安装 Vue-cli pm2 > pm2 start server.js # 使用 pm2 管理 server.js进程 > pm2 list # 使用 pm2 查看服务器进程
如果发现
status显示为
errored,可能端口被占用
可通过下面命令行 查看端口占用情况
> netstat -anp # 查看端口占用情况,找到端口对应进程的pid > kill -9 13230 # 杀死 pid 为 13230 的进程
关于pm2的命令
> pm2 show server # 查看服务器详细信息 > pm2 stop server # 停止 pm2 > pm2 logs # 查看日志 > pm2 restart server #重启
查看版本
> node -v # node > npm -v # npm > pm2 -v # pm2 > yarn --version # yarn > vue -V # vue
卸载Apache2
> sudo service apache2 stop # 停止 Apache2 > update-rc.d -f apache2 remove # 进一步删除 Apache2 > sudo apt-get remove apache2 # 彻底删除 Apache2
安装Nginx
> sudo apt-get update # 更新服务器软件 > sudo apt-get install nginx # 安装 Nginx > nginx -v # 查看 Nginx 版本
配置Nginx
> cd /etc/nginx/conf.d/ # 目录切换到 nginx 的配置路径 > vi type-www-domain-com-conf # 新建一个 conf 文件 , 以 '项目类型-域名.conf' 格式命名
# fileName: type-www-domain-com-conf upstream m { # 规则名 m ip_hash; # 开启ip_hash,对流量分流 server xxx.xxx.xxx.xxx:xxxx; # 负载均很服务器A server 127.0.0.1:3000; # 负载均很服务器A keepalive 64; } server { listen 80; server_name domain.cn; # 域名 root /web/mazey.cn; # 默认根路径 index index.html index.htm; # 默认主页 location /server { # 网站切到/server下时走nodejs 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_pass http://m; # http://规则名 => http://m } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; # 静态资源文件 过期时间 30天 } location ~ .*.(js|css)?$ { expires 1h; # css/js文件 过期时间 1小时 } } # upstream domain { # server 127.0.0.1:3000; # } # server { # listen 80; # server_name domain.com; # # location / { # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwrad-For $proxy_add_x_forwarded_for; # proxy_set_header Host $http_host; # proxy_set_header X-Nginx-Proxy true; # # proxy_pass http://domain; # proxu_redirect off; # } # # location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt) { # root /www/movie/production/current/public; # } #}
添加域名解析
登录DNSpod官网,找到
添加域名
![](https://img-blog.csdnimg.cn/20190816023414161.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzc0NjAwOQ==,size_16,color_FFFFFF,t_70)
添加域名 并在
域名注册商处修改
NS服务器,使 DNSpod
接管域名
点击
添加记录
主机记录为刚刚在
nginx配置文件里面的
upstream的
规则名
记录值为
服务器IP,点击保存
回到
ssh命令行,重启Nginx
> sudo service nginx restart # 重启nginx,使配置文件生效 > pm2 restart server.js # 重启 nodejs 的server.js
访问
m.domain.com有出现server.js 的
hello world代表成功
— 时间不早,未完待续 —
相关文章推荐
- Ubuntu下安装Nginx环境以及搭建反向代理服务器
- 阿里云Ubuntu系统从0搭建LAMP(Apache+MySQL+PHP)以及服务器生产环境常用配置
- 阿里云Ubuntu系统从0搭建LAMP(Apache+MySQL+PHP)以及服务器生产环境常用配置
- 阿里云Ubuntu系统从0搭建LAMP(Apache+MySQL+PHP)以及服务器生产环境常用配置
- ubuntu下开发环境配置:nfs服务器的搭建
- Ubuntu15.10下安装arm交叉编译器并搭建编译环境以及hello.c的实现
- 新手使用Ubuntu18.04 搭建python的环境(anaconda以及pycharm)
- 在ubuntu系统环境下搭建java环境,以及各种软件的安装
- Ubuntu 搭建svn服务器 ,以及常见错误解决方案
- ubuntu服务器pytorch环境搭建
- Centos6.5服务器环境搭建之安装Nginx以及Https访问方式的配置
- 服务器搭建javaWeb开发环境—svn安装与配置以及使用
- Ubuntu 14.04 下搭建SVN服务器以及基本使用
- 【深度学习】笔记1_Ubuntu14.04下caffe环境的搭建,无GPU版本以及python可视化环境的配置
- Mac下OpenGL环境搭建以及OpenGL解析渲染OBJ模型
- 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案
- Ubuntu 16.04 搭建LAMP服务器环境流程
- ubuntu16.04搭建LAMP环境以及遇到的问题
- 【转】SVN服务器客户端以及环境的搭建和使用