网络浪子 发表于 2018-11-22 10:10:02

apache、php、mysql各工作于独立主机的lamp平台实现

  实验环境:

  VM1:192.168.1.132,用于apache服务器

  VM2:192.168.1.134,用于php服务器

  VM3:192.168.1.137,用户mariadb服务器

  软件版本:httpd-2.4.9,php-5.4.26,mariadb-5.5.39
  一、编译安装httpd-2.4.9
  1、解决依赖关系
  # yum -y install pcre-devel
  # yum -y install mod_ssl
  # yum groupinstall -y "Development tools"
  # yum groupinstall -y "Server Platform Development"
  编译安装apr-1.5.0.tar.bz2
  # tar xf apr-1.5.0.tar.bz2
  # cd apr-1.5.0
  # ./configure --prefix=/usr/local/apr
  # make && make install
  编译安装apr-until-1.5.3.tar.bz2
  # tar xf apr-util-1.5.3.tar.bz2
  # cd apr-util-1.5.3
  # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
  # make && make install

  2、编译安装httpd-2.4.9
  # tar xf httpd-2.4.9.tar.bz2

  # cd httpd-2.4.9
  # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --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-mpms-shared=all --with-mpm=event
  # make && make install
  3、修改httpd配置文件
  启用fcgi模块

  在配置文件中添加如下语句让apache支持php格式的页面,并支持php格式的主页
  AddType application/x-httpd-php.php
  AddType application/x-httpd-php-source.phps


  关闭正向代理,启用反向代理,添加语句,fcgi:后面的IP地址是php应用服务器的IP地址,/var/phpscript是php服务器中用于存放php页面文件的目录位置,用户在向前端的apache服务器请求php页面时,httpd进程会将用户请求反向代理至php服务器对应的目录下的页面文件,经过php解释器处理后返回给前端apache服务器html页面格式的文件。

  ProxyRequests Off
  ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.134:9000/var/phpscript/$1

  设置Pid文件路径,这个条目是为了使用apache服务脚本而设置的。


  4、提供SysV服务脚本,在/etc/rc.d/init.d目录下创建名称为httpd24的文件,编辑并添加如下内容

  #!/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-phrase prompt if
  # mod_ssl needs a pass-phrase from the user.
  INITLOG_ARGS=""
  

  # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
  # with the thread-based "worker" MPM; BE WARNED that some modules may not
  # work correctly with a thread-based MPM; notably PHP will refuse to start.
  

  # 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
  }
  

  # See how we were called.
  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
  修改此脚本的执行权限:
  # chmod +x /etc/rc.d/init.d/httpd24
  添加服务列表:
  # chkconfig --add httpd24
  # 启动服务:
  # service httpd24 start
  apache服务器准备完毕。
  

  二、编译安装php-5.4.26
  1、解决依赖关系:
  # yum groupinstall -y "Desktop Platform Development"
  # yum -y install bzip2-devel libmcrypt-devel
  2、编译安装php-5.4.26

  # tar xf php-5.4.26.tar.bz2
  # cd php-5.4.26
  # ./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml--enable-sockets --enable-fpm --with-mcrypt--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
  数据库服务器是另一台单独主机,所以编译php时指定mysql的安装位置时,需要设置为mysqlnd,同时显式指出使用fpm模块
  # make && make install
  为php提供配置文件:
  # cp php.ini-production /etc/php.ini
  3、配置php-fpm
  为php-fpm提供SysV服务脚本,并将其添加至服务列表
  # cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
  # chmod +x /etc/rc.d/init.d/php-fpm
  # chkconfig --add php-fpm
  # chkconfig php-fpm on
  为php-fpm提供配置文件:
  # cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
  修改php-fpm的配置文件:
  # vim /usr/local/php5/etc/php-fpm.conf
  启用pid文件,该选项的启用位置在全局配置段下方,并修改php-fpm监听的地址,原监听地址是127.0.0.1,需要改为可以与外部通信的地址,设置如图所示:

  启动服务:
  # service php-fpm start
  查看服务监听的端口:

  可以看到php-fpm工作在9000端口,监听的地址是192.168.1.134。
  创建php页面存储目录,存放php页面文件。
  # mkdir /var/phpscript
  # vim /var/phpscript/index.php;编辑php页面文件,内容如下图:

  php应用程序服务器准备完毕。
  

  三、安装mariadb-5.5.39
  1、准备数据存放的文件系统,创建逻辑卷,用于数据库存放数据。

  创建一个磁盘分区,分区格式为LVM:

  # kpartx -af /dev/sda;通知内核重读磁盘分区表
  # partx -a /dev/sda;通知内核重读磁盘分区表
  # cat /proc/partitions;查看内核识别分区情况
  # pvcreate /dev/sda3
  # vgcreate myvg /dev/sda3
  # lvcreate -L 10G -n mylv myvg
  # lvs;查看lv创建结果

  # mke2fs -t ext4 /dev/myvg/mylv
  # mkdir /mydata/data;创建用于存储数据库数据的目录
  # vim /etc/fstab;编辑fstab文件,将/dev/myvg/mylv挂载至/mydata/data

  # mount -a
  2、新建用于以安全方式运行进程
  # groupadd -r mysql
  # useradd -g mysql -s /sbin/nologin -rmysql
  # chown -R mysql:mysql /mydata/data
  3、安装初始化MariaDB-5.5.39

  # tar xf mariadb-5.5.39-linux-x86_64.tar.gz

  # cd /usr/local

  # ln -sv mariadb-5.5.39-linux-x86_64 mysql
  # cd mysql
  # chown -R mysql:mysql .
  # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
  # chown -R root .
  4、为mariadb提供主配置文件
  # cd /usr/local/mysql
  # cp support-files/my-large.cnf /etc/my.cnf
  # vim /etc/my.cnf;修改数据库主配置文件,添加数据存放的路径:

  5、为MariaDB提供服务脚本
  # cd /usr/local/mysql
  # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
  # chown +x /etc/rc.d/init.d/mysqld
  # chkconfig --add mysqld
  # chkconfig mysqld on
  6、mariadb安装完成后续的配置工作:
  输出mariadb的man手册,添加内容如图:
  # vim /etc/man.config

  输出mariadb头文件
  # ln -sv /usr/local/mysql/include /usr/include/mysql
  输出mariadb的库文件:

  # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
  # ldconfig;系统重新装载库文件
  修改PATH环境变量,使系统可以直接使用mariadb相关命令
  # vim /etc/profile.d/mysql.sh

  连接mysql,创建远程连接用户,用于测试数据库连接:

  # mysql
  # CREATE USER tuser@'192.168.1.%' IDENTIFIED BY 'tpass';
  现在三台主机准备完毕,在apache服务器上添加静态页面内容,在php服务器上添加php页面文件用于测试:
  在apache服务器的站点根目录下创建一个名称为test.html的页面文件,编辑的内容如下图:

  在浏览器内测试效果如下:


  测试php页面访问及数据库的连接情况:

  四、为php的应用服务器添加xcache模块,支持opcode缓存功能,xcache版本3.1.0。
  1、编译安装xcache-3.1.0
  # tar xf xcache-3.1.0.tar.bz2
  # cd xcache-3.1.0
  # /usr/local/php5/bin/phpize ;phpize是一个脚本,用来为php扩展功能模块在编译时生成configure配置文件,该命令需要在准备的扩展模块解压出来的源码目录下运行,以便在该目录下生成对应配置文件。

  # ./configure --enable-xcache --with-php-config=/usr/local/php5/bin/php-config

  # make && make install;安装结束后,会生成扩展模块的库文件。

  2、整合php和xcache:
  将xcache提供的样例配置文件导入到php的配置文件目录,之前编译php指定的主配置文件目录为/etc/,主配置文件的组成部分放置在/etc/php.d目录下。
  # mkdir /etc/php.d
  # cp xcache.ini /etc/php.d ;xcache.ini文件在xcache的源码目录中
  # vim /etc/php.d/xcache.ini
  修改zend引擎使用的扩展模块所在的具体目录位置,/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

  xcache模块安装完毕,重启服务。

  在php服务器的/var/phpscripts目录创建一个测试页面,内容如下:

  在浏览器内访问一下该页面:

  使用ab命令测试php页面 # ab -n 500 -c 20 http://192.168.1.132/test.php,效果如下图:

  php每秒钟完成请求次数大约为160次。



页: [1]
查看完整版本: apache、php、mysql各工作于独立主机的lamp平台实现