Linux下启动mongodb(后台方式)

创建数据目录:

# mkdir /data/mongo

创建配置文件

# vi /data/mongo/mongodb.cnf

dbpath=/data/mongo/

logpath=/data/mongo/mongo.log

logappend=true

fork=true

port=27017

或者:不创建配置文件通过mongod参数启动也可以

  1. 配置文件方式启动mongo

/usr/local/mongodb/bin/mongod -f /data/mongodb/mongodb.cnf

# bin/mongod -f /data/mongo/mongodb.cnf

或者 # bin/mongod -f /data/mongo/mongodb.cnf & (放到后台执行)

2、参数启动mongo

bin/mongod -dbpath /data/mongo/ -logpath /data/mongo/mongo.log -logappend -fork -port 27017

终端输入出:

all output going to: /data/mongo/mongo.log

成功后即可以登录mongo

# bin/mongo

终端输入出:

MongoDB shell version: 2.2.3

connecting to: test

>

现在mongo就可以正常使用了

如果让mongo随linux自动启动,在/etc/rc.local添加如下即可:

rm /data/mongo/mongod,.lock #停止可能在运行的mongo

/…/bin/mongod -f /data/mongo/mongodb.cnf

或者

/…/bin/mongod -dbpath /data/mongo/ -logpath /data/mongo/mongo.log -logappend -fork -port 27017

附带提醒:

1、后台启动node命令: nohup node /…/app.js &

2、停止node和mongo都是用kill 

3、如果出现启动报错 ERROR: child process failed, exited with error number 100,一般是由于上一次mongodb没有正常关闭,只需要rm /…/mongod.lock就好

Nginx配置多个服务共用80端口

对于Web而已,80端口和443端口是十分重要的,原则上需要输入http://domain.com:80才可以浏览网页的,但由于默认端口是80,所以‘:80’可以忽略。同理对于https的443端口也一样。

随着服务器性能的提升和业务的需求,一台服务器上往往会同时有多个服务,这些服务都希望监听80端口,比如有vue.msg.com和react.msg.com。这时候我们可以使用nginx的代理转发功能帮我们实现共用80端口的需求。

http

首先我们先在两个空闲的端口上分别部署项目(非80,假设是8080和8081),nginx.conf配置如下:

$ vim /ect/nginx/nginx.conf

// nginx.conf

# vue项目配置

server {

    listen       8080;

    root         /web/vue-base-demo/dist/;

    index        index.html;

    location / {

        try_files $uri $uri/ /index.html; # 路由模式history的修改

    }

}

# react项目配置

server {

    listen       8081;

    root         /web/react-base-demo/build;

    index        index.html;

    location / {}

}

上面就是常规的配置,紧接着如果已经做好域名解析,希望vue.msg.com打开vue项目,react.msg.com打开react项目。我们需要再做两个代理,如下:

// nginx.conf

# nginx 80端口配置 (监听vue二级域名)

server {

    listen  80;

    server_name     vue.msg.com;

    location / {

        proxy_pass      http://localhost:8080; # 转发

    }

}

# nginx 80端口配置 (监听react二级域名)

server {

    listen  80;

    server_name     react.msg.com;

    location / {

        proxy_pass      http://localhost:8081; # 转发

    }

}

nginx如果检测到vue.msg.com的请求,将原样转发请求到本机的8080端口,如果检测到的是react.msg.com请求,也会将请求转发到8081端口。

这样nginx对外就有四个服务,我们只需要公布80端口的就可以了,这样就实现了多个服务共用80端口。

php函数serialize()与unserialize()

serialize()和unserialize()在php手册上的解释是:

serialize — Generates a storable representation of a value

serialize — 产生一个可存储的值的表示

unserialize — Creates a PHP value from a stored representation

unserialize — 从已存储的表示中创建 PHP 的值

<?php 
//声明一个类 
class dog { 

    var $name; 
    var $age; 
    var $owner; 

    function dog( $in_name = “unnamed”, $in_age = “0”, $in_owner = “unknown”) { 
        $this -> name = $in_name; 
        $this -> age = $in_age; 
        $this -> owner = $in_owner; 
    } 

    function getage() { 
        return ( $this -> age * 365); 
    } 
    
    function getowner() { 
        return ( $this -> owner); 
    } 
    
    function getname() { 
        return ( $this -> name); 
    } 

//实例化这个类 
$ourfirstdog = new dog( “Rover”, 12, “Lisa and Graham”); 
//用serialize函数将这个实例转化为一个序列化的字符串 
$dogdisc = serialize( $ourfirstdog); 
print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:”dog”:3:{s:4:”name”;s:5:”Rover”;s:3:”age”;i:12;s:5:”owner”;s:15:”Lisa and Graham”;} 

print ‘<BR>’; 

/* 
—————————————————————————————– 
    在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 

//我们在此注销这个类 
unset( $ourfirstdog); 

/*    还原操作   */ 

/* 
—————————————————————————————– 
    在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 
—————————————————————————————– 
*/ 


//我们在这里用 unserialize() 还原已经序列化的对象 
$pet = unserialize( $dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 
//获得年龄和名字属性 
$old = $pet -> getage(); 
$name = $pet -> getname(); 
//这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 
print “Our first dog is called $name and is $old days old<br>”; 
print ‘<BR>’; 
?>
本文地址:php函数serialize()与unserialize()

Nginx防蜘蛛爬虫处理

假定一个场景:某个网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取。有如下方法:

方法一:修改nginx.conf,禁止网络爬虫的ua,返回403。

server { 

listen 80; 

server_name 127.0.0.1; 

#添加如下内容即可防止爬虫

if ($http_user_agent ~* “qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot”) 

return 403; 

方法2:网站更目录下增加Robots.txt,放在站点根目录下。

http://tool.chinaz.com/robots/站点可以针对现在的搜索引擎按照想要的规则生成robots.txt文件。

知识扩展:

       robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。robots.txt文件告诉蜘蛛程序在服务器上什么文件是可以被查看的。

当一个搜索蜘蛛访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,如果存在,搜索机器人就会按照该文件中的内容来确定访问的范围;如果该文件不存在,所有的搜索蜘蛛将能够访问网站上所有没有被口令保护的页面。百度官方建议,仅当您的网站包含不希望被搜索引擎收录的内容时,才需要使用robots.txt文件。如果您希望搜索引擎收录网站上所有内容,请勿建立robots.txt文件。

Robots协议是国际互联网界通行的道德规范,基于以下原则建立:

1、搜索技术应服务于人类,同时尊重信息提供者的意愿,并维护其隐私权;

2、网站有义务保护其使用者的个人信息和隐私不被侵犯。

当然,如果搜索引擎不遵守约定的Robots协议,那么通过在网站下增加robots.txt也是不起作用的。(在正式环境中,可以适当允许搜索引擎抓取收录)