q666123 发表于 2015-8-19 11:04:18

LAMP平台的搭建,以及虚拟主机的使用

  LAMP=Linux+Apache+Mysql+Perl/PHP/Python,本身各自都是独立的软件,组合起来使用就成了我们的LAMP,说白了,它就是集通用、跨平台、高性能、低价格的一种网站解决方案。我们这里以 Linux+Apache+Mysql+PHP/为例子.这里的PHP要做成Apache的模块,则Apache要事先装好,PHP要能够连接Mysql,则要在Mysql的环境中装好PHP的驱动程序,所以Mysql也要提前装。Mysql和Apache这里是不分先后。
  环境说明:




linux
rhel 5.8
三者对应依赖的rpm包


Apache
httpd-2.4.3.tar.bz2

apr-1.4.6.tar.bz2
  apr-util-1.4.1.tar.bz2


pcre-devel


Mysql
mysql-5.5.2.8-linux2.6-i868.tar.gz



PHP
php-5.4.8.tar.bz2

libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm


Xcache
xcache-2.0.0.tar.bz2
备注:它是PHP的加速器


  mysql Apache
  php(Xcache)




Development Libraries
Development Tools
X Software Development


  第一:HTTPD编译安装:




configure 选项
选项说明


--prefix
指明安装路径


--sysconfdir
指明配置文件的安装目录


--enable-so
支持模块化方式让apache动态支持共享模块,如果这里不加这项,稍后的PHP就无法以模块的方式加载进来,所以亲,要加上哦!


--enable-ssl   
支持ssl


--enable-cgi
支持cgi功能


--enable-rewrite
支持URL重写


--mpms-shared
当采用的是动态编译的方式的时候定义为--mpms-shared=all所有此平台支持的MPM模块都会被安装,动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。


--with-mpm
分为两种编译方式一种是静态编译,一种是动态编译,指定当前使用哪个mpm,静态编译相对于动态编译,唯一优势就是,静态编译进去的mpm肯定要比以模块的方式加载进去的mpm稳定。(笔者是这么理解的哈)


--with-apr
指明apr的安装路径,arp可以让apache的某些特性在跨平台使用


--with-apr-util
  指明apr-util的安装路径,arp-util本身依赖于arp所以在安装它的时候要指明apr的安装路径,而它本身和apr又被httpd所依赖所以在安装httpd的时候要指明apr和apr-util的安装路径




--with-zlib
支持在将数据发送至客户端之前可以先将数据压缩


--with-pcre
为了让Apache支持perl语言的正则表达式,要启用pcre机制,要支持这种机制需要装入pcre-devel.






  
  细节告知:1、selinux要关闭
  零时生效 setenforce 0
  永久生效 /etc/selinux/config中的SELINUX=permissive
  2、在更改配置文件之前要备份(好习惯)
  3、更改配置文件过后,要重启服务,这样更改的东西才可以生效
  4、httpd出现问题时候查看错误志/usr/local/apache/logs/error_log
  
  安装开发环境


[*]yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development"
  1、依次编译安装arp arp-util(记得手动安装pcre-devel)httpd,另外在安装httpd前要确认本机未安装httpd,如果有要卸载掉。


[*]
[*]tar xf apr-1.4.6.tar.bz2
[*]
[*]cd apr-1.4.6
[*]
[*]./configure --prefix=/usr/local/apr
[*]
[*]make && make install
[*]
[*]
[*]tar xf apr-util-1.4.1.tar.bz2
[*]
[*]cd apr-util-1.4.1
[*]
[*]./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[*]
[*]make && make install
[*]
[*]
[*]yum install pcre-devel
[*]
[*]要养成一个习惯在编译安装的时候要读INSTALL这个文件,它给出了安装的详细说明。
[*]less INSTALL
[*]
[*]tar xf httpd-2.4.3.tar.bz2
[*]
[*]cd httpd-2.4.3
[*]
[*]./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --mpms-shared=all --with-mpm=event --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
[*]
[*]make && make install
  第14行命令less INSTALL的图:

  编译完成后就会生成Apache的控制脚本/usr/local/apache/bin/apachectl但是它有很多我们用到的功能是不支持的,并且这个脚本接受参数的方式以及编译风格都与我们默认的控制脚本是不兼容的。所以不能将其直接复制到/etc/rc.d/ini.d当中当做服务脚本来使用的。所以我们这里提自己提供服务脚本。提供脚本后我们就可以使用“service httpd 参数”控制httpd了!
  2、提供服务脚本
  vim /etc/rc.d/init.d


[*]#!/bin/bash
[*]#
[*]# httpd      Startup script for the Apache HTTP Server
[*]#
[*]# chkconfig: - 85 15
[*]# description: Apache is a World Wide Web server.It is used to serve \
[*]#          HTML files and CGI.
[*]# processname: httpd
[*]# config: /etc/httpd/conf/httpd.conf
[*]# config: /etc/sysconfig/httpd
[*]# pidfile: /var/run/httpd.pid
[*]
[*]# Source function library.
[*]. /etc/rc.d/init.d/functions
[*]
[*]if [ -f /etc/sysconfig/httpd ]; then
[*]      . /etc/sysconfig/httpd
[*]fi
[*]
[*]# Start httpd in the C locale by default.
[*]HTTPD_LANG=${HTTPD_LANG-"C"}
[*]
[*]# This will prevent initlog from swallowing up a pass-phraseprompt if
[*]# mod_ssl needs a pass-phrase from the user.
[*]INITLOG_ARGS=""
[*]# Path to the apachectl script, server binary, and short-form for    messages.
[*]apachectl=/usr/local/apache/bin/apachectl
[*]httpd=${HTTPD-/usr/local/apache/bin/httpd}
[*]prog=httpd
[*]pidfile=${PIDFILE-/var/run/httpd.pid}
[*]lockfile=${LOCKFILE-/var/lock/subsys/httpd}
[*]RETVAL=0
[*]
[*]start() {
[*]      echo -n $"Starting $prog: "
[*]      LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd   $OPTIONS
[*]      RETVAL=$?
[*]      echo
[*]      [ $RETVAL = 0 ] && touch ${lockfile}
[*]      return $RETVAL
[*]}
[*]
[*]stop() {
[*]    echo -n $"Stopping $prog: "
[*]    killproc -p ${pidfile} -d 10 $httpd
[*]    RETVAL=$?
[*]    echo
[*]    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
[*]}
[*]reload() {
[*]    echo -n $"Reloading $prog: "
[*]    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
[*]      RETVAL=$?
[*]      echo $"not reloading due to configuration syntax error"
[*]      failure $"not reloading $httpd due to configuration syntax   error"
[*]    else
[*]      killproc -p ${pidfile} $httpd -HUP
[*]      RETVAL=$?
[*]    fi
[*]    echo
[*]}
[*]case "$1" in
[*]   start)
[*]    start
[*]    ;;
[*]   stop)
[*]    stop
[*]    ;;
[*]   status)
[*]      status -p ${pidfile} $httpd
[*]    RETVAL=$?
[*]    ;;
[*]   restart)
[*]    stop
[*]    start
[*]    ;;
[*]   condrestart)
[*]    if [ -f ${pidfile} ] ; then
[*]      stop
[*]      start
[*]    fi
[*]    ;;
[*]   reload)
[*]      reload
[*]    ;;
[*]graceful|help|configtest|fullstatus)
[*]    $apachectl $@
[*]    RETVAL=$?
[*]    ;;
[*]*)
[*]    echo $"Usage: $prog{start|stop|restart|condrestart|reload|status|fullstatus|graceful| help|configtest}"
[*]    exit 1
[*]esac
[*]
[*]exit $RETVAL

[*]为脚本提供执行权限,并且将httpd 加入到服务列表中。
[*]chmod +x /etc/rc.d/init.d/httpd
[*]chkconfig --add httpd
  3、目的:不使用绝对路径,就可以以执行httpd的相关命令。
  在定义PATH路径的时候要ls /usr/local/apache,看看是否存在sbin。如果有则一并加入。
  vim /etc/profile.d/httpd.sh


[*]export PATH=$PATH:/usr/local/apache/bin
[*]
加入服务器列表: chkconfig --add httpd chkconfig --list httpd 查看默认是否启动(默认级别是3) chkconfig httpd on 开机自动启动
[*]此时就可以直接使用httpd stop/start等命令了
  4、修改配置文件:【安全起见在修改配置文件前,对它进行备份】
  我们在编译安装的时候是将配置文件放在了/etc/httpd下,所以我们cd到这个目录下,此时会有一个主配置文件httpd.conf生成,这个配置文件也同样通过inclouce将这写片段式的etc/httpd/extra这个目录下的片断的配置文件。在配置文件中我们应该指明pidfile存放的位置,这里是存放在/war/run/httpd.pid.

      vim /etc/httpd/httpd.conf   

[*]FidFile "/var/run/httpd.pid"
  5、提供测试网页
  默认网页时在/usr/local/apche/htdocs/inidex.html,我们可以指定访问页面的路径,这里我们想自己提供页面。并且存放在/web/htdocts中。


[*]mkdir -pv /web/htdocs
[*]
[*]vim/etc/httpd/httpd.conf 修改一下三项
[*]
[*]DocumentRooot "/etc/htdocs"
[*]<Directory "/web/htdocs">
[*]options none(为安全起见)

[*]为了测试,所以这里只是提供非常简单的页面,另外我们的页面名字是index.html,这个名字可不不是随意些,如果名字是xxx.html,则在访问时候需要加上文件名呢。
[*]
[*]vim /web/htdocs/index.html
[*]
[*] <html>
[*] <h1>hello httpd</h1>
[*] </html>
  在浏览器当中输入http://172.16.16.33即可访问到/web/htdots/index.html
  6、主配置文件/etc/http/http.conf的简要说明

  7、mpm模块切换演示
  vim /etc/httpd/httpd.conf


[*]找到:LoadModule mpm_event_module modules/mod_mpm_event.so
[*]
[*]改为:LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
[*]
[*]启用定义模块功能:
[*]
[*]Include /etc/httpd/extra/httpd-mpm.conf
[*]
[*]编辑每个mpm的配置指令:
[*]
[*]vim /etc/httpd/extra/httpd-mpm.conf
[*]
[*]pidfile "/var/run/httpd.pid"
  8、启用当前服务器状态输出模块


[*]LoadModule status_module modules/mod_status.so
[*]
[*]在主配置/文件etc/httpd/httpd.conf加入:
[*]
[*]<location /server-status>
[*]
[*]SetHandler server-status
[*]
[*]Require all granted
[*]
[*]</location>
  用浏览器验证浏览时候输入http://172.16.16.33/server-status即可
  9、建立虚拟主机
  建立虚拟主机之前先要注释掉中心主机vim /etc/httpd/httpd.conf


  创建虚拟主机的测试网页


[*]mkdir /web/magedu
[*]cd /web/magedu
[*]vim index.html
[*]<html>
[*]<head>magedu</head>
[*]<body>
[*]<h1>welcome to MaGe education<h2>
[*]</body>
[*]<html>
  要想用虚拟主机名称浏览此网页,需要在hosts文件中加入IP以及IP对应的主机名称。(对此不进行详细说明)
  10、验证用到的几个命令




命令
命令效果


httpd -l
  输出一个静态在服务器中的模块的列表。
  不会列出使用LoadModule指令动态加载的模块。




httpd -M
输出一个已经启用的模块列表,包括静态编译在服务器中的模块


httpd -t
检查配置文件的语法错误


apachectl start
开启httpd服务


apachectl stop
关闭httpd服务


netstat -ntlp
查看端口状态


  11、性能测试解析(只是举例子)
  
  测试服务器的性能(这里只是例子用的文件时Index.html)


[*]# ab -c 100 -n 1000 http://www.magedu.com/index.html
[*]This is ApacheBench, Version 2.3 <$Revision: 1373084 $>
[*]Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
[*]Licensed to The Apache Software Foundation, http://www.apache.org/
[*]
[*]Benchmarking www.magedu.com (be patient)
[*]Completed 100 requests
[*]Completed 200 requests
[*]Completed 300 requests
[*]Completed 400 requests
[*]Completed 500 requests
[*]Completed 600 requests
[*]Completed 700 requests
[*]Completed 800 requests
[*]Completed 900 requests
[*]Completed 1000 requests
[*]Finished 1000 requests
[*]
[*]
[*]Server Software:      Apache/2.4.3
[*]Server Hostname:      www.magedu.com
[*]Server Port:            80
[*]
[*]Document Path:          /index.html
[*]Document Length:      17 bytes
[*]
[*]Concurrency Level:      100             并发访问数
[*]Time taken for tests:   0.347 seconds       一共用了多长时间
[*]Complete requests:      1000            完成的请求数目
[*]Failed requests:      0         有多少个请求失败
[*]Write errors:         0         写出的错误多少
[*]Total transferred:      270000 bytes      一共传输的字节数目
[*]HTML transferred:       17000 bytes         有效信息传输
[*]Requests per second:    2878.09 [#/sec] (mean)平均每秒钟完成的请求
[*]Time per request:       34.745 (mean)      每个用户请求所经过的时间,(1000个请求并发请求100,则一个用户平均请求10个)
[*]Time per request:       0.347 (mean, across all concurrent requests) 每个用户请求一个的时间(即用10/100)
[*]Transfer rate:          758.87 received
  
  第二 MYSQL的编译安装:


[*]
[*]我们最好是将mysql的数据库装在一个LVM上面,并为其创建data目录用于存放数据
[*]pvcreate /dev/sda5
[*]vgcreate myvg /dev/sda5
[*]lvcreate -L 10G -n mydata myvg
[*]mke2fs -j /dev/myvg/mydata
[*]让其能够开机自动挂载
[*]vim /etc/fstable
[*]/dev/myvg/mydata    /mydata   ext3       defaults 0 0
[*]
[*]创建系统用户以及系统组以及创建/data目录
[*]
[*]groupadd -r mysql
[*]useradd -g mysql -r mysql
[*]mkdir /mydata/data
[*]chown -R mysql.mysql /mydata
[*]
[*]编译安装mysql
[*]tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
[*]cd /usr/local
[*]cd mysql-5.5.28-linux2.6-i686
[*]ln -sv mysql-5.5.28-linux2.6-i686mysql
[*]cd mysql
[*]chown -R mysql:mysql ./
[*]scripts/mysql_install_db --datadir=/mydata/data --user=mysql
[*]chown -R root ./
[*]cp support-files/mylarge.cnf /etc/my.cnf
[*]vim /etc/my.cnf
[*]找到:thread_concurrency = 8 改为thread_concurrency = 2 (2这个数值是CPU个数乘以二)
[*]添加:datadir = /mydata/data 指定mysql的数据存放的位置
[*]
[*]cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[*]chmod +x /etc/rc.d/init.d/mysqld
[*]chkconfig --add mysqld    加入到系统启动列表当中
[*]chkconfig mysqld on
[*]vim /etc/profile.d/mysql.sh
[*]export PATH=$PATH:/usr/local/mysql/bin将命令路径加入到系统调用的路径当中,直接即可使用命令,不需在写绝对路径
[*]vim /etc/ld.so.conf/mysql.conf
[*]/usr/local/mysql/lib
[*]ldconfig-v      重读库文件立即生效
[*]ln -sv /usr/local/mysql/include /usr/include/mysql
[*]vim /etc/man.config
[*]找到MANPATH在其下面添加MANPATH /usr/local/mysql/man
  第三:PHP编译安装




.configure选项
选项说明


--with-mysql
php要连接mysql,则要指出maysql的安装路径


--with-openssl
支持ssl


--with-mysqli
给各种语言提供与mysql交互的接口,此接口是一个应用程序


--enable-mbstring
支持多字节


--with-freetype-dir
打开对freetype字体库的支持指定其所在位置


  --with-jpeg-dir
  --with-png-dir


支持jpeg图片指明其所在路径
支持png图片指明其所在路径


with-bz2 --enable-maintainer-zts
打开对bz2文件的支持


--enable-xml
支持扩展标记语言


--enable-sockets
让PHP能够基于sockets进行通信


--with-apxs2
表示能把PHP编辑成Apache的模块,通常为第三方提供一个模块的时候都是要通过这样的一个选项来进行定义的,如果不定义这个选项,PHP可能要以fastcgi的方式工作了。


--with-mcrypt
当安装了 mcrypt 函式库后,可在编译 PHP 时加入本选项,让程式可以使用编译解码功能。


--with-config-file-path
配置文件的路径,将php.ini放入此路径下就可以


--with-config-file-scan-dir
同时找这个目录下的一.ini结尾的文件,作为配置文件的一部分


  


[*]rpm -ivhinstall libmcrypt-2.5.7-5.el5.i386.rpm
[*]
[*]rpm -ivhinstall libmcrypt-devel-2.5.7-5.el5.i386.rpm
[*]
[*]tar xf php-5.4.8.tar.bz2
[*]
[*]cd php-5.4.8
[*]
[*]./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml--enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
[*]
[*]make && make install
  注意:这里特别的说一句一旦你的PHP编译成功的话,会在配置文件/etc/httpd/httpd.conf当中出现如下行;如果没有出现的话说明PHP编译并未成功哦!此时还是需要重新编译的!
  LoadModule php5_module      modules/libphp5.so
  让web识别PHP网页类型则需要在配置文件/etc/httpd/httpd.conf当中加入


[*]AddType application/x-httpd-php .php
[*]
[*]AddType application/x-httpd-php-source .phps
[*]
[*]cd /web/magedu.com
[*]
[*]mv index,html index.php
[*]
[*]vim index.php (仅是为了测试是否能够识别这样的页面)
[*]
[*]<?php
[*]
[*]phpinfo();
[*]
[*]?>
  提供主配置文件


[*]cd /root/php-5.4.8
[*]
[*]cp php.ini-production /etc/php.ini
[*]
[*]vim /etc/php.ini
  在这个基础上装个论坛,然后做个压力测试(童鞋们需要有论坛包我们这里以Discuz_7.2_FULL_SC_GBK.zip为例)


[*]cd /web/magedu
[*]
[*]uzip Discuz_7.2_FULL_SC_GBK.zip
  此时会看到有一个upload目录生成,这个目录里面的东东就是我们要的,可以直接把里面的所有文件放到/web/magedu路径下这样访问的时候就不需要加路径。http://www.magedu.com/install,如果不改变的话直接就http://www.magedu.com/upload/install,第一次访问的时候要加/install
  这个时候访问的时候会弹出一个页面要求把short_open_tag=off改为short_open_tag=on即可。
  
  
  
页: [1]
查看完整版本: LAMP平台的搭建,以及虚拟主机的使用