2007/01/31

[转]Linux 下的输出重定向

来自:Linux联盟

Linux Shell 环境中支持输入输出重定向,用符号<和>来表示。0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出,比如 2>a.txt 表示将错误信息输出到文件a.txt中。

同时,还可以在这三个标准输入输出之间实现重定向,比如将错误信息重定向到标准输出,可以用 2>&1来实现。

Linux下还有一个特殊的文件/dev/null,它就像一个无底洞,所有重定向到它的信息都会消失得无影无踪。这一点非常有用,当我们不需要回显程序的所有信息时,就可以将输出重定向到/dev/null。

如果想要正常输出和错误信息都不显示,则要把标准输出和标准错误都重定向到/dev/null, 例如:

# ls 1>/dev/null 2>/dev/null

还有一种做法是将错误重定向到标准输出,然后再重定向到 /dev/null,例如:

# ls >/dev/null 2>&1

注意:此处的顺序不能更改,否则达不到想要的效果,此时先将标准输出重定向到 /dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,于是一切静悄悄:-)

2007/01/30

自己 动手整合 Red Hat enterpise linux 4 update 4的DVD光盘。


这是转载老外的文章,写得不错。不过他的方法里面没有通过光盘自校验的方法。我只是在最后增加

了一步,让自己制作的光盘也能通过光盘安装时的自检验。呵呵。

Creating a custom Red Hat installation DVD

Created on 19th October 2005

How to create a single CD for fast and easy customized installation.

Setting up the build directory:
The first thing to do is to copy all the cdrom ISOs to one location:
mkdir -p /mnt/disk{1,2,3,4}
mount -o loop RHEL4-U1-i386-AS-disc1.iso /mnt/disk1
mount -o loop RHEL4-U1-i386-AS-disc2.iso /mnt/disk2
mount -o loop RHEL4-U1-i386-AS-disc3.iso /mnt/disk3
mount -o loop RHEL4-U1-i386-AS-disc4.iso /mnt/disk4

We now copy all the files from the directories to a single directory:
mkdir -p /data/isobuild
rsync -rv /mnt/disk{4,3,2,1}/* /data/isobuild/

We also need to copy across the .diskinfo file that is not caught by our *:
cp /mnt/disk1/.diskinfo /data/isobuild/

The .diskinfo file identifies the CD as being a correct Red Hat Installer disk
and is checked by anaconda during the start of the install.

We could now build the DVD as it is but we really should have a fiddle first :-)

Adding more software to the DVD
We could add some of our own rpms to /data/isobuild/RedHat/RPMS; however by just
doing this does not make them available at install time. There is an XML file
that is read and ensures that the packages are installed in the correct order.

So let us throw a few random packages into the mix:

Add some java:
cp jre-1_5_0_03-linux-i586.rpm /data/isobuild/RedHat/RPMS/

Some encryption for GAIM:
cp gaim-encryption-2.36-3.rf.i386.rpm /data/isobuild/RedHat/RPMS/

Updating the comps.xml file
We need to ensure that the host computer has anaconda and anaconda-runtime installed:
up2date anaconda anaconda-runtime

Before we update the XML dependency file we need to sort out package orders.
If you have added a lot of new packages you may need to remove some old
packages that you have replaced with newer versions to stop conflicts.

So the first command is:
PYTHONPATH=/usr/lib/anaconda /usr/lib/anaconda-runtime/pkgorder \
/data/isobuild/ i386 > /data/isobuild/xander-pkgorder

This creates a list of files in the order it needs to install them
in the file /data/isobuild/xander-pkgorder. Sometimes an occasional
RPM will not provide the information anaconda needs. You can edit
the file manually and insert your RPMs at the end.

Next we need to generate the dependency file:

/usr/lib/anaconda-runtime/genhdlist --fileorder /data/isobuild/xander-pkgorder \
/data/isobuild/

You will probably have a few hiccoughs the first time you run these commands.
Most may be resolved by adding the missing entries to the pkgorder file or
deleting duplicate packages.

Creating an automated installer
We could *now* if we wanted to build our DVD; however we can make an
automated installer.

So crack open system-config-kickstart and create a kickstart file with
all the packages and partitioning etc you need for your systems.

copy the resulting file to /data/isobuild/ks.cfg

we can now edit the file /data/isobuild/isolinux/isolinux.cfg

copy or change the three lines:
label linux
kernel vmlinuz
append initrd=initrd.img ramdisk_size=8192

to
label xander
kernel vmlinuz
append initrd=initrd.img ramdisk_size=8192 ks=cdrom:/ks.cfg

Then change the default at the top of the file to xander. This means that
the default action is to install directly from the DVD using your kickstart file.

Building the DVD iso
Now to build the iso:

cd /data/isobuild

chmod a+w isolinux/isolinux.bin

mkisofs -r -T -J -V "Custom RHEL4 Build" -b isolinux/isolinux.bin \
-c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
-boot-info-table -o /data/custom-RHEL4-dvd.iso /data/isobuild/

下一步,我们增加光盘自校验操作。这一步可能会花费2-3分钟,请耐心等待。

/usr/lib/anaconda-runtime/implantisomd5 /data/custom-RHEL4-dvd.iso

Inserting md5sum into iso image...

md5 = 135556ca60f1225a265346acab07086f

Setting supported flag to 0

Buring the DVD
Now we can burn the image to the CD. I assume the CD writer is already
set up on your system. We use cdrecord below, but you can use other
programs as well. The command is invoked as:

cdrecord -v speed=4 dev=0,0,0 /data/custom-RHEL4-dvd.iso

The speed and dev options depend on your system. The device for the dev
argument can be determined by using the -scanbus option to cdrecord:

cdrecord -scanbus

Using the DVD

Once the image is burned onto the DVD, insert the DVD into the target
machine and boot the machine. You should get the custom message that
you created earlier. At this point, you can either press Enter at the
boot prompt or let it timeout. When it times out it uses the default
label, which we specified as ks (Kickstart).

postfix 最新版 rpm 下载网站

http://ftp.wl0.org/

可以下载postfix 最新版 rhel 4 rpm
这个老外做的很不错,还可以下载srpm进行 二次编译。

centos 4.4 安装 java 1.5

从下面这个地址下载程序。
http://www.java.com/zh_CN/download/linux_manual.jsp

我下的是jre-1_5_0_10-linux-i586-rpm.bin
先 chmod 777 jre-1_5_0_10-linux-i586-rpm.bin
运行 ./jre-1_5_0_10-linux-i586-rpm.bin
rpm -ivh jre-1_5_0_10-linux-i586-rpm

看看在 /usr/java 下面是否有 jre1.5.0_10 这个文件夹

然后用vi修改 /etc下 profile文件 vi /etc/profile 在文件的最后添加下面几行:

JAVA_HOME=/usr/java/jre1.5.0_10
JAVA_BIN=/usr/java/jre1.5.0_10/bin
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME JAVA_BIN PATH

保存,注销系统。

2007/01/29

centos 4.4 搭建动态网站加防火墙和NAT转发

安装ADSL拨号软件rp-ppp
在DVD安装光盘里面就有。
将光盘放进光驱。
mount /media/cdrom
cd /media/cdrom/CentOS/RPMS/
rpm -ivh rp-ppp-*.rpm

这时我们进行配置ADSL拨号。
[root@bbs RPMS]# adsl-setup

Welcome to the ADSL client setup. First, I will run some checks on
your system to make sure the PPPoE client is installed properly...


LOGIN NAME

Enter your Login Name (default root): 用户名

INTERFACE

Enter the Ethernet interface connected to the ADSL modem
For Solaris, this is likely to be something like /dev/hme0.
For Linux, it will be ethX, where 'X' is a number.
(default eth0): eth0

Do you want the link to come up on demand, or stay up continuously?
If you want it to come up on demand, enter the idle time in seconds
after which the link should be dropped. If you want the link to
stay up permanently, enter 'no' (two letters, lower-case.)
NOTE: Demand-activated links do not interact well with dynamic IP
addresses. You may have some problems with demand-activated links.
Enter the demand value (default no): no

DNS

Please enter the IP address of your ISP's primary DNS server.
If your ISP claims that 'the server will provide dynamic DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you know what you are
doing and not modify your DNS setup.
Enter the DNS information here: 输入你当地的DNS地址,重庆电信为61.128.128.68
Please enter the IP address of your ISP's secondary DNS server.
If you just press enter, I will assume there is only one DNS server.
Enter the secondary DNS server address here: 输入你当地的第二DNS地址,重庆电信为61.128.192.68

PASSWORD

Please enter your Password:输入密码
Please re-enter your Password:重复输入密码

USERCTRL

Please enter 'yes' (two letters, lower-case.) if you want to allow
normal user to start or stop DSL connection (default yes):

FIREWALLING

Please choose the firewall rules to use. Note that these rules are
very basic. You are strongly encouraged to use a more sophisticated
firewall setup; however, these will provide basic security. If you
are running any servers on your machine, you must choose 'NONE' and
set up firewalling yourself. Otherwise, the firewall rules will deny
access to all standard servers like Web, e-mail, ftp, etc. If you
are using SSH, the rules will block outgoing SSH connections which
allocate a privileged source port.

The firewall choices are:
0 - NONE: This script will not set any firewall rules. You are responsible
for ensuring the security of your machine. You are STRONGLY
recommended to use some kind of firewall rules.
1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
for a LAN
Choose a type of firewall (0-2): 选择一个防火墙类型,我们要做网关,所以选择2

Start this connection at boot time

Do you want to start this connection at boot time?
Please enter no or yes (default no):yes

** Summary of what you entered **

Ethernet Interface: eth0
User name: 用户名
Activate-on-demand: No
Primary DNS: 61.128.128.68
Secondary DNS: 61.128.192.68
Firewalling: MASQUERADE
User Control: yes
Accept these settings and adjust configuration files (y/n)? y
Adjusting /etc/sysconfig/network-scripts/ifcfg-ppp0
Adjusting /etc/resolv.conf
(But first backing it up to /etc/resolv.conf.bak)
Adjusting /etc/ppp/chap-secrets and /etc/ppp/pap-secrets
(But first backing it up to /etc/ppp/chap-secrets.bak)
(But first backing it up to /etc/ppp/pap-secrets.bak)



Congratulations, it should be all set up!

Type '/sbin/ifup ppp0' to bring up your xDSL link and '/sbin/ifdown ppp0'
to bring it down.
Type '/sbin/adsl-status /etc/sysconfig/network-scripts/ifcfg-ppp0'
to see the link status.
配置到这里,ADSL开机自动拨号就算完成了。

然后执行
#adsl-start
开始连接。

查看连接成功与否。
# ifconfig
如果出现ppp0,并出现IP地址就算成功了。

然后我们配置网关,让局域网内的机器都能利用这台机器上网。
假设我们有几台机器,通过交换机连接在一起,我们安装服务器的这台机器上有一张网卡。这样我们也可以用一张网卡和一个交换机实现局域网共享internet连接。而不一定要两张网卡。

vi /etc/sysconfig/network
修改如下:
NETWORKING=yes
HOSTNAME=admin.cnprint.org
GATEWAY=

保存。
vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改如下:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.9.120
NETMASK=255.255.255.0
GATEWAY=192.168.9.120

vi /etc/hosts
127.0.0.1 localhost
192.168.1.1 linux001
192.168.1.2 linux002
.......(略)......
192.168.1.100 linux100
.......(略)......
192.168.1.254 linux254


保存。
因为要用本机作为局域网的网关,所以我们设定网关和IP地址相同。

另外,如果要架站,需要注意一个地方:就是连接超时自动断开。解决方法是
vi /etc/sysconfig/network-scripts/ifcfg-ppp0
把PPPOE-TIMEOUT=后面的数字改成0,把CONECT-TIMEOUT=后面的数字也改成0。这样你上网就稳定了。
然后cp /etc/sysconfig/network-scripts/ifcfg-ppp0 /etc/ppp/pppoe.conf
这样你可以用adsl-connect &来连接你的adsl。
在断线时它会自动重拨。还有,如果你需要过程启动服务器话,别忘了把ppoe.conf和 /etc/sysconfig/network-scripts/ifcfg-ppp0中的onboot=no设为onboot=yes,不然你一重启,你的服务器别人和自己就都访问不了了。^-^

编辑防火墙:
vi /usr/local/sbin/fw.sh

将以下脚本命令粘贴到 fw.sh 文件中


#!/bin/bash

# 請先輸入您的相關參數,不要輸入錯誤了!
EXTIF="ppp0" # 這個是可以連上 Public IP 的網路介面
INIF="eth0" # 內部 LAN 的連接介面;若無請填 ""
INNET="192.168.9.0/24" # 內部 LAN 的網域,若沒有內部 LAN 請設定為 ""
export EXTIF INIF INNET

# 第一部份,針對本機的防火牆設定!###########################
# 1. 先設定好核心的網路功能:
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo "0" > $i
done

# 2. 清除規則、設定預設政策及開放 lo 與相關的設定值
PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED -j ACCEPT

# 3. 啟動額外的防火牆 script 模組
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
sh /usr/local/virus/iptables/iptables.deny
fi
if [ -f /usr/local/virus/iptables/iptables.allow ]; then
sh /usr/local/virus/iptables/iptables.allow
fi
if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
sh /usr/local/virus/httpd-err/iptables.http
fi
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

# 4. 允許某些類型的 ICMP 封包進入
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done

# 5. 允許某些服務的進入,請依照您自己的環境開啟
# iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH
# iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --sport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --sport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # WWW
# iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3
# iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS

# 第二部份,針對後端主機的防火牆設定!##############################
# 1. 先載入一些有用的模組
modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack
ip_conntrack_ftp ip_conntrack_irc"
for mod in $modules
do
testmod=`lsmod | grep "${mod} "`
if [ "$testmod" == "" ]; then
modprobe $mod
fi
done

# 2. 清除 NAT table 的規則吧!
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

# 3. 開放成為路由器,且為 IP 分享器!
if [ "$INIF" != "" ]; then
/sbin/iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE
fi

# 如果你的 MSN 一直無法連線,或者是某些網站 OK 某些網站不 OK,
# 可能是 MTU 的問題,那你可以將底下這一行給他取消註解來啟動 MTU 限制範圍
# iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
# --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu

# 4. 內部伺服器的設定:
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
# -j DNAT --to 192.168.1.210:80

# Deny All Other Connections
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


保存。这个文本是直接参考鸟哥的教学资料而来的。鸟哥的教学文章写得真好。

# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
# /usr/local/sbin/fw.sh

# ifdown eth0
# ifup eth0
# route

将会出现下面的提示:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.17.1.1 * 255.255.255.255 UH 0 0 0 ppp0
192.168.9.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 172.17.1.1 0.0.0.0 UG 0 0 0 ppp0

如果这样就说明我们成功了。

然后在局域网内设置其他机器的
IP地址为:192.168.9.***
子网掩码为:255.255.255.0
网关为:192.168.9.120
DNS地址为你的提供ADSL服务的电信商的DNS。

局域网内的机器就能通过这台机器上网了,同时这台机器还向外界提供WEB服务。

安装3322动态IP更新。
申请一个3322的动态域名帐号:
# yum install lynx

lynx -mime_header -auth=username:userpassword "http://www.3322.org/dyndns/update?system=dyndns&hostname=yourdomain"


# vi /etc/ppp/dyndns.sh

写入:/lynx -mime_header -auth=username:userpassword "http://www.3322.org/dyndns/update?system=dyndns&hostname=yourdomain"


#chmod 777 /etc/ppp/dyndns.sh


然后在你的固定域名的mydns设置里面,将你所申请的3322的动态域名设置为固定域名的别名。

用了一段时间的adsl作为动态网站的接入方式后,我们会发现,有时ADSL掉线了,但是拨号程序并没有真正重拨,导致ADSL僵死在那里,当然你的网站就没有戏了。呵呵。

这时怎么办呢。我们可以写一个脚本自动检测连接状态。

vi /etc/ppp/pppoe.sh

ping -c5 www.163.com >; /dev/null 2>;&1
if [ "$?" != "0" ]; then
/usr/bin/killall -9 pppoe
fi

保存。这时在进程里杀掉了pppoe后,因为我们在前面设置了断线立即连接的,所以程序会自动重新拨号,获取新连接。

# crontab -e
*/2 * * * * root /etc/ppp/pppoe.sh
*/3 * * * * root /etc/ppp/dyndns.sh

该行指定每2分钟运行一次/etc/ppp/pppoe.sh,检查是否吊线,如掉线就重拨。
每3分钟运行一次更新动态IP。

centos 4.4 上完美支持读写NTFS文件系统

要下载2个软件进行编译

先下载fuse 2.6以上 版本:
http://sourceforge.net/project/showfiles.php?group_id=121684&package_id=132802
下载链接:

wget http://jaist.dl.sourceforge.net/sourceforge/fuse/fuse-2.6.1.tar.gz
tar zxvf fuse-2.6.1.tar.gz
cd fuse-2.6.1
./configure && make && make install

然后再下载NTFS-3g
http://www.ntfs-3g.org/
下载最新版本的程序,目前是ntfs-3g-0.20070118-BETA.tgz.

wget http://www.ntfs-3g.org/ntfs-3g-0.20070118-BETA.tgz
tar zxvf ntfs-3g-0.20070118-BETA.tgz
cd ntfs-3g-0.20070118-BETA
./configure && make && make install

然后查看你磁盘上那些分区是NTFS格式的:
# fdisk -l
将会列示出,注意其中的红字部分,就是你的NTFS分区:

Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 1530 12289693+ 7 HPFS/NTFS
/dev/hda2 1531 9729 65858467+ 5 Extended
/dev/hda5 1531 4080 20482843+ 7 HPFS/NTFS
/dev/hda6 6631 9729 24892686 7 HPFS/NTFS
/dev/hda7 4081 4093 104391 83 Linux
/dev/hda8 4094 5356 10145016 83 Linux
/dev/hda9 5357 5993 5116671 82 Linux swap
/dev/hda10 5994 6630 5116671 83 Linux

Partition table entries are not in disk order

Disk /dev/sda: 20.0 GB, 20003880960 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 2431 19526976 7 HPFS/NTFS

现在新建目录:
# mkdir /media/c
# mkdir /media/d
# mkdir /media/f

然后文件系统表
# vi /etc/fstab
增加相应的以下几行:
/dev/hda1 /media/c ntfs-3g defaults 0 0
/dev/hda5 /media/d ntfs-3g defaults 0 0
/dev/hda6 /media/f ntfs-3g defaults 0 0

修改一下关于usbdisk的设定,将ntfs改为ntfs-3g:
/dev/sda1 /media/usbdisk ntfs-3g pamconsole,fscontext=system_u:object_r:removable_t,exec,noauto,managed 0 0

保存,退出。

最后挂接分区。

# mount /media/c
# mount /media/d
# mount /media/f
# mount /media/usbdisk

呵呵。这时你就可以cd /media/c了,可以完美的支持ntfs的读写。

centos 4.4 上安装小企鹅五笔输入法

五笔输入法在windows下是极简单的东西,到了linux下却变得很麻烦。
我现在说一下小企鹅五笔在red hat系列上的安装方法。完全不再需要使用以前的那些删除暴多东西了。
以centos 4.4为例:

下载小企鹅输入法最新预编译包:
http://www.fcitx.org/main/?q=node/9
目前是3.4.2
http://www.fcitx.org/download/fcitx-3.4.2-bin.tar.bz2

解压运行fcitx.install程序。
然后修改:
vi /etc/X11/xinit/xinput.d/zh_CN

XIM=fcitx
XIM_PROGRAM=fcitx
GTK_IM_MODULE=xim
gnome-im-settings-daemon

保存,重启系统,小企鹅输入法就安装成功了,是不是挺简单的?

centos 4.4 上编译 kernel 2.6.19

I updated the kernel to 2.6.19. I got the kernel from

http://download.fedora.redhat.com/pub/fedora/linux/core/6/source/SRPMS/

I also needed the unifdef srpm, and built it first:

rpmbuild --rebuild unifdef*.src.rpm
rpm -ivh /usr/src/redhat/RPMS/i386/unidef-1*

And then I built the kernel:

rpm -ivh kernel*.src.rpm
cd /usr/src/redhat/SPECS
rpmbuild -bp --target i386 kernel-2.6.spec
cd /usr/src/redhat/BUILD/kernel-2.6.19/linux-2.6.19.i386
make mrproper
cp configs/kernel-2.6.19-i686.config .config
make
make modules_install
make install

然后重启,系统会显示:
Enforcing mode requested but no policy loaded. Halting now."
Kernel panic: Attempted to kill init!

解决办法:在grub菜单中增加enforcing=0
vi /etc/grub.conf
kernel /vmlinuz-2.6.19-prep ro root=LABEL=/ rhgb quiet enforcing=0

改变默认的启动选项:
vi /boot/grub/menu.lst

中的default字段,如default 3,即把列表中的第4个OS作为默认

现在我们把它改成0

保存。
OK,完成

用Suhosin给你的php打上内核级安全补丁!

这本来是我 的新版安装web服务器教程安全管理中的一部份,今天先发出来吧。超级推荐。
Suhosin是朝鲜语“守护神”的音译,是一个专门的安全小组开发的专门针对php进行安全加固的补丁程 序,已经进入freebsd gentoo的ports系统。效果很好的。具体的介绍大家可以看看http://www.hardened- php.net/suhosin_096_released.131.html ,国内介绍它的人极少,根本找不到中文的编译安装方法。我耐着性子看完了英文。按照操作安装成功了。现在特别将安装方法共享。欢迎大家试用安装,提高你的 服务器的安全水平。以php5.2.0为例,我的编译目录在/usr/local/src。注意红字是安装Suhosin的具体步骤。

Suhosin是php增强型安全补丁,可以编译到静态内核中,也可以编译成php动态扩展。我个人强烈你建议静态联编。下面的以下先说静态安装步骤。当然你也可以在安装php后将它编译成php的动态扩展。

# cd /usr/local/src
# wget [URL="http://cn.php.net/get/php-5.2.0.tar.gz/from/this/mirror"]http://cn.php.net/get/php-5.2.0.tar.gz/from/this/mirror[/COLOR][/URL]
# tar zxvf php-5.2.0.tar.gz
# cd php-5.2.0
wget http://www.hardened-php.net/suhosin/_media/suhosin-patch-5.2.0-0.9.6.2.patch.gz //从官方下载补丁
gunzip suhosin-patch-5.2.0-0.9.6.2.patch.gz // 解压补丁
patch -p 1 -i suhosin-patch-5.2.0-0.9.6.2.patch // 给php打上补丁。

#./buildconf --force //一定要执行这一步。
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib-dir --with-bz2 --with-tiff-dir --with-libxml-dir=/usr/local/libxml2 --with-gd=/usr/local/gd2 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-iconv --disable-ipv6 --enable-static --enable-maintainer-zts --enable-memory-limit --enable-zend-multibyte --enable-sockets --enable-soap --enable-suhosin // 配置
选项

# make

# make install

重启 apachectl,查看phpinfo信息,会出现
This server is protected with the Suhosin Patch 0.9.6.2
Copyright (c) 2006 Hardened-PHP Project

等许多Suhosin信息那么你就成功了。呵呵。


在这里也顺便说一下将suhosin安装成为php的动态扩展的方法。毕竟网上根本不见它的中文安装教程。

虽然我个人不推荐这种方式。

wget http://www.hardened-php.net/suhosin/_media/suhosin-0.9.16.tgz
tar zxvf suhosin-0.9.16.tgz
cd suhosin-0.9.16
./configure --with-php-config=/usr/local/php/bin/php-config //必须在这儿注明php-config所在的绝对路径。
make
make install

会提示编译的模块存在的目录,记住它。
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

然后在php.ini中增加一行下列语句。
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/suhosin.so

重启apachectl.

2007/01/11

CentOS 4.4 编译安装 apache 2.2.3+php 5.2.0+mysql 5.0.27+Zend Optimizer v3.2.0+eaccelerator-0.9.5笔记

CentOS 4.4 编译安装 apache 2.2.3+php 5.2.0+mysql 5.0.27+Zend Optimizer v3.2.0+eaccelerator-0.9.5笔记


先声明,我绝对是个Linux的菜鸟,以下的这些文字个人的安装笔记,是参考了网上许多高手的成功经验组合而成的,说实话我自己的东西不多 :)我想把这些经验写下来,既可以对其他跟我一样摸索的兄弟一个经验,也可以防止以后自己都忘记了,嘿嘿。

所以如果某个大虾看到我借用了你的资源,请你不要生气。也欢迎大家进行指点。

1、下载centos 4.4
我是下载的DVD版本,大家也可以下载服务器CD安装版本,其实都差不多。大家可以到这儿下载,速度很快的。
http://ftp.iasi.roedu.net/mirrors/ce...4.4/isos/i386/

下载后当然就刻录成光盘。我建议你刻录DVD啦,如果是菜鸟,也可以在图形界面进行学习,不会这么抓不住头脑。

2、安装CentOS4.4
作为服务器,不安装不需要的组件,所以在选择组件的时候,除了选择FTP SERVER外取消所有组件的选择。也不要选web服务器。因为我们后面要手动编译安装。


系统约定RPM包和源码包存放位置

RPM包和源码包存放位置 /usr/local/src
源码包编译安装位置(prefix) /usr/local/xxx
脚本以及维护程序存放位置 /usr/local/sbin
MySQL 数据库位置 /var/lib/mysql
Apache 网站根目录 /home/www
Apache 虚拟主机日志根目录 /data/logs/www
yum RPM包信息文件 /etc/yum.list



3、系统环境部署及调整

(1). 检查系统是否正常
# more /var/log/messages //检查有无系统内核级错误信息
# demesg //检查硬件设备是否有错误信息
# ifconfig //检查网卡设置是否正确
# ping www.163.com // 检查网络是否正常

(2). 关闭不需要的服务
# export LANG='en_US' //设置语言
# setup //选择启动的服务
进入system service 选项。
以space 键选定所需服务。
以下仅列出需要启动的服务,未列出的服务一律关闭:
crond
irqbalance 仅当服务器CPU为S.M.P架构或支持双核心、HT技术时,才需开启,否则关闭。
microcode_ctl
network
vsftpd
sshd
syslog

(3)、修改/etc/yum.repos.d/CentOS-Base.repo,将镜象站点地址改为在中国的镜象站点地址。不然我们通过yum安装软件速度会极慢。修改如下:



代码:
# CentOS-Base.repo
#
# This file uses a new mirrorlist system developed by Lance Davis for CentOS.
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
baseurl=http://mirror.vmmatrix.net/CentOS/$releasever/os/$basearch/
http://mirror.be10.com/centos/$releasever/os/$basearch/
http://centos.hostlink.com.hk/$releasever/os/$basearch/
http://ftp.hostrino.com/pub/centos/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4

#released updates
[update]
name=CentOS-$releasever - Updates
baseurl=http://mirror.vmmatrix.net/CentOS/$releasever/os/$basearch/
http://mirror.be10.com/centos/$releasever/os/$basearch/
http://centos.hostlink.com.hk/$releasever/os/$basearch/
http://ftp.hostrino.com/pub/centos/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4

#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
baseurl=http://mirror.vmmatrix.net/CentOS/$releasever/os/$basearch/
http://mirror.be10.com/centos/$releasever/os/$basearch/
http://centos.hostlink.com.hk/$releasever/os/$basearch/
http://ftp.hostrino.com/pub/centos/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirror.vmmatrix.net/CentOS/$releasever/os/$basearch/
http://mirror.be10.com/centos/$releasever/os/$basearch/
http://centos.hostlink.com.hk/$releasever/os/$basearch/
http://ftp.hostrino.com/pub/centos/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos4
保存。

然后导入key升级相应yum数据

# rpm --import /usr/share/doc/centos-release-4/RPM-GPG-KEY-centos4
# yum list tee /etc/yum.list //读取yum list 数据,将其输出到 /etc/yum.list。


(4)、定时校正服务器时间
# yum install ntp
# crontab -e
0 23 * * * root /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1

以上命令设置好后存盘。您的机器将在每天的23:00根据中国国家授时中心的NTP服务器时间自动校准时间。

(5). 对TCP/IP网络参数进行调整,加强抗SYN Flood能力
# echo 'net.ipv4.tcp_syncookies = 1' >> /etc/sysctl.conf //将net.ipv4.tcp_syncookies = 1写入sysctl.conf 文件
# sysctl -p //查看

(6)、FTP服务器的配置
vi /etc/vsftpd/vsftpd.conf
把anonymous_enable=YES注释掉不允许匿名登录。
把chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
前的注释去掉。
把ftpd_banner=*前的注释去掉。后面改成你的欢迎信息(这样设置可以避免显示ftp服务器的版本信息)
然后保存,service vsftpd start就可以了。

这时应当添加用户,因为root默认不能通过FTP方式登录。

# adduser username
# passwd userpassword

这样对于我们上传一些文件到系统中很方便。


4. 重新启动系统
# init 6


5. 使用 yum 程序安装所需开发包(以下为标准的 RPM 包名称)
# yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel

#这里我们将编译GD所必须的一些小软件比如libpng,libtiff,freetype,libjpeg等先用RPM的方式一并安装好,避免手动编 译浪费时间,同时也能避免很多错误,这几个小软件的编译很麻烦。这几个小软件编译错误了,GD当然安装不了,php5的编译当然也没戏了。所以我们抓大放 小,对这些小牛鬼蛇神采取快速简洁的方式进行安装。并且对服务器的性能也不能产生什么影响。

6. 源码编译安装所需包 (Source)
(1) GD2
# cd /usr/local/src
# wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
# tar xzvf gd-2.0.33.tar.gz
# cd gd-2.0.33
# ./configure --prefix=/usr/local/gd2 --mandir=/usr/share/man //./configure 配置。
# make //make 是用来编译的,它从 Makefile 中读取指令,然后编译。
# make install //make install 是用来安装的,它也从 Makefile 中读取指令,安装到指定的位置。

(2) Apache 日志截断程序
# cd /usr/local/src
# wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
# tar xzvf cronolog-1.6.2.tar.gz
# cd cronolog-1.6.2
# ./configure --prefix=/usr/local/cronolog
# make
# make install

(3) libxml 库程序
# cd /usr/local/src
# wget http://ftp.gnome.org/pub/gnome/sources/libxml2/2.6/libxml2-2.6.26.tar.gz
# tar zjvf libxml2-2.6.26.tar.gz
# cd libxml2-2.6.26
# ./configure --prefix=/usr/local/libxml2
# make
# make install

7、编译mysql 5.0.27
cd /usr/local/src
# wget http://mysql.oss.eznetsols.org/Downl...-5.0.27.tar.gz
# tar xzvf mysql-5.0.27.tar.gz
# cd mysql-5.0.27
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer " CXXFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-MysqlServerCommunity --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=latin1 --with-collation=latin1_swedish_ci --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam


配置成功会提示:

MySQL has a Web site at http://www.mysql.com/ which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.

Remember to check the platform specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.

Thank you for choosing MySQL!

# make
编译的时间可能会比较长,毕竟优化的比较厉害。

# make install

编译安装完成后执行后续操作:
# useradd mysql //添加 mysql 用户
# cd /usr/local/mysql
# bin/mysql_install_db --user=mysql
# chown -R root:mysql . //设置权限,注意后面有一个 "."
# chown -R mysql /var/lib/mysql //设置 mysql 目录权限
# chgrp -R mysql . //注意后面有一个 "."
# cp share/mysql/my-huge.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld //开机自动启动 mysql。
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# /etc/rc.d/init.d/mysqld start //启动 MySQL
# bin/mysqladmin -u root password "password_for_root"
# service mysqld stop //关闭 MySQL

8. 编译安装 Apache
# cd /usr/local/src
# wget http://www.ip97.com/apache.org/httpd/httpd-2.2.3.tar.gz
# tar zxvf httpd-2.2.3.tar.gz
# cd httpd-2.2.3
# cd srclib , 依次安装apr和apr-util
# cd apr
# ./configure --prefix=/usr/local/apr --enable-threads --enable-other-child USE=ipv6
# make
# make install
# cd ../apr-util
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ --with-mysql=/usr/local/mysql
# make
# make install
# cd ..
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --enable-deflate=shared --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-static-support --enable-static-htpasswd --enable-static-htdigest --enable-static-rotatelogs --enable-static-logresolve --enable-static-htdbm --enable-static-ab --enable-static-checkgid --disable-cgid --disable-cgi --with-mpm=worker --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-mysql=/usr/local/mysql

# make
# make install
# echo '/usr/local/apache2/bin/apachectl start ' >> /etc/rc.local //将 apachectl 的调用加入到你的系统启动文件中。

注解:
./configure //配置源代码树
--prefix=/usr/local/apache2 //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录。
--enable-module=so //打开 so 模块,so 模块是用来提 DSO 支持的 apache 核心模块
--enable-deflate=shared //支持网页压缩
--enable-expires=shared //支持 HTTP 控制
--enable-rewrite=shared //支持 URL 重写
--enable-cache //支持缓存
--enable-file-cache //支持文件缓存
--enable-mem-cache //支持记忆缓存
--enable-disk-cache //支持磁盘缓存
--enable-static-support //支持静态连接(默认为动态连接)
--enable-static-htpasswd //使用静态连接编译 htpasswd - 管理用于基本认证的用户文件
--enable-static-htdigest //使用静态连接编译 htdigest - 管理用于摘要认证的用户文件
--enable-static-rotatelogs //使用静态连接编译 rotatelogs - 滚动 Apache 日志的管道日志程序
--enable-static-logresolve //使用静态连接编译 logresolve - 解析 Apache 日志中的IP地址为主机名
--enable-static-htdbm //使用静态连接编译 htdbm - 操作 DBM 密码数据库
--enable-static-ab //使用静态连接编译 ab - Apache HTTP 服务器性能测试工具
--enable-static-checkgid //使用静态连接编译 checkgid
--disable-cgid //禁止用一个外部 CGI 守护进程执行CGI脚本
--disable-cgi //禁止编译 CGI 版本的 PHP
--disable-userdir //禁止用户从自己的主目录中提供页面
--with-mpm=worker // 让apache以worker方式运行
--enable-authn-dbm=shared // 对动态数据库进行操作。Rewrite时需要。

将apache设置成开机自启动:

在/etc/rc.d/rc.local文件中加入一行
/usr/local/apache/bin/apachectl start
这样每次重新启动系统以后,apache也会随系统一起启动.
或者
# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)
# chkconfig: 2345 10 90
# description: Activates/Deactivates Apache Web Server
最后,运行chkconfig把Apache添加到系统的启动服务组里面:
# chkconfig --add httpd
# chkconfig httpd on


9、编译php 5.2.0

# cd /usr/local/src
# wget http://cn.php.net/get/php-5.2.0.tar.gz/from/this/mirror
# tar zxvf php-5.2.0.tar.gz
# cd php-5.2.0

# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib-dir --with-bz2 --with-tiff-dir --with-libxml-dir=/usr/local/libxml2 --with-gd=/usr/local/gd2 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-iconv --disable-ipv6 --enable-static --enable-soap --enable-sockets

# make && make install

10 、整合apache 与php
# vi /usr/local/apache2/conf/httpd.conf
在最后一行加上:
AddType application/x-httpd-php .php

查找:(设置 WEB 默认文件)
DirectoryIndex index.html
替换为:
DirectoryIndex index.php index.html index.htm //在 WEB 目录不到默认文件,httpd 就会执行 /var/www/error/noindex.html

找到这一段:
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride none

更改为AllowOverride all
允许apache rewrite

保存httpd.conf,退出。

# /usr/local/apache2/bin/apachectl restart //重启 Apache

11. 查看确认 L.A.M.P 环境信息
# echo ' ' > /home/www/phpinfo.php
# chmod 755 /home/www/phpinfo.php
用浏览器打开 http://127.0.0.1/phpinfo.php
# echo ' ' > /home/www/testdb.php
# chmod 755 /home/www/testdb.php
# service mysqld start
用浏览器打开 http://127.0.0.1/testdb.php
检查 phpinfo 中的各项信息是否正确。


12、安装 Zend Optimizer
# cd /usr/local/src
# wget http://downloads.zend.com/optimizer/...21-i386.tar.gz
# tar xzvf ZendOptimizer-3.2.0-linux-glibc21-i386.tar.gz
# ./ZendOptimizer-3.2.0-linux-glibc21-i386/install.sh
按照它的提示一步步进行就行了。

13、安装eaccelerator // eaccelerator是php的加速软件,使用后php的执行效率会有较大幅度的提升。目前eaccelerator 0.9.5已经和ZendOptimizer-3.2.0能够很好兼容啦。不过我个人觉得,ZendOptimizer-3.2.0没有加速的功能,只不 过起到了执行zend加密的作用而已。闲话不多说了,大家有兴趣的。可以去google下。

# cd /usr/local/src
# wget http://bart.eaccelerator.net/source/...-0.9.5.tar.bz2
# tar -jxvf eaccelerator-0.9.5.tar.bz2
# cd eaccelerator-0.9.5

export PHP_PREFIX="/usr/local/php"
$PHP_PREFIX/bin/phpize //指定一下php的目录

# ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config // 设置

# make & make install

编译安装后我们会看到屏幕提示的eaccelerator.so所在的目录,比如我得到的是/usr/local/php/lib/php/extensions/no-debug-zts-20060613/eaccelerator.so,记住这个路径,待会要用到。

修改php.ini(安装完zend之后,php.ini存放于/usr/local/Zend/etc)
在文件最后,zend之前,注意,这部分内容务必放在zend之前,不然可能会出现不可预期的服务器问题。添加下列信息:

extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

解释:
zend_extension 是安装完程序自动指示给我们的
eaccelerator.shm_size="32" 缓存大小单位MB
eaccelerator.cache_dir="/tmp/eaccelerator" 缓存路径

# mkdir /tmp/eaccelerator // 建立目录
# chmod 0777 /tmp/eaccelerator // 修改目录属性

最后重新启动apachectl

重启apache,phpinfo显示:
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
with eAccelerator v0.9.5, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
with Zend Extension Manager v1.0.11, Copyright (c) 2003-2006, by Zend Technologies
with Zend Optimizer v3.2.0, Copyright (c) 1998-2006, by Zend Technologies

也会有eAccelerator的具体信息。





经过这几个步骤,我们的LAMP服务器就环境基本建立成功啦。感觉上也不是很难,是吧?
当然对于打造基于动态IP的网站,以上的信息还不够,还有建立ADSL拨号,NAT,动态域名更新,防火墙,安装论坛程序等一系列的工作,我会慢慢地写出自己的心得与大家分享。毕竟这对我来说,也是个学习的过程。