在CentOS6上編譯安裝實現LAMP(php-modules)+phpMyAdmin安裝過程全記錄

 

php與apache協做有三種模式:CGI、modules、FastCGI。
  在CGI模式下,用戶請求php文件時,apache會啓動響應進程,調用php處理器處理請求,而後將結果返回給客戶端。用戶響應及處理由前端apache服務器完成;
  在modules模式下,php解析器做爲apache的一個模塊,php處理器與web服務器一塊兒運行處理用戶請求。用戶響應由apache處理,請求處理由apache與php處理器協同完成;
  在fastcgi模式下,apache調用proxcy模塊,發揮代理做用,直接將用戶請求交由後端的php處理器(php-fpm)處理,而後將從後端php-fpm傳來的處理結果返回給用戶。用戶響應由apache處理,請求處理php-fpm完成;php

 

本文檔演示php以modules模式工做的LAMP。
安裝流程:httpd--->mariadb--->php--->phpMyadmin--->Xcachehtml

時間:2017年9月24日前端

1、軟件環境:mysql

IP:192.168.1.67;
Hostname:centos69-2.surmount.net;
Linux:CentOS release 6.9 (Final);
Apache:httpd-2.4.27
MariaDB:mariadb-5.5.57
PHP:php-5.6.31
其餘:apr-1.6.2;apr-util-1.6.0;phpMyAdmin-4.7.4-all-languages;xcache-3.2.0linux


2、LAMP搭建:web

2-1:環境準備:sql

2-1-1:配置網絡yum源:
# wget -O /etc/yum.repos.d/CentOS-Base-Ali.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# sed -i 's/$releasever/6/g' /etc/yum.repos.d/CentOS-Base-Ali.repo 數據庫

2-1-2:配置epel源:
# vim /etc/yum.repos.d/epel.repo
[epel]
name=epel
baseurl=https://mirrors.ustc.edu.cn/epel/6Server/x86_64/
gpgcheck=0
enabled=1apache

2-1-3:配置服務器軟件環境:
# yum groupinstall "Development Tools" "Server Platform Development" -yvim

2-2:安裝及配置Apache:

2-2-1:解決httpd依賴關係,編譯安裝apr及apr-util:
# tar xf apr-1.6.2.tar.bz2
# cd apr-1.6.2
# ./configure --prefix=/usr/local/apr
# make -j 2 && make install

# tar xf apr-util-1.6.0.tar.bz2
# cd apr-util-1.6.0
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
......
xml/apr_xml.c && touch xml/apr_xml.lo
xml/apr_xml.c:35:19: error: expat.h: No such file or directory
make[1]: *** [xml/apr_xml.lo] Error 1 make[1]: *** Waiting for unfinished jo
......
--->報錯後,分析可能缺乏expat的開發庫,安裝expat-devel軟件包後從新編譯成功。
# yum install expat-devel -y
# rm -rf apr-util-1.6.0
# tar xf apr-util-1.6.0.tar.bz2
# cd apr-util-1.6.0
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make -j 2 && make install

--->附:apache官方對APR的介紹:
The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.


2-2-2:安裝httpd其餘依賴軟件包及須要用的軟件包:
# yum install pcre-devel httpd -y
--->pcre-devel提供正則模式匹配庫,httpd只爲提供httpd啓動腳本模板,用完後刪除。

2-2-3:編譯安裝apache:
# tar xf httpd-2.4.27.tar.bz2
# cd httpd-2.4.27
# ./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
# make -j 2 && make install

--->MPM模塊知識補充:
(1)構建MPM爲靜態模塊:
在所有平臺中,MPM均可以構建爲靜態模塊。在構建時選擇一種MPM,連接到服務器中。若是要改變MPM,必須從新構建。爲了使用指定的MPM,請在執行configure腳本時,使用參數 --with-mpm=NAME。NAME是指定的MPM名稱。編譯完成後,可使用 ./httpd -l 來肯定選擇的MPM。此命令會列出編譯到服務器程序中的全部模塊,包括MPM。
(2)構建 MPM 爲動態模塊:
在Unix或相似平臺中,MPM能夠構建爲動態模塊,與其它動態模塊同樣在運行時加載。構建MPM爲動態模塊容許經過修改LoadModule指令內容來改變MPM,而不用從新構建服務器程序。在執行configure腳本時,使用--enable-mpms-shared選項便可啓用此特性。當給出的參數爲all時,全部此平臺支持的MPM模塊都會被安裝。還能夠在參數中給出模塊列表。默認MPM,能夠自動選擇或者在執行configure腳本時經過--with-mpm選項來指定,而後出如今生成的服務器配置文件中。編輯LoadModule指令內容能夠選擇不一樣的MPM。

2-2-4:爲httpd2.4提供啓停腳本:
# cd /etc/rc.d/init.d
# cp httpd httpd24
# vim httpd24
......
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
......
pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
......
# chkconfig --add httpd24
# chkconfig --list | grep httpd24

2-2-5:配置apache:
# httpd -t
--->注:此語法檢測的爲rpm包安裝的httpd,非編譯安裝的httpd。
# vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
# . /etc/profile.d/httpd.sh
# httpd -t
# hash
hits command
1 /usr/local/apache/bin/httpd
--->注:此語法檢測編譯安裝的httpd。
# vim /etc/httpd24/httpd.conf
......
ServerName centos69-2.surmount.net:80
......
LoadModule deflate_module modules/mod_deflate.so
.....
--->修改主機名,啓用deflate模塊(做用:容許服務器在將輸出內容發送到客戶端之前進行壓縮,以節約帶寬。),其餘暫時保持默認。
# httpd -t

2-2-6:啓動apache、訪問測試:
# /etc/init.d/httpd24 start
# ss -tanl
# ps -ef | grep httpd
root 31142 1 0 22:46 ? 00:00:00 /usr/local/apache/bin/httpd
daemon 31144 31142 0 22:46 ? 00:00:00 /usr/local/apache/bin/httpd
# httpd -M | grep event
mpm_event_module (shared)
--->event模塊已啓動
http://192.168.1.69
It works!
# yum remove httpd -y
--->配置完成,刪除使用完畢的httpd軟件包。


2-3:安裝及配置MariaDB:

2-3-1:準備數據存儲的文件系統:
# fdisk /dev/sdb
n--->p--->1--->Enter--->+2G--->t--->8e--->w
# partx -a /dev/sdb
# yum install lvm2 -y
# pvcreate /dev/sdb1
# vgcreate dbvg /dev/sdb1
# lvcreate -L 2G -n dblv dbvg
# yum install xfsprogs -y
# modprobe xfs
# modinfo xfs
# mkfs.xfs /dev/dbvg/dblv
# mkdir -pv /dbdata
# vim /etc/fstab
......
/dev/dbvg/dblv /dbdata xfs defaults 0 0
# mount -a
--->劃分一個2GB的分區,建立lv,便於擴展,格式化爲xfs文件系統,設置開機自動掛載,用於存儲數據。

2-3-2:建立數據庫用戶:
# mkdir -pv /dbdata/data
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /dbdata/data mysql
# chown -R mysql:mysql /dbdata/data/

2-3-3:初始化安裝MariaDB:
# tar xf mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mariadb-5.5.57-linux-x86_64 mysql
# cd mysql
# chown -R root:mysql /usr/loca/mysql/*
# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/dbdata/data

2-3-4:爲MariaDB提供啓停腳本:
# ls /dbdata/data/
# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld

2-3-5:配置MariaDB:
# mkdir -pv /etc/mysql
# cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf
......
thread_concurrency = 8
datadir = /dbdata/data
innodb_file_per_table = on
skip_name_resolve = on
......
--->thread_concurrency的值爲CPU個數的2倍;/dbdata/data爲數據文件存儲的位置;啓用InnoDB引擎獨立表空間;跳過域名反解。

2-3-6:啓動MariaDB,設置DB的root用戶密碼:
# /etc/init.d/mysqld start
# /usr/local/mysql/bin/mysql
MariaDB [(none)]> use mysql
MariaDB [mysql]> UPDATE user SET password=PASSWORD('jingpei') WHERE User='root';
MariaDB [mysql]> SELECT User,Host,Password FROM user;
MariaDB [mysql]> DROP USER ''@'localhost';
MariaDB [mysql]> DROP USER ''@'centos69-2.surmount.net';
MariaDB [mysql]> FLUSH PRIVILEGES;

2-3-7:導出man手冊文件、頭文件、輸出庫文件、路徑:
# vim /etc/man.config
......
MANPATH /usr/local/mysql/man
--->輸出man手冊至man命令的查找路徑;

# ln -sv /usr/local/mysql/include/mysql /usr/include/mysql
--->輸出頭文件至系統頭文件路徑/usr/include;

# echo '/usr/local/mysql/include' > /etc/ld.so.conf.d/mysql.conf
# ldconfig
--->輸出mysql的庫文件給系統庫查找路徑;

# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin/:$PATH
# source /etc/profile.d/mysql.sh
--->修改PATH環境變量,讓系統能夠直接使用mysql的相關命令;

2-4:安裝及配置PHP(PHP爲apache的模塊):

2-4-1:安裝PHP依賴的軟件包:
# yum install bzip2-devel -y

2-4-2:編譯安裝PHP:
# tar xf php-5.6.31.tar.bz2
# cd php-5.6.31
# ./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
......
configure: error: xml2-config not found. Please check your libxml2 installation.
--->出現一個error,分析多是缺失libxml2-devel軟件包:
# yum install libxml2-devel -y
# ./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
......
libtool: warning: remember to run 'libtool --finish /root/sources/php-5.6.31/libs'
--->出現一個warning:
# make && make install

2-4-3:爲php提供配置文件:
# cp php.ini-production /etc/php.ini
--->說明:
一、這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
二、若是使用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

2-4-4:配置apache,使其支持php:
# cd /etc/httpd24/
# cp httpd.conf{,.bak}
# vim httpd.conf
......
LoadModule php5_module modules/libphp5.so
.....
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
......
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
......
# /etc/init.d/httpd24 restart
# httpd -M | grep php
php5_module (shared)

2-4-5:創建php頁面,並測試php與DB的鏈接:
# vim /usr/local/apache/htdocs/index.php
<?php
$link = mysql_connect('localhost', 'root', 'jingpei');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
phpinfo()
?>

2-4-6:訪問測試:
http://192.168.1.67/index.php
Connected successfully
PHP Version 5.6.31

3、安裝phpMyAdmin:

3-1:phpMyAdmin簡介:
phpMyAdmin,是MySQL或MariaDB數據庫的GUI管理工具,以php爲基礎,以web爲工做形式。

3-2:phpMyAdmin安裝:
# unzip phpMyAdmin-4.7.4-all-languages.zip
# mv phpMyAdmin-4.7.4-all-languages /usr/local/apache/htdocs/pma
# cd /usr/local/apache/htdocs/pma
# cp config.sample.inc.php config.inc.php
# vim config.inc.php
......
$cfg['blowfish_secret'] = '1qazxsw23edcvfr45tgbnhy67ujm';
......

3-3:訪問測試:
http://192.168.1.67/pma


4、安裝xcache,爲php加速:

4-1:安裝前壓力測試:
# ab -c 20 -n 100 http://192.168.1.67/pma/index.php
--->記錄每秒響應次數,便於對比。

4-2:安裝及配置xcache:
# tar xf xcache-3.2.0.tar.bz2
# 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/
# mkdir -pv /etc/php.d
# cp /root/sources/xcache-3.2.0/xcache.ini /etc/php.d/
# vim /etc/php.d/xcache.ini
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so
......
xcache.admin.enable_auth = off
# /etc/init.d/httpd24 restart
http://192.168.1.67/index.php
--->在打開的頁面裏找到xcache功能。

4-3:安裝後壓力測試及對比
--->測試環境:
CPU:Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz
內存:512MB
# ab -c 20 -n 100 http://192.168.1.67/pma/index.php
......
Requests per second: 49.72 [#/sec] (mean)
......
# ab -c 20 -n 500 http://192.168.1.67/pma/index.php
......
Requests per second: 195.00 [#/sec] (mean)

--->經屢次壓力測試比較,大約提升3-4倍響應速度。

相關文章
相關標籤/搜索