前言
LAMP網站架構是目前國際流行的Web框架,該框架包括:Linux操作系統,Apache網站服務器,MySQL數據庫,Perl、PHP或者Python編程語言,所有組成產品均是開源軟件,是國際上成熟的架構框架,很多流行的商業應用都是采取這個架構,和Java/J2EE架構相比,LAMP具有Web資源豐富、輕量、快速開發等特點,與微軟的.NET架構相比,LAMP具有通用、跨平臺、高性能、低價格的優勢,因此LAMP無論是性能、質量還是價格都是企業搭建網站的首選平臺。但由于MySQL作為SUN公司的附屬品已被Oracle收購,以后是否還會繼續開源,這個鬼才知道,所以CentOS7上開始采用MySQL的分支MariaDB,估計以后在LAMP中MariaDB也會取代MySQL。
工作原理
編譯安裝LAMP
編譯安裝httpd
系統環境:CentOS6.6
作為web服務器:172.16.10.100
所需軟件包:apr-1.5.0.tar.bz2、apr-util-1.5.3.tar.bz2、httpd-2.4.9.tar.bz2
注意:httpd2.4需要依賴apr和arp-util 1.4以上版本
解決依賴關系
[root@scholar ~]# yum groupinstall Development tools Server Platform Development -y [root@scholar ~]# yum -y install pcre-devel
編譯安裝apr和apr-util
[root@scholar ~]# tar xf apr-1.5.0.tar.bz2 [root@scholar ~]# cd apr-1.5.0 [root@scholar apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@scholar apr-1.5.0]# make && make install [root@scholar apr-1.5.0]# cd .. [root@scholar ~]# tar xf apr-util-1.5.3.tar.bz2 [root@scholar ~]# cd apr-util-1.5.3 [root@scholar apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@scholar apr-util-1.5.3]# make && make install
編譯安裝httpd
[root@scholar apr-util-1.5.3]# cd .. [root@scholar ~]# tar xf httpd-2.4.9.tar.bz2 [root@scholar ~]# cd httpd-2.4.9 [root@scholar httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-deflate --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-mpms-shared=all --with-mpm=event --enable-modules=most [root@scholar httpd-2.4.9]# make && make install
編譯參數詳解: --prefix:#安裝路徑 --sysconfdir:#指定配置文件路徑 --enable-so:#DSO兼容,DSO=Dynamic Shared Object,動態共享對象,可實現模塊動態生效 --enable-ssl:#支持SSL/TLS,可實現https訪問 需已安裝openssl-devel --enable-cgi:#支持CGI腳本(默認對非線程的MPM模式開啟) --enable-rewrite:#啟用Rewrite功能,URL重寫 --enable-deflate:#支持壓縮功能 --with-zlib:#使用指定的zlib庫,不指定路徑會自動尋找 --with-pcre:#使用指定的PCRE庫,不指定路徑會自動尋找 需已安裝pcre-devel --with-apr:#指定apr安裝路徑 --with-apr-util:#指定apr-util安裝路徑 --enable-mpms-shared:#支持動態加載的MPM模塊,可選參數:all --with-mpm:#設置默認啟用的MPM模式,{prefork|worker|event} --enable-modules:#支持動態啟用的模塊,可選參數:all,most,few,reallyall #編譯之前可使用./configure --help查看各項參數
添加環境變量
為了方便命令使用需要添加環境變量
[root@scholar ~]# vim /etc/profile.d/httpd24.sh #名字自定義 export PATH=/usr/local/apache/bin:$PATH [root@scholar ~]# source /etc/profile.d/httpd24.sh #重讀環境變量
導出頭文件
[root@scholar ~]# ln -sv /usr/local/apache/include/ /usr/include/httpd24
導出man手冊
[root@scholar ~]# vim /etc/man.config MANPATH /usr/local/apache/man #添加編譯安裝的man位置
提供腳本
#!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache # Short-Description: start and stop Apache HTTP Server # Description: The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # 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-/usr/local/apache/logs/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $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=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 killproc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart|try-restart) if status -p ${pidfile} $httpd >&/dev/null; then stop start fi ;; force-reload|reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart| force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL
加入服務列表,測試腳本啟動服務,查看80端口是否被監聽
訪問測試,查看是否正常工作
OK,沒問題,httpd編譯安裝完成
安裝MariaDB
系統環境:CentOS6.6
作為數據庫服務器:172.16.10.211
所需軟件包:mariadb-5.5.36-linux-x86_64.tar.gz(二進制格式包)
裝備數據存放文件系統
為了安全起見,一般數據庫數據是被放在其他磁盤上的,這里我們新建一個邏輯卷,并將其掛載至特定目錄即可。
[root@MariaDB ~]# fdisk /dev/sdb n p 1 +20G t 8e w [root@MariaDB ~]# partx -a /dev/sdb [root@MariaDB ~]# pvcreate /dev/sdb1 [root@MariaDB ~]# vgcreate myvg /dev/sdb1 [root@MariaDB ~]# lvcreate -L 10G -n mydata myvg [root@MariaDB ~]# mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata [root@MariaDB ~]# mkdir /mydata [root@MariaDB ~]# vim /etc/fstab LABEL=MYDATA /mydata ext4 defaults,noatime 0 0 [root@MariaDB ~]# mount -a [root@MariaDB ~]# mkdir /mydata/data
為了安全,新建用戶及組,以普通用戶運行
解壓安裝MariaDB,權限修改
提供配置文件,初始化數據庫
[root@MariaDB local]# mkdir /etc/mysql [root@MariaDB local]# cp ./mysql/support-files/my-large.cnf /etc/mysql/my.cnf [root@MariaDB local]# vim /etc/mysql/my.cnf thread_concurrency = 4 #運行CPU數量乘2 datadir = /mydata/data #添加行,指明數據文件存放位置 [root@MariaDB local]# cd mysql/ [root@MariaDB mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
提供腳本
加入服務列表,啟動服務,查看3306端口是否被監聽
添加環境變量
[root@MariaDB mysql]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@MariaDB mysql]# source /etc/profile.d/mysql.sh
導出man手冊
[root@MariaDB mysql]# vim /etc/man.config MANPATH /usr/local/mysql/man
導出頭文件
[root@MariaDB ~]# ln -sv /usr/local/mysql/include /usr/include/mysql
輸出庫文件給系統庫查找路徑
[root@MariaDB ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
測試MariaDB
OK,MariaDB安裝完成
編譯安裝php
系統環境:CentOS6.6
作為php服務器:172.16.10.110
所需安裝包:php-5.4.26.tar.bz2、xcache-3.1.0.tar.bz2(非必須,加速軟件)
解決依賴關系
[root@scholar ~]# yum groupinstall Development tools Server Platform Development -y [root@scholar ~]# yum -y groupinstall Desktop Platform Development [root@scholar ~]# yum -y install bzip2-devel libmcrypt-devel
解壓編譯
[root@scholar ~]# tar xf php-5.4.26.tar.bz2 [root@scholar ~]# cd php-5.4.26 [root@scholar php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --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 --with-openssl [root@scholar php-5.4.26]# make && make install
提供配置文件
[root@scholar php-5.4.26]# cp php.ini-production /etc/php.ini
為php-fpm提供腳本
為php-fpm提供配置文件
編輯配置文件
[root@scholar php]# vim etc/php-fpm.conf #注意文件位置/usr/local/php/etc/ pid =/usr/local/php/var/run/php-fpm.pid listen = 172.16.10.110:9000 pm.max_children = 25 #最大子進程數 pm.start_servers = 5 #開機預啟動子進程數 pm.min_spare_servers = 2 #最小空閑子進程數 pm.max_spare_servers = 6 #最大空閑子進程數
啟動服務,檢查9000端口是否被監聽
到此為止,各軟件算是編譯安裝完了,我們通過一個實例讓他們工作起來
啟動LAMP
案例要求:
DNS服務器:172.16.10.10
web服務器:172.16.10.100(已編譯安裝httpd-2.4.9)
php服務器:172.16.10.110(已編譯安裝php-5.4.26)
數據庫服務器:172.16.10.211(已安裝MariaDB-5.5.36)
要求web服務器提供3個站點:www.scholar.com(靜態)、admin.scholar.com(動態,phpMyAdmin)、blog.scholar.com(動態、wordpress),站點文件目錄分別為:/web/www、/web/pma、/web/blog,其中admin.scholar.com由于是管理數據庫的站點,所以必須基于https進行通信。
DNS服務器配置
修改DNS正反向區域文件
正向解析
反向解析
檢查語法,重啟服務
web服務器配置
啟用相關模塊
[root@scholar ~]# vim /etc/httpd24/httpd.conf LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
啟用虛擬主機
#DocumentRoot "/usr/local/apache/htdocs" #關閉中心主機 Include /etc/httpd24/extra/httpd-vhosts.conf #啟用虛擬主機
使之支持php
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule> AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
配置虛擬主機
[root@scholar ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/web/www" ServerName www.scholar.com <Directory "/web/www"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/web/pma" ServerName admin.scholar.com ProxyRequests Off #關閉正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/pma/$1 #代理至php服務器 <Directory "/web/pma"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/web/blog" ServerName blog.scholar.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/blog/$1 <Directory "/web/blog"> Options none AllowOverride none Require all granted </Directory> </VirtualHost>
站點文件準備
web服務器:
[root@scholar php]# mkdir /web/{pma,blog} -pv #此為php服務器,不要混淆 mkdir: created directory `/web/pma' mkdir: created directory `/web/blog' [root@scholar php]# scp 172.16.10.100:/web/pma/* /web/pma/ [root@scholar php]# scp 172.16.10.100:/web/blog/* /web/blog/ [root@scholar php]# cd /web/pma [root@scholar pma]# cp config.sample.inc.php config.inc.php [root@scholar pma]# vim config.inc.php $cfg['blowfish_secret'] = 'a8b7c6dhfgs'; $cfg['Servers'][$i]['host'] = '172.16.10.211'; [root@scholar pma]# cd ../blog/ [root@scholar blog]# cp wp-config-sample.php wp-config.php [root@scholar blog]# vim wp-config.php /** WordPress 數據庫的名稱 */ define('DB_NAME', 'wpdb'); /** MySQL 數據庫用戶名 */ define('DB_USER', 'wpuser'); /** MySQL 數據庫密碼 */ define('DB_PASSWORD', 'wppass'); /** MySQL 主機 */ define('DB_HOST', '172.16.10.211');
數據庫準備
檢查語法,重啟服務
測試各站點,訪問是否正常
至此,分離式的LAMP就可以統一正常工作了,接下來我們為admin.scholar.com提供https。
生成私鑰,提供證書
簽署證書,我這里本機就是CA就自簽了,CA配置詳見博客
啟用ssl功能
[root@scholar ssl]# vim /etc/httpd24/httpd.conf LoadModule socache_shmcb_module modules/mod_socache_shmcb.so LoadModule ssl_module modules/mod_ssl.so Include /etc/httpd24/extra/httpd-ssl.conf
[root@scholar ssl]# vim /etc/httpd24/extra/httpd-ssl.conf DocumentRoot "/web/pma" #站點目錄 ServerName admin.scholar.com:443 #啟用ssl的站點 ProxyRequests Off #同樣關閉正向向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/pma/$1 #反向代理至php服務器 <Directory "/web/pma"> Options none AllowOverride none Require all granted </Directory> SSLCertificateFile "/etc/httpd24/ssl/httpd.crt" #證書位置 SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key" #私鑰位置
測試語法,重啟服務,查看443端口是否被監聽
將CA證書導出,重命名為*.crt格式,安裝在受信任的根證書頒發機構中,測試https
成功了,我們登陸數據庫看一下
OK,登陸成功,可以管理數據庫啦,至此,基于https的加密通信部署完成
xcache加速
最后,我們補充講解一下xcache,xcache是一個開源的 opcode 緩存器/優化器,它可以提高php的性能,加快頁面生成速率,從而降低服務器負載。
為了比較加速前后的效果,在安裝xcache之前,我們來對現在的服務器進行壓力測試
編譯安裝xcache
[root@scholar ~]# tar xf xcache-3.1.0.tar.bz2 [root@scholar ~]# cd xcache-3.1.0 [root@scholar xcache-3.1.0]# /usr/local/php/bin/phpize [root@scholar xcache-3.1.0]# ./configure --enable-xcache --with-php-config= /usr/local/php/bin/php-config [root@scholar xcache-3.1.0]# make && make install
配置xcache
[root@scholar xcache-3.1.0]# mkdir /etc/php.d [root@scholar xcache-3.1.0]# cp xcache.ini /etc/php.d/ [root@scholar xcache-3.1.0]# vim /etc/php.d/xcache.ini [xcache-common] ;; non-Windows example: extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
安裝成功,重啟服務,再次測試
經過xcache加速,響應和傳輸速度快了好多倍,由此可見,xcache還是很實用的。
The end
好了,LAMP就先講解到這里啦,搭建LAMP平臺還是挺費時間的,希望本文可以對你有所幫助,部署過程中遇到問題可留言,欲了解更多功能的LAMP,請關注后續文章。以上僅為個人學習整理,如有錯漏,大神勿噴~~~
原創文章,作者:書生,如若轉載,請注明出處:http://www.www58058.com/3414
漂亮專業,幾乎已經是免檢產品了。贊
@stanley:卡章 出廠 :mrgreen: