<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-436801038062762809</id><updated>2025-04-23T02:17:52.931-07:00</updated><category term="CentOS"/><category term="FreeBSD"/><category term="Git"/><category term="Hadoop"/><category term="Cassandra"/><category term="GAE"/><category term="GoDaddy"/><category term="HDFS"/><category term="HTTPS"/><category term="Jetty"/><category term="NoSQL"/><category term="SMTP"/><category term="SSL"/><category term="Sendmail"/><category term="Subversion"/><category term="Svn"/><category term="Tomcat"/><category term="Xen"/><category term="java"/><category term="logging"/><category term="mysql"/><category term="php"/><category term="pptpd"/><category term="slf4j"/><category term="ssh"/><category term="sshd"/><category term="vpn"/><category term="wordpress"/><category term="云计算"/><category term="安全"/><title type='text'>Hippo notes</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>15</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-4635023922744189646</id><published>2011-04-05T20:13:00.000-07:00</published><updated>2016-09-26T04:30:08.634-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Cassandra"/><category scheme="http://www.blogger.com/atom/ns#" term="NoSQL"/><title type='text'>Apache Cassandra 0.7 的集群配置</title><content type='html'>跟 &lt;a href=&quot;http://hbase.apache.org/&quot;&gt;Hadoop/HBase&lt;/a&gt; 一样，&lt;a href=&quot;http://cassandra.apache.org/&quot;&gt;Apache Cassandra&lt;/a&gt; 也是 &lt;a href=&quot;http://nosql-database.org/&quot;&gt;NoSQL&lt;/a&gt; 产品中最为重要的成员之一，跟 HBase 相比，因为 Cassandra 使用了一种去中心化的模式（类似memcached集群）， 使用 Cassandra 搭建 NoSQL 集群更为简单容易，特别是在 0.7 版本之后，下面简述使用 Cassandra 0.7 搭建一个集群。&lt;a href=&quot;http://twitter.com/ivarptr&quot;&gt;@ivarptr&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
前提条件&lt;br /&gt;
a、准备3台或以上的计算机。下面假设有3台运行Linux操作系统的计算机，局域网的IP地址分别为 192.168.0.100, 192.168.0.101 和 192.168.0.102。&lt;br /&gt;
b、Java 1.6。&lt;br /&gt;
c、到&lt;a href=&quot;http://cassandra.apache.org/&quot;&gt;这里下载&lt;/a&gt; 0.7.x 版本的Cassandra 二进制发行包。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1、基本配置&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
挑选其中的一台机开始配置，先展开 cassandra 发行包：&lt;br /&gt;
$ tar -zxvf apache-cassandra-$VERSION.tar.gz&lt;br /&gt;
$ cd apache-cassandra-$VERSION&lt;br /&gt;
&lt;br /&gt;
其中的&amp;nbsp;conf/cassandra.yaml 文件为主要配置文件，由于 0.7 版不再采用XML格式配置文件，如果对 YAML 格式不熟悉的话最好先到&lt;a href=&quot;http://www.yaml.org/&quot;&gt;这里&lt;/a&gt;了解一下。&lt;br /&gt;
&lt;br /&gt;
Cassandra 在配置文件里默认设定了几个目录：&lt;br /&gt;
&lt;br /&gt;
data_file_directories:&amp;nbsp;/var/lib/cassandra/data&lt;br /&gt;
commitlog_directory: /var/lib/cassandra/commitlog&lt;br /&gt;
&lt;div&gt;saved_caches_directory: /var/lib/cassandra/saved_caches&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
data_file_directories 可以一次同时设置几个不同目录，cassandra 会自动同步所有目录。另外在日志配置文件 log4j-server.properties 也有一个默认设定日志文件的目录：&lt;br /&gt;
log4j.appender.R.File=/var/log/cassandra/system.log&lt;br /&gt;
&lt;br /&gt;
一般情况下采用默认的配置即可，除非你有特殊的数据储存要求，所以现在有两种方案：一是按照默认配置创建相关的目录，二是修改配置文件采用自己指定的目录。&lt;br /&gt;
下面为了简单起见采用第一种方案：&lt;br /&gt;
&lt;br /&gt;
$ sudo mkdir -p /var/log/cassandra&lt;br /&gt;
$ sudo chown -R `whoami` /var/log/cassandra&lt;br /&gt;
$ sudo mkdir -p /var/lib/cassandra&lt;br /&gt;
$ sudo chown -R `whoami` /var/lib/cassandra&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
上面的 `whoami` 是 Linux 指令用于获取当前登录的用户名，如果你不准备用当前登录用户运行 Cassandra，那么需要把 `whoami` 替换成具体的用户名。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;2、有关集群的配置&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
由于 Cassandra 采用去中心化结构，所以当集群里的一台机器（节点）启动之后需要一个途径通知当前集群，Cassandra 的配置文件里有一个 seeds 的设置项，所谓的 seeds 就是能够联系集群中所有节点的一台机器，假如集群中所有的节点位于同一个机房同一个子网，那么只要随意挑选几台运行比较稳定的机器即可。在当前的例子中因为只有3台机器，所以我挑选第一台作为种子节点，配置如下：&lt;br /&gt;
&lt;br /&gt;
seeds:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;- 192.168.0.100&lt;br /&gt;
&lt;br /&gt;
然后配置节点之前通信的IP地址：&lt;br /&gt;
listen_address: 192.168.0.100&lt;br /&gt;
&lt;br /&gt;
需要注意的是这里必须使用具体的IP地址，而不能使用 0.0.0.0 这样的地址。&lt;br /&gt;
&lt;br /&gt;
配置 Cassandra Thrift 客户端（应用程序）访问的IP地址：&lt;br /&gt;
rpc_address: 192.168.0.100&lt;br /&gt;
&lt;br /&gt;
这项可以使用 0.0.0.0 监听一台机器所有的网络接口，不过为了明确起见，这里仍然指定具体的IP地址。Cassandra 的 Keyspaces 和 ColumnFamilies 不再需要配置了，他们需要在运行时创建和维护。&lt;br /&gt;
把配置好的 Cassandra 复制到第2和第3台机器，同时创建相关的目录，还需要修改&amp;nbsp;listen_address 和&amp;nbsp;rpc_address 为实际机器的IP地址。至此所有的配置完成了。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;3、启动 Cassandra 各个节点以及集群管理&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
启动顺序没什么所谓，只要保证最终种子节点启动就可以了：&lt;br /&gt;
$&amp;nbsp;bin/cassandra -f&lt;br /&gt;
&lt;br /&gt;
参数 -f 的作用是让 Cassandra 以前端程序方式运行，这样有利于调试和观察日志信息，而在实际生产环境中这个参数是不需要的（即 Cassandra 会以 daemon 方式运行）。&lt;br /&gt;
&lt;br /&gt;
所有节点启动后可以通过 bin/nodetool 工具管理集群，比如查看所有节点运行情况：&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$ bin/nodetool -host 192.168.0.101 ring&lt;br /&gt;
Address &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Status State &amp;nbsp; Load &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Owns &amp;nbsp; &amp;nbsp;Token &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 159559... &amp;nbsp; &lt;br /&gt;
192.168.0.100 &amp;nbsp; Up &amp;nbsp; &amp;nbsp; Normal &amp;nbsp;49.27 KB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;39.32% &amp;nbsp;563215... &amp;nbsp; &amp;nbsp; &lt;br /&gt;
192.168.0.101 &amp;nbsp; Up &amp;nbsp; &amp;nbsp; Normal &amp;nbsp;54.42 KB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16.81% &amp;nbsp;849292... &amp;nbsp; &amp;nbsp; &lt;br /&gt;
192.168.0.102 &amp;nbsp; Up &amp;nbsp; &amp;nbsp; Normal &amp;nbsp;73.14 KB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;43.86% &amp;nbsp;159559...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
命令中 -host 参数用于指定 nodetool 跟哪一个节点通信，对于 nodetool ring 命令来说，跟哪个节点通信都没有区别，所以可以随意指定其中一个节点。&lt;br /&gt;
从上表可以看到运行中的节点是否在线、State、数据负载量以及节点Token（可以理解为节点名称，这个是节点第一次启动时自动产生的）。我们可以使用 nodetool 组合 token 对具体节点进行管理，比如查看指定节点的详细信息：&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$ bin/nodetool -host 192.168.0.101 info&lt;br /&gt;
84929280487220726989221251643883950871&lt;br /&gt;
Load &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 54.42 KB&lt;br /&gt;
Generation No &amp;nbsp; &amp;nbsp;: 1302057702&lt;br /&gt;
Uptime (seconds) : 591&lt;br /&gt;
Heap Memory (MB) : 212.14 / 1877.63&lt;br /&gt;
&lt;br /&gt;
查看指定节点的数据结构信息：&lt;br /&gt;
&lt;br /&gt;
$ bin/nodetool -host 192.168.0.101 cfstats&lt;br /&gt;
Keyspace: Keyspace1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Read Count: 0&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Write Count: 0&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Pending Tasks: 0&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Column Family: CF1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SSTable count: 1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;…………&lt;br /&gt;
&lt;br /&gt;
移除一个已经下线的节点（比如第2台机器关机了或者坏掉了）&lt;br /&gt;
&lt;br /&gt;
$ bin/nodetool -host 192.168.0.101 removetoken&amp;nbsp;84929280487220726989221251643883950871&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;下了线的节点如何重新上线呢？什么都不用做，再次运行 Cassandra 程序它就会自动加入集群了。&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;在实际运作中我们可能会需要隔一段时间备份一次数据（创建一个快照），这个操作在 Cassandra 里非常简单：&lt;/div&gt;&lt;div&gt;$ bin/nodetool -host 192.168.0.101 snapshot&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4、测试数据的读写&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;使用客户端组件加单元测试是首选的，如果仅想知道集群是否正常读写数据，可以用cassandra-cli 作一个简单测试：&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;$ bin/cassandra-cli -host 192.168.0.101&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;create keyspace Keyspace1;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;use Keyspace1;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;create column family Users with comparator=UTF8Type and default_validation_class=UTF8Type;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;set Users[jsmith][first] = &#39;John&#39;;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;set Users[jsmith][last] = &#39;Smith&#39;;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;get Users[jsmith];&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;上面我们创建了一个名为“Keyspace1”的 keyspace，还创建了一个名为“Users”的 Column Family，最后向 Users 添加了一个 item。正常的话应该看到类似下面的结果：&lt;/div&gt;&lt;div&gt;&lt;div&gt;=&amp;gt; (column=first, value=John, timestamp=1302059332540000)&lt;/div&gt;&lt;div&gt;=&amp;gt; (column=last, value=Smith, timestamp=1300874233834000)&lt;/div&gt;&lt;div&gt;Returned 2 results.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/4635023922744189646/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/04/apache-cassandra-07.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4635023922744189646'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4635023922744189646'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/04/apache-cassandra-07.html' title='Apache Cassandra 0.7 的集群配置'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-6342713960180832651</id><published>2011-04-01T02:16:00.000-07:00</published><updated>2016-09-26T04:30:08.639-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hadoop"/><category scheme="http://www.blogger.com/atom/ns#" term="HDFS"/><title type='text'>如何在应用程序里使用 Hadoop HDFS ——分布式计算Hadoop配置及实践（二）</title><content type='html'>&lt;a href=&quot;http://ivarptr.blogspot.com/2011/03/100hadoop.html&quot;&gt;上一篇&lt;/a&gt;讲到 Hadoop 的配置，我们在搭建分布式计算系统的同时也已经搭建好分布式储存系统了。下面简述如何在应用程序（可以是 Console Application，也可以是 Web Application）调用 Hadoop HDFS。&lt;br /&gt;
&lt;br /&gt;
我们除了可以使用 &lt;a href=&quot;http://hadoop.apache.org/common/docs/r0.18.2/cn/hdfs_shell.html&quot;&gt;Hadoop 命令行&lt;/a&gt;管理里面的文件和目录之外，也可以通过 &lt;a href=&quot;http://hadoop.apache.org/common/docs/r0.20.2/api/index.html&quot;&gt;Hadoop API&lt;/a&gt; 管理。&lt;br /&gt;
&lt;br /&gt;
1、先创建一个Java Application (Console) 程序，然后引用 hadoop-core-0.20.2.jar ，因为这个包同时引用非常多其他包，所以最好使用 Maven 引用这个包。&lt;br /&gt;
&lt;br /&gt;
2、在项目根目录创建 core-site.xml ：&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;property&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;lt;name&amp;gt;fs.default.name&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;lt;value&amp;gt;hdfs://192.168.0.10:9000&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
程序会自动寻找 CLASS_PATH 里面的 core-site.xml 文件，假如缺少这个文件的话，程序会使用本地文件系统。&lt;br /&gt;
&lt;br /&gt;
3、创建 Helloworld.java：&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import java.io.IOException;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import org.apache.hadoop.conf.Configuration;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import org.apache.hadoop.fs.FSDataInputStream;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import org.apache.hadoop.fs.FSDataOutputStream;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import org.apache.hadoop.fs.FileSystem;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;import org.apache.hadoop.fs.Path;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;public class HelloWorld {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public static void main(String[] args) {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;try {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;HelloWorld helloWorld = new HelloWorld();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;helloWorld.run();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;} catch (IOException e) {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;e.printStackTrace();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public void run() throws IOException {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Configuration conf = new Configuration();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;FileSystem fs = FileSystem.get(conf);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Path filenamePath = new Path(&quot;hello.txt&quot;);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;if (fs.exists(filenamePath)) {&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;fs.delete(filenamePath, false);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;FSDataOutputStream out = fs.create(filenamePath);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;out.writeUTF(&quot;hello world&quot;);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;out.close();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;FSDataInputStream in = fs.open(filenamePath);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;String messageIn = in.readUTF();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;System.out.print(messageIn);&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;in.close();&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;}&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
这段代码应该简单到不用怎么说明了。&lt;br /&gt;
&lt;br /&gt;
4、编译及运行，没有问题的话应该在HDFS里创建例如&lt;br /&gt;
/user/YOUR_NAME/hello.txt&lt;br /&gt;
这样的一个文本文件，可以通过浏览器访问 http://192.168.0.10:50070 查阅。&lt;br /&gt;
&lt;br /&gt;
5、注意权限。HDFS 的文件权限跟操作系统的文件权限概念上是一致的，假如你用 yang 帐号运行 Hadoop，然后上面程序是用 huang 帐号运行，那么会出现权限错误。你应该先用启动 Hadoop 的帐号创建文件夹并且赋权给 huang 这样上面的程序才能正常运行：&lt;br /&gt;
$ bin/hadoop fs -mkdir /user/huang&lt;br /&gt;
$ bin/hadoop fs -chown huang /user/huang</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/6342713960180832651/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/04/hadoop-hdfs-hadoop.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/6342713960180832651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/6342713960180832651'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/04/hadoop-hdfs-hadoop.html' title='如何在应用程序里使用 Hadoop HDFS ——分布式计算Hadoop配置及实践（二）'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-8444410875779167643</id><published>2011-03-31T12:36:00.000-07:00</published><updated>2016-09-26T04:30:08.644-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hadoop"/><category scheme="http://www.blogger.com/atom/ns#" term="云计算"/><title type='text'>如何让100台计算机同时为你计算同一道算术题？——分布式计算Hadoop配置及实践（一）</title><content type='html'>假如你有一个比较复杂的算术题，用1台普通计算机需要计算3个月才能出结果，有什么办法可以让它能在1天内出结果呢？&lt;br /&gt;
比较“廉价”的方式可能是用100台普通计算机搭建一台“超级计算机”，让100台计算机同时计算，那么理论上1天就可以完成。&lt;br /&gt;
&lt;a href=&quot;http://hadoop.apache.org/&quot;&gt;Apache Hadoop&lt;/a&gt; 是一个分布式计算框架，你只需要定义如何将一个计算任务拆分（Map）以及如何将各个分任务的结果合并（Reduce），剩下的事情比如如何协调100台计算机一起工作，如何保证即使某一台计算机中途坏掉了也不会影响整个工作等，都由 Hadoop 帮你代劳。Hadoop 同时也是一个分布式的基础架构，在其基础上有：&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://hadoop.apache.org/hdfs/&quot;&gt;HDFS&lt;/a&gt;&amp;nbsp;分布式储存&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://hbase.apache.org/&quot;&gt;HBase&lt;/a&gt;&amp;nbsp;分布式NoSQL数据库&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://nutch.apache.org/&quot;&gt;Nutch&lt;/a&gt;&amp;nbsp;分布式全文搜索&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
等等一大堆重量级产品，这些都是构建一个大型应用所必须的基础设施。@ivarptr&lt;br /&gt;
&lt;br /&gt;
这里我简单介绍基本的 Hadoop 系统的搭建方法，Hadoop 在运行时由5个程序组成：&lt;br /&gt;
1个NameNode，用于管理命名空间以及客户端对文件的访问；&lt;br /&gt;
1个JobTracker，用于调度工作以及分配任务（Task）；&lt;br /&gt;
1个SecondaryNameNode，用于辅助NameNode的工作；&lt;br /&gt;
1～N个DataNode 用于数据储存；&lt;br /&gt;
1～N个TaskTracker&amp;nbsp;用于执行任务，DataNode 和 TaskTracker 总是在同一台计算机里运行。&lt;br /&gt;
&lt;br /&gt;
前3个程序一般是分别在三台不同的计算机里运行，为了简单起见下面的例子我会在同一台计算机里运行这3个程序，然后再另外找3台计算机运行DataNode（连同TaskTracker），一共需要4台计算机。结构图如下：&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRH4wTPRDCW8Vt7JNu-1lgU2LlAlqUSALHP6vqNvNgFtZLyX7FeoV15AGen8ztWrf6rVcWRHe8Y3VrTZcvT9XMfZhNG5mxpj6nc40tHmgw0P-QC4gKUOpFY-Ee-6TFt8c6ySEQImMD-5o/s1600/hadoop.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRH4wTPRDCW8Vt7JNu-1lgU2LlAlqUSALHP6vqNvNgFtZLyX7FeoV15AGen8ztWrf6rVcWRHe8Y3VrTZcvT9XMfZhNG5mxpj6nc40tHmgw0P-QC4gKUOpFY-Ee-6TFt8c6ySEQImMD-5o/s1600/hadoop.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
下面是详细的搭建过程&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1、下载 Apache Hadoop&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
到这里&amp;nbsp;&lt;a href=&quot;http://hadoop.apache.org/&quot;&gt;http://hadoop.apache.org/&lt;/a&gt;&amp;nbsp;下载 HDFS，我使用的版本是 0.20.2，下载回来的一个压缩包就已经包含了上面提到的5个程序。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;2、配置 Hadoop&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
虽然在一个典型的 Hadoop 系统里会有 4种不同角色的计算机，不过我们为了简单起见可以先在某一台计算机编辑好配置文件，然后再分发到其他计算机，这样可以避免一台台地配置浪费时间。&lt;br /&gt;
&lt;br /&gt;
a、编辑 conf/hadoop-env.sh，设置正确的 JAVA_HOME 环境变量，比如&lt;br /&gt;
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk （Ubuntu + OpenJDK）&lt;br /&gt;
或者&lt;br /&gt;
export JAVA_HOME=/usr/lib/jvm （CentOS + OpenJDK）&lt;br /&gt;
&lt;b&gt;Tips：&lt;/b&gt;不知道是不是我现在使用的版本有 bug，Hadoop 在配置了IPv6的系统中会监听错网络地址，所以需要增加如下一行以屏蔽 java 的IPv6功能：&lt;br /&gt;
export HADOOP_OPTS=-Djava.net.preferIPv4Stack=true&lt;br /&gt;
&lt;br /&gt;
b、编辑&amp;nbsp;conf/core-site.xml&lt;br /&gt;
&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
&amp;lt;property&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;name&amp;gt;hadoop.tmp.dir&amp;lt;/name&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;value&amp;gt;/var/lib/hadoop/hadoop-${user.name}&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;property&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;name&amp;gt;fs.default.name&amp;lt;/name&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;value&amp;gt;hdfs://192.168.0.10:9000&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
&lt;br /&gt;
第一段设置数据的储存位置（相应的目录当前用户应该有全权限），第二段设置 namenode 服务地址，用于跟其他计算机交流。&lt;br /&gt;
&lt;br /&gt;
c、编辑&amp;nbsp;conf/mapred-site.xml&lt;br /&gt;
&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
&amp;lt;property&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;name&amp;gt;mapred.job.tracker&amp;lt;/name&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;value&amp;gt;192.168.0.10:9001&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
这段设置了 JobTracker 的服务地址。&lt;br /&gt;
&lt;br /&gt;
d、编辑&amp;nbsp;conf/hdfs-site.xml&lt;br /&gt;
&lt;br /&gt;
&amp;lt;configuration&amp;gt;&lt;br /&gt;
&amp;lt;property&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;name&amp;gt;dfs.replication&amp;lt;/name&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;value&amp;gt;3&amp;lt;/value&amp;gt;&lt;br /&gt;
&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;lt;/configuration&amp;gt;&lt;br /&gt;
这段设置一个文件数据在 Hadoop 系统里冗余多少份，一般设置为3。这样一旦有新的文件数据写入 Hadoop，系统会随机将文件分别写入到3台datanode，这样一旦系统中的某一台datanode坏掉了（比如硬盘或者主板烧了）只需要将这台计算机卸下，再连接一台新的计算机到网络即可，数据会自动复制以保证始终有3份数据。这也是分布式储存系统维护方便的原理（请忘掉硬盘阵列吧）。&lt;br /&gt;
&lt;br /&gt;
e、编辑 /etc/hosts&lt;br /&gt;
由于 Hadoop 有时会使用 hostname 访问系统中的其他计算机，而 hostname 有时会解析错误，所以比较笨的方法是在 /etc/hosts 里增加每一台计算机的名称和IP地址。比如：&lt;br /&gt;
192.168.0.10 &amp;nbsp; &amp;nbsp;master&lt;br /&gt;
192.168.0.100 &amp;nbsp; &amp;nbsp;data001&lt;br /&gt;
192.168.0.101 &amp;nbsp; &amp;nbsp;data002&lt;br /&gt;
192.168.0.102 &amp;nbsp; &amp;nbsp;data003&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;3、复制已配置好的 Hadoop 到每一台计算机&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
首先要保证从 namenode 计算机能够使用key方式登录（即无密码登录）到其他的每一台计算机（包括1台自己和3台datanode），实现的方法是在 namenode 计算机使用 ssh-keygen 命令生成key，对于Fedora linux发行版可能还需要运行 ssh-add 命令，然后使用 ssh-copy-id 的方法把这个key复制到每一台计算机，然后你至少使用 ssh 登录一次所有计算机（包括自己）。因为这是属于 ssh 的内容，具体的方法请查阅 ssh 相关文章。&lt;br /&gt;
然后就可以使用 scp 命令复制配置好的 Hadoop 程序到每一台 datanode 计算机了，Hadoop 程序的存放位置必须一致，比如在 namenode 计算机中Hadoop程序位于 /usr/local/hadoop，那么其他计算机也应该存放在这里。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4、设置 namenode 特有的配置&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
因为我们可以在 namenode 控制所有计算机的 Hadoop 启动或者关闭，所以需要列出有哪些计算机是受控制的。&lt;br /&gt;
a、编辑 conf/master 文件，写入如下1行&lt;br /&gt;
192.168.0.10&lt;br /&gt;
这一行表示要启动的 secondaryNameNode 位于 192.168.0.10 这台计算机（共 nameNode 同一台）&lt;br /&gt;
b、编辑 conf/slaves 文件，写入如下3行&lt;br /&gt;
192.168.0.100&lt;br /&gt;
192.168.0.101&lt;br /&gt;
192.168.0.102&lt;br /&gt;
这表示当前 Hadoop 系统一共有3台datanode以及他们的IP地址。&lt;br /&gt;
&lt;br /&gt;
5、格式化 namenode&lt;br /&gt;
这一步只需做一遍，切换到 Hadoop 程序目录，运行：&lt;br /&gt;
$ bin/hadoop namenode -format&lt;br /&gt;
&lt;br /&gt;
6、启动 hadoop&lt;br /&gt;
$ bin/start-dfs.sh&lt;br /&gt;
这时应该看到启动了 namenode, secondaryNamenode 以及3个 datanode。&lt;br /&gt;
$ bin/start-mapred.sh&lt;br /&gt;
这时应该看到启动了 jobTracker 和 3个taskTracker。&lt;br /&gt;
上面的两个命令也可以用一个命令代替：bin/start-all.sh&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;7、测试一个分布式计算&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
在 Hadoop 程序目录里附带了一个统计一个文件里每个单词出现次数的示例，我们将会用它来测试。&lt;br /&gt;
首先你需要准备几个纯文本文件（比如有文件 /tmp/text/hello.txt 和 /tmp/text/world.txt ），把他写入到 Hadoop。&lt;br /&gt;
$ bin/hadoop fs -copyFromLocal /tmp/text text&lt;br /&gt;
这样就会在 Hadoop 里创建了一个 text 文件夹，并且把 hello.txt 和 world.txt 复制到其中了。可以运行 ls 命令查看 Hadoop 里面的文件：&lt;br /&gt;
$ bin/hadoop fs -ls&lt;br /&gt;
&lt;br /&gt;
然后运行示例程序&lt;br /&gt;
$ bin/hadoop jar hadoop-0.20.2-examples.jar wordcount text text-output&lt;br /&gt;
运行完毕之后使用 ls 命令可以查看运行结果：&lt;br /&gt;
$ bin/hadoop fs -ls text-output&lt;br /&gt;
&lt;br /&gt;
你可以根据示例的源码修改成自己的程序，并且运行测试一下。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;8、Web 监控页面&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
访问 http://192.168.0.10:50070 可以查看 namenode 的总体运行情况以及已连入的 datanode 列表&lt;br /&gt;
访问&amp;nbsp;http://192.168.0.10:50030 可以查看 jobTracker 的运行情况以及各个 taskTracker 的运行情况和日志&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;9、关闭Hadoop系统&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
$bin/stop-all.sh&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;10、如何新增加一台 datanode 到系统&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
如果某台 datanode 重启了，为了让它重新加入 Hadoop 系统，需要在 datanode 计算机上运行：&lt;br /&gt;
$ bin/hadoop -daemon.sh start datanode&lt;br /&gt;
假如系统需要扩容或者换了一台新机器，这时需要把之前配置好的 Hadoop 复制过来，然后运行上面一行命令即可。假如新增了IP地址或者IP地址发生改变，记得还要在 conf/slaves 文件里作相应的增改。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;11、遇到问题？&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
需要成功配置 Hadoop 可能会有些难度，因为除了程序问题之外，还牵涉网络配置以及ssh登录设置，这些都要求你对 Linux 有比较熟悉的了解。如果遇到问题，最好的解决方法是查看 Hadoop 的日志，一般来根据日志提供的线索都能把问题解决。或者 follow me @ivarptr&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;参考资料：&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Hadoop中文文档&lt;br /&gt;
&lt;a href=&quot;http://hadoop.apache.org/common/docs/r0.20.2/cn/index.html&quot;&gt;http://hadoop.apache.org/common/docs/r0.20.2/cn/index.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Hadoop 命令手册&lt;br /&gt;
&lt;a href=&quot;http://hadoop.apache.org/common/docs/r0.20.2/cn/commands_manual.html&quot;&gt;http://hadoop.apache.org/common/docs/r0.20.2/cn/commands_manual.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running Hadoop On Ubuntu Linux (Single-Node Cluster)&lt;br /&gt;
&lt;a href=&quot;http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-single-node-cluster/&quot;&gt;http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-single-node-cluster/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Running Hadoop On Ubuntu Linux (Multi-Node Cluster)&lt;br /&gt;
&lt;a href=&quot;http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-multi-node-cluster/&quot;&gt;http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-multi-node-cluster/&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/8444410875779167643/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/03/100hadoop.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8444410875779167643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8444410875779167643'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/03/100hadoop.html' title='如何让100台计算机同时为你计算同一道算术题？——分布式计算Hadoop配置及实践（一）'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRH4wTPRDCW8Vt7JNu-1lgU2LlAlqUSALHP6vqNvNgFtZLyX7FeoV15AGen8ztWrf6rVcWRHe8Y3VrTZcvT9XMfZhNG5mxpj6nc40tHmgw0P-QC4gKUOpFY-Ee-6TFt8c6ySEQImMD-5o/s72-c/hadoop.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-187140285179832538</id><published>2011-03-31T03:25:00.000-07:00</published><updated>2016-09-26T04:30:08.632-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="Sendmail"/><category scheme="http://www.blogger.com/atom/ns#" term="SMTP"/><title type='text'>使用 Sendmail 在 CentOS 5.5 架设邮件服务器</title><content type='html'>假如你要架设一个有会员管理的网站，可能会遇到诸如“我忘记密码了”、“你有一年没来过了，有空过来瞧瞧吧”这类的问题，一般这类问题都是使用邮件解决的，比如发送一个重置密码的链接或者发送一封提醒邮件。&lt;br /&gt;
发送邮件的方法有很多，比如使用外面专业SMTP中继服务（一般收费惊人）、使用类似 &lt;a href=&quot;http://www.google.com/apps/intl/en/group/index.html&quot;&gt;Google Apps (Free)&lt;/a&gt; 的免费邮件发送服务（限制比较多，比如一天不能超过500封等），而比较廉价且彻底的解决方法应该是在自己的服务器上搭建邮件服务器（SMTP服务器）。假如你使用的是 Linux 系统，那么一般邮件服务器早已安装好了，比如 CentOS（RedHat、Fedora等）内置的&amp;nbsp;&lt;a href=&quot;http://www.sendmail.com/sm/open_source/&quot;&gt;Sendmail&lt;/a&gt; 服务程序，你只需设置几下即可使用。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1、域名的设置&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
首先你要设置域名DNS的MX记录和SPF记录，一般我们只要有如下几条DNS记录就可以了：&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;记录类型 &amp;nbsp; &amp;nbsp;主机名 &amp;nbsp; 目标&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 123.123.123.123 （这里应该是你的服务器IP）&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;CNAME &amp;nbsp; &amp;nbsp; &amp;nbsp;www &amp;nbsp; &amp;nbsp; &amp;nbsp; your-domain.com&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;CNAME &amp;nbsp; &amp;nbsp; &amp;nbsp;SMTP &amp;nbsp; &amp;nbsp; &amp;nbsp;your-domain.com&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;MX &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;smtp.your-domain.com&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;TXT &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;v=spf1 a mx ~all&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
MX记录是邮件服务器必须依赖的，而TXT（SPF）是用来防止自己的邮件服务器发送的邮件不被当成垃圾邮件（spam email），假如不增加这条记录的话对方邮箱服务要么把你的邮件直接丢弃、要么塞到垃圾邮件文件夹里，所以这条记录也是必不可少的，更多的SPF设置方法参阅这里：&lt;a href=&quot;http://www.openspf.org/&quot;&gt;http://www.openspf.org/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;2、设置 Sendmail&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
我现在使用的 Sendmail 版本是 8.14，设置方法已经变得非常简单了，首先确保你系统已经有 sendmail 及其相关服务了：&lt;br /&gt;
&lt;br /&gt;
#yum install sendmail &amp;nbsp;（这个必须的）&lt;br /&gt;
#yum install sendmail-cf&lt;br /&gt;
#yum install sendmail-devel&lt;br /&gt;
#yum install sendmail-milter&lt;br /&gt;
&lt;br /&gt;
然后切换到 /etc/mail 目录，&lt;br /&gt;
a、编辑&amp;nbsp;local-host-names 文件，在里面添加你的域名，比如 yourdomain.com，一行一个域名。&lt;br /&gt;
b、编辑 sendmail.mc，把下面两行的注释（这里是 &quot;dnl #&quot;）删除掉：&lt;br /&gt;
&lt;br /&gt;
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN&#39;)dnl&lt;br /&gt;
define(`confAUTH_MECHANISMS&#39;, `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN&#39;)dnl&lt;br /&gt;
&lt;br /&gt;
c、编辑 sendmail.mc，把下面这行：&lt;br /&gt;
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA&#39;)dnl&lt;br /&gt;
当中的“127.0.0.1”替换成“0.0.0.0”，这样外网就可以访问你的 SMTP 服务了，假如你有多个IP地址，而且只想让其中的某一个用作邮件服务，那么就把具体的IP地址填上去。&lt;br /&gt;
d、添加一个本地帐号，比如“webmaster”，并设置密码。&lt;br /&gt;
e、编辑&amp;nbsp;virtusertable 文件，增加这样的一行：&lt;br /&gt;
webmaster@yourdomain.com &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;webmaster&lt;br /&gt;
&lt;div&gt;f、重新编译一下 sendmail 的设置：&lt;/div&gt;&lt;div&gt;&lt;div&gt;# m4 sendmail.mc &amp;gt; sendmail.cf&lt;/div&gt;&lt;div&gt;# make&lt;/div&gt;&lt;/div&gt;&lt;div&gt;g、重启 sendmail 服务，这样就设置好了，你已经可以使用 mail 命令发送邮件了。&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;3、设置 IMAP 或 POP3 服务&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;虽然经过上面的设置我们已经可以对外发送邮件了（使用命令或编程方式），而且回复的邮件也能接收，不过如果我们提供IMAP或者POP3服务，就能更方便使用各种客户端查收邮件。dovecot 就是这样的一个服务。&lt;/div&gt;&lt;div&gt;a、先安装 dovecot 服务：&lt;/div&gt;&lt;div&gt;#yum install dovecot&lt;/div&gt;&lt;div&gt;b、编辑dovecot的配置文件，位于 /etc/dovecot.conf，有些Linux发行版有可能位于 /etc/dovecot/conf.d/，在&amp;nbsp;#protocols 一行下面增加：&lt;/div&gt;&lt;div&gt;protocols imap, pop3&lt;/div&gt;&lt;div&gt;假如你不需要 pop3，则直接去除。&lt;/div&gt;&lt;div&gt;c、编辑dovecot的配置文件，在 #mail_location 下面增加：&lt;/div&gt;&lt;div&gt;mail_location = mbox:~/mail:INBOX=/var/mail/%u&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;e、由于这样简单配置的IMAP或者POP3的数据传输是没有经过加密的，有些Linux发行版会禁止明文密码登录（当然位于服务器的web程序发送邮件不受此限制），所以还需要在 #disable_plaintext_auth 这行下面增加：&lt;/div&gt;&lt;div&gt;disable_plaintext_auth = no&lt;/div&gt;&lt;div&gt;f、重启 dovecot 服务，现在可以使用邮件客户端软件来测试收发邮件了。&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4、在 Web App 里发送邮件&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;这里只举一个 Java 的例子，在 Java 里发送邮件一般用 JavaMail，不过更简单的方法应该是使用 &lt;a href=&quot;http://commons.apache.org/email/&quot;&gt;Apache Commons Email&lt;/a&gt; 组件：&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;Email email = new SimpleEmail();&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setHostName(&quot;smtp.yourdomain.com&quot;);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setAuthenticator(new DefaultAuthenticator(&quot;webmaster&quot;, &quot;password&quot;));&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setTLS(false);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setFrom(&quot;webmaster@yourdomain.com&quot;);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setSubject(&quot;Hello&quot;);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.setMsg(&quot;This is a test mail&quot;);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.addTo(&quot;foo@bar.com&quot;);&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;email.send();&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;参考资料：&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div&gt;CentOS Sendmail Roundcube Configuration Tutorial&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://nixcraft.com/mail-servers/14855-centos-sendmail-roundcube-configuration-tutorial.html&quot;&gt;http://nixcraft.com/mail-servers/14855-centos-sendmail-roundcube-configuration-tutorial.html&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Apache Commons Email&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://commons.apache.org/email/&quot;&gt;http://commons.apache.org/email/&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/187140285179832538/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/03/sendmail-centos-55.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/187140285179832538'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/187140285179832538'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/03/sendmail-centos-55.html' title='使用 Sendmail 在 CentOS 5.5 架设邮件服务器'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-4677388804113052452</id><published>2011-03-30T23:48:00.000-07:00</published><updated>2016-09-26T04:30:08.637-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="GoDaddy"/><category scheme="http://www.blogger.com/atom/ns#" term="HTTPS"/><category scheme="http://www.blogger.com/atom/ns#" term="SSL"/><category scheme="http://www.blogger.com/atom/ns#" term="Tomcat"/><title type='text'>为 Tomcat 增加 GoDaddy SSL 证书</title><content type='html'>为了信息安全，一般网站的用户注册和登录过程会使用加密的 HTTP（即HTTPS）传输用户的帐号和密码，这样可以防止“中间人”盗取帐号资料。&lt;br /&gt;
&lt;br /&gt;
假如你的网站是用 Tomcat 直接搭建的，那么主要需要如下几个步骤：&lt;br /&gt;
1、购买 SSL 证书。&lt;br /&gt;
2、在你的服务器上生成一个私密钥匙（Private Key）。&lt;br /&gt;
3、在 SSL 证书销售商那里签名你的 Key。&lt;br /&gt;
4、将最终生成的认证证书导入 Tomcat KeyStore&lt;br /&gt;
5、修改 Tomcat 的配置。&lt;br /&gt;
&lt;br /&gt;
下面详细讲解。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1、购买 SSL 证书&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;为网站添加 HTTPS 功能第一步需要购买一个SSL认证证书，目前使用比较广泛的有：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;a href=&quot;http://www.verisign.com/&quot;&gt;VeriSign&lt;/a&gt;&amp;nbsp;典型客户有 Microsoft Hotmail&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;a href=&quot;http://www.thawte.com/&quot;&gt;Thawte&lt;/a&gt;&amp;nbsp;典型客户有 Google Gmail&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;不过这些品牌的证书价格不菲，假如你的网站只是想提供加密传输的话，可以购买&amp;nbsp;&lt;a href=&quot;http://www.godaddy.com/ssl/ssl-certificates.aspx?isc=IAPtssl1&quot;&gt;GoDaddy&lt;/a&gt;&amp;nbsp;的廉价SSL证书，目前报价是 $49.99/年，不过你可以通过推销链接以 $12.99/年买到，比如这个推销链接：&lt;a href=&quot;http://www.godaddy.com/ssl/ssl-certificates.aspx?isc=IAPtssl1&quot;&gt;http://www.godaddy.com/ssl/ssl-certificates.aspx?isc=IAPtssl1&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;2、生成私密钥匙&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;使用OpenSSL工具可以很容易生成私钥，不过由于我们准备给Tomcat使用，所以直接使用keytool更为方便。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;转到 Tomcat 的安装目录，运行&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$keytool -keysize 2048 -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;然后它会提示你设置一个密码，使用Tomcat常用的“changeit”即可，然后根据提示输入DN信息：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;输入keystore密码：changeit &amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;再次输入新密码: changeit&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;您的名字与姓氏是什么？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;yourdomain.com&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;您的组织单位名称是什么？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;Networking&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;您的组织名称是什么？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;yourdomain.com&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;您所在的城市或区域名称是什么？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;Shenzhen&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;您所在的州或省份名称是什么？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;Guangdong&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;该单位的两字母国家代码是什么&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[Unknown]： &amp;nbsp;CN&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;CN=floatmark.com, OU=Networking, O=Floatmark.com, L=Shenzhen, ST=Guangdong, C=CN 正确吗？&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;[否]： &amp;nbsp;y&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;输入&amp;lt;tomcat&amp;gt;的主密码&amp;nbsp;（如果和 keystore 密码相同，按回车）：&amp;lt;直接回车&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;完成后会在当前目录产生一个 tomcat.keystore 文件，然后在此基础上生成证书签名请求文件：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$ keytool -certreq -keyalg RSA -alias tomcat -file tomcat-yourdomain.csr -keystore tomcat.keystore&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;同样地，当它问起密码时，输入“changeit”即可，完成后你会得到一个 tomcat-yourdomain.csr 文件。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;3、签名你的私钥&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;登录到你购买 SSL 证书商（比如 GoDaddy），根据提示会进入要求输入 CSR &amp;nbsp;的页面，用文本编辑器打开刚才生成的 tomcat-yourdomain.csr 文件，把里面的文本内容复制粘贴到页面的表单里即可。SSL 证书商会根据你的域名发送一封验证邮件到域名注册者的Email（所以你要提前注意你的域名注册的Email地址是什么，如果是错误的话需要先修正，否则收不到验证Email）。这个过程可能需要及分钟或者几小时，收到验证Email之后点击里面的链接地址就完成私钥签名了。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4、将证书导入 Tomcat&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;登录到你购买 SSL 的证书商，下载你的网站证书文件，在 GoDaddy 里你下载会得到一个压缩包，里面放有你的网站证书文件 yourdomain.com.crt，另外还有 GoDaddy 自己的证书（gd_cross_intermediate.crt，gd_intermediate.crt，gd_bundle.crt），这些都需要导入 tomcat 的 keystore 里的。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;GoDaddy 的官方教程有些许错误，经过实验发现正确的过程如下：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;a、到这里（&lt;a href=&quot;https://certs.godaddy.com/anonymous/repository.seam&quot;&gt;https://certs.godaddy.com/anonymous/repository.seam&lt;/a&gt;）下载&amp;nbsp;valicert_class2_root.crt 文件，然后导入到 tomcat.keystore：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$ keytool -import -alias root -keystore tomcat.keystore -trustcacerts -file valicert_class2_root.crt&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;b、导入&amp;nbsp;gd_cross_intermediate.crt，gd_intermediate.crt 和你网站的证书&amp;nbsp;yourdomain.com.crt：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$ keytool -import -alias cross -keystore tomcat.keystore -trustcacerts -file gd_cross_intermediate.crt&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$ keytool -import -alias intermed -keystore tomcat.keystore -trustcacerts -file gd_intermediate.crt&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;$ keytool -import -alias tomcat -keystore tomcat.keystore -trustcacerts -file yourdomain.com.crt&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;5、修改 Tomcat 配置&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;编辑 Tomcat 的主配置文件 server.xml，在标准的 Connector 下面增加一个 HTTPS 的 Connector，如下：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;Connector port=&quot;80&quot; protocol=&quot;HTTP/1.1&quot;&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; connectionTimeout=&quot;20000&quot;&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; redirectPort=&quot;443&quot; /&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;lt;Connector port=&quot;443&quot; protocol=&quot;HTTP/1.1&quot; SSLEnabled=&quot;true&quot;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; maxThreads=&quot;150&quot; scheme=&quot;https&quot; secure=&quot;true&quot;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; clientAuth=&quot;false&quot; sslProtocol=&quot;TLS&quot;&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; keystoreFile=&quot;/PATH/TO/YOUR/TOMCAT/tomcat.keystore&quot;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; keystorePass=&quot;changeit&quot; /&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;其中第二行是新增加的，重启 Tomcat 之后，访问 https://yourdomain.com 应该能看到安全链接的效果。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;6、其他&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
假如你在 Tomcat 之前使用了 Apache Httpd、nginx 等方向代理，那么就需要把上面的第2，4，5步骤替换成相应的方法，详细的可以参照 GoDaddy 的官方文档：&lt;a href=&quot;http://community.godaddy.com/help/category/ssl-certificates/186&quot;&gt;http://community.godaddy.com/help/category/ssl-certificates/186&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;参考资料：&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Generating a Certificate Signing Request (CSR) - Tomcat 4.x/5.x/6.x&lt;br /&gt;
&lt;a href=&quot;http://community.godaddy.com/help/5276/generating-a-certificate-signing-request-csr--tomcat-4x5x6x&quot;&gt;http://community.godaddy.com/help/5276/generating-a-certificate-signing-request-csr--tomcat-4x5x6x&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Installing an SSL Certificate in Tomcat 4.x/5.x/6.x&lt;br /&gt;
&lt;a href=&quot;http://community.godaddy.com/help/article/5239&quot;&gt;http://community.godaddy.com/help/article/5239&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
A few frequently used SSL commands&lt;br /&gt;
&lt;a href=&quot;http://shib.kuleuven.be/docs/ssl_commands.shtml&quot;&gt;http://shib.kuleuven.be/docs/ssl_commands.shtml&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/4677388804113052452/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/03/tomcat-godaddy-ssl.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4677388804113052452'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4677388804113052452'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/03/tomcat-godaddy-ssl.html' title='为 Tomcat 增加 GoDaddy SSL 证书'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-8128078921264172289</id><published>2011-03-24T05:46:00.000-07:00</published><updated>2016-09-26T04:30:08.642-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Jetty"/><title type='text'>使用 Jetty 7 搭建虚拟主机</title><content type='html'>同 Tomcat 一样，Jetty 也是纯 Java 的 Web 服务器和 Servlet 容器。跟 Tomcat 不同的是 Jetty 本身模块化机制，最基本模块的只负责网络 IO 和 HTTP 协议，所以程序运行效率非常高。很多程序都会嵌入 Jetty 以提供 HTTP 服务，比如一些提供 HTTP 接口的 NoSQL、分布式文件系统和全文索引引擎等产品，还有 Google 的 App Engine （java版）也使用 Jetty 提供 Web 服务，另外 Jetty 的 Comet 特性经常用于实现实时数据通信。&lt;br /&gt;
除此以外，Jetty 也能够以 Standalone 形式运行，可以很轻巧地实现搭建多（Java web application）站点服务。&lt;br /&gt;
下面以在一台服务器（只有一个公网IP地址）上搭建 abc.com，xyz.com 作为例子讲解搭建及配置过程。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1、下载 Jetty 7&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Jetty 7 的各个部分包括：Core Web Server (HTTP &amp;amp; Websocket)，Core Servlet Container，JNDI, JMX, OSGi, JASPI modules 都是单独提供，所以为了简单起见，可以下载一个整合包：&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;JETTY_VERSION=7.0.2.v20100331&lt;/li&gt;
&lt;li&gt;wget http://download.eclipse.org/jetty/$JETTY_VERSION/dist/jetty-distribution-$JETTY_VERSION.tar.gz&lt;/li&gt;
&lt;li&gt;tar xfz jetty-distribution-$JETTY_VERSION.tar.gz&lt;/li&gt;
&lt;li&gt;cd jetty-distribution-$JETTY_VERSION&lt;/li&gt;
&lt;li&gt;java -jar start.jar&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;这样你就已经完成下载和运行 Jetty 了，试试访问 http://localhost:8080/。&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;2、Jetty 的配置&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Jetty 的主目录里有几个比较重要的目录：&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;contexts 存放各个 web app 的配置文件&lt;/li&gt;
&lt;li&gt;contexts-avaiable 存放各个暂时不使用的 web app （一般是自带的示例）的配置文件&lt;/li&gt;
&lt;li&gt;etc jetty程序本身的配置文件&lt;/li&gt;
&lt;li&gt;webapps 存放各个 web app 的具体资源文件，即 war 压缩包。&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;当使用 java -jar jetty 这样的方式启动 Jetty 时，实际上使用的是 etc/jetty.xml 这个配置文件启动，相当于 java -jar jetty etc/jetty.xml，可以指定 etc 里面的一个或多个配置文件，比如 java -jar jetty etc/jetty.xml etc/jetty-jmx.xml，一般来说用默认配置（etc/jetty.xml）就足够了。&lt;br /&gt;
可以尝试打开并阅读这个文件，里面的内容比较容易理解，主要定义了两个定时监控文件变化的目录：contexts 和 webapps，在 Jetty 运行的状态下添加、删除或修改这两个目录的内容都会导致 Jetty 重新加载相应的 web app，这个特性可以实现 web app 的热部署，需要注意的是这种热部署是针对整个 web app的，更改web app里面的资源文件并不会导致 context reload，所以这点跟 Tomcat 是有区别的。&lt;br /&gt;
&lt;br /&gt;
在生产环境中最好先清除 Jetty 发行包自带的示例，方法是把 webapps 目录里面的文件全部删除，把 contexts 里面的所有文件删除或者移动到 contexts-avaiable 里。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;3、开始部署 abc.com 和 xyz.com&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
a、先把这两个 web app 的资源文件放置在 Jetty启动者有访问权限的目录，比如&lt;br /&gt;
/var/www/abc&lt;br /&gt;
/var/www/xyz&lt;br /&gt;
b、 在 contexts 目录创建 abc.xml，内容如下：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&quot;1.0&quot; &amp;nbsp;encoding=&quot;ISO-8859-1&quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE Configure PUBLIC &quot;-//Jetty//Configure//EN&quot; &quot;http://www.eclipse.org/jetty/configure.dtd&quot;&amp;gt;&lt;br /&gt;
&amp;lt;Configure class=&quot;org.eclipse.jetty.webapp.WebAppContext&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;Set name=&quot;contextPath&quot;&amp;gt;/&amp;lt;/Set&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;Set name=&quot;resourceBase&quot;&amp;gt;/var/www/abc&amp;lt;/Set&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;Set name=&quot;virtualHosts&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Array type=&quot;String&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Item&amp;gt;abc.com&amp;lt;/Item&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Item&amp;gt;xyz.com&amp;lt;/Item&amp;gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/Array&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;/Set&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;!-- disable cookies&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;Get name=&quot;sessionHandler&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;Get name=&quot;sessionManager&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Set name=&quot;usingCookies&quot; type=&quot;boolean&quot;&amp;gt;false&amp;lt;/Set&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;/Get&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;/Get&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;--&amp;gt;&lt;br /&gt;
&amp;lt;/Configure&amp;gt;&lt;br /&gt;
c、再创建 xyz.xml，内容跟 abc.xml 几乎一样，只需把“abc”改为“xyz”就可以了。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4、测试&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
在 /etc/hosts 里加入两行&lt;br /&gt;
abc.com &amp;nbsp;127.0.0.1&lt;br /&gt;
xyz.com &amp;nbsp; &amp;nbsp;127.0.0.1&lt;br /&gt;
然后可以在浏览器里分别访问 http://abc.com 和 http://xyz.com。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;参考资料：&lt;/b&gt;&lt;br /&gt;
http://wiki.eclipse.org/Jetty/Starting/Downloads&lt;br /&gt;
http://wiki.eclipse.org/Jetty/Reference&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/8128078921264172289/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/03/jetty-7.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8128078921264172289'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8128078921264172289'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/03/jetty-7.html' title='使用 Jetty 7 搭建虚拟主机'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-7818051261599231974</id><published>2011-02-24T05:34:00.000-08:00</published><updated>2016-09-26T04:30:52.406-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="GAE"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="logging"/><category scheme="http://www.blogger.com/atom/ns#" term="slf4j"/><title type='text'>日志工具 SLF4J 的来龙去脉</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-large;&quot;&gt;J&lt;/span&gt;ava 界里有许多实现日志功能的工具，最早得到广泛使用的是 &lt;a href=&quot;http://logging.apache.org/log4j/&quot;&gt;log4j&lt;/a&gt;，许多应用程序的日志部分都交给了 log4j，不过作为组件开发者，他们希望自己的组件不要紧紧依赖某一个工具，毕竟在同一个时候还有很多其他很多日志工具，假如一个应用程序用到了两个组件，恰好两个组件使用不同的日志工具，那么应用程序就会有两份日志输出了。&lt;br /&gt;
&lt;br /&gt;
为了解决这个问题，&lt;a href=&quot;http://commons.apache.org/logging/&quot;&gt;Apache Commons Logging&lt;/a&gt;&amp;nbsp;（之前叫 Jakarta Commons Logging，JCL）粉墨登场，JCL 只提供 log 接口，具体的实现则在运行时动态寻找。这样一来组件开发者只需要针对 JCL 接口开发，而调用组件的应用程序则可以在运行时搭配自己喜好的日志实践工具。&lt;br /&gt;
&lt;br /&gt;
所以即使到现在你仍会看到很多程序应用 JCL + log4j 这种搭配，不过当程序规模越来越庞大时，JCL的动态绑定并不是总能成功，具体原因大家可以 Google 一下，这里就不再赘述了。解决方法之一就是在程序部署时静态绑定指定的日志工具，这就是&amp;nbsp;&lt;a href=&quot;http://www.slf4j.org/&quot;&gt;SLF4J&lt;/a&gt;&amp;nbsp;产生的原因。&lt;br /&gt;
&lt;br /&gt;
跟 JCL 一样，SLF4J 也是只提供 log 接口，具体的实现是在打包应用程序时所放入的&lt;a href=&quot;http://www.slf4j.org/manual.html&quot;&gt;绑定器&lt;/a&gt;（名字为 slf4j-XXX-version.jar）来决定，XXX 可以是 log4j12, jdk14, jcl, nop 等，他们实现了跟具体日志工具（比如 log4j）的绑定及代理工作。举个例子：如果一个程序希望用 log4j 日志工具，那么程序只需针对 slf4j-api 接口编程，然后在打包时再放入 slf4j-log4j12-version.jar 和 log4j.jar 就可以了。&lt;br /&gt;
&lt;br /&gt;
现在还有一个问题，假如你正在开发应用程序所调用的组件当中已经使用了 JCL 的，还有一些组建可能直接调用了 java.util.logging，这时你需要一个&lt;a href=&quot;http://www.slf4j.org/legacy.html&quot;&gt;桥接器&lt;/a&gt;（名字为 XXX-over-slf4j.jar）把他们的日志输出重定向到 SLF4J，所谓的桥接器就是一个假的日志实现工具，比如当你把 jcl-over-slf4j.jar 放到 CLASS_PATH 时，即使某个组件原本是通过 JCL 输出日志的，现在却会被 jcl-over-slf4j “骗到”SLF4J 里，然后 SLF4J 又会根据绑定器把日志交给具体的日志实现工具。过程如下&lt;br /&gt;
&lt;br /&gt;
Component&lt;br /&gt;
&amp;nbsp;&amp;nbsp; |&lt;br /&gt;
&amp;nbsp;&amp;nbsp; | log to Apache Commons Logging&lt;br /&gt;
&amp;nbsp;&amp;nbsp; V&lt;br /&gt;
&amp;nbsp;jcl-over-slf4j.jar --- (redirect) ---&amp;gt; SLF4j ---&amp;gt; slf4j-log4j12-version.jar ---&amp;gt; log4j.jar ---&amp;gt; 输出日志&lt;br /&gt;
&lt;br /&gt;
看到上面的流程图可能会发现一个有趣的问题，假如在 CLASS_PATH 里同时放置 log4j-over-slf4j.jar 和 slf4j-log4j12-version.jar 会发生什么情况呢？没错，日志会被踢来踢去，最终进入死循环。&lt;br /&gt;
&lt;br /&gt;
所以使用 SLF4J 的比较典型搭配就是把 slf4j-api、JCL 桥接器、java.util.logging（JUL）桥接器、log4j 绑定器、log4j 这5个 jar 放置在 CLASS_PATH 里。&lt;br /&gt;
&lt;br /&gt;
不过并不是所有APP容器都是使用 log4j 的，比如 &lt;a href=&quot;http://code.google.com/appengine/docs/java/runtime.html#Logging&quot;&gt;Google AppEngine&lt;/a&gt; 它使用的是 java.util.logging（JUL），这时应用 SLF4J 的搭配就变成 slf4j-api、JCL桥接器、logj4桥接器、JUL绑定器这4个 jar 放置在 WEB-INF/lib 里。&lt;br /&gt;
&lt;br /&gt;
转载本文时请保留此出处链接：&lt;a href=&quot;http://ivarptr.blogspot.com/2011/02/slf4j.html&quot;&gt;http://ivarptr.blogspot.com/2011/02/slf4j.html&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/7818051261599231974/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/02/slf4j.html#comment-form' title='1 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/7818051261599231974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/7818051261599231974'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/02/slf4j.html' title='日志工具 SLF4J 的来龙去脉'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-2191996163287458906</id><published>2011-02-23T00:32:00.000-08:00</published><updated>2016-09-26T04:30:52.390-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Git"/><title type='text'>一个人用的 Git</title><content type='html'>&lt;a href=&quot;http://git-scm.com/&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-large;&quot;&gt;G&lt;/span&gt;it&lt;/a&gt; 是目前比较时髦的版本控制系统，虽然 linux 内核源码是使用它来管理的，但系统非常小巧而且使用简单，比起 svn （&lt;a href=&quot;http://subversion.tigris.org/&quot;&gt;subversion&lt;/a&gt;） 系统，git 有一个明显的优势就是在不联网的情况下也可以查阅或者回滚历史版本，有时带着笔记本出外工作这个功能是非常有用的。下面介绍一下用 Git 管理个人电脑上的个人文档文件。(&lt;a href=&quot;http://twitter.com/ivarptr&quot;&gt;@ivarptr&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;这篇文章有几个假设前提：&lt;/span&gt;&lt;br /&gt;
a、你从事的工作有很多文档需要书写或者整理，而且文档篇幅比较大需要经常修修改改，有时还可能需要回档（即恢复到历史的某个版本）。&lt;br /&gt;
b、你的文档具有关联性的，比如一个项目策划书，有多份文本文档、电子表格、图片，希望平时能整体记录一个版本（快照）。&lt;br /&gt;
c、你有多台工作用的电脑，为了简单起见，下文假设只有两台电脑。&lt;br /&gt;
d、你有一台能够在互联网上访问的服务器，最好是跑 linux 系统而且有 ssh 登录权限。&lt;br /&gt;
e、为了简明，省略了安全权限设置和Git最为突出的特性“分支”。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;1、几个必须厘清的概念&lt;/span&gt;&lt;br /&gt;
a、svn只有一个远程仓库集中管理所有的版本，而Git分有“本地仓库”和“远程仓库”之分，在提交你的最新文档过程中，需要两层提交，一次是提交到本地仓库，然后才可以提交到远程仓库。&lt;br /&gt;
b、svn的远程仓库保存的总是全局最新的版本，各个支点（小组成员）必须对此仓库负责。而Git则没有集中远程仓库之说，理论上每个人的地位是平等的，同时每个人的本地仓库也可以配置为给他人访问的“远程仓库”。不过对于个人或者一个小团队来说，为了简便起见我们通常会协商好让某个远程服务器上的仓库保持最新并且集中管理，这点跟Svn类似。&lt;br /&gt;
c、svn的每次提交会产生一个递增的数字作为版本号码，而git每次提交会产生一个“快照”，快照不是用一个递增的数字作为名称的，而是一个hash值，当然用一个hash值来呼叫某一次提交也太累赘了，所以在实际应用过程中我们往往只用hash指的头4~6位就可以了（类似一个人的昵称）。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;2、在服务器上创建 Git 仓库&lt;/span&gt;&lt;br /&gt;
关于在 linux 系统里如何安装 Git 程序可以参考这篇《&lt;a href=&quot;http://ivarptr.blogspot.com/2010/12/centos-55gitgit.html&quot;&gt;在CentOS 5.5里安装Git以及搭建Git服务器最简单教程&lt;/a&gt;》，要记住&amp;nbsp;Git 程序是不区分服务端或客户端的。&lt;br /&gt;
&lt;br /&gt;
首先我们要创建一个Git仓库，使用 ssh 登录到你的服务器，在用户home文件夹里创建一个文件夹，为了容易识别，最好在文件夹名字后面加一个“.git”后缀，比如“homework.git”，然后进入这个文件夹并且执行命令&lt;br /&gt;
git --bare init&lt;br /&gt;
&lt;br /&gt;
那么仓库就建立好了。远程访问这个仓库的地址是：&lt;br /&gt;
your_account_name@your_host_name:homework.git&lt;br /&gt;
&lt;br /&gt;
假如你在用户home文件夹子层里创建仓库，那么就把上面的地址“homework.git”部分替换成完整的路径，比如“mydocuments/projects/homework.git”，当然也可以使用绝对路径。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;3、在第一台电脑上创建本地仓库&lt;/span&gt;&lt;br /&gt;
首先在待版本控制的文件夹里（可以是刚刚新建的）执行下面命令创建本地仓库：&lt;br /&gt;
git init&lt;br /&gt;
&lt;br /&gt;
然后将需要版本控制的文件和文件夹创建（或者从别处copy过来）并执行下面的命令将之逐一添加到本地仓库：&lt;br /&gt;
git add file1.txt&lt;br /&gt;
git add dir1&lt;br /&gt;
......&lt;br /&gt;
&lt;br /&gt;
如果希望文件夹里所有的文件都加入本地仓库，那么只需简单执行：&lt;br /&gt;
git add .&lt;br /&gt;
&lt;br /&gt;
需要注意的是，git add命令仅仅是将文件标识为“受版本控制”，并没有真正进入git仓库。在添加过程中可以使用 git status 命令查看哪些文件已经添加了，哪些还没有。&lt;br /&gt;
&lt;br /&gt;
有时可能希望排除一些文件，可以把希望排除的文件或文件夹名字书写到文件夹根目录的 .gitignore 文件，没有这个文件的话则新建即可。内容如下：&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599;&quot;&gt;bin/&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599;&quot;&gt;.notes&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599;&quot;&gt;/temp/&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
上面三行分别表示排除所有文件夹里面名字为 bin 的文件夹，排除所有文件夹里面名字为 .notes 的文件，排除首层文件夹里面名字为 temp 的文件夹。&lt;br /&gt;
当然别忘了把这个文件也添加到本地仓库：&lt;br /&gt;
git add .gitignore&lt;br /&gt;
&lt;br /&gt;
所有文件和文件夹添加完毕之后，就可以提交了（要注意这次提交仅仅提交到本地仓库，接下来才是提交到服务器），提交之后就会形成一个快照：&lt;br /&gt;
git commit&lt;br /&gt;
&lt;br /&gt;
上面的命令执行的时候你需要输入提交备忘文字，跟 svn 不同，git 每次提交的备忘文字是必须填写的。&lt;br /&gt;
&lt;br /&gt;
你可以使用命令 git log 来查看这次提交的hash值。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;4、把第一台电脑上的本地仓库提交（上传）到服务器&lt;/span&gt;&lt;br /&gt;
在受版本控制的文件夹里执行下面命令添加一个远程仓库&lt;br /&gt;
git remote add origin&amp;nbsp;your_account_name@your_host_name:homework.git&lt;br /&gt;
&lt;br /&gt;
这里的 origin 表示远程仓库的名字，默认的主要的远程仓库我们一般命名为 origin。然后就可以把本地仓库提交到远程仓库了：&lt;br /&gt;
git push&lt;br /&gt;
&lt;br /&gt;
不过先等等，由于服务器上的版本库是刚刚创建的，也就是说里面是空的，所以第一次提交需要指定一个远程仓库分支名称，默认主分支名称为 master，所以上面的命令要改为：&lt;br /&gt;
git push origin master&lt;br /&gt;
&lt;br /&gt;
而以后的第二次第N次提交就可以省略“master”这个分支名称了。“分支”是Git的重要特性，建议读者阅读文章末尾的书籍，而此文为了简便则省略了。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;5、在第二台电脑上创建本地仓库并拉取（下载）远程仓库&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;转到待版本控制的文件夹里（可以是刚刚新建的）执行下面命令创建本地仓库：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git init&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;添加远程仓库：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git remote add origin&amp;nbsp;your_account_name@your_host_name:homework.git&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;拉取远程仓库的最新版本下来&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git fetch origin master&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;拉取下来之后会发现本地文件夹里面仍然没有任何改变（假如文件夹是新建的，则里面是空荡荡的），那是因为本地仓库仍然停留在旧时状态，只要把远程最新的提交合并到本地仓库就好了：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git merge origin/master&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;6、第一台电脑上有文档发生更改，或者有新文档需要增加&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;对于新增加的文档，需要用 git add 命令标识它受版本控制，否则使用 git commit 命令提交时是不会自动包含新增加文件的。比如新增加 readme.txt：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git add readme.txt&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;对于修改过的文档，同样需要用 git add 命令标识它需要重新提交到本地仓库，否则提交时不会自动提交发生更改的文档。比如 file1.txt 发生了更改：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git add file1.txt&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;然后提交到本地仓库&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git commit&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
如果没有新文档需要增加，仅仅是提交发生过更改的文档，则上面的命令可以简化为：&lt;br /&gt;
git commit -a&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;合并到远程分支&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git merge origin/master&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;提交到远程仓库&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git push （默认提交到origin/master分支，此命令相等于 git push origin master）&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;7、第二台电脑也有文档发生更改&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;通常我们在提交新的更改之前，最好先拉取一下远程仓库，以保证你得到最新的版本，你的工作是在最新版本的基础之上开始进行的，所以上面的第6步其实缺少了下面的两个操作。&lt;br /&gt;
git fetch （默认拉取origin/master分支，此命令相等于 git fetch origin master）&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;把远程分支合并到本地分支：&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git merge origin/master&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;对于修改过和新增的文件需要先使用 git add 和 git commit 提交到本地仓库，然后合并到远程分支，再提交：&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git merge origin/master&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git push&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;日常操作流程&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;往后第一台和第二台电脑的操作都跟上面的第7步一样了，实际上日常的操作大致如下：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;a、（工作开始之前，或者上班之后第一件事）拉取远程仓库最新的提交，合并到本地分支。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;b、工作中途时不时提交到本地仓库，以制造更多的快照以便日后能更细粒度地回档。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;c、一个阶段完毕之后（或者下班之前），把本地分支合并到远程分支，并提交到远程仓库。&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;补充1、简化第二台电脑的初始化工作&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;即上面的第5步，可以用下面命令一步完成：&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;git clone&amp;nbsp;your_account_name@your_host_name:homework.git&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;补充2、一些常用的命令&lt;/span&gt;&lt;/div&gt;&lt;div&gt;a、修正最后一次提交，比如刚刚提交完毕之后发现漏掉添加一个文件了，或者备注文字写错了：&lt;/div&gt;&lt;div&gt;&lt;div&gt;先把该添加的文件添加上（git add），然后：&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;git commit --amend&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;b、取消不小心添加进入暂存的文件&lt;/div&gt;&lt;div&gt;git status （先查看是否被暂存了）&lt;/div&gt;&lt;div&gt;git reset HEAD some.file&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;c、还原文件的修改&lt;/div&gt;&lt;div&gt;git checkout -- some.file （两个减号）&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;d、恢复到某次快照（即回档）&lt;/div&gt;&lt;div&gt;git log （查看所有修改记录，记住快照的hash值，一般记住hash值前头4到6位就足够了）&lt;/div&gt;&lt;div&gt;git checkout HASH （把HASH替换成相应的hash值）&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;e、恢复到最新版本&lt;/div&gt;&lt;div&gt;git checkout master&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
f、已经修改了一些文件，希望创建多一个分支来处理这些修改（这点已经有少许超纲了，不过因为很常用所以在此列出来）&lt;br /&gt;
先用 git status 看看是否真的已经修改过文件了，确定之后再继续。&lt;br /&gt;
git stash （创建临时储存堆栈）&lt;br /&gt;
现在用 git status 查看应该是干净了。&lt;br /&gt;
git branch NAME（创建一个新分支，名字为 NAME）&lt;br /&gt;
git apply （调出临时储存堆栈）&lt;br /&gt;
git commit -a （提交到新的分支）&lt;br /&gt;
&lt;br /&gt;
对于临时储存堆栈，还有 git stash list, git stash drop 这两个常用命令，看名字就知道他们的作用了。&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;补充3、Git 的学习资料&lt;/span&gt;&lt;/div&gt;&lt;div&gt;《Pro Git》可以在这里免费获取：&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://progit.org/&quot;&gt;http://progit.org/&lt;/a&gt;&lt;/div&gt;&lt;div&gt;还有中文版&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://progit.org/2010/06/09/pro-git-zh.html&quot;&gt;http://progit.org/2010/06/09/pro-git-zh.html&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/2191996163287458906/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/02/git.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2191996163287458906'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2191996163287458906'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/02/git.html' title='一个人用的 Git'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-2318785425389271419</id><published>2011-01-19T00:24:00.000-08:00</published><updated>2016-09-26T04:30:52.395-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="FreeBSD"/><title type='text'>在家用PC里体验 FreeBSD 8.1（下）</title><content type='html'>在&lt;a href=&quot;http://ivarptr.blogspot.com/2011/01/pc-freebsd-81.html&quot;&gt;上一章&lt;/a&gt;讲到如何配置 gnome 桌面环境以及 firefox 浏览器，很快你会发现上网时不能播放 flash，这是因为 Adobe Flash 没有提供 FreeBSD 版本，为了运行这类没有开源只有编译好的 Linux 版程序，我们需要安装一个 Linux 二进制兼容包。[注1]&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;1、安装 Linux 二进制兼容包&lt;/span&gt;&lt;br /&gt;
为了确保安装顺利，最好把 FreeBSD Ports 更新一下。[注2]&lt;br /&gt;
# portsnap fetch&lt;br /&gt;
&lt;br /&gt;
第一次运行这个命令会下载最新的 Ports Collection，大概半小时左右下载完，然后展开安装文件：&lt;br /&gt;
# portsnap extract&lt;br /&gt;
&lt;br /&gt;
使用 portsnap update 命令更新已安装的 Ports：&lt;br /&gt;
# portsnap update&lt;br /&gt;
&lt;br /&gt;
这样更新就完成了。下面开始安装 Linux 二进制兼容包，[注3]先开启 Linux KLD 模块：&lt;br /&gt;
#kldload linux&lt;br /&gt;
&lt;br /&gt;
如果希望 Linux 兼容支持在系统初始化过程中自动启用，则应在 /etc/rc.conf 中增加：&lt;br /&gt;
linux_enable=&quot;YES&quot;&lt;br /&gt;
&lt;br /&gt;
可以使用 kldstat 命令检查KLD模块是否已经加载成功（以百分号开头的命令表示可以用普通帐号操作，下同）：&lt;br /&gt;
% kldstat&lt;br /&gt;
&lt;br /&gt;
如果看到 linux.ko 字样则表示加载成功。下面还要安装 Linux 运行时库，&lt;br /&gt;
&lt;br /&gt;
# cd /usr/ports/emulators/linux_base-f10&lt;br /&gt;
# make install distclean&lt;br /&gt;
&lt;br /&gt;
这又是一个漫长的等待过程，运行完毕之后还要挂载 Linux 进程文件系统，使用如下命令：&lt;br /&gt;
# mount -t linprocfs linproc /usr/compat/linux/proc&lt;br /&gt;
&lt;br /&gt;
如果希望机器启动时自动挂载，需要在文件 /etc/fstab 里面加上这一行&lt;br /&gt;
linproc &amp;nbsp; &amp;nbsp;/usr/compat/linux/proc &amp;nbsp;linprocfs &amp;nbsp; rw &amp;nbsp;0 &amp;nbsp; 0&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;2、安装浏览器 Flash 插件 &lt;/span&gt;[注4]&lt;br /&gt;
我们开始下载安装 Flash 的 Linux 版插件：&lt;br /&gt;
# /usr/ports/www/linux-f10-flashplugin10&lt;br /&gt;
# make install clean&lt;br /&gt;
&lt;br /&gt;
安装 wrapper：&lt;br /&gt;
# /usr/ports/www/nspluginwrapper&lt;br /&gt;
# make install clean&lt;br /&gt;
&lt;br /&gt;
还要创建一个符号链接&lt;br /&gt;
# ln -s /usr/local/lib/npapi/linux-f10-flashplugin/libflashplayer.so \&lt;br /&gt;
&amp;nbsp;/usr/local/lib/browser_plugins/&lt;br /&gt;
&lt;br /&gt;
按照 FreeBSD 版本， 在安装了正确的 Flash port 之后， 插件必须由每个用户运行 nspluginwrapper 安装：&lt;br /&gt;
% nspluginwrapper -v -a -i&lt;br /&gt;
&lt;br /&gt;
现在重启浏览器并转到有 flash 的网站，终于看到 Flash 了。帖一幅效果图：&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX4nr22UzJippukauHP5FtLnI9Dpj7yF2oszE4sZwaQ-0kDx-ih2lT3HCVj_6JTtPRE-RKChydLqhQJw1eXgv4XTTPUt3TMvIYK6v_Q0aAyaaepIFZeReK6ArnvLvMigmIK8fGHA4PeI7B/s1600/freebsd-flash+Screenshot.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;250&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX4nr22UzJippukauHP5FtLnI9Dpj7yF2oszE4sZwaQ-0kDx-ih2lT3HCVj_6JTtPRE-RKChydLqhQJw1eXgv4XTTPUt3TMvIYK6v_Q0aAyaaepIFZeReK6ArnvLvMigmIK8fGHA4PeI7B/s400/freebsd-flash+Screenshot.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;3、美化一下桌面&lt;/span&gt;&lt;br /&gt;
使用过 Ubuntu 的用户会发现 FreeBSD 的 gnome 界面比较简朴，比较明显的是窗口和菜单缺少阴影，解决方法其实很简单，只要在 gnome 里面激活 compositing_manager 就可以了，执行下面的命令：&lt;br /&gt;
% gconftool-2 -s --type bool /apps/metacity/general/compositing_manager true&lt;br /&gt;
&lt;br /&gt;
阴影就出来了，如果不用命令的话，打开 gconf-editor 修改 /apps/metacity/general/compositing_manager 一项的值也是可以的。看一幅效果图吧：&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3JRHtBIObfkkOwzZLM0_I6SUbmHmDiN2DiPGSEi0KJT41qkjyMuU-U3esR1x_64_3KFpvfrLr-Ss3cY5wdbSOi77ycTN_AFxgit00SSOa7bTSuIrNSRddyPiu6rXobIjXvh44toeSrxtZ/s1600/freebsd+shadow+Screenshot.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;250&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3JRHtBIObfkkOwzZLM0_I6SUbmHmDiN2DiPGSEi0KJT41qkjyMuU-U3esR1x_64_3KFpvfrLr-Ss3cY5wdbSOi77ycTN_AFxgit00SSOa7bTSuIrNSRddyPiu6rXobIjXvh44toeSrxtZ/s400/freebsd+shadow+Screenshot.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;资料来源&lt;/span&gt;&lt;br /&gt;
[1] &lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/linuxemu.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/linuxemu.html&lt;/a&gt;&lt;br /&gt;
[2] &lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/updating-upgrading-portsnap.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/updating-upgrading-portsnap.html&lt;/a&gt;&lt;br /&gt;
[3] &lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/linuxemu-lbc-install.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/linuxemu-lbc-install.html&lt;/a&gt;&lt;br /&gt;
[4] &lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/desktop-browsers.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/desktop-browsers.html&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/2318785425389271419/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/01/pc-freebsd-81_19.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2318785425389271419'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2318785425389271419'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/01/pc-freebsd-81_19.html' title='在家用PC里体验 FreeBSD 8.1（下）'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiX4nr22UzJippukauHP5FtLnI9Dpj7yF2oszE4sZwaQ-0kDx-ih2lT3HCVj_6JTtPRE-RKChydLqhQJw1eXgv4XTTPUt3TMvIYK6v_Q0aAyaaepIFZeReK6ArnvLvMigmIK8fGHA4PeI7B/s72-c/freebsd-flash+Screenshot.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-2221700940564902251</id><published>2011-01-18T05:59:00.000-08:00</published><updated>2016-09-26T04:30:52.392-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="FreeBSD"/><title type='text'>在家用PC里体验 FreeBSD 8.1（上）</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-large;&quot;&gt;F&lt;/span&gt;reeBSD （&lt;a href=&quot;http://www.freebsd.org/&quot;&gt;http://www.freebsd.org/&lt;/a&gt;） 是一个开源免费的操作系统，最早接触FreeBSD是约7年前在 &lt;a href=&quot;http://twitter.com/hplcsz&quot;&gt;@hplcsz&lt;/a&gt; 挂bbs的服务器，后来很少关注了，最近接触到 Apple 的 MacOS 和 iOS （基于FreeBSD衍生出来的操作系统）联想起FreeBSD项目，才发现它已经更新到8.1版了。&lt;br /&gt;
恰好家里多出一台电脑暂时没有用，于是尝试用FreeBSD 8.1配置一台适合家用的电脑（用来看看新闻灌灌水的那种），于是有了如下一篇文章。&lt;br /&gt;
（注：这里假设读者已经有一定的Linux/BSD基础）&lt;br /&gt;
&lt;br /&gt;
首先访问 FreeBSD 项目的官方网站，下载 FreeBSD 8.1的安装镜像文件（&lt;a href=&quot;ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/8.1/&quot;&gt;ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/8.1/&lt;/a&gt;），这里提供CD版、DVD版和U盘版，考虑到我有DVD刻录机，所以下载了DVD版。&lt;br /&gt;
&lt;br /&gt;
下载过程是一个漫长的等待，期间强烈推荐阅读一下官方的《FreeBSD使用手册》（中文版，以下简称《使用手册》）（&lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/index.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/index.html&lt;/a&gt;），如果对Linux/BSD不是很熟悉，前3章是很有必要阅读的，否则会被折腾到2012年。等待DVD镜像下载完，大概已经看到第10章了。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;1、安装 FreeBSD 8.1&lt;/span&gt;&lt;br /&gt;
刻录下载回来的DVD镜像文件，从光驱启动然后选择“Quick模式”即开始安装过程。结合《使用手册》一般没有多大问题，因为这篇文章重点是如何搭建家用PC的环境，所以具体的安装过程这里就省略了。小技巧：&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;A、&lt;/span&gt;FreeBSD 必须安装在主分区，在安装之前你必须腾出一个至少10来GB的主分区。&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;B、&lt;/span&gt;当安装问你是否安装“Ports（相当于软件商店、软件中心）”时，要选择“YES”。&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;C、&lt;/span&gt;当安装接近完成时，程序会问你 “Would you like to browse the collection now?“，这一步是问你是否安装DVD上附带的软件，选择Yes，然后把下面这两个大块头选中，这样能避免从网络下载桌面环境的安装文件，节省时间和网络流量，减低碳排放：&lt;br /&gt;
+ X11/xorg-7.5&lt;br /&gt;
+ gnome/gnome2-2.30.1-1&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;2、基础网络设置&lt;/span&gt;&lt;br /&gt;
系统安装好之后，重启就会进入命令行界面，使用 root 帐号登录。注：为了简化起见，以下所有带井号开头的命令都是用 root 帐号操作的，实际应用时最好创建一个普通帐号并以普通帐号使用sudo方式执行。&lt;br /&gt;
&lt;br /&gt;
FreeBSD默认使用的Shell是sh，因为我更习惯 Bash Shell，所以第一步先把 Shell 改为 Bash，&lt;br /&gt;
# chsh -s /usr/local/bin/bash&lt;br /&gt;
&lt;br /&gt;
#exit 退出后重新登录，熟悉的 Bash 迎面而来 :)&lt;br /&gt;
&lt;br /&gt;
下面开始配置网卡，先用 ifconfig 命令查看你的网卡名称，我这台电脑的网卡名是 msk0，分配一个IP给它以及设置默认路由：&lt;br /&gt;
# ifconfig msk0 192.168.0.123/24&lt;br /&gt;
# route add default 192.168.0.1&lt;br /&gt;
测试能不能PING通家用的那台路由器：&lt;br /&gt;
# ping 192.168.0.1&lt;br /&gt;
&lt;br /&gt;
如果能PING通，则可以在 /etc/rc.conf 文件里添加如下文字，让每次开机自动配置网卡：&lt;br /&gt;
ifconfig_msk0=&quot;inet 192.168.0.123 netmask 255.255.255.0&quot;&lt;br /&gt;
defaultrouter=&quot;192.168.0.1&quot;&lt;br /&gt;
hostname=&quot;yangbsd.home&quot;&lt;br /&gt;
&lt;br /&gt;
如果你的家用路由器开启了DHCP功能，还可以把上面文字的头两行用这样一行替代：&lt;br /&gt;
ifconfig_msk0=&quot;DHCP&quot;&lt;br /&gt;
&lt;br /&gt;
因为上面的第三行修改了本机的hostname，所以需要编辑文件 /etc/hosts 把 my.domain 替换为 yangbsd.home，并且加上以下两行：&lt;br /&gt;
::1 yangbsd.home&lt;br /&gt;
127.0.0.1 yangbsd.home&lt;br /&gt;
&lt;br /&gt;
然后设置域名解析，新建文件 /etc/resolv.conf，添加如下文字：&lt;br /&gt;
nameserver 208.67.222.222&lt;br /&gt;
nameserver 8.8.8.8&lt;br /&gt;
nameserver 8.8.4.4&lt;br /&gt;
&lt;br /&gt;
看能不能PING通互联网上的网站&lt;br /&gt;
# ping www.google.com&lt;br /&gt;
&lt;br /&gt;
ps：如果 google.com 不能PING通的话，最好换另一个互联网上“存在”的网站试试，因为 google.com 有时会在国内消失。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;3、配置 Gnome 桌面环境&lt;/span&gt;&lt;br /&gt;
作为家用电脑，没有桌面环境是不行的，这里我选择个人比较喜欢的 Gnome，先创建一个 ~/.xinitrc 文件：&lt;br /&gt;
# echo &quot;gnome-session&quot; &amp;gt; ~/.xinitrc&lt;br /&gt;
&lt;br /&gt;
然后使用 startx 命令启动 Xorg 以及 Gnome：&lt;br /&gt;
# startx&lt;br /&gt;
&lt;br /&gt;
如果桌面环境能进入但鼠标指针动不了，需要在 /etc/rc.conf 加入如下两行：&lt;br /&gt;
dbus_enable=&quot;YES&quot;&lt;br /&gt;
hald_enable=&quot;YES&quot;&lt;br /&gt;
&lt;br /&gt;
重启电脑再次进入 Gnome，熟悉的 Gnome 迎面扑来 :)&lt;br /&gt;
Gnome 自带的那个浏览器功能有点简单，下面安装个 firefox ：&lt;br /&gt;
# pkg_add -r firefox&lt;br /&gt;
&lt;br /&gt;
好了，至此一个能上网浏览网页的电脑已经配置好了。不过还不能上网灌水，是不是缺少了中文输入法？&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;4、中文输入法及中文字体设置&lt;/span&gt;&lt;br /&gt;
安装输入法引擎 ibus：&lt;br /&gt;
# pkg_add -r ibus&lt;br /&gt;
&lt;br /&gt;
安装完 ibus 之后会提示添加环境变量，下面是针对 Bash Shell 的，在 ~/.bashrc 文件里加入如下文字&lt;br /&gt;
&lt;br /&gt;
export XIM=ibus&lt;br /&gt;
export GTK_IM_MODULE=ibus&lt;br /&gt;
export QT_IM_MODULE=xim&lt;br /&gt;
export XMODIFIERS=@im=ibus&lt;br /&gt;
export XIM_PROGRAM=&quot;ibus-daemon&quot;&lt;br /&gt;
export XIM_ARGS=&quot;--daemonize --xim&quot;&lt;br /&gt;
&lt;br /&gt;
然后安装拼音输入法 ibus-pinyin：&lt;br /&gt;
# pkg_add -r zh-ibus-pinyin&lt;br /&gt;
&lt;br /&gt;
安装完拼音输入法之后在 ~/.profile 文件里加入如下一行文字，否则iBus会一直显示 No input window：&lt;br /&gt;
export LC_CTYPE=zh_CN.UTF-8&lt;br /&gt;
&lt;br /&gt;
重启 Gnome，现在中文输入法有了，不过。。。是不是觉得中文显示特别别扭？下面添加一个名为“文泉驿”免费字体，这样中文就变靓靓了。&lt;br /&gt;
# pkg_add -r wqy-fonts&lt;br /&gt;
&lt;br /&gt;
下载了字体之后并不能立即生效，还需要配置 Xorg，先生成一个配置文件：&lt;br /&gt;
# Xorg -configure&lt;br /&gt;
&lt;br /&gt;
执行之后会在 $HOME 目录里产生一个 xorg.conf.new 文件，编辑这个文件，在 Section &quot;Module&quot; 一节最后一行插入：&lt;br /&gt;
Load &quot;freetype&quot;&lt;br /&gt;
&lt;br /&gt;
在 Section &quot;Files&quot; 一节最后一行插入：&lt;br /&gt;
FontPath &quot;/usr/local/lib/X11/fonts/wqy/&quot;&lt;br /&gt;
&lt;br /&gt;
然后把这个配置文件复制到 /etc/X11&lt;br /&gt;
# cp xorg.conf.new /etc/X11/xorg.conf&lt;br /&gt;
&lt;br /&gt;
再次重启 Gnome，靓靓中文扑面而来。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;5、安装声卡驱动&lt;/span&gt;&lt;br /&gt;
作为家用电脑，没有音乐是不行的 :)，先探测一下自己电脑的声卡：&lt;br /&gt;
# kldload snd_driver&lt;br /&gt;
&lt;br /&gt;
执行之后查看一下 /dev/sndstat 文件的内容：&lt;br /&gt;
# cat /dev/sndstat&lt;br /&gt;
&lt;br /&gt;
我的电脑结果如下：&lt;br /&gt;
FreeBSD Audio Driver (newpcm: 32bit 2009061500/i386)&lt;br /&gt;
Installed devices:&lt;br /&gt;
pcm0: &amp;lt;HDA Realtek ALC662 PCM #0 Analog&amp;gt; (play/rec) default&lt;br /&gt;
pcm1: &amp;lt;HDA Realtek ALC662 PCM #1 Analog&amp;gt; (play/rec)&lt;br /&gt;
&lt;br /&gt;
这里大致知道 FreeBSD 能驱动你的声卡以及你的声卡型号，根据文件 /boot/defaults/loader.conf 罗列的声卡找到相应驱动的名称（只要某些字眼配对就足够了），我的声卡有 HDA 字样，所以估计对应的应该是 snd_hda_load=&quot;NO&quot; 这行。&lt;br /&gt;
&lt;br /&gt;
把 snd_hda_load=&quot;NO&quot; 这一行文字复制到 /boot/loader.conf 里面，并且把“NO”改为&quot;YES&quot;，重启电脑，播放一首mp3听听。&lt;br /&gt;
&lt;br /&gt;
帖一幅播放音乐的截图：&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW_kKS_ArGvD7iHqVqKwnecj9uTNSZGAn8VNUaVIxXNp8P5rP3XY5hhM3Mt5WjORZF1BE1kQ3M1hjuvA0U5TWQmhFcOlIuAMDV4J-Obv5np2cN__5jym4wwikVmZA_jSt_u_SEXITjlFu0/s1600/FreeBSD+Screenshot+2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW_kKS_ArGvD7iHqVqKwnecj9uTNSZGAn8VNUaVIxXNp8P5rP3XY5hhM3Mt5WjORZF1BE1kQ3M1hjuvA0U5TWQmhFcOlIuAMDV4J-Obv5np2cN__5jym4wwikVmZA_jSt_u_SEXITjlFu0/s400/FreeBSD+Screenshot+2.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
至此一台基本能用于上网看看新闻灌灌水的 FreeBSD 配置完成了，如果中途遇到什么问题，最好看一下《使用手册》。下面讲一下小技巧。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;想要完整体验一下全新操作系统，就不应该把它放在虚拟机里运行。&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
解决 FreeBSD 跟其他操作系统共存的方法&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;1、跟 Windows 7 共存&lt;/span&gt;&lt;br /&gt;
先装 Windows 7，再装 FreeBSD。安装 FreeBSD 过程中选择安装 BootManager 引导程序即可。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;2、跟 Ubuntu（Linux） 10.x 共存&lt;/span&gt;&lt;br /&gt;
Ubuntu 10.x 使用 Grub2 引导，所以在安装 FreeBSD 过程中不需要安装任何引导程序，安装完 FreeBSD 之后，重启进入 Ubuntu，&lt;br /&gt;
修改 /etc/grub.d/40_custom 文件，增加如下文字&lt;br /&gt;
&lt;br /&gt;
menuentry &quot;FreeBSD&quot; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp; set root=(hd0,2)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; chainloader +1&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
这里需要注意的是要确认你的 FreeBSD 安装在哪个主分区，如果是在第二个主分区，那么上面一段的第二行应该写 (hd0,1)，如果是第三个主分区则应该写 (hd0,2)。执行 sudo update-grub2 更新 grub 引导配置文件，重启完成。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;FreeBSD 8.1 跟 Ubuntu 10.x 的主观比较&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
1、Ubuntu 的安装比较简单，几乎不用费劲，相对来说 FreeBSD 显得有点困难。&lt;br /&gt;
2、FreeBSD 的系统资源占用比较少，运行程序感觉身轻如燕，Firefox 3貌似能跑出 Chrome的速度（这点基于目测以及惨杂了大量个人感情 :)）。&lt;br /&gt;
&lt;br /&gt;
帖一幅截图：&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggDF5cjqJdqBNdGoYn4w7sR2ZzyRcFtBJsvcZNNnegRsnm20niIjd-mJ-FDDa3a2EpehAGfEKyAeoW8EFfzEPfe0hqZIaCrdv5XLxMngGxZNoWjXGESra58ZiMypD1eIznN6ldvGwGoSkO/s1600/FreeBSD+Screenshot+1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggDF5cjqJdqBNdGoYn4w7sR2ZzyRcFtBJsvcZNNnegRsnm20niIjd-mJ-FDDa3a2EpehAGfEKyAeoW8EFfzEPfe0hqZIaCrdv5XLxMngGxZNoWjXGESra58ZiMypD1eIznN6ldvGwGoSkO/s400/FreeBSD+Screenshot+1.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;由于开始接触 FreeBSD不久，如果文章有错误的地方欢迎批评指正，转载本文请保留原文链接地址：&lt;a href=&quot;http://ivarptr.blogspot.com/2011/01/pc-freebsd-81.html&quot;&gt;http://ivarptr.blogspot.com/2011/01/pc-freebsd-81.html&lt;/a&gt;，谢谢！&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;http://ivarptr.blogspot.com/2011/01/pc-freebsd-81_19.html&quot;&gt;下一章&lt;/a&gt;将会讲在FreeBSD里安装 flash 。&lt;br /&gt;
&lt;br /&gt;
参考资料：&lt;br /&gt;
[1]《FreeBSD使用手册》&lt;a href=&quot;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/index.html&quot;&gt;http://www.freebsd.org/doc/zh_CN.GB2312/books/handbook/index.html&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/2221700940564902251/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/01/pc-freebsd-81.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2221700940564902251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/2221700940564902251'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/01/pc-freebsd-81.html' title='在家用PC里体验 FreeBSD 8.1（上）'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW_kKS_ArGvD7iHqVqKwnecj9uTNSZGAn8VNUaVIxXNp8P5rP3XY5hhM3Mt5WjORZF1BE1kQ3M1hjuvA0U5TWQmhFcOlIuAMDV4J-Obv5np2cN__5jym4wwikVmZA_jSt_u_SEXITjlFu0/s72-c/FreeBSD+Screenshot+2.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-4567034504782226236</id><published>2011-01-12T06:48:00.000-08:00</published><updated>2016-09-26T04:30:52.397-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="ssh"/><category scheme="http://www.blogger.com/atom/ns#" term="sshd"/><category scheme="http://www.blogger.com/atom/ns#" term="安全"/><title type='text'>修改CentOS Linux 的 SSH服务默认设置，提高系统安全性</title><content type='html'>最近登录一台CentOS Linux(5.5)服务器发现&amp;nbsp;/var/log/secure 文件出奇巨大，查看其中的记录发现原来不断有人试图穷举服务器的帐号密码，幸好当时 root 密码设置非常复杂，否则可能早就被人攻陷了。（ps：那台服务器平时只做对内的服务，从没在公网上暴露过，看来即使这样的服务器也不能掉以轻心啊）接着使用 netstat -nat 查看网络连接发现扫描器仍在乐此不彼地工作，所以决定还是更改 ssh 服务的默认设置。&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;步骤如下&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;1、禁用 root 的 ssh 远程登录&lt;/b&gt;&lt;br /&gt;
这步我觉得很有必要，因为扫描器一般都是冲着这个帐号过来的。修改 /etc/ssh/sshd_config 文件里的&amp;nbsp;PermitRootLogin 一行的值为 no 即可。不过要注意在禁用 root 的 ssh 登录之前一定要确定服务器内还有其他帐号可以远程登录，否则你的服务器自己也别想进去了。所以比较妥当的方法是建立一个普通的帐号，并把它加入到 wheel 组，&lt;br /&gt;
&lt;u&gt;&lt;/u&gt;然后在 /etc/sudoers 里启用 wheel 组的 sudo 功能，再禁用 root 的远程登录。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2、设置登录密码输入的等待时间和最大重试次数&lt;/b&gt;&lt;br /&gt;
还是修改&amp;nbsp;&amp;nbsp;/etc/ssh/sshd_config 文件，找到&amp;nbsp;LoginGraceTime 和&amp;nbsp;MaxAuthTries 两行，分别设置为 30 和 3，则表示打开ssh连接之后你要在30秒内输入正确的密码否则断开连接，以及密码最多给重试 3 次。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;3、重启 sshd 服务，这次 secure log 终于平静下来了&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;一些对安全有益的心得&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
1、平时 ssh 登录尽量避免密码输入方式，应该采用验证钥匙方式登录，如果客户端也是 linux 系统，则这步骤很简单，先&amp;nbsp;ssh-keygen -C &#39;yang@home&#39; 创建一个验证钥匙，然后使用&amp;nbsp;ssh-copy-id somebody@host.domain 把钥匙复制到服务器上，这样以后 ssh 登录就再也不用输入密码了。对于客户端是 windows 和 mac os 系统的估计也有相类似的工具，具体的操作不太清楚。&lt;br /&gt;
&lt;br /&gt;
2、审视一下 /etc/passwd 文件，看看有没有平时不会直接登录帐号，比如有时安装某些软件会自动创建一些帐号，对于这类帐号赶快把最后一段 /bin/bash 改为&amp;nbsp;/sbin/nologin 吧，没错就是禁止它们登录系统&lt;br /&gt;
&lt;br /&gt;
the end</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/4567034504782226236/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2011/01/centos-linux-ssh.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4567034504782226236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/4567034504782226236'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2011/01/centos-linux-ssh.html' title='修改CentOS Linux 的 SSH服务默认设置，提高系统安全性'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-8669550617200189202</id><published>2010-12-14T21:39:00.000-08:00</published><updated>2016-09-26T04:30:52.408-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="php"/><category scheme="http://www.blogger.com/atom/ns#" term="wordpress"/><title type='text'>全新 CentOS 5.5环境挂WordPress3</title><content type='html'>本文目标：在全新的 CentOS 5.5 （32位）里安装&lt;br /&gt;
1、apache 2.2+&lt;br /&gt;
2、mysql 5.1+&lt;br /&gt;
3、php 5.3+&lt;br /&gt;
4、wordpress 3 +&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;安装 apache, mysql, php&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
apache 2 可以使用 yum 安装。&lt;br /&gt;
而 mysql 和 php 由于CentOS源的版本太旧了，所以需要添加另外一个源。这里选用的是 &lt;a href=&quot;http://rpms.famillecollet.com/&quot;&gt;REMI&lt;/a&gt; 源，直接下载这个rpm包：&lt;br /&gt;
&lt;br /&gt;
http://rpms.famillecollet.com/enterprise/remi-release-5.rpm&lt;br /&gt;
&lt;br /&gt;
安装这个包然后就可以安装 httpd和php了&lt;br /&gt;
yum --enablerepo=remi install httpd php php-common&lt;br /&gt;
&lt;br /&gt;
安装php5的附加模块&lt;br /&gt;
yum --enablerepo=remi install php-pear php-pdo php-mysql php-pgsql php-pecl-memcache php-gd php-mbstring php-mcrypt php-xml&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
启动httpd服务之后，在 /var/www/html 里创建一个PHP测试文件比如 test.php，里面写入如下内容&lt;br /&gt;
&amp;lt;?php phpinfo(); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
浏览一下这个页面 http://localhost/test.php，如果出现PHP测试页面的话就说明 apache 和 php 已经安装成功了。&lt;br /&gt;
&lt;br /&gt;
安装mysql&lt;br /&gt;
yum --enablerepo=remi install mysql&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
安装完毕之后登录到 mysql 创建一个名字为 blog 的数据库，添加一个访问帐号。&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;安装 wordpress&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
下面开始安装 wordpress 了，首先到官网下载最新版的 &lt;a href=&quot;http://www.blogger.com/&quot;&gt;wordpress&lt;/a&gt;，然后解压到&amp;nbsp; /var/www ，要注意因为 apache 会以 root 帐号权限运行，所以为了减少麻烦，最好切换到 root 帐号再做刚才的解压操作以及下面的所有步骤。解压之后形成 /var/www/wordpress 目录，然后把该目录下面的 wp-content 添加其他人写入权限，即&lt;br /&gt;
chmod o+w wp-content&lt;br /&gt;
因为这个目录是用来存放主题、上传附件等可变文件的。&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;在 apache 里创建虚拟主机（站点）&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
通常一个服务器可能还挂有其他程序，比如一套CMS或者BBS，所以最好在 apache 里创建虚拟主机来运行 blog，在 /etc/httpd/conf.d 里创建一个名为 wordpress.conf，然后在里面写入&lt;br /&gt;
NameVirtualHost *:80&lt;br /&gt;
&lt;br /&gt;
&amp;lt;VirtualHost *:80&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;ServerName blog.MYDOMAIN.com&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;DocumentRoot /var/www/wordpress&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;ServerAlias MYDOMAIN.com www.MYDOMAIN.com&lt;br /&gt;
&amp;lt;/VirtualHost&amp;gt;&lt;br /&gt;
&lt;br /&gt;
上面一段配置了一个监听80端口的虚拟主机，然后域名是 blog.MYDOMAIN.com（注意要把MYDOMAIN.com换成你的域名），并且把 MYDOMAIN.com www.MYDOMAIN.com 这两个域名同时映射到这个虚拟主机（假如这两个域名有其他用途，则不要这行）。另外还要注意第一行，假如其他配置文件（/etc/httpd/conf.d/*.conf）已经存在了，则应该去掉这行。&lt;br /&gt;
&lt;br /&gt;
重启 apache 服务，然后可以找另外一台机器使用域名访问你的 blog 了。</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/8669550617200189202/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55wordpress3.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8669550617200189202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/8669550617200189202'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55wordpress3.html' title='全新 CentOS 5.5环境挂WordPress3'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-5911838996822133906</id><published>2010-12-03T19:14:00.000-08:00</published><updated>2016-09-26T04:30:52.401-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="Subversion"/><category scheme="http://www.blogger.com/atom/ns#" term="Svn"/><title type='text'>在CentOS 5.5里搭建 Svn 服务器最简单教程</title><content type='html'>&lt;a href=&quot;http://subversion.tigris.org/&quot;&gt;Svn (subversion)&lt;/a&gt; 是一个经典的版本控制系统，比起 &lt;a href=&quot;http://ivarptr.blogspot.com/2010/12/centos-55gitgit.html&quot;&gt;Git&lt;/a&gt; 有着容易使用容易管理的优点，适合小团队或者小项目使用，当项目比较庞大时，这时推荐 &lt;a href=&quot;http://ivarptr.blogspot.com/2010/12/centos-55gitgit.html&quot;&gt;Git&lt;/a&gt;。&lt;br /&gt;
&lt;br /&gt;
在 CentOS 5.5 里搭建 svn 服务非常简单，首先安装 subversion 软件，然后指定 svn 库的主目录，最后让它以独立服务形式运行。&lt;br /&gt;
&lt;br /&gt;
su -c &#39;yum install subversion&#39; #安装 svn 软件&lt;br /&gt;
mkdir ~/svnrepo #创建一个用于容纳 svn 源码库的主目录&lt;br /&gt;
cd ~/svnrepo&lt;br /&gt;
svnadmin create project1 #创建一个源码库&lt;br /&gt;
su -c &#39;svnserve -d -r ~/svnrepo --listen-host 0.0.0.0 --listen-port 12345&#39; #以独立服务形式运行 svn，其中网络端口指定为 12345（可以根据实际情况更改为其他端口号）&lt;br /&gt;
&lt;br /&gt;
那么客户端访问的地址就是&lt;br /&gt;
svn://myhost:12345/project1&lt;br /&gt;
&lt;br /&gt;
That&#39;s all</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/5911838996822133906/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55-svn.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/5911838996822133906'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/5911838996822133906'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55-svn.html' title='在CentOS 5.5里搭建 Svn 服务器最简单教程'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-5614485004748642060</id><published>2010-12-03T19:04:00.000-08:00</published><updated>2016-09-26T04:30:52.399-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="Git"/><title type='text'>在CentOS 5.5里安装Git以及搭建Git服务器最简单教程</title><content type='html'>&lt;a href=&quot;http://git-scm.com/&quot;&gt;Git&lt;/a&gt;是目前比较流行的免费开源的分布式版本控制系统，在Git里能够高效灵活地创建和管理分支。下面讲述如何在CentOS 5.5里安装 Git以及使用Git搭建服务端（严格来说是让一台服务器作为Git服务角色）。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;安装 Git 程序&lt;/span&gt;&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
最简单的方法是在 CentOS 里添加 &lt;a href=&quot;http://fedoraproject.org/wiki/EPEL&quot;&gt;EPEL&lt;/a&gt; 软件库：&lt;br /&gt;
su -c &#39;rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm&#39;&lt;br /&gt;
&lt;br /&gt;
然后就可以简单地通过库安装 Git 了。&lt;br /&gt;
su -c &#39;yum install git&#39;&lt;br /&gt;
&lt;br /&gt;
使用软件库安装 Git 比较方便，而且组件依赖也会自动解决，不过由于 epel 库的 git 版本是 1.5.5 的，而目前 git 已经更新到 1.7.3 了，如果你比较纠结新版本，那么就使用源码方式安装吧，相关文章 google 一下就有了。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;创建 Git 服务端（角色）&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
这里介绍一种比较简单方法，适合整个项目组成员都是平等的情况，方法是大家共同使用一个 ssh 帐号访问 Git 库。&lt;br /&gt;
&lt;br /&gt;
先创建一个用户帐号，比如叫 devteam，然后在 devteam 的 home 目录里创建第一个库文件夹，比如叫 project1.git，然后使用 git 初始化一个裸库。&lt;br /&gt;
&lt;br /&gt;
su -c &#39;useradd devteam&#39; #创建 devteam 帐号&lt;br /&gt;
su -c &#39;passwd devteam&#39; #更改 devtem 帐号的密码&lt;br /&gt;
su - devteam #切换到 devteam 帐号&lt;br /&gt;
mkdir project1.git #创建第一个项目库源码库&lt;br /&gt;
cd project1.git #&lt;br /&gt;
git --bare init #初始化一个裸库&lt;br /&gt;
ssh-keygen #生成 ssh 钥匙，以备后用。&lt;br /&gt;
&lt;br /&gt;
至此，服务端角色已经搭建完毕了。&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;客户端访问 Git 库&lt;/span&gt;&lt;/b&gt; &lt;br /&gt;
客户端可以通过如下地址访问该库：&lt;br /&gt;
devteam@myhost:project1.git&lt;br /&gt;
&lt;br /&gt;
其中 myhost 是指你的服务器ip或者机器名或者域名。&lt;br /&gt;
那么克隆下来的命令就是这样了：&lt;br /&gt;
git clone devteam@myhost:project1.git&lt;br /&gt;
&lt;br /&gt;
剩下的 git 常用操作命令你懂的。&lt;br /&gt;
&lt;br /&gt;
这样访问 git 库必须每次都输入 devteam 帐号的密码，比较烦人。所以比较省事的方法是在客户端生成一个 ssh 公钥，并且把这个公钥复制到 服务端。&lt;br /&gt;
ssh-keygen&lt;br /&gt;
ssh-copy-id devteam@myhost&lt;br /&gt;
&lt;br /&gt;
这样就可以免密码访问了。</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/5614485004748642060/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55gitgit.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/5614485004748642060'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/5614485004748642060'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2010/12/centos-55gitgit.html' title='在CentOS 5.5里安装Git以及搭建Git服务器最简单教程'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-436801038062762809.post-7593035108144544182</id><published>2010-11-26T09:32:00.000-08:00</published><updated>2016-09-26T04:30:52.404-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CentOS"/><category scheme="http://www.blogger.com/atom/ns#" term="pptpd"/><category scheme="http://www.blogger.com/atom/ns#" term="vpn"/><category scheme="http://www.blogger.com/atom/ns#" term="Xen"/><title type='text'>在基于Xen的VPS(CentOS 5)下面使用pptpd搭建VPN服务</title><content type='html'>目前比较常见的虚拟服务器（VPS）大部分都建立于 OpenVZ 或者 Xen 虚拟平台之上，下面记录一下在基于 Xen 的 VPS 里面使用&amp;nbsp; pptpd 搭建 VPN 的方法。注：操作系统是CentOS 5&lt;br /&gt;
&lt;br /&gt;
1、先安装 ppp 和 iptables，后者一般系统已经安装，不过为了保证下面的步骤能成功，用yum测试一下也无妨。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#yum install ppp&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#yum install iptables&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;2、下载 pptpd&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;这里采用源码的安装方式，进入当前用户目录&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#cd ~&lt;/code&gt;
&lt;code&gt; &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;从 Poptop 项目（地址：&lt;/code&gt;&lt;a href=&quot;http://sourceforge.net/projects/poptop/files/&quot;&gt;http://sourceforge.net/projects/poptop/files/&lt;/a&gt;&lt;code&gt;）&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;下载 pptpd-1.3.4.tar.gz&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#wget http://...（具体地址）...&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;具体的地址应该到上面的地址点击下载链接然后复制之。 &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;3、编译 pptpd&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;先解压缩并进入源码目录 &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#tar -zxvf pptpd-1.3.4.tar.gz&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#cd pptpd-1.3.4&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&lt;/code&gt;然后是很常规的编译操作&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#./configure&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#make&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#make install&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;4、必要的配置文件&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;复制几个必要的配置文件到相应的目录&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#cp samples/pptpd.conf /etc/&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#cp samples/options.pptpd /etc/ppp/&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#cp samples/chap-secrets /etc/ppp/&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;5、编辑 /etc/pptpd.conf &lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;去除这行的注释：ppp /usr/sbin/pppd&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;配置VPN服务端IP和VPN客户端的IP，这里只要让客户端IP跟你现在看这篇文章时的电脑IP不在&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;同一个网段就可以了，我的配置如下：&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;localip 192.168.8.1&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;remoteip 192.168.8.100-200&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;这两行可以照抄，如果你的电脑IP刚好是8段的，那么就把上面的数字8通通改成9吧 :)&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;另外由于源码包附带的一个插件版本有问题，还要注释掉插件 logwtmp，在前面加个井号就是了。&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;6、编辑 /etc/ppp/chap-secrets&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;这个文件是用来配置客户端连接时所使用的用户名和密码的，用明文书写即可，比如创建一个登录名和密码&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;都是“boy”的帐号，那么添加如下一行&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;boy &amp;nbsp;pptpd boy &quot;*&quot;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;最后一个星号表示接受任何IP的客户端连接。 &lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;7、编辑 /etc/ppp/options.pptpd&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;这个文件不作任何修改也可以，比较常见的是指定DNS解析服务器，用于防止当地的DNS污染。&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;修改这两行 ms-dns 10.0.0.1 和 ms-dns 10.0.0.2&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;为 ms-dns 8.8.8.8 和 ms-dns 208.67.222.222&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;前者是google的DNS服务器，后者是OpenDNS的服务器。&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;到这一步实际上 pptpd 的VPN服务已经安装配置好，只要运行&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#/usr/local/sbin/pptpd &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;就启动 pptpd 程序了，可以用&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#ps -e |grep pptpd&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;确认程序是否正在运行，可以用&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#netstat -na |grep 1723&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;确认 pptpd 已经打开网络端口准备接受客户端连接了。&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;不过现在客户端连接上来一般还不能浏览网页，还需继续以下步骤。&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt; &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;8、允许IP转发&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;编辑 /etc/sysctl.conf，找到 “net.ipv4.ip_forward = 1” 这一行，如果前面有注释则删除注释，&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;如果最后一个数字不是1，则改为1。&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;运行下面命令让它生效：&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#sysctl -p&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;9、在 iptables 里添加 nat 转发&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#service iptables start
#iptables -t nat -A POSTROUTING -o eth0 -s 192.168.8.0/24 -j MASQUERADE
#service iptables save&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;好了，现在应该可以使用客户机测试 VPN 连接了。&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;最后，如果你对 logwtmp 这个插件很纠结，那么照下面的步骤启用它：&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;1、修改源码当中的 ~/pptpd-1.3.4/plugins/patchlevel.h 的版本信息&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#cd ~/pptpd-1.3.4/plugins &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;将 patchlevel.h 的 #define VERSION &quot;2.4.3&quot; 改为 &quot;2.4.4&quot;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;重新编译&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#make&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt; &lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;2、复制 pptpd-logwtmp.so 到相应的目录&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#mkdir /usr/lib/pptpd/&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#cp &lt;/code&gt;&lt;code&gt;pptpd-logwtmp.so&lt;/code&gt; /usr/lib/pptpd/&lt;code&gt;pptpd-logwtmp.so&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;3、修改 /etc/pptpd.conf&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;把上面步骤5禁用插件 logwtmp 的那行注释去除&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;4、重新运行 pptpd&lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;#pkill pptpd &lt;/pre&gt;&lt;pre class=&quot;code&quot;&gt;&lt;code&gt;#/usr/local/sbin/pptpd&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
这4步如果不做的话，会在 /var/log/message 得到如下的错误：&lt;br /&gt;
Plugin /usr/lib/pptpd/pptpd-logwtmp.so is for pppd version 2.4.3, this is 2.4.4&lt;br /&gt;
或者 &lt;br /&gt;
Couldn&#39;t load plugin /usr/lib/pptpd/pptpd-logwtmp.so&lt;br /&gt;
&lt;br /&gt;
最后，Poptop 的官方说明文档有兴趣或者安装失败则应该看看&lt;br /&gt;
&lt;a href=&quot;http://poptop.sourceforge.net/dox/&quot;&gt;http://poptop.sourceforge.net/dox/&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://ivarptr.blogspot.com/feeds/7593035108144544182/comments/default' title='博文评论'/><link rel='replies' type='text/html' href='http://ivarptr.blogspot.com/2010/11/xenvpscentos-5pptpdvpn.html#comment-form' title='0 条评论'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/7593035108144544182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/436801038062762809/posts/default/7593035108144544182'/><link rel='alternate' type='text/html' href='http://ivarptr.blogspot.com/2010/11/xenvpscentos-5pptpdvpn.html' title='在基于Xen的VPS(CentOS 5)下面使用pptpd搭建VPN服务'/><author><name>Hippo Spark</name><uri>http://www.blogger.com/profile/17210551730252523295</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpxLpC1uT0JMN9enEzSGMmn0miIWW86NS6lageR1JfW-0CMdILJhPoiEozCb7yppkHN4taOT3EiPNzJUGMAywiJEQzSPQ9iAYqdngtJNNJmpi0utxHF4kkXoB4Xl_J_g/s220/avatar+wide+square.png'/></author><thr:total>0</thr:total></entry></feed>