實驗說明:
分別在三臺CentOS6.5的服務器上安裝httpd,php,MariaDB服務,使用戶通過訪問web服務器可以訪問動態網頁,並且在動態網頁中實現對MairaDB數據庫的操作.實驗拓撲如下:
1.搭建web服務器,以實現對靜態頁面的訪問
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
2. 在db.test.com服務器上搭建MariaDB服務器
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
3.在php.test.com服務器上編譯安裝以fpm方式運行的php服務器
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
4.配置httpd-2.4.9支持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