分别在三台CentOS6.5的服务器上安装httpd,php,MariaDB服务,使用户通过访问web服务器可以访问动态网页,并且在动态网页中实现对MairaDB数据库的操作.实验拓扑如下:
a) 解决依赖关系,通过源码安装apr,apr-util,通过yum安装pcre-devel
[root@web src]# tar xf apr-1.5.0.tar.bz2 [root@web src]# tar xf apr-util-1.5.3.tar.bz2 #编译安装apr [root@web apr-1.5.0]# ./configure --prefix=/usr/local/apr #配置apr并指定安装目录 [root@web apr-1.5.0]# make &&make install #编译安装apr-util [root@web apr-1.5.0]# cd ../apr-util-1.5.3 [root@web apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util \ > --with-apr=/usr/local/apr [root@web apr-util-1.5.3]# make && make install #通过yum安装pcre-devel [root@web ~]# yum install pcre-devel
b) 下载源码包并解压
[root@web src]# tar xf httpd-2.4.9.tar.bz2 [root@web src]# cd httpd-2.4.9 #指定httpd安装目录及一些工作特性 [root@web 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 [root@web httpd-2.4.9]# make && make install
c) 在httpd配置文件中指定其pidFile
[root@web httpd24]# vim /etc/httpd24/httpd.conf #添加下面一行 PidFile "/var/run/httpd.pid"
d) 为httpd24提供服务启动脚本
vim /etc/rc.d/init.d/httpd24 #!/bin/bash # # httpd24 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 #添加httpd24到服务列表,并设置开机启动 [root@web httpd24]# chmod +x /etc/rc.d/init.d/httpd24 [root@web httpd24]# chkconfig -add httpd24 [root@web httpd24]# chkconfig httpd24 on
e) 启动服务
[root@web httpd24]# service httpd24 start Starting httpd: AH00557: httpd: apr_sockaddr_info_get() failed for web.test.com AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message [ OK ] #由于没有DNS解析,随意出现上面提示信息,将解析添加到/etc/hosts文件中 [root@web httpd24]# vim /etc/hosts 172.16.21.250 web.test.com
f) 重启服务
[root@web httpd24]# service httpd24 restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
g) 从客户机访问测试
h) 后续处理工作:
#添加httpd/bin目录到$PATH变量中 [root@web httpd24]# vim /etc/profile.d/httpd.sh [root@web httpd24]# . /etc/profile.d/httpd.sh [root@web httpd24]# echo $PATH /usr/local/httpd:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
a) 为了使数据库文件动态可扩展,新建一个逻辑卷myvg,在此逻辑卷上创建一个逻辑\卷data,以用于数据备份和防止随着数据库的增大出现空间不足的情况,
#创建存储空间 [root@db ~]# pvcreate /dev/sda3 dev_is_mpath: failed to get device for 8:3 Physical volume "/dev/sda3" successfully created [root@db ~]# vgcreate myvg /dev/sda3 Volume group "myvg" successfully created [root@db ~]# lvcreate -L5G -n data myvg Logical volume "data" created #格式化/dev/myvg/data,并设置开机自动挂载到/data [root@db ~]# mke2fs -L data -T ext4 /dev/myvg/data [root@db ~]# mkdir /data [root@db ~]# vim /etc/fstab #添加如下一行信息 /dev/myvg/data /data ext4 defaults 0 0
b) 安装二进制版本的MariaDB
[root@db src]# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local [root@db src]# ln -sv /usr/local/mariadb-5.5.36-linux-x86_64/ /usr/local/mysql `/usr/local/mysql' -> `/usr/local/mariadb-5.5.36-linux-x86_64/' [root@db src]# cd /usr/local/mysql/ [root@db mysql]# groupadd -r mysql [root@db mysql]# useradd -g mysql -r mysql #初始化数据库 [root@db mysql]# chown -R mysql:mysql ./* [root@db mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/ [root@db mysql]# chown -R root ./* #为mysql提供配置文件 [root@db mysql]# cp support-files/my-large.cnf /etc/my.cnf cp: overwrite `/etc/my.cnf'? y [root@db mysql]# vim /etc/my.cnf #在[mysqld]下添加修改如下信息 datadir =/data #设置数据库存放位置 thread_concurrency = 4 #此处一般设置为CPU个数*2 #设置mysqld为启动项,并启动测试 [root@db mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@db mysql]# chmod +x /etc/init.d/mysqld [root@db mysql]# chkconfig --add mysqld [root@db mysql]# chkconfig mysqld on [root@db mysql]# service mysqld start Starting MySQL.. [ OK ]
c) 后续处理工作:
#添加文件/etc/profile.d/mysql,以便与启动其附带的工具 [root@db mysql]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@db mysql]# . /etc/profile.d/mysql.sh [root@db mysql]# echo $PATH /usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin #导出mysql的头文件 # ln -sv /usr/local/mysql/include /usr/include/mysql #导出mysql的库文件给系统库查找路径: [root@db mysql]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@db mysql]# ldconfig
a) 解决依赖关系
[root@php php-5.4.26]# yum -y groupinstall "Desktop Platform Development" [root@php php-5.4.26]# yum -y install bzip2-devel libmcrypt-devel
b) 解压并安装php
[root@php src]# tar xf php-5.4.26.tar.bz2 [root@php src]# cd php-5.4.26 [root@php 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 [root@php php-5.4.26]# make && make install
c) 为php提供配置文件并配置php-fpm
[root@php php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@php php-5.4.26]# chmod +x /etc/init.d/php-fpm [root@php php-5.4.26]# chkconfig --add php-fpm [root@php php-5.4.26]# chkconfig php-fpm on #为php-fpm提供配置文件:部分信息如下 [root@php php-5.4.26]# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf [root@php php-5.4.26]# vim /usr/local/php5/etc/php-fpm.conf pm.max_children = 30 ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2 pm.start_servers = 7 pm.min_spare_servers = 5 pm.max_spare_servers = 10 listen = *:9000 pid = /usr/local/php5/var/run/php-fpm.pid#配置pid文件 #启动服务 [root@php php-5.4.26]# service php-fpm start Starting php-fpm done [root@php php-5.4.26]# netstat -anpt |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 65063/php-fpm
a) 启用httpd相关模块,配置中心服务器支持fcgi
[root@web ~]# vim /etc/httpd24/httpd.conf LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #配置httpd为php的反向代理 [root@web extra]# vim httpd-fcgi.conf ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.21.251:9000/var/www/html/$1 #使服务器可以解析php网页 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps DirectoryIndex index.php
b) 在php服务器上创建一个php测试文件
[root@php php5]# cat /var/www/html/index.php <?php phpinfo(); ?>
#访问
访问成功,可见php-fpm和httpd已将连接成功
c) 配置MariaDB,使特定用户可以从php服务器172.16.21.251访问
[root@db ~]# mysql -p MariaDB [(none)]> GREAT all OM test.* TO 'test'@'172.16.21.251' IDENTIFIED BY '123'; Query OK, 0 rows affected (0.05 sec) MariaDB [(none)]> FLUSH PRIVILEGES; #修改php服务器上的index.php, [root@php php5]# vim /var/www/html/index.php <?php $link=mysql_connect('172.16.21.252','test','123'); if ($link) echo "Yes"; else echo "No"; mysql_close(); ?>
#从网页访问验证php可否访问MairaDB