数字货币量化交易

阿里云Ubuntu 搭建nodejs生产环境

2017-04-18  本文已影响295人  51bitquant

本篇文章主要介绍如何利用阿里云搭建搭建nodejs生产环境。

购买阿里云的服务器

首先需要在阿里云上购买服务器,地址为:https://www.aliyun.com. 各位同学可以通过这里
链接

登录服务器

在终端输入: ssh root@xx.xx.xx.xx // xx.xx.xx.xx为服务器的IP地址,
会提示你输入服务器的密码的。

安装软件

登录服务器后可以开始安装软件,当然你可以在创建个用户,在该用户下,进行安装软件。

  1. 首先更新 apt-get 软件源,终端输入:

sudo apt-get update

  1. 安装 build-essential 和 libssl-dev包, 终端输入如下命令:

sudo apt-get install build-essential libssl-dev

  1. 安装nvm,nvm是node version manager , 可以在改网址下获取安装nvm的安装脚本
    在终端下输入如下内容:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash

  1. 待上一步脚本执行完成后执行如下命令:

source ~/.bashrc

当然你也可以把控制台关闭了在重新打开。
5 . 选择要安装的nodejs版本,关于nodejs的版本可以通过如下命令获取:

nvm ls-remote

可以看到nodejs版本的更新历史。我个人选择的是安装最新的版本:6.10.2,所以输入命令:

nvm install v6.10.2

  1. 安装nodejs 进程管理工具 pm2, 采用全局安装的方式。

npm install pm2 -g

  1. 安装git 代码管理工具

sudo apt-get install git

  1. 安装nginx服务器:

sudo apt-get install nginx

关于nginx的命令,大家可以百度google下。这里介绍几个常用的命令。

service nginx reload // 重新加载命令,修改配置文件的时候使用,才能生效。

systemctl restart nginx // nginx 重新启动

  1. 编辑nginx 配置文件

sudo vi /etc/nginx/nginx.config

  1. 个人的配置文件如下:
service nginx reload
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
    worker_connections 768;
    # multi_accept on;
}

##添加的个人配置服务器。
http {
       server {
       listen 80; # 监听80端口
           server_name  www.sousoublog.com; # 服务地址
           client_max_body_size 10240000000M;
           location /{
        proxy_pass http://www.sousoublog.com:3000; # nodejs监听的端口
                proxy_set_header Host $host:$server_port;   
       }
    }
    ##
    # Basic Settings
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

创建代码仓库。

mkdir myproject

cd myproject

git init

git clone root@xxx.xxx.xxx.xxx:myproject

git config --global user.email "you@example.com"

git config --global user.name "Your Name"

// 设置你git 的名字和邮箱

git config receive.denyCurrentBranch updateInstead

如果大家在电脑端无法提交代码的时候,如果遇到以下打印的log错误信息:

Compressing objects: 100% (12/12), done.
Writing objects: 100% (15/15), 2.67 KiB | 0 bytes/s, done.
Total 15 (delta 1), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable t
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in som
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, se
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To www.sousoublog.com:/home/search/addgrouppro
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 

可以通过以下命令解决, 通过终端,进入到服务的代码仓库下输入:

git config receive.denyCurrentBranch updateInstead

发布项目:

项目完成之后,在项目下面启动服务器:

pm2 start bin/www --name "myproject"

当然过程是复杂的,比如你还要配置阿里云服务安全组。让服务器的哪些端口可以让远程访问,如果要真正项目,过程没有那么简单。

由于我经常使用阿里云服务器,我有成为阿里云的推广大使,如果你遇到问题,可以咨询我。在文章下留言,或者给我打赏。同时可以通过我的推广链接获取优惠券,节省你的资金投入。

如果你使用阿里云服务器可以戳链接,有优惠的。

上一篇 下一篇

猜你喜欢

热点阅读