Linux 常用命令

老是忘记一些命令。于是便记了下来:)

SSH常用命令

1. 不带端口号的连接:
$ ssh -l username ip
$ ssh -l ubuntu0 192.168.123.100

2. 带端口号的链接:
$ ssh -l username ip -p port
$ ssh -l root 224.217.33.111 -p 8888

SCP常用命令

1. 从远端拷贝单个文件到本地,带端口号(-P 为大写):
$ scp -P port root@ip:path_remote path_local
$ scp -P 8888 root@224.217.33.111:/home/aku/www/weekphp/sql.txt /home/david/

2. 从远端拷贝整个文件目录到本地,带端口号(-P 为大写):
$ scp -r -p port username@ip:path_remote path_local
$ scp -r -P 8888 root@224.217.33.111:/home/aku/www/weekphp/ /tmp/

3. 从本地拷贝单个文件到远端,不带端口号:
$ scp path_local username@ip:path_remote
$ scp index.html ubuntu0@192.168.123.100:/tmp/

查找文件内容

1. 在某一目录下查找是否有包含指定内容的文件
$ grep STRING PATH -r
$ grep “function render(” icampus/ -r

2. 使用find在某一目录下查找某一文件
$ find <指定目录> <指定条件> <指定动作>
$ find / -name “mysql.h” -ls
搜索根目录中,所有文件名以mysql.h开头的文件,并显示它们的详细信息。

C++调用自己的.so

由于一些原因,需要在C++中动态加载自己写的动态链接库(.so)文件。网络上的资源挺多,我也看了不少,参考最多的是下面这三篇

1. dlopen加载c++ 函数及类: http://blog.csdn.net/lwj1396/article/details/5204484

2. 上一篇的英文版本: http://www.isotton.com/devel/docs/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html#theproblem

3. 动态调用动态库方法 .so: http://blog.csdn.net/lbmygf/article/details/7401862

再说一说自己的心得吧。

首先介绍一下动态库和静态库之间的区别

静态库是指编译连接时,把库文件的代码全部加入到可执行文件中,所以生成的文件较大,但运行时,就不再需要库文件了。即,程序与静态库编译链接后,即使删除静态库文件,程序也可正常执行。

动态库正好相反,在编译链接时,没有把库文件的代码加入到可执行文件中,所以生成的文件较小,但运行时,仍需要加载库文件。即,程序只在执行启动时才加载动态库,如果删除动态库文件,程序将会因为无法读取动态库而产生异常。

那么如何调用动态库?如何在C语言下,其实是很简单的(调用dlopen、dlsym和dlclose就够了),但对C++来说,情况稍微复杂。动态加载一个C++库的困 难一部分是因为C++的name mangling

然后从介绍Name Mangling开始

在每个C++程序(或库、目标文件)中,所有非静态(non-static)函数在二进制文件中都是以“符号(symbol)”形式出现的。这些符号都是唯一的字符串,从而把各个函数在程序、库、目标文件中区分开来。
在C中,符号名正是函数名:strcpy函数的符号名就是“strcpy”,等等。这可能是因为两个非静态函数的名字一定各不相同的缘故。
而C++允许重载(不同的函数有相同的名字但不同的参数),并且有很多C所没有的特性──比如类、成员函数、异常说明──几乎不可能直接用函数名作符 号名。为了解决这个问题,C++采用了所谓的name mangling。它把函数名和一些信息(如参数数量和大小)杂糅在一起,改造成奇形怪状,只有编译器才懂的符号名。例如,被mangle后的foo可能 看起来像foo@4%6^,或者,符号名里头甚至不包括“foo”。
其中一个问题是,C++标准(目前是[ISO14882])并没有定义名字必须如何被mangle,所以每个编译器都按自己的方式来进行name mangling。有些编译器甚至在不同版本间更换mangling算法(尤其是g++ 2.x和3.x)。即使您搞清楚了您的编译器到底怎么进行mangling的,从而可以用dlsym调用函数了,但可能仅仅限于您手头的这个编译器而已, 而无法在下一版编译器下工作。

解决方案 extern “C”

C++有个特定的关键字用来声明采用C binding的函数:extern “C” 。 用 extern “C”声明的函数将使用函数名作符号名,就像C函数一样。因此,只有非成员函数才能被声明为extern “C”,并且不能被重载。尽管限制多多,extern “C”函数还是非常有用,因为它们可以象C函数一样被dlopen动态加载。冠以extern “C”限定符后,并不意味着函数中无法使用C++代码了,相反,它仍然是一个完全的C++函数,可以使用任何C++特性和各种类型的参数。

示例程序1. 加载简单函数

目录结构
Selection_141
示例程序1在test1目录下,这个例子也主要是参考第一篇博客写的。有一些修改。

main.cpp的代码如下

[cpp]
//———-
//main.cpp:
//———-
#include
#include

int main() {
using std::cout;
using std::cerr;

cout << "C++ dlopen demo\n\n";

// open the library
cout << "Opening hello.so…\n";
void* handle = dlopen("libhello.so", RTLD_LAZY);

if (!handle) {
cerr << "Cannot open library: " << dlerror() << ‘\n’;
return 1;
}

// load the symbol
cout << "Loading symbol hello…\n";
typedef void (*hello_t)();

// reset errors
dlerror();
hello_t hello = (hello_t) dlsym(handle, "hello");
const char *dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol ‘hello’: " << dlsym_error <<
‘\n’;
dlclose(handle);
return 1;
}

// use it to do the calculation
cout << "Calling hello…\n";
hello();

// load the symbol
cout << "Loading symbol add…\n";
typedef int (*add_t)(int, int);

// reset errors
dlerror();
add_t add = (add_t) dlsym(handle, "add");
if (dlsym_error) {
cerr << "Cannot load symbol ‘add’: " << dlsym_error <<
‘\n’;
dlclose(handle);
return 1;
}

// use it to do the calculation
cout << "Calling the add()…\n";
cout << add(98, 99) << " is the result\n";

// close the library
cout << "Closing library…\n";
dlclose(handle);
}

[/cpp]

然后是hello.cpp的代码,一会儿将会把这个源文件编译打包成.so文件。

[cpp]
//———-
// hello.cpp:
//———-
#include

extern "C" void hello() {
std::cout << "hello" << ‘\n’;
}

extern "C" int add(int a, int b){
return a + b;
}
[/cpp]

介绍一下上面用到的接口函数

1)       dlopen

函数原型:void *dlopen(const char *libname,int flag);

功能描述:dlopen必须在dlerror,dlsym和dlclose之前调用,表示要将库装载到内存,准备使用。如果要装载的库依赖于其它库,必须首先装载依赖库。如果dlopen操作失败,返回NULL值;如果库已经被装载过,则dlopen会返回同样的句柄。

参数中的libname一般是库的全路径,这样dlopen会直接装载该文件;如果只是指定了库名称,在dlopen会按照下面的机制去搜寻:

a.根据环境变量LD_LIBRARY_PATH查找

b.根据/etc/ld.so.cache查找

c.查找依次在/lib和/usr/lib目录查找。

flag参数表示处理未定义函数的方式,可以使用RTLD_LAZY或RTLD_NOW。RTLD_LAZY表示暂时不去处理未定义函数,先把库装载到内存,等用到没定义的函数再说;RTLD_NOW表示马上检查是否存在未定义的函数,若存在,则dlopen以失败告终。

2)       dlerror

函数原型:char *dlerror(void);

功能描述:dlerror可以获得最近一次dlopen,dlsym或dlclose操作的错误信息,返回NULL表示无错误。dlerror在返回错误信息的同时,也会清除错误信息。

3)       dlsym

函数原型:void *dlsym(void *handle,const char *symbol);

功能描述:在dlopen之后,库被装载到内存。dlsym可以获得指定函数(symbol)在内存中的位置(指针)。如果找不到指定函数,则dlsym会返回NULL值。但判断函数是否存在最好的方法是使用dlerror函数,

4)       dlclose

函数原型:int dlclose(void *);

功能描述:将已经装载的库句柄减一,如果句柄减至零,则该库会被卸载。如果存在析构函数,则在dlclose之后,析构函数会被调用。

好了,现在来编译打包,命令如下:

$ g++ -shared -fPIC -o libhello.so hello.cpp
$ g++ main.cpp -ldl

在上面dlopen函数中,看到我们传的第一个参数并没有指定路径,只给出了库的名称。那是因为已经在环境变量LD_LIBRARY_PATH中指定了 ./ 目录,如下图所示。
Selection_142

如果你想放在其他目录,修改该环境变量即可。

运行a.out,输入结果如下图所示

Selection_143

示例程序2. 加载类

几篇文章的意思基本都是这样:

加载类有点困难,因为我们需要类的一个实例,而不仅仅是一个函数指针。我们无法通过new来创建类的实例,因为类不是在可执行文件(这里即指由main.cpp编译成的a.out文件)中定义的,况且(有时候)我们连它的名字都不知道。
解决方案是:利用多态性! 我们在可执行文件中定义一个带虚成员函数的接口基类,而在模块中定义派生实现类。通常来说,接口类是抽象的(如果一个类含有虚函数,那它就是抽象的)。
因为动态加载类往往用于实现插件,这意味着必须提供一个清晰定义的接口──我们将定义一个接口类和派生实现类。
接下来,在模块中,我们会定义两个附加的helper函数,就是众所周知的“类工厂函数(class factory functions)(译者注:或称对象工厂函数)”。其中一个函数创建一个类实例,并返回其指针; 另一个函数则用以销毁该指针。这两个函数都以extern “C”来限定修饰。
为了使用模块中的类,我们用dlsym像示例1中加载hello函数那样加载这两个函数,然后我们就可以随心所欲地创建和销毁实例了。

———————————-polygon.hpp—————————————————————

[cpp]
//———-
//polygon.hpp:
//———-
#ifndef POLYGON_HPP
#define POLYGON_HPP

class polygon {
protected:
double side_length_;

public:
polygon()
: side_length_(0) {}

virtual ~polygon() {}

void set_side_length(double side_length) {
side_length_ = side_length;
}

virtual double area() const = 0;
};

// the types of the class factories
typedef polygon* create_t();
typedef void destroy_t(polygon*);

#endif

[/cpp]

这里把我小小的纠结了一下,函数名后面加 const=0是什么意思呢?两个typedef好像是要定义函数指针,但又不是在定义函数指针。google了一阵以后,大致都了解了。

首先,const 和 =0 没有关系,要分开理解
成员函数后面用 const 修饰,通俗的理解就是在这个函数内不能修改类的成员变量,除非那个成员变量是 mutable 的

=0表示这个成员函数是纯虚函数,也就是它可以没有定义,只有接口,由它的继承类具体定义它的行为,当然,你也可以给它定义缺省的函数体
一个类里如果包含 =0 的纯虚函数,那么这个类就是一个抽象类,它不能具体实例化(不能创建它的对象),而只能由它去派生子类

然后是两个typedef,这里其实是typedef了两个函数类型,在后面
create_t* create_triangle = (create_t*) dlsym(triangle, “create”);
的时候是加了指针符号的。
对比一下示例1中的typedef,示例1中是直接定义的函数指针,所以在dlsym这里就不用再添加指针符号。
[cpp]
typedef int (*add_t)(int, int);
add_t add = (add_t) dlsym(handle, "add";
[/cpp]

———————————————-main.cpp———————————————-

[cpp]
//———-
//main.cpp:
//———-
#include "polygon.hpp"
#include
#include

int main() {
using std::cout;
using std::cerr;

// load the triangle library
void* triangle = dlopen("triangle.so", RTLD_LAZY);
if (!triangle) {
cerr << "Cannot load library: " << dlerror() << ‘\n’;
return 1;
}

// reset errors
dlerror();

// load the symbols
create_t* create_triangle = (create_t*) dlsym(triangle, "create");
const char* dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol create: " << dlsym_error << ‘\n’;
return 1;
}

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol destroy: " << dlsym_error << ‘\n’;
return 1;
}

// create an instance of the class
polygon* poly = create_triangle();

// use the class
poly->set_side_length(10);
cout << "The area is: " << poly->area() << ‘\n’;

// destroy the class
destroy_triangle(poly);

// unload the triangle library
dlclose(triangle);
}
[/cpp]

———————————————-triangle.cpp———————————————-

[cpp]
//———-
//triangle.cpp:
//———-
#include "polygon.hpp"
#include

class triangle : public polygon {
public:
virtual double area() const {
return side_length_ * side_length_ * sqrt(3) / 2;
}
};

// the class factories
extern "C" polygon* create() {
return new triangle;
}

extern "C" void destroy(polygon* p) {
delete p;
}

[/cpp]

go on..

好了,现在来编译打包,命令如下:

$ g++ -shared -fPIC -o triangle.so triangle.cpp
$ g++ main.cpp -ldl
$ ./a.out

结果如下图所示

Selection_144

调用类成功。恩,大致就是这些啦。

Animal Farm

未命名

🙂

一个猪变人的童话故事有木有——《Animal Farm》

这本书是大学姐陪我去买的哟!!!很有纪念意义有木有!!!其他已知出场人物动物都没有这个待遇哟有木有!!!(*^__^*) 嘻嘻……

================================背景割====================================

看完整本书,依然是角色的名字都记不全,甚至连Napoleon这个大大大大大反派头头的名字都是在有道词典里搜了一下“拿破仑”才拼全,实在有点不好意思。

Napoleon这头顶着男主角光环的猪,这头腹黑猪,这头盗取了革命果实的猪,这头越来越残暴的猪,这头应该已经被无数世人咒骂过的猪,早已死在了一九不知多少年(好的,如果一头猪能活过55岁,那它也可能死于二零多少年。不要纠结这里了好吗,强迫症么有)。

被盗取了革命果实,其他动物确是值得同情的。好不容易推翻了农场主的统治,好不容易击退了敌人的反扑,好不容易盼来了平等、自由,即使是在最后的最后的前一刻,在那划不清人猪界限的前一刻,动物们依然是乐观的,依然认为:至少,我们现在是在为自己劳动,不被人类所奴役,我们是自由的,这就够了。殊不知,理想已经离他们很远很远,只是逐渐麻木而不觉罢了。

逐渐麻木——我想,这或许是Napoleon采取的连它自己都不知道的一个策略。这么对比并不那么恰当,就像你觉得你大学室友4年都没变,但一个4年不见他的人觉得几乎换了一个人一样。慢慢的,总是不易察觉的。

在欲望本生会无限膨胀这个属性的作用下,Napoleon慢慢的一点一点的往集权的方向走去。动物们慢慢适应,慢得觉得生活好像就应该是这样的,慢得即使有一些觉得不对的地方也只会是短暂的不安而已。最终它终于达到了权利的巅峰,当然也伴随着残暴的统治。最初的乌托邦,最终的不知道什么汤(加了两勺铊盐的?)。

不了解那个年代的历史,也就无清楚作者在映射什么了。不过能看到猪变人这个神奇的事情也挺高兴呢。

晚安,

2013-4-28

02:21:27

记—浮躁

记—浮躁

by 囡囡

fz

开始,支持一下北大荒文艺2B青年—猴子,如果你今天不是喊我写,我也不想装文艺。
结束,几年没写东西了,心乱了,写出来的也乱。

其实 ,我狠不想用电脑写东西。
记2008,那时候我们随波逐流,看到猴子买了一个小黑,自己也想要一个,总是觉得自己用它的时候有种小小的骄傲。
在人前人后说,它有好好好好,直到现在我也不否认它的好。
但是当时只因为我们都是T系,4个小黑。
随着时间过来,我们更多的是选择自己适合的东西,而不是像当年一样,选择一样不适合自己的,其实它对于当时的我来说,更好的用处没真正的发挥出来。
也许,在我们盲目的,不停的在追寻我们所需要的东西的时候,我们没有停下我们寻找的脚步,我听听我们自己的心声,也许,最适合的一直停留在我们身边,只是眼睛看花了这个世界而已。
我们不停的东张西望,根本就不知道自己需要的是什么,觉得这样的是好的,那样的也是好的,但是就是没想过,这些东西不是自己的,而是别人的,我们看的仅仅是别人外表显示出来给我们看的表面现象而已,从而我们忘了去讨论本质的。于是,我们就说,原来我需要的就是这种,因为我需要在别人面前炫耀这些属于我的东西,来证明我是不错的,因为我拥有这些。但是却忘了,当我们拥有的越多的时候,心是越累的。

心乱了,写上面一段话的时候,完全听歌都听不进去了。曾经,我们只是单纯的听歌,它所表达给我们的意思。听它单纯的旋律,因为那些旋律正好扣住了当时听歌的心情。

猴子喊我支持他的校内,keping,空间.。请问,现在的你们有多少的交流工具?有多少的地址是一直留到现在也在用的?
就连现在的电话,某些人也是1年多2年换一个。QQ空间永远都对人设有权限。无意之间点开某一人的消息,才发现。你不在他的好友范围之内,你没有访问的权利,原来以前的电话已经换人了。原来有这么多人,我们都一直没有了联系,联系时候才知道“噢,那就这样嘛,我还有事情”。这就是几年之前你为之自豪,现在漠视的友情。
因为你现在不能给予我更多的好处,你留给我的就是那段青春,我需要更多的朋友来让我不停的往前走,从而,我遗忘了过去的我们。走走停停,越走越空虚,越走越孤单。不停的留宿,不停的想找人代替,却找不到那曾经的心有灵犀。

今年过年之前,买了4本书,包括一本《百年孤独》,好久没翻开这类型的书了。我以为我还可以像以前一样感兴趣的全部看完,心完全净下来,去感受里面的故事,心乱了。

如果,还可以半夜不睡觉聊聊单纯的心事,还可以坐在江边的寺庙里吹着江风喝茶,或者在书店里看一天的书,在巴国城的坝坝上骑自行车,看着那些跳坝坝舞的阿姨,曾经开玩笑的说,如果当我们老了,还能这样在这里以这样的心情散步就是最大的幸福。

我们总是不停的追求着我们想要过的生活,却忘了停下来。
At:
2013.04.26
1:19

认识一群技术男真好,嘎嘎

IT技术男——居家好帮手,省钱好伙伴!吼吼吼!

本文里面顶着男猪脚光环的就是楼下这个胖子啦,艺名:鲁胖子

large_lSxn_52d60001971e125f

某天,楼主发现相机没有无线遥控器,太不方便了撒,买又太贵老。遂找鲁胖子商量,答曰小菜,于是搞之。

不知道他去哪里找了两个小灯泡(还真不清楚这玩意儿叫啥)?

IMG_0381

加上一根在他们公司垃圾桶里翻出来的报废塑料软管

IMG_0382

还有一个不知道哪个设备又被他拆了以后,撤下来的零件

IMG_0383

估计左焊焊,右焊焊,yeah~~音频信号转红外信号的遥控器出来了。

IMG_0384

硬件就这么愉快的搞定了,插在iphone上就能用。软件也早就去app store下好了

IMG_0385

好了,剩下就是测试无线遥控效果了,好使。

IMG_36962

就这么欢乐地省下了买无线遥控器的钱,以后出门遥控器能忘,手机还能忘嘛。。。除非又被摸走了,我勒个去。

哦对,我猜,他用申通快递邮给我的费用,比原料贵谄笑

Compile and Install LAMP(Linux/Apache/MySQL/PHP) from source on Ubuntu 12.10

This blog will guide you finish the compiling and installing a LAMP Server from source on Ubuntu step by step.

Of course, it will take you a little much time(Thanks for the 15 source packages ) ,so a cup of coffee will be a good choice 🙂

Zeroth. Here follows some source packages, which will be used when we build the LAMP.Some of them are the newest, others not.

1. httpd-2.4.4

2. mysql-5.6.10

3. php.5.3.22

4. libxml2-2.6.30

5. libmcrypt-2.5.8

6. zlib-1.2.7

7. gd-2.0.35

8. autoconf-2.61

9. freetype-2.3.5

10. libpng-1.6.0

11. jpeg-6b

12. apr-1.4.6

13. apr-util-1.4.1

14. pcre-8.32

15. libtool-2.2.6

First. check the basic system info

1. $ uname -a:
Selection_103

2. check whether there is gcc or not
$ gcc -v
Selection_104
Whoops!!!There is not,but never mind ,just install it.
$ sudo apt-get install gcc

Now, we can begin to build all the source packages:)

Second. Compile and install the source packages

2.1 install the newest libxml2 library files
2.1.1 download the libxml2-2.6.30.tar.gz from the link above or the official site, and put it into the directory /usr/local/src/, then extract it to directory  libxml2-2.6.30/ and then get into that derectory. Command lines as follow:
$ cd /usr/local/usr/                                      // enter the directory which the source package lays
$ sudo tar zxvf libxml2-2.6.30.tar.gz         // extract it
$ cd libxml2-2.6.30/                                    // enter the directory

2.1.2 use command “configure” to check and configure the system environment and generate the configured files. Command lines as follow:
$ ./configure –prefix=/usr/local/libxml2
the option –prefix=/usr/local/libxml2 here is to tell the installer to install it to directory /usr/local/libxml2 .When it finished, there will be a tips “Done configuring”, as the pic shows below.
Selection_106

2.1.3 use  command “make” to compile and generate install files
$ sudo make                                                          // compile
Here comes a error:
In function ‘open’,inlined from ‘xmlNanoHTTPSave__internal_alias’ at nanohttp.c:1588:12:

/usr/include/x86_64-linux-gnu/bits/fcntl2.h:51:24: error: call to ‘__open_missing_mode’ declared with attribute error: open with O_CREAT in second argument needs 3 arguments

solution:
open and edit the nanohttp.c which is  on the current directory,see the 1588th line, and modify
fd = open(filename, O_CREAT | O_WRONLY);                   to
fd = open(filename, O_CREAT | O_WRONLY,0777);
as the pic shows below.
Selection_108

2.14 use command”make install” to install the software.Command lines as follow:
$ sudo make install                                               // install
if installed success, there will be 5 subdirectories bin, include ,lib, man and share under /usr/local/libxml2/, as the pic shows below.
Selection_109
*when we install php5 later, we’ll add “–with-libxml-dir=/usr/local/libxml2” to the configure options to  specify the location of  libxml2 library files.

2.2 install the newest libmcrypt library files
2.2.1 download the libmcrypt-2.5.8.tar.gz from the link above or the official site, and put it into the directory /usr/local/src/, then extract it to directory libxml2-2.6.30/ and then get into that derectory. Command lines as follow:
$ cd /usr/local/usr/                                      // enter the directory which the source package lays
$ sudo tar libmcrypt-2.5.8.tar.gz                // extract it
$ cd libxml2-2.6.30/                                    // enter the directory

2.2.2 use command “configure” to check and configure the system environment and generate the configured files. Command lines as follow:
$ ./configure –prefix=/usr/local/libmcrypt
the option –prefix=/usr/local/libmcrypt here is to tell the installer to install it to directory /usr/local/libmcrypt.
Here will use the g++ complier, and if there is not a g++, you should first do the sudo apt-get install g++ first.

2.2.3 use  command “make” to compile and generate install files
$ sudo make                                                          // compile

2.2.4 use command”make install” to install the software.Command lines as follow:
$ sudo make install                                              // install
if installed success, there will be 5 subdirectories bin, include ,lib, man and share under /usr/local/libmcrypt/, as the pic shows below.
Selection_110
*when we install php5 later, we’ll add “–with-mcrypt-dir=/usr/local/libmcrypt” to the configure options to  specify the location of  libmcrypt library files.

2.2.5 install the libltdl library files
when finished installing the libmcrypt, enter into the directory /usr/local/src/libmcrypt-2.5.8, and then enter the subdirectory libltdl. Follow the command lines below to finish the configure, compile and install:
$ cd /usr/local/src/libmcrypt-2.5.8/libltdl    // enter the directory which the source package lays
$ ./configure  –enable-ltdl-install                  // configure it
$ make                                                             // compile
$ make install                                                  // install
if installed success, there will be a header file ltdl.h under the directory /usr/local/include,as the pic show below.
Selection_112

2.3 install the newest zlib library files
2.3.1 download the zlib-1.2.7.tar.gz from the link above or the official site, and put it into the directory /usr/local/src/, then extract it to directory zlib-1.2.7/ and then get into that derectory. Command lines as follow:
$ cd /usr/local/usr/                                      // enter the directory which the source package lays
$ sudo tar zxvf zlib-1.2.7.tar.gz                  // extract it
$ cd zlib-1.2.7/                                             // enter the directory

2.3.2 use command “configure” to check and configure the system environment and generate the configured files. Command lines as follow:
$ ./configure
you’d better not use the option –prefix=/usr/local/zlib here, because it will lead to failing  to locate the zlib library when install libpng.

2.3.3 use  command “make” to compile and generate install files
$ sudo make                                                          // compile

2.3.4 use command”make install” to install the software.Command lines as follow:
$ sudo make install                                              // install
if installed success, there will be 3 subdirectories include ,lib and share under /usr/local/zlib/, as the pic shows below.
Selection_113*when we install php5 later, we’ll add “–with-zlib-dir=/usr/local/zlib” to the configure options to  specify the location of  zlib library files.

2.4 install the newest libpng library files

2.4.1 Download the file: libpng-1.6.0.tar.gz from the above link or the official site , put it under /usr/local/src/, and extract it to libpng-1.6.0/, then enter the directory using following command:

$ cd /usr/local/usr/                                      // enter the directory where the source code is
$ sudo tar zxvf libpng-1.6.0.tar.gz             // extract the file
$ cd libpng-1.6.0/                                        // enter the current directory

2.4.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows:

$ ./configure –prefix=/usr/local/libpng

“–prefix=/usr/local/libpng” means that the installing software will be installed under /usr/local/libpng.

ERROR: configure: error: zlib not installed

Solution at this blog: http://keping.me/2013-3-12-2/ (see the English version… I don’t like this solution but it’s the only effective one I have ever found. 🙁 )

2.4.3 use the make command to compile the source file and generate the installation file:

$ make                                                          // compile

2.4.4 use command make install to install:

$ make install                                              // install

If the installation succeeded, there would be four directories(bin/, include/, lib/, share/)generated under /usr/local/libpng, as following picture:

* When installing the GD2 lib, it should add the option –with-png-dir=/usr/local/libpng behind configure command to locate the position of libpng file.

2.5 install the newest jpeg6 library files

2.5.1 Download the jpegsrc.v6b.tar.gz file from above link or the official site,  put it under /usr/local/src/, and extract it into  directory jpeg-6b/  and then enter the current directory jpeg-6b/. Commands as follows:

$ cd /usr/local/usr/                                      // enter the directory where the source file is
$ sudo tar zxvf jpegsrc.v6b.tar.gz              // extract the file
$ cd jpeg-6b/                                                // enter the current directory

2.5.2 We need to create the installation directory manually when installing the jpeg6 lib files before installing the GD2. The installation directory will not be created automatically. The command as follows:

$ sudo mkdir /usr/local/jpeg6                          // create a installation directory
$ sudo mkdir /usr/local/jpeg6/bin                   // create a directory saving commands
$ sudo mkdir /usr/local/jpeg6/lib                     // create a jpeg6 directory
$ sudo mkdir /usr/local/jpeg6/include              // create a directory saving header files
$ sudo mkdir -p /usr/local/jpeg6/man/man1   // create a directory saving manual

2.5.3 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows: ( for displaying explicitly, use “\” to split the command for several lines):

$ sudo ./configure \
> –prefix=/usr/local/jpeg6/ \                // install the software into /usr/local/jpeg6
> –enable-shared \                                 // GUN’s libtool will be used when creating shared lib
> –enable-static                                       // GUN’s libtool will be used when creating static  lib

2.5.4 use the make command to compile the source file and generate the installation file:

$ make                                                          // compile

ERROR:

./libtool –mode=compile gcc -O2 -I. -c ./jcapimin.c
make: ./libtool: Command not found
make: *** [jcapimin.lo] Error 127

Solution:  http://keping.me/2013-4-12/ ( see the English version…:) )

2.5.5 use command make install to install:

$ make install                                              // install

* When installing the GD2 lib, it should add the option –with-jpeg-dir=/usr/local/jpeg6 behind configure command to locate the position of jpeg lib file.

So far, we have installed so many packages as following picture~~~Have a rest, please~~

2.6 install the newest freetype library files

2.6.1 Download the file freetype-2.3.5.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it into freetype-2.3.5/ and then enter the current directory. The commands as follows:

$ cd /usr/local/usr/                                      //  enter the directory where the source code is
$ sudo tar zxvf freetype-2.3.5.tar.gz          // extract the file
$ cd freetype-2.3.5/                                     // enter the current directory

2.6.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows:

$ ./configure –prefix=/usr/local/freetype

“–prefix=/usr/local/freetype” means that the installing software will be installed under /usr/local/freetype.

2.6.3 use the make command to compile the source file and generate the installation file:
$make                                                      // compile

2.6.4 use command make install to install:
$ make install                                              // install

If the installation succeeded, there would be four directories(bin/, include/, lib/, share/)generated under /usr/local/freetype, as following picture:

* When installing the freetype lib, it should add the option –with-jpeg-dir=/usr/local/freetype behind configure command to locate the position of freetype lib file.

2.7 install the newest autoconf library files

2.7.1 Download the file autoconf-2.61.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it into autoconf-2.61/ and then enter the current directory. The commands as follows:

$ cd /usr/local/usr/            // enter the directory where the source code is
$ sudo tar zxvf autoconf-2.61.tar.gz          // extract the file
$ cd autoconf-2.61/                              // enter the current directory

2.7.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows:
$ ./configure

ERROR: configure: error: GNU M4 1.4 is required

Solution: $ sudo apt-get install m4

2.7.3 use the make command to compile the source file and generate the installation file:
$make                                                      // compile

2.7.4 use command make install to install:
$ make install                                              // install

2.8 install the newest GD library files

2.8.1 Download the file gd-2.0.35.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it into gd-2.0.35/ and then enter the current directory. The commands as follows:

$ cd /usr/local/usr/            // enter the directory where the source code is
$ sudo tar zxvf gd-2.0.35.tar.gz          // extract the file
$ cd gd-2.0.35/                              // enter the current directory

2.8.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows:

$ sudo ./configure \
> –prefix=/usr/local/gd2/ \                             // install the software into /usr/local/gd2
> –with-zlib=/usr/local/zlib/ \                         // locate zlib
> –with-jpeg=/usr/local/jpeg6/ \                    // locate jpeg6
> –with-png=/usr/local/libpng/ \                    // locate libpng
> –with-freetype=/usr/local/freetype/           // locate freetype 2.x font lib

2.8.3 use the make command to compile the source file and generate the installation file:
$make                                                      // compile

ERROR: gd_png.c:16:53: fatal error: png.h: No such file or directory

Solution: http://keping.me/2013-3-13/( see the English version 🙂 )

2.8.4 use command make install to install:
$ make install                                              // install

If the installation succeeded, there would be four directories(bin/, include/, lib/, share/)generated under /usr/local/gd2, as following picture:

* When installing the GD2 lib, it should add the option –with-jpeg-dir=/usr/local/gd2/ behind configure command to locate the position of GD lib file.

2.9 install the newest Apache server

2.9.1 Download the file httpd-2.4.4.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it into httpd-2.4.4/ and then enter the current directory. The commands as follows:

$ cd /usr/local/usr/            // enter the directory where the source code is
$ sudo tar zxvf  httpd-2.4.4.tar.gz          // extract the file
$ cd httpd-2.4.4/                              // enter the current directory

2.9.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows: ( for displaying explicitly, use “\” to split the command for several lines):

$ sudo ./configure \
> –prefix=/usr/local/apache2 \                // specify the installation location of Apache
> –sysconfdir=/etc/httpd  \                             // specify the location saving configuration files of Apache server
> –with-z=/usr/local/zlib/ \                             // specify the location of zlib
> –with-included-apr  \                                    // using the copy of bundled APR / APR-Util
> –disable-userdir \                                          // requests mapped to user-specific directories
> –enable-so \                                                   // compiled as dynamic sharing object(DSO)
> –enable-deflate=shared \                             // reduce the support of transmission encoding
> –enable-expires=shared \                            // support the control of the header files expiration
> –enable-rewrite=shared \                            // url control based on rules
> –enable-static-support                                 // create a support of a statically linked version

ERROR: configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

Solution: http://keping.me/2013-3-13-2/ (see the English version 🙂 )

2.9.3  use the make command to compile the source file and generate the installation file:
$make                                                      // compile

2.9.4 use command make install to install:
$ make install                                              // install

If the installation succeeded, there would be twelve directories(bin/, build/, cgi-bin/, error/, htdocs/, icons/, include/, lib/, logs/, man/, manual/, modules/)generated under /usr/local/apache2, as following picture:

2.9.5 Start Apache server using following command:

$ sudo /usr/local/apache2/bin/apachectl start

ERROR: AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message

Solution: modify the file httpd.conf under the directory /etc/httpd( the location specified when installing Apache server), as following picture:

and use following command to modify the file:

$ sudo vim /etc/httpd/httpd.conf

find the location of ServerName, as following picture:

add a line “ServerName localhost” under the ServerName, as following picture:

Restart Apache server with following command:

sudo /usr/local/apache2/bin/apachectl restart

To check whether it started successfully, use the grep command:

$ ps -ef | grep httpd

If there are 4/5 lines output, it started successfully, as following picture:

Also, we can check it by entering “localhost” in the your browser. If succeeded, the following content will occur:

2.9.6 Run on startup

Each server software need to be configured to run on startup. About Apache, we just need to add the start command line of Apache server in the file “/etc/rc.local”. Command as follows:

$ sudo vim /etc/rc.local

2.10 install the Mysql

2.10.1 Download the file mysql-5.6.10.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it to mysql-5.6.10/, and enter the current directory. The commands as follows:
$ cd /usr/local/usr/            // enter the directory where the source code is
$ sudo tar zxvf  mysql-5.6.10.tar.gz         // extract the file
$ cd mysql-5.6.10/                              // enter the current directory

2.10.2 Install cmake. The mysql 5.5 doesn’t use “./configure” command to configure and change to cmake, so we should install cmake. Check if there is cmake in your system with command as follows:

$ cmake -version

If there is no output as the following picture

you should install it by yourself:

$ sudo apt-get install cmake

2.10.3 Add new user group:

$ sudo groupadd mysql

2.10.4 Add new user:

$ sudo useradd mysql -g mysql

2.10.5 Create a new directory of database execution file :

$ sudo mkdir -p /usr/local/mysql

2.10.6 Create a new directory of new data file

$ sudo mkdir -p /db/mysql/data

2.10.8 Modify the directory owners:

$ sudo chown -R mysql:mysql /usr/local/mysql
$ sudo chown -R mysql:mysql /db/mysql/data
$ sudo chown -R mysql:mysql /usr/local/mysql/.
$ sudo chown -R mysql:mysql /db/mysql/data/.

2.10.8 Configure with cmake:

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/db/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306

ERROR:CMake Error at cmake/readline.cmake:83 (MESSAGE):
Curses library not found. Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:126 (FIND_CURSES)
cmake/readline.cmake:193 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:325 (MYSQL_CHECK_READLINE)

Solution: according the prompt, install the missing package ncurses:

$ sudo apt-get install libncurses5-dev

adn then delete the cache files under current diretory:

$ sudo rm CMakeCache.txt

then reconfigure using cmake. Just copy commands above.

2.10.9 use the make command to compile the source file and generate the installation file:
$sudo make                                                      // compile

It shall take a long time to compile:

2.10.10 use command make install to install:
$ sudi make install                                              // install

2.10.11 copy the configuration file

$ sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

2.10.12 enter the installation path:

$ cd /usr/local/mysql

2.10.13 run the configuration script;

$ sudo ./scripts/mysql_install_db –user=mysql –datadir=/db/mysql/data

2.10.14 copy the service start script:

$ sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

2.10.15 start MySQL:

$ sudo service mysql start

2.10.16 set  password for root:

$ sudo /usr/local/mysql/bin/mysqladmin -u root password 123456

2.10.17 run on startup:

$ sudo update-rc.d mysql defaults

2.10.18 set mysql as system command:

$ sudo ln -s /usr/local/mysql/bin/* /bin/

after that, just execute the following command:

$ mysql -u root -p

you can login to mysql

2.10.19 set access permission

During installing MySQL, the application mysql_install_db installed the MySQL database authorization table. This table defines the initial accounts and authorizations of MySQL, and all accounts have no passwords. These accounts are super user accounts, they can perform any operations. Root account’s initial has no password, so anyone an use the root account without password to connect to MySQL server and get all permissions, which means MySQL installation is unprotected. If you want to prevent client form connecting without password, you should specify a password for anonymous account or delete anonymous account and set a password for MySQL users. Start MySQL client  console with “mysql –u root” to connect to MySQL server, command as follows:

$ mysql -u root -p

use the password set above (123456) to login, and perform:

mysql> DELETE FROM mysql.user WHERE Host=’localhost’ AND User=”;
Query OK, 1 rows affected (0.08 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 1 rows affected (0.01 sec)

This means MySQL installed succeessfully~~

2.11 install the PHP

2.11.1 Download the file php-5.3.22.tar.gz on the above link or the official site, put it under /usr/local/src/, and extract it to php-5.3.22/, and enter the current directory. The commands as follows:
$ cd /usr/local/usr/            // enter the directory where the source code is
$ sudo tar zxvf  pcphp-5.3.22.tar.gz          // extract the file
$ cd php-5.3.22/                              // enter the current directory

2.11.2 Check and configure the installation environment with “configure” command, which will generate installation configuration file.  The command line as follows: ( for displaying explicitly, use “\” to split the command for several lines):

$ sudo ./configure \
> –prefix=/usr/local/php \                                                     // set the installation path for PHP5
> –with-config-file-path=/usr/local/php/etc \                     // specify the path saving PHP5 configuration files
> –with-apxs2=/usr/local/apache2/bin/apxs \                   // locate Apache2
> –with-mysql=/usr/local/mysql/ \                                      // specify the installation directory of PHP5
> –with-libxml-dir=/usr/local/libxml2/ \                             // locate libxml2
> –with-png-dir=/usr/local/libpng/ \                                   // locate libpng
> –with-jpeg-dir=/usr/local/jpeg6/ \                                    // locate jpeg
> –with-freetype-dir=/usr/local/freetype/ \                        // locate freetype
> –with-gd=/usr/local/gd2/ \                                                // locate gd
> –with-zlib-dir=/usr/local/zlib/ \                                         // locate zlib
> –with-mcrypt=/usr/local/libmcrypt/ \                              // locate libmcrypt
> –with-mysqli=/usr/local/mysql/bin/mysql_config \       // locate  MySQLi
> –enable-soap \                                                                    // enable SOAP
> –enable-mbstring=all \                                                       // enable multiple string
> –enable-sockets                                                                   // enable socket

2.11.3 use the make command to compile the source file and generate the installation file:
$make                                                      // compile
2.11.4 use command make install to install:
$ make install                                              // install
2.11.5 Create configuration file. Specify the location of configuration file by adding the option “–with-config-file-path=/usr/local/php/etc/” when using “configure” command to install the configuration. Copy the “php.ini- dist” file from the source directory to specified
directory “/usr/local/php/etc/” and change its name to “php.int”:

$ sudo cp php.ini-dist /usr/local/php/etc/php.ini      // create the configuration

2.11.6 Integrate Apache and PHP. Before compiling PHP, we add the option “–with-apxs2=/usr/local/apache2/bin/apxs” behind the configure command to make PHP as the Apache function. But we still need to modify Apache configuration file by adding PHP support to tell Apache certain extensions as PHP parse. For example, let Apache parse files with extensions like .php and .phtml to PHP. Open Apache configuration file /etc/httpd/httpd.conf, find the line “AddType application/x-gzip .gz .tgz” and under it add a command line “Addtype application/x-httpd- php .php .phtml”. Files with any extension can be parsed to PHP, as long as we add the type to the added command and separated with backspace, as following picture:

We add a line “AddType application/x-httpd-php-source .phps” in the end to take the file with .phps extensions as PHP source file for syntax highlighting.

2.11.7 Restart Apache server, for only after the restart changes of configuration file would take effect:

$ sudo /usr/local/apache2/bin/apachectl stop           // stop Apache service
$ sudo /usr/local/apache2/bin/apachectl start           // start Apache service

2.11.8 Test the PHP environment. Create a directory named phpinfo/ under /usr/local/apache2/htdocs and create a file named index.php. Add following lines to the file:

<?php
phpinfo();
?>

Open your browser enter the URL”http://localhost/phpinfo/index.php”, if the following picture occurs, it means your LAMP is successfully installed.

The function phpinfo() is to output most information about the PHP current status. It includes the information of compilation and extension, the PHP version, server information and environment, PHP environment, system information, path, local configuration value , HTTP header information and PHP License. Due to the different of each system’s installation, the  function phpinfo() can be used to check the configuration of a particular system and available predefined variable. It’s also a valuable debug tool, because it includes all EGPCS data(Environment,GET,POST,Cookie,Server).

large

ahaaaaaaaaaaaaaa~

Finally, we finished it

Sophia 译