一、LAMP企業(yè)架構(gòu)集群講解
Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一組常用來(lái)搭建動(dòng)態(tài)網(wǎng)站或者服務(wù)器的開(kāi)源軟件虱饿,本身都是各自獨(dú)立的程序拥诡,但是因?yàn)槌1环旁谝黄鹗褂茫瑩碛辛嗽絹?lái)越高的兼容度氮发,共同組成了一個(gè)強(qiáng)大的Web應(yīng)用程序平臺(tái).(為了提高性能也可以用Nginx代替Apache發(fā)布網(wǎng)頁(yè))
隨著開(kāi)源潮流的蓬勃發(fā)展渴肉,開(kāi)放源代碼的LAMP已經(jīng)與J2EE和.Net商業(yè)軟件形成三足鼎立之勢(shì),并且該軟件開(kāi)發(fā)的項(xiàng)目在軟件方面的投資成本較低爽冕,因此受到整個(gè)IT界的關(guān)注仇祭。
目前LAMP架構(gòu)是大多數(shù)中小企業(yè)最青睞的PHP架構(gòu)選擇,也是眾多Linux SA喜歡選擇的一套架構(gòu).
- YUM安裝
yum install httpd httpd-devel mysql mysql-server mysql-devel php php-devel php-mysql -y
安裝完成后可以直接使用,源碼安裝的話需要整合apache和php.
二颈畸、源碼安裝整合LAMP
- 檢查安裝環(huán)境卸載已安裝的服務(wù)如httpd,mysql(利用rpm命令不帶依賴關(guān)系移除)
[root@mini ~]# rpm -qa|grep httpd
httpd-tools-2.2.15-29.el6.centos.x86_64
httpd-2.2.15-29.el6.centos.x86_64
[root@mini ~]# rpm -e --nodeps httpd-2.2.15-29.el6.centos.x86_64
[root@mini ~]# rpm -e --nodeps httpd-tools-2.2.15-29.el6.centos.x86_64
[root@mini src]# rpm -qa|grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@mini ~]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
- 切換下載目錄下載所用的各種安裝包,這里為了測(cè)試LAMP環(huán)境下載discuz論壇源碼包作為測(cè)試或者wordpross博客論壇源碼
[root@mini ~]# cd /usr/src/ #切換下載目錄依次下載discuz論壇源碼包,http/mysql/php源碼包
[root@mini src]# wget http://download.comsenz.com/DiscuzX/3.4/Discuz_X3.4_SC_UTF8.zip
[root@mini src]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.2.34.tar.gz
[root@mini src]# wget http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.63.tar.gz
[root@mini src]# wget http://mirrors.sohu.com/php/php-5.3.28.tar.bz2
[root@mini src]# wget https://cn.wordpress.org/wordpress-4.9.1-zh_CN.tar.gz
[root@mini src]# ls
debug httpd-2.2.34.tar.gz mysql-5.1.63.tar.gz wordpress-4.9.1-zh_CN.tar.gz
Discuz_X3.4_SC_UTF8.zip kernels php-5.3.28.tar.bz2
- 安裝httpd服務(wù)
參考資料
[root@mini src]# tar -zxvf httpd-2.2.34.tar.gz #解壓
[root@mini src]# cd httpd-2.2.34 #切換源碼包
[root@mini httpd-2.2.34]# ls
ABOUT_APACHE CHANGES httpd.dsp libhttpd.dep NOTICE server
acinclude.m4 config.layout httpd.mak libhttpd.dsp NWGNUmakefile srclib
Apache.dsw configure httpd.spec libhttpd.mak os support
build configure.in include LICENSE README test
BuildAll.dsp docs INSTALL Makefile.in README.platforms VERSIONING
BuildBin.dsp emacs-style InstallBin.dsp Makefile.win README-win32.txt
buildconf httpd.dep LAYOUT modules ROADMAP
[root@mini httpd-2.2.34]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite
#預(yù)編譯指定安裝目錄,添加動(dòng)態(tài)擴(kuò)展模塊,rewrite模塊
[root@mini httpd-2.2.34]# make #編譯
[root@mini httpd-2.2.34]# make install #安裝
[root@mini local]# chown -R apache.apache /usr/local/apache/
#更改apache安裝目錄的所有者和屬組
#修改配置文件更改服務(wù)使用者,apache用戶系統(tǒng)已創(chuàng)建
[root@mini local]# vim /usr/local/apache/conf/httpd.conf
66 User apache
67 Group apache
啟動(dòng)故障排查
安裝完成后檢查配置文件報(bào)錯(cuò)如下
[root@mini ~]# /usr/local/apache/bin/apachectl -t
httpd: apr_sockaddr_info_get() failed for mini
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
如果不解決直接啟動(dòng)報(bào)錯(cuò)如下
[root@mini ~]# /usr/local/apache/bin/apachectl start
httpd: apr_sockaddr_info_get() failed for mini
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
解決方案:在配置文件/usr/local/apache/conf/httpd.conf中添加語(yǔ)句
ServerName localhost:80
-將httpd服務(wù)加入系統(tǒng)服務(wù)并設(shè)置開(kāi)機(jī)自啟動(dòng)
[root@mini ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
#將apache的啟動(dòng)腳本復(fù)制到/etc/rc.d/init.d這個(gè)目錄下
#直接修改啟動(dòng)腳本在最上面添加如下兩行帶#的注釋語(yǔ)句
[root@mini ~]# vi /etc/init.d/httpd
#chkconfig: 2345 70 60
#description: apache
[root@mini ~]# chkconfig --add httpd
#添加系統(tǒng)服務(wù)
[root@mini ~]# chkconfig httpd on
#設(shè)置開(kāi)機(jī)自啟動(dòng)
[root@mini local]# /etc/init.d/httpd -t
Syntax OK
#檢查配置文件無(wú)問(wèn)題
[root@mini local]# /etc/init.d/httpd start
[root@mini local]# ps -ef|grep httpd
root 24813 1 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
apache 24814 24813 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
apache 24815 24813 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
apache 24816 24813 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
apache 24817 24813 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
apache 24818 24813 0 09:53 ? 00:00:00 /usr/local/apache/bin/httpd -k start
root 24820 1305 0 09:53 pts/0 00:00:00 grep httpd
#啟動(dòng)httpd服務(wù)查看進(jìn)程正常
- 安裝mysql服務(wù)
參考資料
mysql源碼安裝編譯參數(shù)詳解參考
MySQL的常見(jiàn)的三種方式
[root@mini src]# tar zxvf mysql-5.1.63.tar.gz
[root@mini src]# cd mysql-5.1.63
[root@mini mysql-5.1.63]# ls
aclocal.m4 config.guess Docs libmysqld mysql-test server-tools tests
BUILD config.sub extra libmysql_r mysys sql unittest
ChangeLog configure include ltmain.sh netware sql-bench vio
client configure.in install-sh Makefile.am plugin sql-common win
CMakeLists.txt COPYING INSTALL-SOURCE Makefile.in README storage ylwrap
cmd-line-utils dbug INSTALL-WIN-SOURCE man regex strings zlib
config depcomp libmysql missing scripts support-files
[root@mini mysql-5.1.63]# ./configure --prefix=/usr/local/mysql --enable-assembler
#預(yù)編譯指定安裝目錄,使用匯編模式提高性能
[root@mini mysql-5.1.63]# make #編譯
[root@mini mysql-5.1.63]# make install #安裝
故障排除
mysql預(yù)編譯時(shí)報(bào)錯(cuò)
報(bào)錯(cuò)信息如下(echo $?可查看命令執(zhí)行結(jié)果,返回0正常,其余任何數(shù)字都是異常)
checking for tgetent in -lncursesw... no
checking for tgetent in -lncurses... no
checking for tgetent in -lcurses... no
checking for tgetent in -ltermcap... no
checking for tgetent in -ltinfo... no
checking for termcap functions library... configure: error: No curses/termcap library found
[root@mini mysql-5.1.63]# echo $?
1
故障原因:缺少ncurses安裝包
解決辦法:下載安裝相應(yīng)軟件包
[root@mini mysql-5.1.63]# yum install ncurses ncurses-devel -y
將mysql配置為系統(tǒng)服務(wù)
[root@mini mysql-5.1.63]# cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
#拷貝源碼包的配置文件到系統(tǒng)配置目錄下
[root@mini mysql-5.1.63]# cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
#拷貝源碼包的服務(wù)文件到系統(tǒng)啟動(dòng)目錄下
[root@mini mysql-5.1.63]# chkconfig --add mysqld
#將mysql文件加入開(kāi)機(jī)啟動(dòng)項(xiàng)列表
[root@mini mysql-5.1.63]# chkconfig --level 345 mysqld on
#設(shè)置各級(jí)別開(kāi)機(jī)自啟動(dòng)
[root@mini mysql-5.1.63]# cd /usr/local/mysql/
#切換源碼安裝目錄
[root@mini mysql]# useradd mysql
#創(chuàng)建普通用戶mysql
[root@mini mysql]# chown -R mysql.mysql /usr/local/mysql/
#更改mysql目錄的所有者及屬組
[root@mini bin]# /usr/local/mysql/bin/mysql_install_db --user=mysql
#初始化數(shù)據(jù)庫(kù)
[root@mini mysql]# chown -R mysql var
#更改安裝目錄下var目錄的所有者,var目錄是初始化以后才創(chuàng)建的
[root@mini mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql&
#以安全模式后臺(tái)啟動(dòng)mysql服務(wù)
[root@mini mysql]# /usr/local/mysql/bin/mysql
#用安裝包下的絕對(duì)路徑進(jìn)入數(shù)據(jù)庫(kù)
[root@mini mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin
#創(chuàng)建軟鏈接,系統(tǒng)默認(rèn)會(huì)查找/usr/bin下的命令,這樣就能直接通過(guò)mysql進(jìn)入數(shù)據(jù)庫(kù)
補(bǔ)充源碼停止mysql數(shù)據(jù)庫(kù)命令/usr/local/mysql/share/mysql/mysql.server stop
- 安裝PHP服務(wù)(展示動(dòng)態(tài)網(wǎng)頁(yè))
PHP源碼編譯安裝參數(shù)參考
[root@mini src]# tar jxf php-5.3.28.tar.bz2
[root@mini src]# cd php-5.3.28
[root@mini php-5.3.28]# ls
acconfig.h INSTALL README.EXTENSIONS scripts
acconfig.h.in install-sh README.EXT_SKEL server-tests-config.php
acinclude.m4 LICENSE README.GIT-RULES server-tests.php
aclocal.m4 ltmain.sh README.input_filter snapshot
build main README.MAILINGLIST_RULES stamp-h.in
buildconf makedist README.namespaces stub.c
buildconf.bat Makefile.frag README.PARAMETER_PARSING_API svnclean.bat
CODING_STANDARDS Makefile.gcov README.PHP4-TO-PHP5-THIN-CHANGES tests
config.guess Makefile.global README.REDIST.BINS TODO
config.sub makerpm README.RELEASE_PROCESS TODO-5.1
configure missing README.SELF-CONTAINED-EXTENSIONS TODO-PHP5
configure.in mkinstalldirs README.STREAMS TSRM
CREDITS netware README.SUBMITTING_PATCH UPGRADING
ext NEWS README.TESTING UPGRADING.INTERNALS
EXTENSIONS pear README.TESTING2 vcsclean
footer php5.spec.in README.UNIX-BUILD-SYSTEM win32
generated_lists php.gif README.WIN32-BUILD-SYSTEM Zend
genfiles php.ini-development run-tests.php
header php.ini-production sapi
[root@mini php-5.3.28]# ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql/
#預(yù)編譯,指定安裝目錄,指定php.ini位置,整合 apache加載指定模塊到 apache乌奇,要求 apache 要打開(kāi)SO模塊,指定mysql安裝目錄,對(duì)mysql的支持
[root@mini php-5.3.28]# make
[root@mini php-5.3.28]# make install
故障排查
安裝PHP模塊預(yù)編譯時(shí)報(bào)錯(cuò)信息如下
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.
[root@mini php-5.3.28]# echo $?
1
檢查是否安裝了libxm包
[root@mini php-5.3.28]# rpm -qa|grep libxml2
libxml2-python-2.7.6-14.el6.x86_64
libxml2-2.7.6-14.el6.x86_64
重新安裝libxml2和libxml2-devel包
[root@mini php-5.3.28]# yum install libxml2 libxml2-devel -y
安裝完之后查找xml2-config文件是否存在
[root@mini php-5.3.28]# find / -name "xml2-config"
/usr/bin/xml2-config
至此問(wèn)題解決重新對(duì)PHP模塊進(jìn)行編譯安裝
補(bǔ)充:當(dāng)mysql數(shù)據(jù)庫(kù)服務(wù)器在其他主機(jī)時(shí)php編譯參數(shù)支持mysql擴(kuò)展
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
mysqlnd是php5.3以后版本加入的眯娱,5.3以下版本必須還是要編譯mysql华弓。
- 修改配置文件整合apache及php
修改httpd.conf配置文件
[root@mini php-5.3.28]# vim /usr/local/apache/conf/httpd.conf
54 LoadModule php5_module modules/libphp5.so
#PHP預(yù)編譯安裝參數(shù) --with-apxs2=/usr/local/apache/bin/apxs,指定http加載模塊后生成的配置語(yǔ)句,之前測(cè)試實(shí)驗(yàn)時(shí)參數(shù)--with-apxs2輸入有誤,寫(xiě)成-with-apx2導(dǎo)致整合失敗
319 AddType application/x-compress .Z
320 AddType application/x-gzip .gz .tgz
321 AddType application/x-httpd-php .php
#319、320是已存在的,321是新增語(yǔ)句
169 DirectoryIndex index.php index.html
#指定默認(rèn)訪問(wèn)頁(yè)面優(yōu)先級(jí),添加index.php到最前面
創(chuàng)建index.php測(cè)試頁(yè)面
[root@mini php-5.3.28]# vim /usr/local/apache/htdocs/index.php
<?php
phpinfo();
?>
- 整合完成重啟httpd服務(wù)困乒,訪問(wèn)頁(yè)面http://192.168.15.142得到php配置信息頁(yè)面整合成功
三寂屏、配置MySQL主從服務(wù)器
按照上述步驟192.16815.142主機(jī)已安裝整合好LAMP環(huán)境,再為其配置從數(shù)據(jù)庫(kù)服務(wù)器
克隆一臺(tái)最小化安裝的主機(jī)后源碼安裝mysql數(shù)據(jù)庫(kù)服務(wù)
主從服務(wù)器分別登錄mysql,設(shè)置root用戶密碼mysql> grant all on *.* to root@'localhost' identified by '123456';
,刷新授權(quán)mysql> flush privileges;
修改master配置文件
[root@mini ~]# vim /etc/my.cnf
18 [client]
20 port = 3306
21 socket = /tmp/mysql.sock
22 default-character-set=utf8
#22行為新增修改客戶端字符集
27 [mysqld]
50 log-bin=mysql-bin
#50行默認(rèn)打開(kāi)bin-log日志
58 server-id = 1
59 character-set-server=utf8
#58行默認(rèn),但是slave的id必須改掉不能一樣,59行也是新增修改字符集
133 [mysql]
137 default-character-set=utf8
#137行新增修改字符集
修改slave配置文件配置字符集,修改id號(hào)
主從服務(wù)器分別重啟mysqld服務(wù)/etc/init.d/mysqld restart
master服務(wù)器授權(quán)slave同步自己mysql> grant replication slave on *.* to 'tongbu'@'%' identified by '123456';
查看同步點(diǎn)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 256 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
slave服務(wù)器
#設(shè)置同步主服務(wù)器
mysql> change master to master_host='192.168.15.143',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=256;
Query OK, 0 rows affected (0.39 sec)
#開(kāi)啟同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
#查看I/O線程娜搂、SQL線程已開(kāi)啟
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.15.143 #master的IP地址
Master_User: tongbu #同步使用的用戶名(mysql數(shù)據(jù)庫(kù)里的用戶)
Master_Port: 3306 #使用的端口號(hào)
Connect_Retry: 60
Master_Log_File: mysql-bin.000006 #開(kāi)始同步的bin-log日志
Read_Master_Log_Pos: 256 #開(kāi)始的同步點(diǎn)
Relay_Log_File: mini-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes ####I/O線程開(kāi)啟
Slave_SQL_Running: Yes ####SQL線程開(kāi)啟
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 256
Relay_Log_Space: 405
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
至此mysql服務(wù)器主從配置完成
四迁霎、在已配置好的LAMP環(huán)境下搭建discuz論壇
[root@mini data]# cd /usr/src/ #切換下載目錄
[root@mini src]# unzip Discuz_X3.4_SC_UTF8.zip #解壓論壇源碼包
[root@mini src]# mv upload/ /data/ #把內(nèi)容主目錄移動(dòng)到/data下
[root@mini data]# mv /data/upload/ /data/discuz #重命名目錄
[root@mini data]# ln -s /data/discuz/ /usr/local/apache/htdocs/
#創(chuàng)建鏈接到apache的發(fā)布目錄
至此可通過(guò)http://192.168.15.143/discuz
訪問(wèn)論壇創(chuàng)建向?qū)ы?yè)面
[root@mini discuz]# cd /usr/local/apache/htdocs/discuz/
#切換網(wǎng)站發(fā)布目錄
[root@mini discuz]# chmod -R 777 config/ data/ uc_client/ uc_server/
#更改相關(guān)目錄的權(quán)限
[root@mini data]# mysql -uroot -p
Enter password:
#進(jìn)入mysql數(shù)據(jù)庫(kù)
mysql> create database discuz charset=utf8;
Query OK, 1 row affected (0.00 sec)
#創(chuàng)建論壇數(shù)據(jù)庫(kù)并指定字符集
mysql> grant all on discuz.* to root@'localhost' identified by "123456";
Query OK, 0 rows affected (0.00 sec)
#授權(quán)
進(jìn)入向?qū)ы?yè)面根據(jù)提示設(shè)置管理員密碼完成安裝
五、優(yōu)化數(shù)據(jù)庫(kù)連接
當(dāng)LAMP主機(jī)master上的數(shù)據(jù)庫(kù)出故障時(shí)候,可以通過(guò)修改discuz源碼配置文件將數(shù)據(jù)庫(kù)的鏈接IP指向slave服務(wù)器的數(shù)據(jù)庫(kù)(slave同樣要對(duì)master授權(quán)訪問(wèn))來(lái)快速的恢復(fù)故障.
discuz論壇配置文件位置/usr/local/apache/htdocs/discuz/conf/config_ucenter.php
修改UC_DBHOST
把原本的localhost換成slave_ip或者域名
但以上方法每次都需要改動(dòng)配置文件比較麻煩,換思路在創(chuàng)建論壇時(shí),數(shù)據(jù)庫(kù)的IP地址用域名代替,這樣即時(shí)有問(wèn)題只需要改動(dòng)主機(jī)的host文件修改數(shù)據(jù)庫(kù)域名對(duì)應(yīng)的IP即可.