Ubuntu编译安装PHP 5.5.3+Nginx 1.5.4

因系统Ubuntu12.04,官方源能升级到最新的PHP版本为5.3.10,感觉版本太旧有漏洞,所以决定编译升级到最新的nginx和php。下面是安装笔记。

注:MySQL用的依然是源里的二进制包,编译MySQL耗时太久,所以就放弃了。

1.编译安装Nginx

#安装nginx依赖
apt-get install build-essential libpcre3-dev libssl-dev
wget http://nginx.org/download/nginx-1.5.4.tar.gz
tar -zxf nginx-1.5.4.tar.gz
cd nginx-1.5.4/
./configure --with-http_ssl_module
make
make install

安装后默认目录在/usr/local/nginx,执行/usr/local/nginx/sbin/nginx 测试是否安装成功。

2.编译安装PHP

#安装PHP依赖,重要
apt-get install libiconv-hook-dev libmcrypt-dev libxml2-dev libmysqlclient-dev libcurl4-openssl-dev libjpeg8-dev libpng12-dev libfreetype6-dev

#下载源码,用的是美国镜像
wget -O php5.5.3.tar.gz http://cn2.php.net/get/php-5.5.3.tar.gz/from/us1.php.net/mirror
tar -zxf php5.5.3.tar.gz
cd php-5.5.3/

#配置,MySql用的是mysqlnd
./configure -prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-iconv-dir --with-freetype-dir --with
-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr -enable-xml -disable-rpath -enable-bcmath -enable-shmop -enable-sysv
sem -enable-inline-optimization --with-curl  -enable-mbregex -enable-fpm -enable-mbstring --with-mcrypt -enable-ftp --with-gd -e
nable-gd-native-ttf --with-openssl --with-mhash -enable-pcntl -enable-sockets --with-xmlrpc -enable-zip -enable-soap --without-p
ear --with-gettext --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-pdo-mysql

make
make install

默认安装目录在/usr/local/php

3.配置nginx+php

#进入/usr/local/php/etc目录,复制一份php-fpm配置
cp php-fpm.conf.default php-fpm.conf

#从源码目录复制一份php.ini文件到/usr/local/php
cp {PHP_SOURCE_PATH}/php.ini-production /usr/local/php/etc/php.ini

#准备php-fpm服务脚本
cp {PHP_SOURCE_PATH}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

 4.结束

启动php-fpm
/etc/init.d/php-fpm start

启动nginx
/usr/local/nginx/sbin/nginx

最后收尾工作就是权限配置和nginx的站点配置了。

node.js 批量下载百度贴吧图片

有需求就有动力,你懂的…

才学Node.js,如法炮制了一个脚本,有不足之处多多指教!

用法:
指定要下载的贴吧URL,node执行即可。(默认是第一页50个帖子)
图片保存在以帖子id命名的文件夹中,由于百度返回的是gbk编码,貌似node.js不支持非utf-8 显示乱码,只能用id当文件夹名字了。

依赖: jquery,request,mkdirp 用npm 安装即可。

//
//  By [email protected]
//

var fs = require('fs');
var mkdirp = require('mkdirp');
var request = require('request');
var $ = require('jquery').create();

//萌妹吧
//var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E8%90%8C%E5%A6%B9%E5%AD%90";
//美女吧
//var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E7%BE%8E%E5%A5%B3";
//黑丝吧
var url = "http://tieba.baidu.com/f?ie=utf-8&kw=%E9%BB%91%E4%B8%9D";

var baseUrl = "http://tieba.baidu.com";
var posts = [];
var count = 0;

$.get(url,function(data){
	$(data).find("a.j_th_tit").each(function(){
		posts.push({title: this.title,url: baseUrl + $(this).attr("href")});
	});
	console.log("共有帖子"+posts.length);
	$.each(posts,function(index,post){
		downpost(post.url);
	});
});

var downpost = function(url){
	$.get(url,function(data){
		var imgs = [];
		var dir = "";
		$(data).find(".BDE_Image").each(function(){
			imgs.push(this.src);
		});
		if(imgs.length == 0) return;
		dir = url.match(/\d+$/)[0];
		mkdirp(dir,function(err){
			if(err) console.log(err);
		});
		$.each(imgs,function(index,value){
			download(value,dir,Math.floor(Math.random()*100000)+ value.substr(-4,4));
		});
		console.log("已完成" + ++count);
	});
}

var download = function(uri, dir,filename){
	request.head(uri, function(err, res, body){
		request(uri).pipe(fs.createWriteStream(dir + "/" + filename));
	});
};