因为篇幅的原因,这一篇博客写起来应该很庞大,很臃肿,难免会让人失去读它的兴趣,所以我打算分成块来讲。

本篇:主要将LAMP搭建起来。

注:这套架构并不是本人基于企业应用而做的,可能只是一个企业对WEB架设的一个雏形。有什么写的不好的地方,还望有经验的大鸟们能提点。

   NFS(Network FileSystem)网络文件系统,用于实现网络文件共享(本文中作为web页面信息存储)

   NFS能够保证WEB服务的稳定性,文中会用到两个NFS服务器来保证其稳定性。

LAMP:Linux + apache + mysql + php(本文采用手动编译安装)

   用于搭建web服务。apache做web服务器,php用来解析动态站点,mysql用来和php实现表单,以及登录式交互。

iptables:和netfilter组成防火墙,用来拒绝外网用户对除web访问之外的访问。

SElinux:Security-Enhanced Linux,安全的linux操作系统,需要对所有文件进行打标操作,设置了更严密的权限机制。

注意:本文大多数软件都是手动编译安装的,所以我们应该事先安装好编译所需要的开发包组。

      一般包括三个包组:“Development tools”“Server Platform Developmnent”“Desktop Platform Development”,大家自行安装。

     所有软件都可以在互联网中进入官网去下载。

     本文用到的所有软件版本分别是:

           DNS->bind-9.9.95  apache->httpd-2.4.9  Mysql->mysql-5.5.33

本次实验是将每个服务都分开在不同的服务器上,所以,要大概准备6.7个虚拟机吧。

1、首先搭建DNS服务:

   linux上提供DNS服务的软件是BIND,本文用BIND9。其生成的服务叫named。

[root@station81 ~]# ls   // /root 目录下有我们用的软件 bind-9.9.5.tar.gzanaconda-ks.cfg    apr-util-1.5.3          bind-9.9.5.tar.gz    install.log         memcache-2.2.7.tgz   phpMyAdmin-4.0.5-all-languages.zipapr-1.5.0          apr-util-1.5.3.tar.bz2  httpd-2.4.9          install.log.syslog  mysql-5.6.13.tar.gz  xcache-3.0.3.tar.bz2apr-1.5.0.tar.bz2  bind-9.9.5              httpd-2.4.9.tar.bz2  iptables-server     php-5.4.26.tar.bz2[root@station81 ~]# tar xf bind-9.9.5.tar.gz  //解压缩[root@station81 ~]# cd bind-9.9.5[root@station81 bind-9.9.5]# ./configure --prefix=/usr/local/bind9 --sysconfdir=/etc/named/ --enable-threads --enable-epoll --disable-chroot//--enable-threads 支持多核处理器并发处理  --enable-epoll 支持多路复用IO接口,能提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率  --disaable-chroot 不支持根转换,不支持“伪根”[root@station81 bind-9.9.5]# make && make install  //编译和安装

  编译安装完之后,就要为其提供配置文件和服务脚本了(一般的RPM包安装,会自动在对应位置生成其配置文件和服务脚本)

   先来写配置脚本:/etc/named/named.conf        

[root@station81 bind-9.9.5]# vim /etc/named/named.conf

options{        directory "var/named";              //全局指定程序位置        PidFile "/usr/local/bind9/var/run"; //指定PID文件目录        };zone "."IN {        type hint;        file "named.ca";        };zone "localhost" IN{        type master;        file "named.localhost";        allow-transfer { none; };        };zone "0.0.127.in-addr.arpa" IN{        type master;        file "named.loopback";        allow-transfer { none; };        };

写好配置文件后,就要去/etc/named/下面去创建区域数据文件了。named.ca   named.localhost  named.loopback

分别写3个区域数据文件:

   对应“.”的区域配置文件,我们可以用 dig -t NS . @172.16.0.1 > /var/named/name.ca

   localhost的区域配置文件:                            

[root@station81 named]# vim /var/named/named.localhost$TTL 86400@       IN      SOA     localhost.      admin.localhost. (                        2014033001  //序列号,不能超过10位,通常用日期表示                        2H    //刷新时间,即每隔多久去主服务器上检查一次                        10M    //重试时间,小于刷新时间                        7D    //过期时间,联系不到主服务器,从服务器过期时间                        1D )    //否定答案缓存时间        IN      NS      localhost. //域名服务器localhost.      IN      A       127.0.0.1 //域名服务器对应的IP地址

   loopback的区域配置文件:(反向解析

[root@station81 named]# vim /var/named/named.loopback$TTL 86400@       IN      SOA     localhost.      admin.localhost. (                        2014033001                        2H                        15M                        7D                        1D )        IN      NS      localhost.        IN      PTR     localhost.

写完配置脚本,下来就要写服务脚本了

   配置文件写在 /etc/rc.d/init.d/named

#!/bin/bash## description: named daemon# chkconfig: - 25 80#pidFile=/usr/local/bind9/var/run/named.pidlockFile=/var/lock/subsys/namedconfFile=/etc/named/named.conf[ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functionsstart() {if [ -e $lockFile ]; then        echo "named is already running..."        exit 0fiecho -n "Starting named:"daemon --pidfile "$pidFile" /usr/local/bind9/sbin/named -u named -c "$confFile"RETVAL=$?echoif [ $RETVAL -eq 0 ]; then        touch $lockFile        return $RETVALelse        rm -f $lockFile $pidFile        return 1fi}stop() {if [ ! -e $lockFile ]; then        echo "named is stopped."        exit 0fiecho -n "Stopping named:"killproc namedRETVAL=$?echoif [ $RETVAL -eq 0 ];then        rm -f $lockFile $pidFile        return 0else        echo "Cannot stop named."        failure        return 1        fi}restart() {stopsleep 2start}reload() {echo -n "Reloading named: "killproc named -HUP#killall -HUP namedRETVAL=$?echoreturn $RETVAL}status() {if pidof named &> /dev/null; then    echo -n "named is running..."    success    echoelse    echo -n "named is stopped..."    success    echo    fi}usage() {echo "Usage: named {start|stop|restart|status|reload}"}case $1 instart)    start ;;stop)    stop ;;restart)    restart ;;status)    status ;;reload)    reload ;;*)    usage    exit 4    ;;esac

写完配脚本和服务脚本,下来就改配置环变量了

[root@station81 sbin]# vim /etc/profile.d/named.shexport PATH=/usr/local/bind9/sbin/:/usr/local/bind9/bin:$PATH

然后重读配置文件:  #source /etc/profile.d/named.sh


2.接着手动编译安装 apache      (apache安装依赖三个包 apr、apr-util、pcre-devel)

安装 apr

lftp 172.16.0.1:/pub/Sources/sources/httpd> bye[root@station136 ~]# lsanaconda-ks.cfg  apr-1.5.0.tar.bz2  apr-util-1.5.3.tar.bz2  httpd-2.4.9.tar.bz2  install.log  install.log.syslog[root@station136 ~]# tar xf apr-1.5.0.tar.bz2[root@station136 ~]# cd apr-1.5.0[root@station136 apr-1.5.0]# ./configure --prefix=/usr/local/apr[root@station136 apr-1.5.0]# make && make install

安装apr-util

[root@station136 ~]# lsanaconda-ks.cfg  apr-1.5.0  apr-1.5.0.tar.bz2  apr-util-1.5.3.tar.bz2  httpd-2.4.9.tar.bz2  install.log  install.log.syslog[root@station136 ~]# tar xf apr-util-1.5.3.tar.bz2[root@station136 ~]# cd apr-util-1.5.3[root@station136 apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@station136 ~]#make && make install

安装pcre-devel   linux安装光盘就有

[root@station136 apr-util-1.5.3]# yum -y install pcre-devel

下来编译安装httpd

[root@station136 ~]# tar xf httpd-2.4.9.tar.bz2[root@station136 ~]# cd httpd-2.4.9[root@station136 httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/ --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpm-shared=most --with-mpm=event[root@station136 httpd-2.4.9]# make && make installhe

和上面安装DNS一样,需要给配置文件和服务脚本,在这里就不给出详细的服务脚本文件了。位置在 /etc/rc.d/init.d/httpd

当然写完之后不要忘记给执行权限(因为默认是644,是没有执行权限的)chmod u+x /etc/rc.d/init.d/httpd

然后把服务加入服务列表中 chkconfig --add httpd

安装完一个软件记得要测试

[root@station136 ~]# chkconfig --add httpd[root@station136 ~]# chkconfig httpd on[root@station136 ~]# chkconfig | grep httpdhttpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off[root@station136 ~]# service httpd startStarting httpd:

然后可以进浏览器中输入这个开启httpd主机的ip地址,就可以看到效果了

wKioL1M3vDaR-Tx-AABftGQnaR8939.jpg


3.下来就到我们的Mysql的编译安装了

对了Mysql安装前提工作比较多,首先你要为他创建一个逻辑卷(LVM)专门用来存放数据库文件(表之类的关系文件),还要给他一个RAID做专门的数据文件。

那我们就为他创建一个逻辑卷下面,这还不算完,mysql还需要依赖mysql这个用户来启用和安装mysql。

[root@station138 ~]# cat /proc/partitions  查看分区major minor  #blocks  name   8        0  125829120 sda   8        1     204800 sda1   8        2   62914560 sda2 253        0   20971520 dm-0 253        1    2097152 dm-1 253        2   10485760 dm-2 253        3   20971520 dm-3//我们的机器上只有两个分区,那就在sda上面再创建一个10G的分区给mysql独用,这家伙是费事。[root@station138 ~]# fdisk /dev/sdaWARNING: DOS-compatible mode is deprecated. It's strongly recommended to         switch off the mode (command 'c') and change display units to         sectors (command 'u').Command (m for help): p   //查看磁盘分区Disk /dev/sda: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00065757   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *           1          26      204800   83  LinuxPartition 1 does not end on cylinder boundary./dev/sda2              26        7859    62914560   8e  Linux LVMCommand (m for help):
Command (m for help): n     // n 表示new,新建一个分区Command action   e   extended   p   primary partition (1-4)pPartition number (1-4): 3   //这个是序号,表示第三个分区First cylinder (7859-15665, default 7859):Using default value 7859Last cylinder, +cylinders or +size{K,M,G} (7859-15665, default 15665): +10G  // +10G 表示空间为10GCommand (m for help): t  // t 表示修改磁盘类型Partition number (1-4): 3Hex code (type L to list codes): 8e  // 8e 就表示作为 LVM用Changed system type of partition 3 to 8e (Linux LVM)Command (m for help): wThe partition table has been altered!

当然此时新建的分区还没有被系统所认到,来看看是不是没有认到。

[root@station138 ~]# cat /proc/partitionsmajor minor  #blocks  name   8        0  125829120 sda   8        1     204800 sda1   8        2   62914560 sda2 //还是只有这两个,显然还没有被读取。当然我们有办法 253        0   20971520 dm-0 253        1    2097152 dm-1 253        2   10485760 dm-2 253        3   20971520 dm-3

我们可以让操作系统强制去重读这个磁盘去识别出这个分区。可以反复试用如下命令:

kpartx -af /dev/sda      partx -a /dev/sda

[root@station138 ~]# kpartx -af /dev/sda    //表示强制去读sda磁盘device-mapper: reload ioctl on sda1 failed: Invalid argumentcreate/reload failed on sda1device-mapper: reload ioctl on sda2 failed: Invalid argumentcreate/reload failed on sda2device-mapper: reload ioctl on sda3 failed: Invalid argumentcreate/reload failed on sda3[root@station138 ~]# partx -a /dev/sda   //这条也类似BLKPG: Device or resource busyerror adding partition 1BLKPG: Device or resource busyerror adding partition 2[root@station138 ~]# kpartx -af /dev/sdadevice-mapper: reload ioctl on sda1 failed: Invalid argumentcreate/reload failed on sda1device-mapper: reload ioctl on sda2 failed: Invalid argumentcreate/reload failed on sda2device-mapper: reload ioctl on sda3 failed: Invalid argumentcreate/reload failed on sda3[root@station138 ~]# partx -a /dev/sdaBLKPG: Device or resource busyerror adding partition 1BLKPG: Device or resource busyerror adding partition 2BLKPG: Device or resource busyerror adding partition 3[root@station138 ~]# cat /proc/partitionsmajor minor  #blocks  name   8        0  125829120 sda   8        1     204800 sda1   8        2   62914560 sda2   8        3   10489446 sda3   //反复两次就认出来了,不错吧。 253        0   20971520 dm-0 253        1    2097152 dm-1 253        2   10485760 dm-2 253        3   20971520 dm-3

接着我说下下来创建逻辑卷的余下步骤,然后一并将创建时的代码贴出来。

要创建逻辑卷,首先要创建“物理卷”,之后将“物理卷”加入到“卷组”之中,最后才创建“逻辑卷”

[root@station138 ~]# pvcreate /dev/sda3 //创建物理卷  Physical volume "/dev/sda3" successfully created[root@station138 ~]# vgcreate myvg /dev/sda3 //创建卷组叫 myvg  Volume group "myvg" successfully created[root@station138 ~]# lvcreate -L 10G -n mydata myvg //创建逻辑卷叫 mydata  Logical volume "mydata" created创建完成逻辑卷后,下来进行格式化[root@station138 ~]# mke2fs -t ext4 /dev/myvg/mydata  //格式化逻辑卷为ext4格式mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks655360 inodes, 2621440 blocks131072 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=268435456080 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632Writing inode tables: done                      Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 27 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.

创建完成后,我们去创建一个目录去挂载他,当然我们可以直接写到配置文件中,让他开机自动挂载并且永久有效。

[root@station138 ~]# mkdir /mydata  //创建一个目录用来挂载[root@station138 ~]# vim /etc/fstab  //写入开机挂载文件,开机自动挂载,永久有效/dev/mapper/vg0-root    /                       ext4    defaults        1 1UUID=60104fc5-1d73-44a5-98a1-61c0b03144b7 /boot                   ext4    defaults        1 2/dev/mapper/vg0-usr     /usr                    ext4    defaults        1 2/dev/mapper/vg0-var     /var                    ext4    defaults        1 2/dev/mapper/vg0-swap    swap                    swap    defaults        0 0tmpfs                   /dev/shm                tmpfs   defaults        0 0devpts                  /dev/pts                devpts  gid=5,mode=620  0 0sysfs                   /sys                    sysfs   defaults        0 0proc                    /proc                   proc    defaults        0 0/dev/cdrom              /media/cdrom            iso9660 defaults        0 0/dev/myvg/mydata        /mydata                 ext4    defaults        0 0  // 这个就是我们要挂载的,按照上面的格式写[root@station138 ~]# mount -a  //让系统按照/etc/fstab为基准挂载[root@station138 ~]# cd /mydata/  //挂载完成后[root@station138 mydata]# ls    //查看已经挂载成功了lost+found

刚说了mysql编译安装已经运行,都要基于mysql用户来完成,因为我们系统上默认是没有这个用户的,所以我们还需要手动去创建。

[root@station138 mydata]# groupadd -r mysql  //创建mysql系统组[root@station138 mydata]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql //创建muysql用户为系统用户,属于mysql组,不自动创建家目录,用户没有登录系统的权限,指定mysql所在家目录[root@station138 mydata]# chown -R mysql:mysql /mydata/data //此时/mydata/data是root用户,root组的,需要讲其属主和属组都改为mysql,让mysql享有全部可拥有的权限。

终于把准备工作做完了,下来正式要编译安装mysql了,这家伙真的特别费事情,不经常去编译安装的话,过不了多久就会忘记了。

对了忘记讲了,我们这用到的mysql的不是源码包,而是二进制的包,当然安装起来就和源码的差一点了,也就是说此时mysql相当于一个绿色软件,直接放在应用程序目录下,进行一步简单的编译就可以安装使用了。

[root@station138 ~]# lsanaconda-ks.cfg  install.log  install.log.syslog  mysql-5.5.33.tar.gz[root@station138 ~]# tar xf mysql-5.5.33.tar.gz -C /usr/local //-C 指定解压到/usr/local下[root@station138 ~]# cd /usr/local/mysql-5.5.33/[root@station138 mysql-5.5.33]# cd ..[root@station138 local]# lsbin  etc  games  include  lib  lib64  libexec  mysql-5.5.33  sbin  share  src[root@station138 local]# ln -sv mysql-5.5.33 mysql //为 mysql-5.5.33做软链接mysql,只是为了以后方便使用而已。`mysql' -> `mysql-5.5.33'

[root@station138 local]# cd mysql //进入mysql中[root@station138 mysql]# ll //发现我们下载下来的mysql文件属主属组很奇怪,那是因为那是制作者自己用的用户打包的,//我们系统上没有他系统上的那种用户,所以,要改他们的属组和属主为mysql才能为我们的mysql所使用。total 640drwxr-xr-x  2 7161 wheel   4096 Jul 15  2013 BUILD  //文件属主为7161.属组为wheel,不正常吧,没关系,下面我们直接改成mysql-rw-r--r--  1 7161 wheel   8528 Jul 15  2013 BUILD-CMAKEdrwxr-xr-x  2 7161 wheel   4096 Jul 15  2013 clientdrwxr-xr-x  4 7161 wheel   4096 Jul 15  2013 cmake-rw-r--r--  1 7161 wheel  14051 Jul 15  2013 CMakeLists.txt[root@station138 mysql]# chown -R mysql:mysql . //把当前目录的全部文件改为属主和属组都为mysql[root@station138 mysql]# lltotal 640drwxr-xr-x  2 mysql mysql   4096 Jul 15  2013 BUILD //如此就改回来了。-rw-r--r--  1 mysql mysql   8528 Jul 15  2013 BUILD-CMAKEdrwxr-xr-x  2 mysql mysql   4096 Jul 15  2013 client
[root@station138 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data  //--user指定mysql的用户 --datadir顾名思义是数据库的目录了Installing MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your system[root@station138 mysql]# chown -R root . //安装完成后,就要讲文件的属主改为root用户的了。

接着给mysql提供配置文件了。配置文件在mysql目录中的 support-file目录中有提供。

[root@station138 mysql]# cp support-files/my-large.cnf /etc/my.cnfcp: overwrite `/etc/my.cnf'? y[root@station138 mysql]# vim /etc/my.cnf[client]#password       = your_passwordport            = 3306socket          = /tmp/mysql.sockdatadir = /mydata/data //手动添加# Try number of CPU's*2 for thread_concurrencythread_concurrency = 4 //默认是8.改为自己cpu核心数的2倍

配置好配置文件,下来肯定是服务脚本了

[root@station138 mysql]# cd support-files/mysql.server /etc/rc.d/init.d/mysqld  //复制主程序中的support-files中的mysql-server到服务脚本目录中[root@station138 mysql]# chmod +x /etc/rc.d/init.d/mysqld //然后给执行权限,默认是没有执行权限的。

加入服务列表,启动服务

[root@station138 mysql]# chkconfig --add mysqld[root@station138 mysql]# chkconfig mysqld on[root@station138 mysql]# service mysqld startStarting MySQL                                             [  OK  ][root@station138 mysql]# mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.5.33-log MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;   //显示所有的数据库+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+4 rows in set (0.03 sec)mysql>

到此为止,数据库已经安装完成了。


4.PHP编译安装:

我们让PHP在为前端WEB服务去解释PHP网页文件的传输过程进行加密传送,而加入mcrypt加密扩展,除了PHP自带的几种加密函数,功能更全面的PHP加密扩展库McryptMhash

   libmcrypt-devel-2.5.7-5.el5.i386.rpm       mhash-devel-0.9.9-1.el5.centos.i386.rpm


2、LAMP的实现:

   安装apache实现httpd服务开启web功能