nginx+nodejs+mysql+memcached服务器后台架设centos6.5

需要的下面四个工具最好都采用yum安装,不要采用编译安装的方法,因为编译安装会导致某些依赖关系丢失。

nginx 作为HTTP和反向代理,处理静态页面,动态服务交由nodejs服务。

nodejs作为处理动态事件的服务器。

mysql是数据库。

memcahed是一个数据缓存系统,为mysql提供缓存功能

nginx+nodejs+mysql+memcached服务器后台架设centos6.5

首先你需要会使用linux的命令行操作,然后你至少需要简单理解js语言,还要熟悉mysql的基本操作

系统环境

系统环境是centos6.5,yum源可能需要更新,为了下载更快,可以更换国内源,taobao源或者163源,具体操作自行百度,这里我直接采用默认源。

使用下面的命令更新yum源

安装nginx

命令如下

安装nodejs

#安装epel源,否则yum上找不到nodejs
yum install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
 
#安装nodejs 和 npm ,npm是nodejs的一个包管理器之类的软件。
yum install nodejs npm
 
#可以通过node -v 查看安装版本

express是nodejs的一个框架软件,很好很方便

安装mysql和memcached

安装mysql

#安装mysql
yum install mysql
yum install mysql-server
yum install mysql-devel
#mysql查看是否安装成功,进入mysql命令行输exit退出来

 使用mysql

mysqladmin -u root password root   #第一个root是root账户,第二个root是需要设置的密码,这里以“root”为例
mysql -u root -p       #以root用户登录mysql,会要求你输入密码,就输入刚刚设置的密码
#接下来就是基本的数据库操作,这里不再赘述

下面设置mysqld开机自启动,mysql的服务叫做mysqld

修改mysql远程连接:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;    //允许root用户以root密码远程连接
FLUSH   PRIVILEGES;        //刷新    

安装 memcached

#安装memcached
yum install memcached
#查看是否安装成功
memcached -h
#启动服务,默认会在11211端口启动,其他参数自行百度
service memcached start
#依然是设置开机自启
Chkconfig memcached on

架设服务器

环境配置好了,就可以架设服务器了

  1. 通过nginx建立nodejs服务器的反向代理
  2.  /etc/nginx/nginx.conf 这个文件就是调用conf.d中所有配置文件的配置文件,想添加自己的配置文件最好不要修改/nginx.conf和default.conf了,自己在conf.d中建立一个.conf文件
  3. cd /etc/nginx/conf.d 可以看到default.conf 这是默认的配置文件,可以通过修改它使nginx支持php等操作(默认支持php页面的代码被注释掉了,可以取消注释来建立)
  4.  /etc/nginx/conf.d 中建一个支持反向代理的.conf文件
  5. 接下来重启nginx服务 这时访问localhost:8080应该能看到localhost:3000的对应页面,如果出现502错误可以去/var/log/nginx里的err.log看看错误日志,比如权限问题,可以
     
  6. 以上基本的nodejs服务器和nginx代理服务器都算建立了,可以修改nginx.conf配置文件来确定哪些页面请求使用代理,哪些页面请求直接处理

附一篇nodejs通过memcached查询mysql的代码

//这个文件命名为sel.js,放在nodejs项目中的routes文件夹里
var express = require('express');
var URL = require("url");
var mysql = require("mysql");
var cache = require('memcached');
var router = express.Router();
 
router.get('/', function(req, res, next) {
    var memcached = new cache('localhost:11211');//新建cache连接
    memcached.get(URL.parse(req.url,true).query.name,function(err,rows){
        if(err) console.log(err);
        if (rows) {            //如果mmecache查到了
            res.send(rows);
            console.log("find in cache");    //显示查询结果
        } else{                    //否则新建mysql连接
                console.log("not find in cache");
                var con = mysql.createConnection({
                    host:'localhost',
                    user:'root',
                    password:'root',
                    database:'nodetest',
                    port:3306
                });
                con.connect();                //连接查询
                console.log("connect the mysql");
                con.query('select id from peo where name =?',[URL.parse(req.url,true).query.name], function(err0,rows1) {
                    if (err0) console.log(err0);
                    if (rows1 != "") {            //MYSQL中存在记录,将记录写回memcache,同时送给前台
                        console.log("find in mysql");
                        res.send(rows1);
                        memcached.set(URL.parse(req.url,true).query.name,rows1,10000,function(err0,rows1){
                            if(err0) console.log(err0);
                            console.log("save into cache");
                        });
                    } else{            //mysql中不存在记录,输出不存在
                        console.log("no record");
                        res.send("no record");
                    };        
                });
                con.end();
                console.log("end line of mysql");
            };
    memcached.end();
    console.log("end line of cache");
    });
});
module.exports = router;
//修改nodejs项目文件夹中的app.js文件
app.use(express.static(path.join(__dirname, 'public')));
 
app.use('/', routes);
app.use('/users', users);
app.use('/sel', require('./routes/sel'));  //这里是刚刚建立的sel路由文件,上下文对照代码自己找
 
 
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');

声明: 本文由(fengneng)原创,转载请保留本文链接:http://www.yalewoo.com/nginx_nodejs_mysql_memcached_on_centos65.html

当然,你还可以在app.js中增加路由调用,然后在routes文件夹中写更多自己的模块,那就需要你深入的学习nodejs了。

posted @ 2015-10-11 21:50  程序员小波与Bug  阅读(520)  评论(0编辑  收藏  举报