Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36240
  • 博文数量: 12
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 123
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-27 11:20
个人简介

Passion for technology and design!

文章分类
文章存档

2015年(8)

2014年(4)

我的朋友

分类: LINUX

2015-04-12 16:16:00

一、安装与配置nginx

nginx是一个高性能的web服务程序,在处理高并发web请求时比apache的性能要好。

我在自己的AWS虚拟机(RHEL 7.1)上安装nginx的步骤如下:
注意我的系统是RHEL 7.1,如果你的系统是其他版本,需要下载相应版本的文件,不要照抄。

1. 下载并安装nginx的yum源配置文件:

wget
rpm -ivh nginx-release-rhel-7-0.el7.ngx.noarch.rpm

2. 下载并安装nginx的RPM包签名文件:

wget
rpm --import nginx_signing.key

3. 修改nginx.repo,改为使用mainline最新版发布包:

编辑 /etc/yum.repos.d/nginx.repo 文件:
[nginx]
name=nginx
baseurl=
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-nginx

4. 安装nginx及相关软件包

yum -y install nginx nginx-debug nginx-debuginfo nginx-nr-agent

5. 启动nginx服务,并设为开机启动

systemctl start nginx
systemctl status nginx -l
systemctl enable nginx

6. 配置nginx,设置代理

修改 /etc/nginx/nginx.conf 配置文件,修改http段的内容:
http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  sendfile on;
  upstream nodejs {
    server 127.0.0.1:8080 max_fails=0 fail_timeout=10s;
    keepalive 512;
  }
  server {
    listen 80;
    client_max_body_size 16M;
    keepalive_timeout 10;
    location / {
      proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_http_version 1.1;
      proxy_pass
    }
  }
  access_log off;
  error_log /dev/null crit;
  include /etc/nginx/conf.d/*.conf;
}

我这里是用nginx做了一个反向代理,指向了本机的8080端口,以后要在8080端口部署NodeJS。

7. 针对高并发web应用,调整系统参数:

修改 /etc/sysctl.conf 配置文件:

net.core.somaxconn = 4096
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.ipv4.ip_local_port_range = 1000 65535
net.ipv4.tcp_max_tw_buckets = 400000
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_max_orphans = 131072
vm.min_free_kbytes = 65536
fs.nr_open = 2097152

运行 sysctl -p 命令,使这些系统参数生效。

调高limit值,使得能同时开启的文件描述符数量达到上面的2097152。
修改 /etc/security/limits.conf 文件:
root   soft  nofile  2097152
root   hard  nofile  2097152
admin  soft  nofile  2097152
admin  hard  nofile  2097152

8. 设置SELinux变量

如果在下文的Express启动后,出现502 Bad Gateway,可用下列命令,查找设置方法:
grep nginx /var/log/messages|grep denied|tail -n1|audit2why
设置httpd_can_network_relay变量:
setsebool -P httpd_can_network_relay 1

二、安装与配置Node.JS

1. 先安装gcc编译器等工具

yum groupinstall 'Development Tools' -y

2. 直接调用NodeJS的setup安装脚本,然后用yum安装NodeJS

curl -sL | bash -
yum install -y nodejs

3. 输入命令 node --help 测试一下,可看到nodejs的帮助消息。

4. 安装Express框架

npm install -gd express
npm install -gd express-generator
其中g参数是把express安装到NodeJS的lib目录,d参数表示同时安装依赖模块包。

进入nodejs模块目录
cd /usr/lib/node_modules/
查看所有模块
npm list
检查模块是否有更新
npm outdated
更新可更新的模块
npm update

5. 配置一个测试站点

cd /root/
建立一个叫nodeTest的测试项目
express ejs nodeTest
安装依赖的包
cd nodeTest && npm install

编辑nodeTest目录内的 bin/www ,修改端口号为8080:
var port = normalizePort(process.env.PORT || '8080');

开启服务
npm start

另开一个终端,测试一下:
curl
如果能看到包含Welcome to Express内容的网页,则说明Express正常。

如果nginx的反向代理运行正常,执行:
curl
也会看到相同的网页。

安装Forever,Forever能让NodeJS应用在后台执行,成为守护进程。
npm install forever -gd
forever --help

6. 设置systemd,由systemd来启动NodeJS

新建 /usr/lib/systemd/system/nodejs.service 文件:
[Service]
ExecStart=/bin/node /var/www/html/app.js
Restart=always
StandardOutput=syslog
SyslogIdentifier=nodejs
User=nobody
Group=nobody
[Install]
WantedBy=multi-user.target

安装NodeJS的systemd相关模块:
npm install systemd -gd
npm install autoquit -gd

新建 /var/www/html/app.js 文件,作为测试用JS脚本:
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World, Node.JS ! ! !\n');
}).listen(8080);

启动服务并设置为开机自动启动:
systemctl start nodejs
systemctl status -l nodejs
systemctl enable nodejs

运行 curl 127.0.0.1:8080 可看到测试脚本的输出:
Hello World, Node.JS ! ! !

三、安装与配置MongoDB

安装NodeJS的MongoDB数据库驱动:
npm install mongodb -gd
安装Mongoose等轻量级NodeJS对象文档映射器(Object Document Mapper)。
npm install mongoose -gd
npm install mongous -gd
npm install mongoskin -gd
安装mongoskin的时候会报错:
Peer mongoskin@1.4.13 wants mongodb@~1.4
我们安装是2.0以上版本的mongodb,所以暂且不去管它,记得不要去调用mongoskin的库函数就行了。

添加yum源,新建文件 /etc/yum.repos.d/mongodb-org-3.0.repo 内容为:
[mongodb-org-3.0]
name=MongoDB Repository
baseurl=
gpgcheck=0
enabled=1

执行yum安装:
yum install -y mongodb-org mongodb-org-server mongodb-org-mongos mongodb-org-shell mongodb-org-tools

查看MongoDB的配置文件:
rpm -qc mongodb-org-server
vim /etc/mongod.conf

启动MongoDB服务:
systemctl start mongod
systemctl status mongod -l
chkconfig mongod on

未完待续!
阅读(3159) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~