LAMP分离环境的搭建:
环境说明: CentOS 6.5最小化安装,安装相应的组件: [root@http ~]# yum groupinstall -y"Development tools" [root@mysql ~]# yum groupinstall -y"Server Platform Development"
准备三台主机: http: 192.168.1.217 MySQL:192.168.1.218 PHP:192.168.1.1.219
准备的软件包: [root@rh5 ap]# ll total 92228 -rw-r--r-- 1 root root 826885 May 22 13:09 apr-1.5.2.tar.bz2 -rw-r--r-- 1 root root 694427 May 22 13:09 apr-util-1.5.4.tar.bz2 -rw-r--r--. 1 root root 5691656 Jul 9 00:55 cmake-2.8.8.tar.gz -rwxr--r-- 1 nobody nobody 12490656 Apr 1717:59 Discuz_X3.2_SC_UTF8.zip -rwxr--r-- 1 nobody nobody 5054838 May 21 17:20 httpd-2.4.12.tar.bz2 -rwxr--r-- 1 nobody nobody 634589 May 22 13:09 libmcrypt-2.5.6.tar.gz -rwxr--r-- 1 nobody nobody 24680463 Jun 1416:14 mysql-5.5.33.tar.gz -rwxr--r-- 1 nobody nobody 13724681 May 2117:41 php-5.6.8.tar.bz2 -rwxr--r-- 1 nobody nobody 9955372 May 21 17:58phpMyAdmin-4.4.4-all-languages.zip -rwxr--r-- 1 nobody nobody 166123 Apr 21 15:37 xcache-3.0.1.tar.gz [root@rh5 ap]# 一、在192.168.1.217主机上安装httpd-2.4.12
从samba共享中下载相应的软件: [root@http ~]# yum -y install samba-client [root@http ~]# smbclient //192.168.1.155/public smb: \> ls smb: \> cd ap smb: \ap\> ls smb: \ap\> get apr-1.5.2.tar.bz2 getting file \ap\apr-1.5.2.tar.bz2 of size 826885as apr-1.5.2.tar.bz2 (4835.4 KiloBytes/sec) (average 4835.4 KiloBytes/sec) smb: \ap\> get apr-util-1.5.4.tar.bz2 getting file \ap\apr-util-1.5.4.tar.bz2 ofsize 694427 as apr-util-1.5.4.tar.bz2 (9042.0 KiloBytes/sec) (average 6139.1KiloBytes/sec) smb: \ap\>
1、在安装httpd之前需要先安装apr、apr-util以及pcre-devel等相关包。在编译安装httpd之前需要Development Tools开发工具集。具体步骤如下:
(1)安装apr: [root@http bao]# tar -xf apr-1.5.2.tar.bz2 [root@http bao]# cd apr-1.5.2 [root@http apr-1.5.2]# ./configure --prefix=/usr/local/apr [root@http apr-1.5.2]# make [root@http apr-1.5.2]# make install
(2)安装apr-util: [root@http bao]# tar xf apr-util-1.5.4.tar.bz2 [root@http bao]# cd apr-util-1.5.4 [root@http apr-util-1.5.4]# ./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@http apr-util-1.5.4]# make [root@http apr-util-1.5.4]# make install
(3)安装pcre-devel: [root@http ~]# yum install -y pcre-devel
(4)安装httpd: [root@http bao]# tar xf httpd-2.4.12.tar.bz2 [root@http bao]# cd httpd-2.4.12 [root@http httpd-2.4.12]# ./configure--prefix=/usr/local/apache --sysconfdir=/etc/httpd --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 --with-included-apr -enable-proxy-enable-proxy-fcgi 说明: --prefix=/usr/local/apache :安装指定路径。 --sysconfdir=/etc/httpd :指定配置文件安装路径。 --enable-so :支持动态共享模块,如果没有此功能,php无法与apache一起工作,必须安。 --enable-ssl :启用ssl功能,不安装无法启用https。 --enable-rewrite :支持url重写。 --enable-cgi :支持cgi。 在执行configure脚本时,使用–enable-mpms-shared选项即可启用动态构建MPM模块。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。
…………………………………….. configure: configure: error: Bundled APR requested butnot found at ./srclib/. Download and unpack the corresponding apr and apr-utilpackages to ./srclib/. [root@http httpd-2.4.12]# 错误信息,解决: [root@http bao]# mv apr-1.5.2 apr [root@http bao]# mv apr-util-1.5.4 apr-util [root@http bao]# cp -r apr apr-util httpd-2.4.12/srclib/ [root@http bao]#
在编译安装: [root@http httpd-2.4.12]# ./configure--prefix=/usr/local/apache --sysconfdir=/etc/httpd --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 --with-included-apr -enable-proxy-enable-proxy-fcgi [root@http httpd-2.4.12]# make [root@http httpd-2.4.12]# make install
2、启动服务进行简单测试 [root@http ~]# setenforce 0 [root@http ~]# service iptables stop iptables: Setting chains to policy ACCEPT:filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] [root@http ~]# /usr/local/apache/bin/apachectl start 失败,日志: [root@http logs]# cat error_log [Wed Jul 08 22:38:15.155125 2015][proxy_balancer:emerg] [pid 45344:tid 139946921051904] AH01177: Failed tolookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded?? [Wed Jul 08 22:38:15.155262 2015] [:emerg][pid 45344:tid 139946921051904] AH00020: Configuration Failed, exiting [Wed Jul 08 22:39:07.108730 2015][proxy_balancer:emerg] [pid 45348:tid 139707455522560] AH01177: Failed tolookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded?? [Wed Jul 08 22:39:07.108852 2015] [:emerg][pid 45348:tid 139707455522560] AH00020: Configuration Failed, exiting [root@http logs]# 原因: 配置文件conf/httpd.conf没有开启 mod_slotmem_shm模块,编辑该文件,放开 LoadModule slotmem_shm_modulemodules/mod_slotmem_shm.so。 [root@http logs]# vim /etc/httpd/httpd.conf 128 LoadModule slotmem_shm_modulemodules/mod_slotmem_shm.so [root@http ~]# /usr/local/apache/bin/apachectl start [root@http ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1673/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1026/master tcp 0 0 :::80 :::* LISTEN 45379/httpd tcp 0 0 :::22 :::* LISTEN 1673/sshd tcp 0 0 ::1:25 :::* LISTEN 1026/master [root@http ~]#
浏览器访问: 192.168.1.217 It works!
3、为了更好的管理和使用httpd服务,可为其提供PATH环境变量和SysV风格的脚本。
(1) 提供PATH环境变量: [root@http ~]# vim /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin "/etc/profile.d/httpd.sh" [New]1L, 40C written [root@http ~]# . /etc/profile.d/httpd.sh [root@http ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin [root@http ~]#
(2) 提供SysV风格的启动脚本: [root@http ~]# vim /etc/init.d/httpd #!/bin/bash # # chkconfig: - 85 15 # description: Apache is a World Wide Webserver. It is used to serve \ . /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 swallowingup a pass-phrase prompt if # mod_ssl needs a pass-phrase from theuser. 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 mays not # work correctly with a thread-based MPM;notably PHP will refuse to start. # Path to the apachectl script, serverbinary, 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 sysntax 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|htlp|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo "Usage: $prog {start|stop|restart|reload|status|help}" exit 1 ;; esac exit $RETVAL "/etc/rc.d/init.d/httpd" [New]85L, 2084C written [root@http ~]# chmod +x /etc/init.d/httpd [root@http ~]# service httpd status httpd is stopped [root@http ~]# service httpd start Starting httpd: httpd (pid 45379) alreadyrunning [ OK ] [root@http ~]# [root@http ~]# chkconfig --add httpd [root@http ~]# chkconfig | grep httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@http ~]# chkconfig httpd on [root@http ~]# chkconfig | grep httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@http ~]#
4、更换pid进程文件存放目录: 每个进程启动后都会有个pid文件,编译安装httpd的pid文件在logs目录里面: [root@http logs]# pwd /usr/local/apache/logs [root@http logs]# ll total 12 -rw-r--r--. 1 root root 3527 Jul 8 22:53 access_log -rw-r--r--. 1 root root 999 Jul 8 22:48 error_log -rw-r--r--. 1 root root 6 Jul 8 22:48 httpd.pid [root@http logs]#
系统进程一般pid文件都放在/var/run目录下,如果想更换pid进程文件存放目录,需要在主配置文件中加入PidFile "/var/run/httpd.pid"项,注意:编译安装默认没有该项,需要手工加入: [root@http logs]# vim /etc/httpd/httpd.conf 34 pidfile "/var/run/httpd.pid" [root@http logs]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 :::80 :::* LISTEN 45379/httpd [root@http logs]# kill 45379 [root@http logs]# service httpd start Starting httpd: [ OK ] [root@http logs]# cd /var/run/ [root@http run]# ll -rw-r--r--. 1 root root 6 Jul 8 23:04 httpd.pid [root@http run]#
5、为httpd添加man帮助及输出头文件:
(1) 为httpd添加man帮助: [root@http ~]# yum install -y man pages (最小化安装系统如果没有man手册需要安装) [root@http ~]# vim /etc/man.config 48 MANPATH /usr/local/apache/man [root@http ~]# man httpd
(2) 输出头文件: [root@http ~]# ln -sv /usr/local/apache/include//usr/include/httpd `/usr/include/httpd' ->`/usr/local/apache/include/' [root@http ~]#
至此,httpd服务安装成功。 二、编译安装MySQL
1、创建mysql用户和mysql组 要想初始化mysql,需要有mysql用户和组。 [root@mysql ~]# groupadd -r -g 306 mysql [root@mysql ~]# useradd -g 306 -r -u 306 mysql [root@mysql ~]# id mysql uid=306(mysql) gid=306(mysql)groups=306(mysql) [root@mysql ~]#
2、创建mysql数据存放目录 这里练习就在根目录下创建了。 [root@mysql ~]# mkdir /mydata/data -pv mkdir: created directory `/mydata' mkdir: created directory `/mydata/data' [root@mysql ~]# ll -d /mydata/ /mydata/data/ drwxr-xr-x. 3 root root 4096 Jul 9 00:45 /mydata/ drwxr-xr-x. 2 root root 4096 Jul 9 00:45 /mydata/data/ [root@mysql ~]#
3、mysql数据目录需要把属主属组更改为mysql,权限更改为0-rwx: [root@mysql mydata]# pwd /mydata [root@mysql mydata]# ll total 4 drwxr-xr-x. 2 root root 4096 Jul 9 00:45 data [root@mysql mydata]# chown -R mysql:mysql data/ [root@mysql mydata]# chmod o-rwx data/ [root@mysql mydata]# ll total 4 drwxr-x---. 2 mysql mysql 4096 Jul 9 00:45 data [root@mysql mydata]#
4、安装cmake编译工具 cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。 [root@mysql bao]# ll total 49548 -rw-r--r--. 1 root root 5691656 Jul 9 00:55 cmake-2.8.8.tar.gz [root@mysql bao]# [root@mysql bao]# tar xf cmake-2.8.8.tar.gz [root@mysql bao]# cd cmake-2.8.8 [root@mysql cmake-2.8.8]# ./configure [root@mysql cmake-2.8.8]# make [root@mysql cmake-2.8.8]# make install
5、使用cmake编译mysql-5.5: cmake指定编译选项的方式不同于make,其实现方式对比如下: ./configure等于cmake . ./configure –help等同于cmake .-LH or ccmake .
[root@mysql bao]# tar xf mysql-5.5.33.tar.gz [root@mysql bao]# cd mysql-5.5.33 [root@mysql mysql-5.5.33]# cmake .-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1-DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0-DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci [root@mysql mysql-5.5.33]# make [root@mysql mysql-5.5.33]# make install
说明: [root@msql mysql-5.5.28]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql (安装路径) -DMYSQL_DATADIR=/mydata/data (数据目录存放位置) -DSYSCONFDIR=/etc (配置文件目录) -DWITH_INNOBASE_STORAGE_ENGINE=1 (是否包含INNOBASE存储引擎) -DWITH_ARCHIVE_STORAGE_ENGINE=1 (是否包含ARCHIVE存储引擎) -DWITH_BLACKHOLE_STORAGE_ENGINE=1 (是否包含BLACKHOLE存储引擎,黑洞) -DWITH_READLINE=1 (使用捆绑的readline,批量导入数据) -DWITH_SSL=system (启用ssl功能) -DWITH_ZLIB=system (启用zlib压缩功能) -DWITH_LIBWRAP=0 (不支持libwrap(TCP包装)) -DMYSQL_UNIX_ADDR=/tmp/mysql.sock (套接字路径) -DDEFAULT_CHARSET=utf8 (字符集为utf8) -DDEFAULT_COLLATION=utf8_general_ci (字符集排序为utf8_general_ci)
编译选项说明: 指定安装文件的安装路径时常用的选项: -DCMAKE_INSTALL_PREFIX=/usr/local/mysql (安装路径) -DMYSQL_DATADIR=/data/mysql (数据文件路径,不指定默认为安装路径下data目录下) -DSYSCONFDIR=/etc (配置文件路径)
默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项: -DWITH_INNOBASE_STORAGE_ENGINE=1 (安装INNOBASE存储引擎,就是InnoDB) -DWITH_ARCHIVE_STORAGE_ENGINE=1 (安装archive存储引擎) -DWITH_BLACKHOLE_STORAGE_ENGINE=1 (黑洞存储引擎,类似/dev/null) -DWITH_FEDERATED_STORAGE_ENGINE=1 (5.5不支持,联合存储引擎)
若要明确指定不编译某存储引擎,可以使用类似如下的选项: -DWITHOUT_<ENGINE>_STORAGE_ENGINE=1 比如: -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 (不编译EXAMPLE引擎) -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1
如若要编译进其它功能,如SSL等,则可使用类似如下选项来实现编译时使用某库或不使用某库: -DWITH_READLINE=1 (使用捆绑的readline,批量导入数据) -DWITH_SSL=system (是否支持SSL) -DWITH_ZLIB=system (是否支持Zlib,压缩库) -DWITH_LIBWRAP=0 (是否包括支持libwrap(TCP包装),不使用)
其它常用的选项: -DMYSQL_TCP_PORT=3306 (TCP/IP 端口号) -DMYSQL_UNIX_ADDR=/tmp/mysql.sock (Unix Socket 套接字文件) -DENABLED_LOCAL_INFILE=1 (是否启用本地录入文件) -DEXTRA_CHARSETS=all (额外的字符集,各种字符) -DDEFAULT_CHARSET=utf8 (指定MYSQL默认字符集) -DDEFAULT_COLLATION=utf8_general_ci (默认排序字符集规则) -DWITH_DEBUG=0 (是否包括调试支持) -DENABLE_PROFILING=1 (是否启用代码查询分析,性能分析)
什么是字符集: 字符(Character)是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。字符集(Character set)是多个字符的集合,字符集种类较多,每个字符集包含的字符个数不同,常见字符集名称:ASCII字符集、GB2312字符集、BIG5字符集、 GB18030字符集、Unicode字符集等。计算机要准确的处理各种字符集文字,需要进行字符编码,以便计算机能够识别和存储各种文字。 汉字字符集:GBK,GB2312,GB18030,UTF8。
如果想清理此前的编译所生成的文件,则需要使用如下命令: make clean rm CMakeCache.txt
6、安装完后会在指定安装路径/usr/local目录下有个mysql目录,接下来步骤就和二进制安装方法相同,更改mysql目录下文件的属组为mysql:
[root@mysql mysql]# chown -R :mysql /usr/local/mysql/ [root@mysql mysql]# ll total 200 drwxr-xr-x. 2 root mysql 4096 Jul 9 01:12 bin -rw-r--r--. 1 root mysql 17987 Jul 15 2013 COPYING drwxr-xr-x. 3 root mysql 4096 Jul 9 01:11 data drwxr-xr-x. 2 root mysql 4096 Jul 9 01:11 docs drwxr-xr-x. 3 root mysql 4096 Jul 9 01:11 include -rw-r--r--. 1 root mysql 134493 Jul 15 2013INSTALL-BINARY drwxr-xr-x. 3 root mysql 4096 Jul 9 01:11 lib drwxr-xr-x. 4 root mysql 4096 Jul 9 01:12 man drwxr-xr-x. 10 root mysql 4096 Jul 9 01:11 mysql-test -rw-r--r--. 1 root mysql 2496 Jul 15 2013 README drwxr-xr-x. 2 root mysql 4096 Jul 9 01:12 scripts drwxr-xr-x. 27 root mysql 4096 Jul 9 01:12 share drwxr-xr-x. 4 root mysql 4096 Jul 9 01:12 sql-bench drwxr-xr-x. 3 root mysql 4096 Jul 9 01:12 support-files [root@mysql mysql]#
7、手动完成mysql数据库初始化: [root@mysql mysql]# pwd /usr/local/mysql [root@mysql mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
8、提供mysql主配置文件,把实例文件复制替换到/etc目录下为my.cnf文件: [root@mysql mysql]# cp support-files/my-large.cnf /etc/my.cnf cp: overwrite `/etc/my.cnf'? y [root@mysql mysql]# ll /etc/my.cnf -rw-r--r--. 1 root root 4647 Jul 9 01:18 /etc/my.cnf [root@mysql mysql]#
9、在配置文件中/etc/my.cnf指定数据文件位置: [root@mysql mysql]# vim /etc/my.cnf 38 datadir=/mydata/data "/etc/my.cnf" 147L, 4668Cwritten [root@mysql mysql]#
10、复制mysql服务脚本到init.d目录下: [root@mysql mysql]# cp support-files/mysql.server/etc/init.d/mysqld [root@mysql mysql]# ll -d /etc/init.d/mysqld -rwxr-xr-x. 1 root root 10871 Jul 9 01:21 /etc/init.d/mysqld [root@mysql mysql]# chkconfig --add mysqld [root@mysql mysql]# chkconfig mysqld on [root@mysql mysql]#
11、启动mysql服务: [root@mysql ~]# service mysqld start Starting MySQL.. SUCCESS! [root@mysql ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21145/mysqld [root@mysql ~]#
12、把mysql的bin下命令添加到PATH路径中去,让系统可以直接使用mysql的相关命令: [root@mysql ~]# vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin "/etc/profile.d/mysql.sh" [New]1L, 39C written [root@mysql ~]# . /etc/profile.d/mysql.sh [root@mysql ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin [root@mysql ~]#
13、登陆mysql测试连接: [root@mysql ~]# mysql ……………………………
mysql>
14、修改MySQL的root用户的密码以及打开远程连接: mysql> use mysql; Database changed mysql> desc user; mysql> GRANT ALL PRIVILEGES ON *.* TOroot@"%" IDENTIFIED BY "123456"; Query OK, 0 rows affected (0.00 sec)
mysql> update user set Password = password('123456')where User='root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 5 Changed: 4 Warnings: 0
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql>
15、为mysql添加man帮助及输出头文件和库文件 [root@mysql ~]# yum install -y man pages [root@mysql ~]# vim /etc/man.config 48 MANPATH /usr/local/mysql/man [root@mysql ~]# ln -sv /usr/local/mysql/include//usr/include/mysql `/usr/include/mysql' ->`/usr/local/mysql/include/' [root@mysql ~]# vim /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib "/etc/ld.so.conf.d/mysql.conf"[New] 1L, 21C written [root@mysql ~]#
至此,mysql安装完毕 三、安装PHP php以php-fpm服务的形式运行,单独调度。如果分开部署可以大大减轻httpd服务器的压力。缺点网络通信不畅的情况下,带宽有可能成为瓶颈。
1、解压并编译安装php-5.6.8: [root@php bao]# ll total 14024 -rw-r--r--. 1 root root 634589 Jul 9 01:40 libmcrypt-2.5.6.tar.gz -rw-r--r--. 1 root root 13724681 Jul 9 01:39 php-5.6.8.tar.bz2 [root@php bao]# [root@php bao]# tar xf php-5.6.8.tar.bz2 [root@php bao]# cd php-5.6.8 [root@php php-5.6.8]# ./configure --prefix=/usr/local/php--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 --with-mcrypt --with-config-file-path=/etc--with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts-enable-fpm -with-pdo-mysql=mysqlnd -with-freetype-dir
错误: checking for xml2-config path... configure: error: xml2-config not found.Please check your libxml2 installation. [root@php php-5.6.8]# [root@php php-5.6.8]# yum install libxml2-devel –y 在编译: 错误: checking for BZip2 support... yes checking for BZip2 in default path... notfound configure: error: Please reinstall theBZip2 distribution [root@php php-5.6.8]# [root@php php-5.6.8]# yum install bzip2-devel -y 在编译: 错误: ……………… checking for mcrypt support... yes configure: error: mcrypt.h not found.Please reinstall libmcrypt. [root@php php-5.6.8]# [root@php bao]# tar xf libmcrypt-2.5.6.tar.gz [root@php bao]# cd libmcrypt-2.5.6 [root@php libmcrypt-2.5.6]# ./configure [root@php libmcrypt-2.5.6]# make [root@php libmcrypt-2.5.6]# make install
在编译: [root@php php-5.6.8]# ./configure --prefix=/usr/local/php--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 --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d--with-bz2 --enable-maintainer-zts -enable-fpm -with-pdo-mysql=mysqlnd-with-freetype-dir [root@php php-5.6.8]# make [root@php php-5.6.8]# make install
安装选项说明: --prefix=/usr/local/php (安装目录) --with-openssl (支持openssl功能) --with-mysql 几个选项都要为mysqlnd;因为mysql服务器单独为另一台主机 --enable-mbstring --with-freetype-dir (支持freetype功能,字体库,引用特定字体) --with-jpeg-dir (支持jpeg) --with-png-dir (支持png) --with-zlib (支持通用压缩库) --with-libxml-dir=/usr (扩展标记语言,xml库路径位置) --enable-xml (支持扩展标记语言) --enable-sockets (套接字) --with-apxs2=/usr/local/httpd24/bin/apxs 替换为 --enable-fpm --with-mcrypt (加密库) --with-config-file-path=/etc (配置文件位置) --with-config-file-scan-dir=/etc/php.d (文件php.d目录下也是配置文件一部分) --with-bz2 (压缩) --enable-maintainer-zts (apahce是prwork模式不需要用,其他需要用)
2、给php提供配置文件: 在php安装程序中,有两个文件叫php.ini-development和php.ini-production: php.ini-development :开发用的配置文件。 php.ini-production :生产环境用的配置文件。
[root@php ~]# cp /root/bao/php-5.6.8/php.ini-production/etc/php.ini [root@php ~]# ll -d /etc/php.ini -rw-r--r--. 1 root root 72369 Jul 9 21:38 /etc/php.ini [root@php ~]#
3、为php-fpm提供Sysv init脚本: [root@php ~]# cp /root/bao/php-5.6.8/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm [root@php ~]# chmod +x /etc/init.d/php-fpm [root@php ~]# chkconfig --add php-fpm [root@php ~]# chkconfig php-fpm on [root@php ~]#
4、为php-fpm提供配置文件: [root@php ~]# cp /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf [root@php ~]# [root@php ~]# vim /usr/local/php/etc/php-fpm.conf 235 pm.max_children = 50 240 pm.start_servers = 5 245 pm.min_spare_servers = 2 250 pm.max_spare_servers = 8 26 pid = /usr/local/php/var/run/php-fpm.pid listen = 192.168.1.219:9000 "/usr/local/php/etc/php-fpm.conf"538L, 22836C written [root@php ~]#
5、启动php-fpm服务: [root@php ~]# service php-fpm start Starting php-fpm done [root@php ~]# netstat -lntp tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 43631/php-fpm [root@php ~]#
6、到web服务器192.168.1.217上配置httpd,使其能够支持php: [root@http ~]# vim /etc/httpd/httpd.conf 250 <IfModule dir_module> 251 DirectoryIndex index.php index.html 252 </IfModule>
让apache能够处理php结尾的页面文件,添加php类型:: 380 AddType application/x-httpd-php.php 381 AddType application/x-httpd-php-source .phps
查看是否启用proxy_module proxy-fcgi_module等模块: 116 LoadModule proxy_modulemodules/mod_proxy.so 117 LoadModule proxy_connect_modulemodules/mod_proxy_connect.so 118 LoadModule proxy_ftp_modulemodules/mod_proxy_ftp.so 119 LoadModule proxy_http_modulemodules/mod_proxy_http.so 120 LoadModule proxy_fcgi_modulemodules/mod_proxy_fcgi.so 121 LoadModule proxy_scgi_modulemodules/mod_proxy_scgi.so
7、配置虚拟主机,创建虚拟主机目录: [root@http ~]# vim /etc/httpd/httpd.conf 217 #DocumentRoot"/usr/local/apache/htdocs"
468 # Virtual hosts 469 Include/etc/httpd/extra/httpd-vhosts.conf "/etc/httpd/httpd.conf" 507L,18451C written [root@http ~]# [root@http extra]# pwd /etc/httpd/extra [root@http extra]# vim httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/www/da.ok/ ServerName www.da.ok ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.1.219:9000/www/da.ok/$1 <Directory "/www/da.ok"> Options none AllowOverride none Require all granted </Directory> ErrorLog "/usr/local/apache/logs/da.ok-error_log" CustomLog "/usr/local/apache/logs/da.ok-access_log" common </VirtualHost> "httpd-vhosts.conf" 56L, 1904Cwritten [root@http extra]# 说明: ProxyRequests Off:关闭正向代理。
注意:需要分别在wen服务器和PHP主机上创建相关目录: [root@http ~]# mkdir -pv /www/da.ok mkdir: created directory `/www' mkdir: created directory `/www/da.ok' [root@http ~]#
[root@php ~]# mkdir -pv /www/da.ok mkdir: created directory `/www' mkdir: created directory `/www/da.ok' [root@php da.ok]# pwd /www/da.ok [root@php da.ok]# vim index.php <h1>www.da.ok</h1> <?php phpinfo(); ?> [root@php da.ok]#
浏览器访问:192.168.1.217测试。 我们把网页放在 192.168.1.219主机上,/www/da/ok下面就可以了,现在当访问192.168.1.217/index.php时,将会通过fcgi协议反向代理到192.168.1.219主机的相应目录下面。
四、配置MySQL整合PHP:
授权PHP主机可以访问MySQL(192.168.1.218数据库服务器): mysql> use mysql; Database changed mysql> grant all privileges on *.* to'root'@'192.168.1.219' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql>
测试在php主机上是否可以连接mysql(192.168.1.219PHP服务器上): [root@php da.ok]# pwd /www/da.ok [root@php da.ok]# vim index.php <h1>www.da.ok</h1> <?php
$link = mysql_connect('192.168.1.218','root','123456');
if($link)
echo"HaHa..OK!.....";
else
echo "..NO OK!....";
mysql_close;
phpinfo();
?> "index.php" 18L, 247Cwritten [root@php da.ok]#
浏览器访问:192.168.1.217,如果显示HaHa..OK!.....,表示成功。 五、为PHP安装xcache加速器以提高性能
[root@php bao]# ll -rw-r--r--. 1 root root 173368 Jul 10 22:04xcache-3.2.0.tar.gz [root@php bao]#
用phpize生成configure配置文件: [root@php bao]# tar xf xcache-3.2.0.tar.gz [root@php bao]# cd xcache-3.2.0 [root@php xcache-3.2.0]#/usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@php xcache-3.2.0]#
[root@php xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@php xcache-3.2.0]# make [root@php xcache-3.2.0]# make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/ [root@php xcache-3.2.0]#
整合php和xcache,将xcache提供的样例配置导入php.ini,修改extension路径: [root@php ~]# mkdir /etc/php.d [root@php ~]# cp /root/bao/xcache-3.2.0/xcache.ini/etc/php.d/ [root@php ~]# vim /etc/php.d/xcache.ini 4 extension =/usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so "/etc/php.d/xcache.ini" 84L,3168C written [root@php ~]#
重启php-fpm服务: [root@php ~]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done [root@php ~]#
浏览器访问:192.168.1.217 六、利用phpMyAdmin工具测试httpd+php+mysql的连接:
把phpMyAdmin-4.4.4-all-languages.zip包分别下载到web192.168.1.217和PHP192.168.1.219主机上网页目录中: [root@http bao]# ll -rw-r--r--. 1 root root 9955372 Jul 10 22:20phpMyAdmin-4.4.4-all-languages.zip [root@http bao]#
web192.168.1.217上: [root@http bao]# unzip phpMyAdmin-4.4.4-all-languages.zip [root@http bao]# mv phpMyAdmin-4.4.4-all-languages/www/da.ok/pam [root@http bao]# cd /www/da.ok/ [root@http da.ok]# ll drwxr-xr-x. 10 root root 4096 Apr 26 06:45pam [root@http da.ok]#
PHP192.168.1.219上: [root@php bao]# unzip phpMyAdmin-4.4.4-all-languages.zip [root@php bao]# mv phpMyAdmin-4.4.4-all-languages/www/da.ok/pam [root@php bao]# cd /www/da.ok/ [root@php da.ok]# ll -rw-r--r--. 1 root root 247 Jul 10 21:53index.php drwxr-xr-x. 10 root root 4096 Apr 26 06:45pam [root@php da.ok]#
注意:这是两台不同主机;一个Apache;一个PHP,都要有!
需要连接数据库的操作在PHP主机上192.168.1.219上操作,连接数据库: [root@php pam]# pwd /www/da.ok/pam [root@php pam]# cp config.sample.inc.php config.inc.php [root@php pam]# vim config.inc.php 29 $cfg['Servers'][$i]['auth_type'] = 'cookie'; 30/* Server parameters */ 31$cfg['Servers'][$i]['host'] = '192.168.1.218'; (修改为数据库主机地址,要有权限) 32$cfg['Servers'][$i]['connect_type'] = 'tcp'; 33$cfg['Servers'][$i]['compress'] = false; 34$cfg['Servers'][$i]['AllowNoPassword'] = false; "config.inc.php" 152L, 4316Cwritten [root@php pam]#
浏览器登陆测试:http://192.168.1.217/pam/
七、discuz安装
把Discuz_X3.2_SC_UTF8.zip包分别下载到web192.168.1.217和PHP192.168.1.219主机上网页目录中: [root@http bao]# ll -rw-r--r--. 1 root root 12490656 Jul 10 22:34Discuz_X3.2_SC_UTF8.zip [root@http bao]#
web192.168.1.217上: [root@http bao]# unzipDiscuz_X3.2_SC_UTF8.zip [root@http bao]# mv upload/ /www/da.ok/bbs [root@http bao]# cd /www/da.ok/ [root@http da.ok]# ll total 8 drwxr-xr-x. 12 root root 4096 Dec 26 2014 bbs drwxr-xr-x. 10 root root 4096 Apr 26 06:45pam [root@http da.ok]#
PHP192.168.1.219上: [root@php bao]# unzipDiscuz_X3.2_SC_UTF8.zip [root@php bao]# mv upload/ /www/da.ok/bbs [root@php bao]# cd /www/da.ok/ [root@php da.ok]# ll total 12 drwxr-xr-x. 12 root root 4096 Dec 26 2014 bbs -rw-r--r--. 1 root root 247 Jul 10 21:53index.php drwxr-xr-x. 10 root root 4096 Jul 10 22:29pam [root@php da.ok]#
在PHP主机上192.168.1.219给安装权限: [root@php da.ok]# pwd /www/da.ok [root@php da.ok]# cd bbs/ [root@php bbs]# chmod -R 777 * [root@php bbs]#
在数据库主机上192.168.1.218创建数据库: mysql> create database discudb; Query OK, 1 row affected (0.02 sec) mysql>
浏览器安装: 192.168.1.217/bbs
指定数据库服务器:192.168.1.218 数据库名:discudb (责任编辑:IT) |