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

 

安装开发环境

  1. yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development" 

1、依次编译安装arp arp-util(记得手动安装pcre-devel)httpd,另外在安装httpd前要确认本机未安装httpd,如果有要卸载掉。

  1.  
  2. tar xf apr-1.4.6.tar.bz2 
  3.  
  4. cd apr-1.4.6 
  5.  
  6. ./configure --prefix=/usr/local/apr 
  7.  
  8. make && make install
  9.  
  10.  
  11. tar xf apr-util-1.4.1.tar.bz2 
  12.  
  13. cd apr-util-1.4.1 
  14.  
  15. ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr 
  16.  
  17. make && make install 
  18.  
  19.  
  20. yum install pcre-devel 

  21.  
  22. 要养成一个习惯在编译安装的时候要读INSTALL这个文件,它给出了安装的详细说明。
  23. less INSTALL
  24.  
  25. tar xf httpd-2.4.3.tar.bz2 
  26.  
  27. cd httpd-2.4.3 
  28.  
  29. ./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/ 
  30.  
  31. 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

  1. #!/bin/bash 
  2. # httpd        Startup script for the Apache HTTP Server 
  3. # chkconfig: - 85 15 
  4. # description: Apache is a World Wide Web server.  It is used to serve \ 
  5. #          HTML files and CGI. 
  6. # processname: httpd 
  7. # config: /etc/httpd/conf/httpd.conf 
  8. # config: /etc/sysconfig/httpd 
  9. # pidfile: /var/run/httpd.pid 
  10.  
  11. # Source function library. 
  12.   . /etc/rc.d/init.d/functions 
  13.  
  14.   if [ -f /etc/sysconfig/httpd ]; then 
  15.         . /etc/sysconfig/httpd 
  16.   fi 
  17.  
  18. # Start httpd in the C locale by default. 
  19. HTTPD_LANG=${HTTPD_LANG-"C"} 
  20.  
  21. # This will prevent initlog from swallowing up a pass-phrase  prompt if 
  22. # mod_ssl needs a pass-phrase from the user. 
  23.   INITLOG_ARGS="" 
  24. # Path to the apachectl script, server binary, and short-form for    messages. 
  25.   apachectl=/usr/local/apache/bin/apachectl 
  26.   httpd=${HTTPD-/usr/local/apache/bin/httpd} 
  27.   prog=httpd 
  28.   pidfile=${PIDFILE-/var/run/httpd.pid} 
  29.   lockfile=${LOCKFILE-/var/lock/subsys/httpd} 
  30.   RETVAL=0 
  31.  
  32.   start() { 
  33.         echo -n $"Starting $prog: " 
  34.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd     $OPTIONS 
  35.         RETVAL=$? 
  36.         echo 
  37.         [ $RETVAL = 0 ] && touch ${lockfile} 
  38.         return $RETVAL 
  39.  
  40.   stop() { 
  41.     echo -n $"Stopping $prog: " 
  42.     killproc -p ${pidfile} -d 10 $httpd 
  43.     RETVAL=$? 
  44.     echo 
  45.     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 
  46.   reload() { 
  47.     echo -n $"Reloading $prog: " 
  48.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 
  49.         RETVAL=$? 
  50.         echo $"not reloading due to configuration syntax error" 
  51.         failure $"not reloading $httpd due to configuration syntax   error" 
  52.     else 
  53.         killproc -p ${pidfile} $httpd -HUP 
  54.         RETVAL=$? 
  55.     fi 
  56.     echo 
  57.   case "$1" in 
  58.    start) 
  59.     start 
  60.     ;; 
  61.    stop) 
  62.     stop 
  63.     ;; 
  64.    status) 
  65.         status -p ${pidfile} $httpd 
  66.     RETVAL=$? 
  67.     ;; 
  68.    restart) 
  69.     stop 
  70.     start 
  71.     ;; 
  72.    condrestart) 
  73.     if [ -f ${pidfile} ] ; then 
  74.         stop 
  75.         start 
  76.     fi 
  77.     ;; 
  78.    reload) 
  79.         reload 
  80.     ;; 
  81.   graceful|help|configtest|fullstatus) 
  82.     $apachectl $@ 
  83.     RETVAL=$? 
  84.     ;; 
  85.   *) 
  86.     echo $"Usage: $prog  {start|stop|restart|condrestart|reload|status|fullstatus|graceful| help|configtest}" 
  87.     exit 1 
  88.   esac 
  89.   
  90.   exit $RETVAL 
  1. 为脚本提供执行权限,并且将httpd 加入到服务列表中。 
  2. chmod +x /etc/rc.d/init.d/httpd 
  3. chkconfig --add httpd 

3、目的:不使用绝对路径,就可以以执行httpd的相关命令。

   在定义PATH路径的时候要ls /usr/local/apache,看看是否存在sbin。如果有则一并加入。

vim /etc/profile.d/httpd.sh 

  1. export PATH=$PATH:/usr/local/apache/bin  

  2. 加入服务器列表: chkconfig --add httpd chkconfig --list httpd 查看默认是否启动(默认级别是3) chkconfig httpd on 开机自动启动
  3. 此时就可以直接使用httpd stop/start等命令了

 4、修改配置文件:【安全起见在修改配置文件前,对它进行备份】

    我们在编译安装的时候是将配置文件放在了/etc/httpd下,所以我们cd到这个目录下,此时会有一个主配置文件httpd.conf生成,这个配置文件也同样通过inclouce将这写片段式的etc/httpd/extra这个目录下的片断的配置文件。在配置文件中我们应该指明pidfile存放的位置,这里是存放在/war/run/httpd.pid.

      vim /etc/httpd/httpd.conf   
  1. FidFile "/var/run/httpd.pid" 

5、提供测试网页

    默认网页时在/usr/local/apche/htdocs/inidex.html,我们可以指定访问页面的路径,这里我们想自己提供页面。并且存放在/web/htdocts中。

  1. mkdir -pv /web/htdocs 
  2.  
  3. vim  /etc/httpd/httpd.conf 修改一下三项
  4.  
  5. DocumentRooot "/etc/htdocs" 
  6. <Directory "/web/htdocs"> 
  7. options none(为安全起见) 
  1. 为了测试,所以这里只是提供非常简单的页面,另外我们的页面名字是index.html,这个名字可不不是随意些,如果名字是xxx.html,则在访问时候需要加上文件名呢。 
  2.  
  3. vim /web/htdocs/index.html 
  4.  
  5.  <html> 
  6.  <h1>hello httpd</h1> 
  7.  </html> 

在浏览器当中输入http://172.16.16.33即可访问到/web/htdots/index.html

 6、主配置文件/etc/http/http.conf的简要说明

 7、mpm模块切换演示

vim /etc/httpd/httpd.conf 

  1. 找到:LoadModule mpm_event_module modules/mod_mpm_event.so 
  2.  
  3. 改为:LoadModule mpm_prefork_module modules/mod_mpm_prefork.so 
  4.  
  5. 启用定义模块功能:
  6.  
  7. Include /etc/httpd/extra/httpd-mpm.conf 
  8.  
  9. 编辑每个mpm的配置指令: 
  10.  
  11. vim /etc/httpd/extra/httpd-mpm.conf 
  12.  
  13. pidfile "/var/run/httpd.pid"  

8、启用当前服务器状态输出模块

  1. LoadModule status_module modules/mod_status.so 
  2.  
  3. 在主配置/文件etc/httpd/httpd.conf加入: 
  4.  
  5. <location /server-status>  
  6.  
  7. SetHandler server-status 
  8.  
  9. Require all granted 
  10.  
  11. </location> 

用浏览器验证浏览时候输入http://172.16.16.33/server-status即可

9、建立虚拟主机

   建立虚拟主机之前先要注释掉中心主机vim /etc/httpd/httpd.conf

创建虚拟主机的测试网页

  1. mkdir /web/magedu 
  2. cd /web/magedu 
  3. vim index.html 
  4. <html> 
  5. <head>magedu</head> 
  6. <body> 
  7. <h1>welcome to MaGe education<h2> 
  8. </body> 
  9. <html> 

要想用虚拟主机名称浏览此网页,需要在hosts文件中加入IP以及IP对应的主机名称。(对此不进行详细说明)

10、验证用到的几个命令

命令 命令效果
httpd -l 

输出一个静态在服务器中的模块的列表。

不会列出使用LoadModule指令动态加载的模块。

httpd -M 输出一个已经启用的模块列表,包括静态编译在服务器中的模块
httpd -t  检查配置文件的语法错误
apachectl start 开启httpd服务
apachectl stop 关闭httpd服务
netstat -ntlp 查看端口状态

11、性能测试解析(只是举例子)

 

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

  1. [root@www ~]# ab -c 100 -n 1000 http://www.magedu.com/index.html 
  2. This is ApacheBench, Version 2.3 <$Revision: 1373084 $> 
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ 
  4. Licensed to The Apache Software Foundation, http://www.apache.org/ 
  5.  
  6. Benchmarking www.magedu.com (be patient) 
  7. Completed 100 requests 
  8. Completed 200 requests 
  9. Completed 300 requests 
  10. Completed 400 requests 
  11. Completed 500 requests 
  12. Completed 600 requests 
  13. Completed 700 requests 
  14. Completed 800 requests 
  15. Completed 900 requests 
  16. Completed 1000 requests 
  17. Finished 1000 requests 
  18.  
  19.  
  20. Server Software:        Apache/2.4.3 
  21. Server Hostname:        www.magedu.com 
  22. Server Port:            80 
  23.  
  24. Document Path:          /index.html 
  25. Document Length:        17 bytes 
  26.  
  27. Concurrency Level:      100             并发访问数 
  28. Time taken for tests:   0.347 seconds       一共用了多长时间 
  29. Complete requests:      1000            完成的请求数目 
  30. Failed requests:        0           有多少个请求失败 
  31. Write errors:           0           写出的错误多少 
  32. Total transferred:      270000 bytes        一共传输的字节数目 
  33. HTML transferred:       17000 bytes         有效信息传输 
  34. Requests per second:    2878.09 [#/sec] (mean)  平均每秒钟完成的请求 
  35. Time per request:       34.745 [ms] (mean)      每个用户请求所经过的时间,(1000个请求并发请求100,则一个用户平均请求10个) 
  36. Time per request:       0.347 [ms] (mean, across all concurrent requests) 每个用户请求一个的时间(即用10/100) 
  37. Transfer rate:          758.87 [Kbytes/sec] received 

 

第二 MYSQL的编译安装:

  1.  
  2. 我们最好是将mysql的数据库装在一个LVM上面,并为其创建data目录用于存放数据 
  3. pvcreate /dev/sda5 
  4. vgcreate myvg /dev/sda5 
  5. lvcreate -L 10G -n mydata myvg 
  6. mke2fs -j /dev/myvg/mydata
  7. 让其能够开机自动挂载
  8. vim /etc/fstable
  9. /dev/myvg/mydata    /mydata     ext3       defaults 0 0 
  10.  
  11. 创建系统用户以及系统组以及创建/data目录 
  12.  
  13. groupadd -r mysql 
  14. useradd -g mysql -r mysql 
  15. mkdir /mydata/data 
  16. chown -R mysql.mysql /mydata 
  17.  
  18. 编译安装mysql 
  19. tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local 
  20. cd /usr/local 
  21. cd mysql-5.5.28-linux2.6-i686  
  22. ln -sv mysql-5.5.28-linux2.6-i686  mysql 
  23. cd mysql 
  24. chown -R mysql:mysql ./ 
  25. scripts/mysql_install_db --datadir=/mydata/data --user=mysql 
  26. chown -R root ./ 
  27. cp support-files/mylarge.cnf /etc/my.cnf 
  28. vim /etc/my.cnf 
  29. 找到:thread_concurrency = 8 改为thread_concurrency = 2 (2这个数值是CPU个数乘以二) 
  30. 添加:datadir = /mydata/data 指定mysql的数据存放的位置 
  31.  
  32. cp support-files/mysql.server /etc/rc.d/init.d/mysqld 
  33. chmod +x /etc/rc.d/init.d/mysqld 
  34. chkconfig --add mysqld    加入到系统启动列表当中 
  35. chkconfig mysqld on 
  36. vim /etc/profile.d/mysql.sh 
  37. export PATH=$PATH:/usr/local/mysql/bin  将命令路径加入到系统调用的路径当中,直接即可使用命令,不需在写绝对路径 
  38. vim /etc/ld.so.conf/mysql.conf  
  39. /usr/local/mysql/lib 
  40. ldconfig  -v        重读库文件立即生效 
  41. ln -sv /usr/local/mysql/include /usr/include/mysql 
  42. vim /etc/man.config 
  43. 找到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结尾的文件,作为配置文件的一部分

 

  1. rpm -ivh  install libmcrypt-2.5.7-5.el5.i386.rpm  
  2.  
  3. rpm -ivh  install libmcrypt-devel-2.5.7-5.el5.i386.rpm 
  4.  
  5. tar xf php-5.4.8.tar.bz2 
  6.  
  7. cd php-5.4.8 
  8.  
  9. ./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 
  10.  
  11. make && make install 

    注意:这里特别的说一句一旦你的PHP编译成功的话,会在配置文件/etc/httpd/httpd.conf当中出现如下行;如果没有出现的话说明PHP编译并未成功哦!此时还是需要重新编译的!

LoadModule php5_module        modules/libphp5.so

让web识别PHP网页类型则需要在配置文件/etc/httpd/httpd.conf当中加入

  1. AddType application/x-httpd-php .php 
  2.  
  3. AddType application/x-httpd-php-source .phps 
  4.  
  5. cd /web/magedu.com 
  6.  
  7. mv index,html index.php 
  8.  
  9. vim index.php (仅是为了测试是否能够识别这样的页面) 
  10.  
  11. <?php 
  12.  
  13. phpinfo(); 
  14.  
  15. ?> 

提供主配置文件

  1. cd /root/php-5.4.8 
  2.  
  3. cp php.ini-production /etc/php.ini 
  4.  
  5. vim /etc/php.ini 

在这个基础上装个论坛,然后做个压力测试(童鞋们需要有论坛包我们这里以Discuz_7.2_FULL_SC_GBK.zip为例)

  1. cd /web/magedu 
  2.  
  3. 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即可。

 

 

 

posted on 2012-12-08 13:10  猫学历险记  阅读(411)  评论(0编辑  收藏  举报

导航