Linux服務--CentOS6實作LAMP(源碼安裝)
實驗要求:
安裝php時實作php子產品嵌入到httpd中和實作fpm兩種方式。在fpm下,提供兩個虛拟主機:
分别用于實作PHPMyadmin和WordPress,其中PhpMyAdmin提供ssl.
實驗環境:
CentOS系統一台(IP:172.16.99.4),所需的httpd,mariadb,php,PhpAdmin,WordPress的源碼包。
實驗步驟:
安裝順序:httpd-->mariadb-->php.
安裝前的準備工作:
1.安裝開發環境:
本地yum安裝開發環境:
yum groupinstall “Development Tools” “Server Platform Development”
2.關閉防火牆或者編寫好相應的防火牆規則,關閉Selinux.
一.安裝LAMP:實作php以子產品嵌入到httpd中
1.編譯安裝httpd:
1.安裝是以來的軟體包pcre-devel軟體包,本地鏡像自帶,是以yum安裝即可。
2.下載下傳apr和apr-util源碼包,并編譯安裝。
APR(Apache portable Run-time libraries,Apache可移植運作庫)的目的如其名稱一樣,主要為上層的應用程式提供一個可以跨越多作業系統平台使用的底層支援接口庫。在早期 的Apache版本中,應用程式本身必須能夠處理各種具體作業系統平台的細節,并針對不同的平台調用不同的處理函數。
随着Apache的進一步開 發,Apache組織決定将這些通用的函數獨立出來并發展成為一個新的項目。這樣,APR的開發就從Apache中獨立出來,Apache僅僅是使用 APR而已。目前APR主要還是由Apache使用,不過由于APR的較好的移植性,是以一些需要進行移植的C程式也開始使用APR,開源項目比如 Flood loader tester(http://httpd.apache.org/test/flood/,該項目用于伺服器壓力測試,不僅僅适用于Apache)
編譯安裝:apr-1.5.0.tar.bz2 ,apr-util-1.5.3.tar.bz2
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install
# tar xf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3.編譯安裝httpd-2.4.12.tar.bz2
# tar xf httpd-2.4.12.tar.bz2
# cd httpd-2.4.12
# ./configure --prefix=/usr/local/httpd --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
# make && make install
4.安裝完成後,進行相應的配置。
a.提供系統服務:
将/usr/local/httpd/bin目錄下的apachectl檔案複制到/etc/init.d/下,并命名為httpd:
# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
修改httpd檔案:在檔案前部分添加如下幾行:
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.
添加系統服務:
# chkconfig --add httpd
b.添加PATH變量:
在目錄/etc/profile.d/目錄下建立一檔案httpd.sh,内容如下:
export PATH=/usr/local/httpd/bin/:$PATH
3.安裝配置完成,啟動服務測試:
# service httpd start
2.編譯安裝Mariadb
1.準備資料存放的檔案系統,使用的邏輯卷:
建立一目錄用于挂在建立的邏輯卷:# mkdir -pv /mydata/data
建立邏輯卷并格式化挂在:
先分區,改變分區ID為8e
建立實體卷: # pvcreate /dev/sda5
建立卷組: # vgcreate myvg /dev/sda5
建立邏輯卷:# lvcreate -n mydata -L 5G myvg
格式化:mkfs -t ext4 /dev/mapper/myvg-mydata
挂在:mount /dev/mapper/myvg-mydata /mydata/data
永久挂在:編寫檔案/etc/fstab 添加一行:
/dev/mapper/myvg-mydate /mydata/data ext4 defaults 0 0
2.建立使用者群組,使服務安全運作:
建立組:# groupadd -g 3333 mysql
建立使用者:# useradd -g 3333 -u 3333 - r -s /sbin/nologin -M -d /mydata/data mysql
3.使用通用二進制格式安裝 mariadb-5.5.43-linux-x86_64.tar.gz
# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mariadb-5.5.43-linux-x86_64 mysql
# cd mysql
進入mysql 目錄之後,修改該目錄下所有檔案和目錄的屬主和屬組:
# chown -R mysql.mysql .
初始化資料庫:
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
再修改該目錄下檔案和目錄的屬主:# chown -R root .
4.進行後續配置:
a.提供系統服務:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
添加至服務清單:
# chkconfig --add mysqld
# chkconfig mysqld on
b.提供配置檔案,并在配置檔案中添加幾項:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
修改配置檔案:
datadir = /mydata/data
innodb_file_per_table = on
skip_name_resolve = on
c.修改PATH變量,直接可以使用mysql的相關工具:
在/etc/profile.d/目錄下建立檔案mysqld.sh:
export PATH=/usr/local/mysql/bin:$PATH
d.輸出mysql的頭檔案至系統頭檔案路徑:建立連結實作:
# ln -sv /usr/local/mysql/include /usr/include/mysql
e.輸出mysql的庫檔案給系統庫查找路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
讓系統重新載入系統庫:
# ldconfig
5.測試:
3.編譯安裝PHP
此次編輯php是把php以子產品的形式編輯到httpd中,下文将介紹使用fpm編輯php,并進行先關的配置,安裝WordPress和phpMyAdmin.
編譯安裝php-5.6.4.tar.xz
1.解決依賴關系:
# yum -y install bzip2-devel libmcrypt-devel libxml2-devel
2.編譯安裝php-5.6.4.tar.xz
# tar xf php-5.6.4.tar.xz
# cd php-5.6.4
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
說明:
1、這裡為了支援apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
2、如果使用PHP5.3以上版本,為了連結MySQL資料庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL用戶端庫綁定形成依賴),但從PHP 5.4開始它就是預設設定了。
#./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make &&make install
3. 為服務提供配置檔案:
# cp php.ini-production /etc/php.ini
4.編輯httpd的配置檔案,是apache支援php
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改為:
DirectoryIndex index.php index.html
5.測試:
在index.php頁面檔案中添加如下内容:
<?php
$link=mysql_connect('127.0.0.1','root','');
if ($link)
echo "OK";
else
echo "Failure";
phpinfo();
?>
4.LAMP到此安裝結束,隻是沒有進行相應的配置。我們先對lamp進行一下壓力測試,檢視伺服器的負載能力。
# ab -c 200 -n 1000 http://172.16.99.4/index.php
Total transferred: 72493898 bytes
HTML transferred: 72313898 bytes
Requests per second: 24.05 [#/sec] (mean)
Time per request: 8316.375 [ms] (mean)
Time per request: 41.582 [ms] (mean, across all concurrent requests)
Transfer rate: 1702.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 31 79.8 1 800
Processing: 92 7224 10262.6 1290 41014
Waiting: 85 7129 10161.5 1275 41013
Total: 111 7255 10267.4 1364 41032
發現每秒處理24.05個請求:Requests per second: 24.05 [#/sec] (mean)
接下來安裝Xcahe,再來進行壓力測試,來檢視結果:
5.安裝xcache ,為php加速:
1、安裝
# tar xf xcache-3.2.0.tar.gz
# cd xcache-3.2.0
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
編譯完出現:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
2、編輯php.ini,整合php和xcache:
首先将xcache提供的樣例配置導入php.ini
# mkdir /etc/php.d
将源碼包解壓的目錄下的xcache.ini複制到/etc/php.d
# cp xcache.ini /etc/php.d
接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改為如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
6.重新啟動httpd服務和mariadb服務進行壓力測試:
]# ab -c 200 -n 1000 http://172.16.99.4/index.php
Total transferred: 76241920 bytes
HTML transferred: 76061920 bytes
Requests per second: 562.74 [#/sec] (mean)
Time per request: 355.406 [ms] (mean)
Time per request: 1.777 [ms] (mean, across all concurrent requests)
Transfer rate: 41898.61 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 32 28.8 27 100
Processing: 15 140 137.7 118 1201
Waiting: 2 101 137.0 75 1200
Total: 22 172 137.1 144 1274
檢視結果Requests per second: 562.74 [#/sec] (mean),明顯比上次快了幾倍!
二.安裝LAMP,實作php以fpm方式與Apache互聯
httpd,mariadb的安裝與前一部分相同,這裡隻需安裝php就行。
1.編譯安裝 php-5.6.4.tar.xz
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --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
make && make install
這裡把--with-apxs2=/usr/local/apache/bin/apxs 改成--enable-fpm ,即,就是在編輯php時,添加fpm子產品。
說明:如果使用PHP5.3以上版本,為了連結MySQL資料庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL用戶端庫綁定形成依賴),但從PHP 5.4開始它就是預設設定了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
為php提供配置檔案:#cp php.ini-production /etc/php.ini
2.配置php-fpm:
安裝完php之後,會在php-5.6.4目錄下的有一個目錄提供了php-fpm的服務腳本。
sapi/fpm/init.d.php-fpm
1.将php-fpm添加至服務清單:
php-5.6.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
]# chkconfig --add php-fpm
]# chkconfig --level php-fpm
2.給php-fpm提供配置檔案:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
3.配置php-fpm的配置檔案,修改内容:
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php5/var/run/php-fpm.pid
4.啟動php-fpm服務,檢視是否成功:
# service php-fpm start
3.配置httpd:添加支援fcig子產品和配置虛拟主機,支援fcgi:
1.讓httpd能識别php檔案,
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改為:
DirectoryIndex index.php index.html
2.添加fcgi子產品
在Apache httpd 2.4以後已經專門有一個子產品針對FastCGI的實作,此子產品為mod_proxy_fcgi.so,它其實是作為mod_proxy.so子產品的擴充,是以,這兩個子產品都要加載
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
3.配置支援fcgi的虛拟主機
在配置檔案中,取消對#Include /etc/httpd24/extra/httpd-vhosts.conf的注釋,使httpd支援虛拟主機。
編輯虛拟主機的配置檔案:
# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost wordp.zkchang.com:80>
DocumentRoot "/web/vhosts/wordp/"
ServerName wordp.zkchang.com
ErrorLog "/var/log/v_wp_error_log"
CustomLog "/var/log/v_wp_access_log" common
ProxyRequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhosts/wordp/$1
<Directory "/web/vhosts/wordp/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
</VirtualHost>
<VirtualHost phpma.zkchang.com:80>
DocumentRoot "/web/vhosts/pma"
ServerName phpma.zkcang.com
ErrorLog "/var/log/v_pma_ror_log"
CustomLog "/var/log/v_pma_access_log" common
ProxyRequests off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/web/vhosts/pma/$1
<Directory "/web/vhosts/pma/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
</VirtualHost>
4.分别在不同主機的站點目錄下建立測試網頁檔案,進行測試:
# vim /web/vhosts/wordp/index.php
<?php
echo "This is a wordprocess page"
?>
# vim /web/vhosts/pma/index.php
<?php
$link = mysql_connect("127.0.0.1","root","");
if ($link)
echo "OK";
else
echo "Worry!";
mysql_close();
phpinfo();
?>
測試結果截圖:
5.對fcgi模式下進行壓力測試,并與php以子產品化嵌入httpd的進行比較:
# ab -c 200 -n 1000 http://phpma.zkchang.com/index.php
Total transferred: 66731883 bytes
HTML transferred: 66531883 bytes
Requests per second: 637.04 [#/sec] (mean)
Time per request: 313.951 [ms] (mean)
Time per request: 1.570 [ms] (mean, across all concurrent requests)
Transfer rate: 41514.64 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 13.3 0 42
Processing: 30 143 125.8 137 1077
Waiting: 26 142 125.7 136 1077
Total: 51 150 123.1 138 1077
6.安裝Xcache,為php進行加速:
# tar xf xcache-3.2.0.tar.bz2
# cd xcache-3.2.0
xcache-3.2.0]# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
編譯完出現:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20131226/
編輯php.ini,整合php和xcache:
首先将xcache提供的樣例配置導入php.ini
# mkdir /etc/php.d
将源碼包解壓的目錄下的xcache.ini複制到/etc/php.d
# cp xcache.ini /etc/php.d
接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改為如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
再次進行壓力測試,進行比較:
# ab -c 200 -n 1000 http://phpma.zkchang.com/index.php
Total transferred: 66731876 bytes
HTML transferred: 66531876 bytes
Requests per second: 1322.20 [#/sec] (mean)
Time per request: 151.263 [ms] (mean)
Time per request: 0.756 [ms] (mean, across all concurrent requests)
Transfer rate: 86165.07 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 4.6 0 14
Processing: 13 135 36.6 145 351
Waiting: 13 134 36.8 144 350
Total: 19 137 34.8 146 365
三.分别在兩個虛拟主機上安裝phpmyadmin. WordPress,并實作通過ssl通路phpmyadmin。
1.解壓安裝phpmyadmin,并實作ssl:
1.提供phpmyadmin:
]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
]# cd phpMyAdmin-4.4.14.1-all-languages
]# cp -a phpMyAdmin-4.4.14.1-all-languages /web/vhosts/pma/pma
]# cp /web/vhost/pma/pma/config.sample.inc.php{,.bak}
]# mv /web/vhost/pma/pma/config.sample.inc.php config.inc.php
# vim /web/vhost/pma/pma/config.inc.php
$cfg['blowfish_secret'] = 'asdfghjkl'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH!
打開網頁測試:
2.給mysql的管理者使用者設定密碼:
MariaDB [(none)]> SET PASSWORD FOR 'root'@'127.0.0.1'=PASSWORD('zkchang');
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('zkchang');
設定完密碼之後就可以登入phpmyadmin
3.啟動ssl子產品,支援https協定。
修改httpd主配置檔案httpd.conf:把:
#Include /etc/httpd24/extra/httpd-ssl.conf 取消注釋
a.為httpd伺服器生成證書:
由于在主機172.16.99.2上已經建立了CA的私有CA,在此就不在建立證書頒發機構的私有證書,隻需在httpd伺服器上建立私鑰和證書請求,然後把證書請求發給172.16.99.2,讓CA頒發機構給伺服器頒發證書即可。
1.在伺服器172.16.99.4上建立私鑰和證書請求,并把證書請求發送給CA頒發機構:
建立私鑰:]# (umask 077;openssl genrsa -out /etc/httpd24/ssl/httpd.key 2048)
建立證書請求:
]# openssl req -new -key /etc/httpd24/ssl/httpd.key -out /etc/httpd24/ssl/httpdpma.csr -days 365
發給CA頒發機構:
]# scp /etc/httpd24/ssl/httpdpma.csr [email protected]:/tmp/
2.在CA頒發機構172.16.99.2上為伺服器頒發證書,并把證書發送給伺服器:
簽發證書:
CA]# openssl ca -in /tmp/httpdpma.csr -out /etc/pki/CA/certs/httpdpma.crt -days 365
發給伺服器:
# scp /etc/pki/CA/certs/httpdpma.crt [email protected]:/etc/httpd24/ssl
b.編輯/etc/httpd24/extra/httpd-ssl.conf 檔案,修改一下内容:
<VirtualHost _default_:443>
DocumentRoot "/web/vhosts/pma/"
ServerName phpma.zkchang.com:443
#ServerAdmin [email protected]
ErrorLog "/var/log/ssl_v_error_log"
TransferLog "/var/log/ssl_v_access_log"
<Directory "/web/vhosts/pma/">
Options None
Allowoverride None
Require all denied
Require ip 172.16.0.0/16
</Directory>
SSLCertificateFile "/etc/httpd24/ssl/httpdpma.crt"
SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key
c.在httpd配置檔案中啟動相應的ssl子產品
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule ssl_module modules/mod_ssl.so
d.将CA頒發機構的CA憑證,導入到浏覽器中進行測試:
2.解壓安裝WordPress:
1.解壓安裝:
]# unzip wordpress-4.3.1-zh_CN.zip
]# cp -a wordpress /web/vhosts/wordp/wordp
]#cd /web/vhosts/wordp/wordp/
]# mv wp-config-sample.php wp-config.php
]# vim wp-config.php
2.建立資料庫,建立使用者和密碼并授權:
MariaDB [(none)]> CREATE DATABASE wordpdb;
MariaDB [(none)]> CREATE USER 'wordpuser'@'127.0.0.1' IDENTIFIED BY 'wordpuser';
MariaDB [(none)]> CREATE USER 'wordpuser'@'localhost' IDENTIFIED BY 'wordpuser';
MariaDB [(none)]> GRANT ALL ON wordpdb.* TO 'wordpuser'@'127.0.0.1';
MariaDB [(none)]> GRANT ALL ON wordpdb.* TO 'wordpuser'@'localhost';
3.網頁打開測試:
4.對一個動态網頁做壓力測試:
# ab -c 200 -n 1000 http://wordp.zkchang.com/wordp/index.php
Requests per second: 16.49 [#/sec] (mean)
Time per request: 12132.181 [ms] (mean)
Time per request: 60.661 [ms] (mean, across all concurrent requests)
Transfer rate: 5.35 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 59 168.4 0 1430
Processing: 271 10657 6602.9 9321 50520
Waiting: 271 10379 6469.7 9191 49844
Total: 275 10716 6585.6 9404 50520
轉載于:https://blog.51cto.com/zkchang/1697968