简单LAMP的实现

LAMP Linux Apache Mysql PHP 这四款自由软件首字母的缩写,这四款开源软件虽然不是专门设计用来一起工作的,但由于开源软件的低廉价格和表现出的不俗实力,越来越受到人们的青睐。随着开源潮流的蓬勃发展, LAMP 已经与 J2EE 和在 web 应用程序市场形成鼎立之势。

 

废话不多说,下面介绍一下简单 LAMP 的具体实现过程:

 

Linux上的Mysql安装及配置
1. Mysql 的数据目录创建单独的逻辑卷
InBlock.gifmkdir /mydata
InBlock.gif#创建存放数据的目录
InBlock.giffdisk /dev/sda
InBlock.gif#新建分区属性为8e
InBlock.gifpvcreate /dev/sda5
InBlock.gifvgcreate myvg /dev/sda5
InBlock.giflvcreate –L 4G –n mylv myvg
InBlock.gifmke2fs –L MYDATA –j /dev/myvg/mylv  
InBlock.gifvim /etc/fstab    
InBlock.gifLABEL=MYDATA                    /mydata                ext3        defaults                0 0    
InBlock.gif#加入这一行实现开机自动挂载
InBlock.gifmount –a
InBlock.gifmkdir /mydata/data

2. 创建mysql用户
InBlock.gifgroupadd –g 3306 mysql
InBlock.gifuseradd –g 3306 –u 3306 –M –s /sbin/nologin
InBlock.gifchown –R mysql:mysql /mydata

3. 解压mysql-5.5.15-linux2.6-i686.tar.gz,使用其中脚本进行初始化
InBlock.giftar xvf mysql-5.5.15-linux2.6-i686.tar.gz –C /usr/local/
InBlock.gifcd /usr/local
InBlock.gifln –vs mysql-5.5.15-linux2.6-i686 mysql
InBlock.gifcd /usr/local/mysql
InBlock.gifchown –R mysql:mysql ./*
InBlock.gifscripts/mysql_install_db --user=mysql --datadir=/mydata/data
InBlock.gif#初始化Mysql用户和数据路径
InBlock.gifchown –R root ./*
InBlock.gif#再将属主交还给root

4. 创建mysqld服务
InBlock.gifcp /usr/local/mysql/support-files/mysql.server    /etc/init.d/mysqld
InBlock.gif#导入服务脚本
InBlock.gifchkconfig –add mysqld

5. 创建Mysql的配置文件
InBlock.gifcp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
InBlock.gifvim /etc/my.cnf
InBlock.gif#找到[mysqld]下面的内容添加
InBlock.gifdatadir= /mydata/data

6. 加入mysql命令
InBlock.gifvim /etc/profile
InBlock.gifPATH=$PATH:/usr/loca/mysql/bin
InBlock.gif#添加这一行

7. 加入mysql库文件的路径
InBlock.gifvim /etc/ld.so.conf.d/mysql.conf
InBlock.gif/usr/local/mysql/lib
InBlock.gif#写入这一行

8. 设置头文件
InBlock.gifln –vs /usr/local/mysql/include /usr/include/mysql

9. 添加man文件(如果你以后不想使用mysql的man文件不添加也是可以的)
InBlock.gifvim /etc/man.conf
InBlock.gifMANPATH /usr/local/mysql/man
InBlock.gif#添加这一行
service mysql start
#启动mysql服务
netstat –tnl
#查看3306端口处于监听状态,说明已经配置成功。

编译安装Apache
 1.解压httpd-2.2.19.tar.bz2软件包
InBlock.giftar xvf httpd-2.2.19.tar.bz2

2. 设置安装规则、编译、安装
InBlock.gif./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable=ssl --enable-modules=most --enable-mods-share=most --enable-so
InBlock.gifmake
InBlock.gifmake install

3. 制作服务启动脚本

InBlock.gifvim /etc/init.d/httpd
InBlock.gif#!/bin/bash
InBlock.gif. /etc/rc.d/init.d/functions
InBlock.gif
InBlock.gif if [ -f /etc/sysconfig/httpd ]; then
InBlock.gif                . /etc/sysconfig/httpd
InBlock.giffi
InBlock.gif
InBlock.gifHTTPD_LANG=${HTTPD_LANG- "C"}
InBlock.gif
InBlock.gifINITLOG_ARGS=""
InBlock.gif
InBlock.gifapachectl=/usr/local/apache/bin/apachectl
InBlock.gifhttpd=${HTTPD-/usr/local/apache/bin/httpd}
InBlock.gif#注意此上两行中路径要与你的安装路径一致
InBlock.gifprog=httpd
InBlock.gifpidfile=${PIDFILE-/var/run/httpd.pid}
InBlock.giflockfile=${LOCKFILE-/var/ lock/subsys/httpd}
InBlock.gifRETVAL=0
InBlock.gif
InBlock.gifstart() {
InBlock.gif                echo -n $ "Starting $prog: "
InBlock.gif                LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
InBlock.gif                RETVAL=$?
InBlock.gif                echo
InBlock.gif                [ $RETVAL = 0 ] && touch ${lockfile}
InBlock.gif                 return $RETVAL
InBlock.gif}
InBlock.gif
InBlock.gifstop() {
InBlock.gif                echo -n $ "Stopping $prog: "
InBlock.gif                killproc -p ${pidfile} -d 10 $httpd
InBlock.gif                RETVAL=$?
InBlock.gif                echo
InBlock.gif                [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
InBlock.gif}
InBlock.gif
InBlock.gifreload() {
InBlock.gif        echo -n $ "Reloading $prog: "
InBlock.gif         if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/ null; then
InBlock.gif                RETVAL=$?
InBlock.gif                echo $ "not reloading due to configuration syntax error"
InBlock.gif                failure $ "not reloading $httpd due to configuration syntax error"
InBlock.gif         else
InBlock.gif                killproc -p ${pidfile} $httpd -HUP
InBlock.gif                RETVAL=$?
InBlock.gif        fi
InBlock.gif        echo
InBlock.gif}
InBlock.gif
InBlock.gif case "$1" in
InBlock.gif    start)
InBlock.gif                start
InBlock.gif                ;;
InBlock.gif    stop)
InBlock.gif                stop
InBlock.gif                ;;
InBlock.gif    status)
InBlock.gif                status -p ${pidfile} $httpd
InBlock.gif                RETVAL=$?
InBlock.gif                ;;
InBlock.gif    restart)
InBlock.gif                stop
InBlock.gif                start
InBlock.gif                ;;
InBlock.gif    condrestart)
InBlock.gif                 if [ -f ${pidfile} ] ; then
InBlock.gif                                stop
InBlock.gif                                start
InBlock.gif                fi
InBlock.gif                ;;
InBlock.gif    reload)
InBlock.gif                reload
InBlock.gif                ;;
InBlock.gif    graceful|help|configtest|fullstatus)
InBlock.gif                $apachectl $@
InBlock.gif                RETVAL=$?
InBlock.gif                ;;
InBlock.gif    *)
InBlock.gif                echo $ "Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
InBlock.gif                exit 1
InBlock.gifesac
InBlock.gif
InBlock.gifexit $RETVAL
InBlock.gifvim /etc/httpd/httpd.conf
PidFile "/var/run/httpd.pid"
#添加这一行
chkconfig --add httpd
service httpd start
#启动httpd服务
netstat -tnl
#查看80端口处于监听状态,说明已经配置成功。
PHP的编译安装
1. 解压php-5.3.6.tar.bz2软件包
InBlock.giftar xvf php-5.3.6.tar.bz2

2. 设置安装选项、编译、安装
InBlock.gif./configure --prefix=/usr/local/php5 --sysconfdir=/etc/php --enable-mbstring --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-libmcrypt --with-gd --with-mysqli
InBlock.gif
InBlock.gifmake    
InBlock.gifmakeinstall

3. 加入命令路径
InBlock.gifvim /etc/profile
InBlock.gifPATH=$PATH:/usr/loca/php5/bin
InBlock.gif#添加这一行

4. 加入php库文件路径
InBlock.gifvim /etc/ld.so.conf.d/php.conf
InBlock.gif/usr/local/php5/lib

5. 创建php配置文件
InBlock.gifcp php.ini-production /usr/local/php5/lib/php.ini
InBlock.gif#注意php.ini-production文件位于php-5.3.6.tar.bz2的解压目录

6.  修改httpd服务配置文件使其支持php
InBlock.gifvim /etc/httpd/httpd.conf
InBlock.gifAddType Application/x-httpd-php .php
InBlock.gifAddType Application/x-httpd-php-source .phps
InBlock.gif#添加这两行在AddType xxxxxx行附近位置
InBlock.gifDirectoryIndex index.php index.html
InBlock.gif#添加index.php
InBlock.gif
InBlock.gifservice httpd restart
InBlock.gif#重启httpd服务
到此简单的LAMP就配置成功了

LAMP的测试

为了测试LAMP,作者在这里选用了
wordpress-3.0.4-zh_CN.zip
phpMyAdmin-3.4.3.2-all-languages.tar.bz2

要使这两个网站都能在新搭建的LAMP运行,就得虚拟主机来实现。
这里作者选用基于主机名的虚拟主机(基于ip的虚拟主机过于消耗ipv4资源;基于端口的虚拟主机只有一个主页能使用默认80端口)

使用于主机名的虚拟主机配置网站:
1. 配置虚拟主机
InBlock.gifvim /etc/httpd/httpd.conf    
InBlock.gif#DocumentRoot "/usr/local/apache/htdocs"
InBlock.gif#把此行注释
InBlock.gifInclude /etc/httpd/extra/httpd-vhosts.conf
InBlock.gif#把此行启用
InBlock.gif
InBlock.gifvim /etc/httpd/extra/httpd-vhosts.conf
InBlock.gif#添加内容
InBlock.gif<VirtualHost *:80>
InBlock.gifDocumentRoot "/web/phpmyadmin"    #网站所在目录
InBlock.gifServerName "phpmyadmin.redhat_hu.com" #虚拟主机名
InBlock.gifErrorLog "logs/phpmyadmin.error"    #错误日志
InBlock.gifCustomLog "logs/phpmyadmin.access" combined #访问日志
InBlock.gif</VirtualHost>
InBlock.gif
InBlock.gif<Directory "/web/phpmyadmin">
InBlock.gifOrder allow,deny
InBlock.gifAllow from all
InBlock.gif</Directory>
InBlock.gif#设置访问控制为允许所有
InBlock.gif<VirtualHost *:80>
InBlock.gifDocumentRoot "/web/wordpress"
InBlock.gifServerName "wordpress.redhat_hu.com"
InBlock.gifErrorLog "logs/wordpress.error"
InBlock.gifCustomLog "logs/wordpress.access" combined
InBlock.gif</VirtualHost>
InBlock.gif
InBlock.gif<Directory "/web/wordpress">
InBlock.gifOrder allow,deny
InBlock.gifAllow from all
InBlock.gif</Directory>

2.配置网站链接数据库
InBlock.gifmkdir /web/{phpmyadmin,wordpress}
InBlock.gif#创建网页所在目录 (此目录最好挂载逻辑卷,具体步骤见上文)
InBlock.gif
InBlock.giftar xvf phpMyAdmin-3.4.3.2-all-languages.tar.bz2
InBlock.gifcp -R phpMyAdmin-3.4.3.2/* /web/phpmyadmin
InBlock.gifunzip wordpress-3.0.4-zh_CN.zip
InBlock.gifcp -R wordpress/* /web/ wordpress
InBlock.gif#将两个网站文件放入对应文件夹
InBlock.gif
InBlock.gifmv /web/wordpress/wp-config-simple.php /web/wordpress/wp-config.php
InBlock.gifvim /web/wordpress/wp-config.php
InBlock.gifdefine('DB_NAME', 'wordpress');
InBlock.gifdefine('DB_USER', 'root');
InBlock.gifdefine('DB_PASSWORD', 'redhat');
InBlock.gif#设置对应数据库名登录用户以及密码
InBlock.gif
InBlock.gif
InBlock.gif在mysql中创建数据库
InBlock.gifmysql
InBlock.gif#未设置密码可以直接登录
InBlock.gifmysql>CREATE DATABASE wordpress;
InBlock.gifmysql>SET PASSWORD FOR root@localhost=PASSWORD('redhat');
InBlock.gifmysql>FLUSH PRIVILEGES;
InBlock.gif#设置密码redhat
InBlock.gifmysql>quit


全部配置完成
打开浏览器输入网址进行测试。

Ps:由于作者使用的是VMware虚拟机,C:\WINDOWS\system32\drivers\etc\hosts
中加入如下两行实现域名解析。
192.168.0.20 phpmyadmin.redhat_hu.com
192.168.0.20 wordpress.redhat_hu.com