查看node.js中文文档时,看见阿里云服务器做广告,心急买了个低配的。就随便配了环境,Vue打包项目可正常访问。版本centos7,x
软件:ftp和xshell
xshell可以说是命令行的形式,对刚接触的可能不熟悉,容易乱;ftp软件,可以帮我们查看文件夹目录层次,友好吧
个人用的时如下
一、登入服务器
打开xshell
输入 ssh root@公网IP,
首次登陆可能会询问公钥,yes 即可。
xshell连接成功
因为我已经布置环境了,使用文件夹可能有所不一样。
虽然ftp软件可能更直观,自己可以看看目录结构,但运行配置还是需要xshell。
常用xshell指令
ssh root@公网IPcd /xx/xx xx //注意绝对地址ls -a 当前文件夹目录明细netstat -nap 查看是哪个程序占用了端口vim xxxi 进入编辑 光标移动修改完成 Esc shift+: wq 回车如果误操作 shift+:q! 回车
二、安装Nginx
因为也是自己百度查找的方法,也遇到了很多坑,安装Nginx有两种方法:个人用的方法二,
方法一
在配置 nginx 时,可能会依赖于 PCRE 包和 zlib 包,先进行安装:
cd /usr/localyum -y install pcre pcre-develyum install -y zlib-devel
在指定文件夹位置下载nginx压缩包
cd /usr/local/src wget /download/nginx-1.13.3.tar.gz
解压缩
tar -xvzf nginx-1.13.3.tar.gz
配置nginx
下载解压openssl
wget /source/openssl-1.0.2l.tar.gztar -xvzf openssl-1.0.2l.tar.gz
cd 进入nginx解压包里,执行之前安装的pcre-devel与openssl-devel解决依赖问题
cd nginx-1.13.3yum -y install pcre-devel openssl openssl-devel
再执行配置脚本来进行编译预处理
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.2l
成功后显示如下信息,
Configuration summary+ using system PCRE library+ using OpenSSL library: /usr/local/src/openssl-1.0.2l+ using system zlib librarynginx path prefix: "/usr/local/nginx"nginx binary file: "/usr/local/nginx/sbin/nginx"nginx modules path: "/usr/local/nginx/modules"nginx configuration prefix: "/usr/local/nginx"nginx configuration file: "/usr/local/nginx/nginx.conf"nginx pid file: "/usr/local/nginx/logs/nginx.pid"nginx error log file: "/usr/local/nginx/logs/error.log"nginx http access log file: "/usr/local/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"
make (时间稍微长,耐心)
make && make install
启动
/usr/local/nginx/sbin/nginx
注意目录文件名,是否重命名
方法二
yum -y install nginx
搞定
nginx -v
当然到现在为止,
启动Nginx并设置开机自动运行
systemctl start nginx.servicesystemctl enable nginx.service
现在开始打开你的公网ip在浏览器打开,始终不行,当时问了公司前辈,阿里云服务器默认端口不开放的,到时在阿里云后台单独开放,或者你把xx-xx区间的端口一次都解开
出入两个都开放下就可以打开了
是个WELCOME TO NGINX!Y页面
修改nginx配置文件
如果是第一个方法安装
vi /usr/local/nginx/nginx.conf
如果是第二种方法
vi /etc/nginx/nginx.conf
安装方式不同,路径不同,但里面配置的东西一样。关键是端口,文件指向项目地址,还有多文件,多端口配置,反向代理。。。
# For more information on configuration, see:# * Official English Documentation: /en/docs/# * Official Russian Documentation: /ru/docs/user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopushon;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See /en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf; // 新增几个监听端口配置文件,导入server {listen 80 default_server;listen [::]:80 default_server;server_name _;root /usr/share/nginx/html; //修改项目指向# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}location /api/{proxy_set_header x-Real-IP $remote_addr;# 反向代理 apiproxy_pass http://localhost:3000/api ; proxy_redirect off;proxy_set_header x-http_x_forwarded_for $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;proxy_set_header x-Nginx-Proxy true;}}#include /etc/nginx/conf.d/*.conf;# Settings for a TLS enabled server.## server {# listen 443 ssl http2 default_server;# listen [::]:443 ssl http2 default_server;# server_name _;# root /usr/share/nginx/html;## ssl_certificate "/etc/pki/nginx/server.crt";# ssl_certificate_key "/etc/pki/nginx/private/server.key";# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 10m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;## # Load configuration files for the default server block.# include /etc/nginx/default.d/*.conf;## location / {# }## error_page 404 /404.html;# location = /40x.html {# }## error_page 500 502 503 504 /50x.html;# location = /50x.html {# }# }}
上面有处,自定义文件导入:
include /etc/nginx/conf.d/*.conf; // 新增几个监听端口配置文件,导入
增加一个接口,配置文件如下(记得阿里云后台端口开放)
server {listen 8080;location / {root /home/app1/dist/;index index.html index.htm index.php;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
当然配置好了,重新启动,记得要在nginx.conf文件平级栏目下
systemctl restart nginx.service 更新,项目有几秒的卡顿nginx -s reload 更新,项目不会有几秒的卡顿systemctl stop iptables
三、 安装 node
1、下载文件
进入存放目录
cd /usr/local/src/
下载压缩包
wget /dist/latest/node-v10.1.0-linux-x64.tar.gz
2、解压文件
执行解压tar -zxvf node-v10.1.0-linux-x64.tar.gz重命名文件件mv node-v10.1.0-linux-x64 node
3、测试安装
进入 node 目录下的bin目录,执行 ls命令:
cd node/bin && ls
会看到node和npm,现在我们测试一下:
./node -v
如果出现 v10.1.0, 安装成功!
4、设置全局
现在node和npm还不能全局使用,我们要添加环境变量,首先在 root 目录下找到 .bash_profile 文件,编辑
vim ~/.bash_profile
找到 PATH=$PATH:$HOME/bin,在后面添加路径为:PATH=$PATH:$HOME/bin:/usr/local/src/node/bin
然后Esc返回命令行输入
:wq
保存成功
最后编译文件
source ~/.bash_profile
再输入node -v 显示版本即为成功
npm成功了,之后关于npm指令的,跟在电脑差不多
四、git
项目放在了home下,因人而异,安装的话大致跟在自己电脑是差不多
cd /home
1. 注意公钥的生成与添加
ssh-keygen -t rsa -C "mywebsite@"
查看与复制公钥 到你的github,gitlab
cat ~/.ssh/id_rsa.pub
2. git安装
yum install git
3. 下载项目
git clone xxxxxxxxxxx
当然环境配好了,你也可以ftp拖拉上传
如果是vue项目的话,打包dist文件就行。当然有些node项目,需要node 文件名 运行,,可我们需要项目一直运行,总不能关闭xshell,就不运行吧。pm2就是这个用途的。地址:/Unitech/pm2 ,在这里就不详细说了。有兴趣的朋友自己看。
如果有朋友细心的话,可以发现我上面配置了一个8080端口,指向了如下的文件
收工,如果有什么问题,还望指正。