centOS下的node部署环境搭建

目录

本文实践的是单机搭建 nginx+node+mongodb+redis 流行组合的具体过程

node的安装过程详情

其他的直接上亲自实践过的安装命令以及需要注意的地方,习惯用英文注释,部分重点已用中文指出

#!/bin/bash
#update software
sudo yum update -y
#back to root dir
cd ~
#Nginx
sudo yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
#If pcre
rpm -qa pcre
#yes download nginx stable version
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz
#switch to root for mkdir
su root
cd nginx-1.12.0
./configure && make && make install
cd ~ && exit
rm -rf nginx-1.12.0
rm nginx-1.12.0.tar.gz
#start nginx
#access public IP should success
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#self-start config
sudo vi /lib/systemd/system/nginx.service
#content
:'
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
'
systemctl start nginx.service
systemctl enable nginx.service
#check if running
systemctl list-units --type=service

#Mongodb
sudo vi /etc/yum.repos.d/mongodb-org-3.4.repo

#https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/
#content
:'
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
'

#yum install mongodb
sudo yum -y install mongodb-org
#start mongodb(default it can self-start when booting)
sudo service mongod start
#self-start
sudo chkconfig mongod on

#pm2 install
sudo cnpm i pm2 -g

#redis
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
su root
tar xzf redis-3.2.9.tar.gz
cd redis-3.2.9
make
cd src
cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-aof redis-check-rdb /usr/local/bin
mkdir /etc/redis
mkdir -p /var/lib/redis/6379
cp redis.conf /etc/redis/6379.conf

vi /etc/redis/6379.conf
:'
daemonize yes
logfile /var/log/redis_6379.log
dir /var/lib/redis/6379
'

cp utils/redis_init_script /etc/init.d/redis

vi /lib/systemd/system/redis.service
#content
:'
[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target

vm.overcommit_memory = 1
net.core.somaxconn=512
'
systemctl start redis.service
systemctl enable redis.service

cd .. && cd .. && rm -rf redis-3.2.9 redis-3.2.9.tar.gz

#kernel config
sysctl -w vm.overcommit_memory=1
sysctl -w net.core.somaxconn=512
echo never > /sys/kernel/mm/transparent_hugepage/enabled

#booting shell
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never >> /sys/kernel/mm/transparent_hugepage/defrag
systemctl restart mongod.service

#nginx conf ( Remember to configure user !!!)
#nginx 配置内容经优化(踩过的坑,最顶部的注释要去掉替换成对应的用户名,否则会无文件权限!!)
#其中含负载均衡及反向代理的配置,模仿着配置好就行
:'
#user userName

http {
    client_header_buffer_size 3k;
    client_max_body_size 2m;
    client_body_buffer_size 7k;
    large_client_header_buffers 2 1k;

    client_body_timeout   32;
    client_header_timeout 32;
    reset_timedout_connection on;

    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    tcp_nopush on;
    tcp_nodelay on;
    types_hash_max_size 2048;

    gzip on;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    types_hash_max_size 2048;

}

# 负载均衡
upstream pro_name {
    server 127.0.0.1:3000;
#   balanceServer ip2;
#   balanceServer ip3;
}

server {
    listen 80;
    server_name pro_name;
    charset utf-8;
    access_log /var/log/nginx/app.log;
    root /home/.../code/...;



    location ~ /api/ {
        proxy_pass http://pro_name;
    	proxy_redirect off;
    	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_http_version 1.1;
    	proxy_read_timeout 7s;
    }

    location ~ /.ht {
        deny all;
    }

    location ~* \.(css|js|gif|jpe?g|png|ico)$ {
        root /home/.../code/...;
        expires 168h;
    }

    location / {
        index  index.html index.htm;
    }


}
'

#实用nginx命令,养成好习惯
#everytime save change of .conf
/usr/local/nginx/sbin/nginx -t  #Check Syntax
/usr/local/nginx/sbin/nginx -s reload #Smooth reload nginx

#nginx optimize
sysctl -w net.core.somaxconn=1024

最后使用pm2进行node项目的管理,包括简单的监控

详情请见

pm2进程管理

keymetrics监控项目

附上腾讯云主机在生产环境下的安全组配置

config1 config2