新版LAMP環境搭建
注意:先安裝MySQL和Apache,後安裝PHP,因爲PHP配置編譯的時候需要指定mysql路徑和apache路徑。
編譯安裝:
httpd-2.4.12.tar.gz + mysql-5.6.22-linux-glibc2.5-x86_64.tar.gz + php-5.4.39.tar.gz + xcache-2.0.0.tar.gz
實驗環境:
CentOS 6.5 x86_64
編譯安裝Apache
(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。
解決依賴關係
注意:httpd-2.4.12需要較新版本的apr和apr-util
編譯安裝apr
1 2 3 4 5 | cd /usr/local/src tar xf apr-1.4.6. tar .bz2 cd apr-1.4.6 . /configure --prefix= /usr/local/apr make && make install |
編譯安裝apr-util
1 2 3 4 5 | cd /usr/local/src tar xf apr-util-1.5.4. tar .gz cd apr-util-1.5.4 . /configure --prefix= /usr/local/apr-util--with-apr = /usr/local/apr make && make install |
編譯安裝httpd-2.4.12
1 2 3 4 5 6 | yum -y install pcre-devel openssl-devel cd /usr/local/src tar xf httpd-2.4.12. tar .gz cd httpd-2.4.12 . /configure --prefix= /usr/local/apache -- 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 -mpms-shared=all --with-mpm=event make && make install |
修改httpd主配置文件
編輯/usr/local/apache/conf/httpd.conf 添加如下行:
1 2 | PidFile "/var/run/httpd.pid" httpdPid文件路徑 ServerName localhost:80 有域名填寫真實域名即可 |
apache工具快捷鍵
修改PATH環境變量,讓系統可以直接使用apache的相關命令。
編輯 /etc/profile.d/httpd.sh 添加如下行:
1 | export PATH=$PATH: /usr/local/apache/bin |
執行/etc/profile.d/httpd.sh 然後退出重新登錄
啓動apache
1 2 3 | apachectl -t 測試httpd是否正常 apachectl graceful 重新加載配置 apachectl start 啓動httpd |
編譯安裝MySQL
建立安全用戶
1 2 3 4 | groupadd mysql useradd -g mysql -s /sbin/nologin -M mysql mkdir -p /mydata/data chown -R mysql:mysql /data/mysql |
安裝並初始化-5.6.22
1 2 3 4 5 6 7 8 | cd /usr/local/src tar xfmysql-5.6.22-linux-glibc2.5-x86_64. tar .gz -C /usr/local cd /usr/local/ ln -svmysql-5.6.22-linux-glibc2.5-x86_64 mysql cd mysql chown -R mysql:mysql /usr/local/mysql/ scripts /mysql_install_db --user=mysql--datadir= /data/mysql chown -R root . #防止文件被修改,初始化完成後修改回root用戶權限 |
爲mysql提供主配置文件
1 2 3 4 5 6 | cp /usr/local/mysql/my .cnf /etc 複製my.cnf到 /etc 目錄下 #vim /etc/my.cnf 編輯my.cnf文件,修改如下內容 basedir = /usr/local/mysql datadir = /data/mysql port = 3006 server_id = 1 |
爲mysql提供sys服務腳本
1 2 3 4 5 6 7 8 9 | cd /usr/local/mysql cp support-files /mysql .server /etc/rc .d /init .d /mysqld chmod +x /etc/rc .d /init .d /mysqld 修改 /etc/rc .d /init .d /mysqld basedir= /usr/local/mysql datadir= /mydata/data chkconfig --add mysqld chkconfig --level 35 mysqld on service mysqld start |
mysql工具快捷鍵
修改PATH環境變量,讓系統可以直接使用mysql的相關命令。
編輯 /etc/profile.d/mysqld.sh 添加如下行:
1 | export PATH=$PATH: /usr/local/mysql/bin |
執行/etc/profile.d/mysql.sh 然後退出重新登錄
可以直接使用mysql 登錄終端
常見問題分析
錯誤:error while loadingshared libraries: libaio.so.1
解決:yum -y install libaio
錯誤:Starting MySQL.Theserver quit without updating PID file (/[FAILED]ql/darker.pid).
InnoDB: mmap(137363456 bytes) failed; errno 12
[ERROR]InnoDB: Cannot allocate memory for the buffer pool
[ERROR] Plugin 'InnoDB' init function returnederror.
[ERROR] Plugin 'InnoDB' registration as aSTORAGE ENGINE failed.
[ERROR] Unknown/unsupported storage engine:InnoDB
[ERROR] Aborting
解決:vim /etc/my.cnf innodb_buffer_pool_size= 8M 修改innodb爲8M,內存過小導致無法啓動
編譯安裝PHP
解決依賴關係
1 2 | #rpm -ivh http://www.lishiming.net/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm #yum -y install libxml2-devel opensslopenssl-devel bzip2 bzip2-devel libjpeg-devel libpng libpng-devel freetypefreetype-devel libmcrypt-devel |
編譯安裝
1 2 3 4 5 | cd /usr/local/src tar -xfphp-5.4.39. tar .gz cd php-5.4.39 . /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 -fpm -- enable -xml -- enable -sockets --with-mcrypt --with-config- file -path= /etc --with-config- file -scan- dir = /etc/php .d--with-bz2 -- enable -maintainer-zts make && make install |
說明:這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
新版本使用第三方模塊PHP-FPM或者編譯成模塊化(默認編譯是fpm)
--enable-fpm
--with-apxs2=/usr/local/apache/bin/apxs
爲php提供配置文件:
1 | cp /usr/local/src/php-5 .4.39 /php .ini-production /etc/php .ini |
配置php-fpm
爲php-fpm提供Sys init腳本,並講其添加至服務列表
1 2 3 4 | cp /usr/local/src/php-5 .4.39 /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 php-fpm on |
爲php-fpm提供配置文件:
1 | cp /usr/local/php/etc/php-fpm .conf.default /usr/local/php/etc/php-fpm .conf |
編輯php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf
配置php-fpm相關選項爲你所需要的值,並啓動pid文件
1 2 3 4 5 6 7 | #pm=dynamic #默認爲動態模式 #pm.max_children = 50 #靜態方式下開啓的php-fpm進程數量。 #pm.start_servers = 10 #動態方式下的起始php-fpm進程數量。 #pm.min_spare_servers = 5 #動態方式下的最小php-fpm進程數量。 #pm.max_spare_servers = 20 #動態方式下的最大php-fpm進程數量。 #user=apache #修改爲apache啓動的用戶或者使用默認 #group=apache #修改爲apache啓動的組或者使用默認 |
如果dm設置爲static,那麼其實只有pm.max_children這個參數生效。系統會開啓設置數量的php-fpm進程。
如果dm設置爲dynamic,那麼pm.max_children參數失效,後面3個參數生效。系統會在php-fpm運行開始的時候啓動pm.start_servers個php-fpm進程,然後根據系統的需求動態在pm.min_spare_servers和pm.max_spare_servers之間調整php-fpm進程數。
xcache加速PHP
1、安裝
1 2 3 4 5 6 | cd /usr/local/src tar xf xcache-2.0.0. tar .gz cd xcache-2.0.0 /usr/local/php/bin/phpize . /configure -- enable -xcache--with-php-config= /usr/local/php/bin/php-config make && make install |
安裝結束時,會出現類似如下行:
1 | Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ |
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
1 2 | mkdir /etc/php .d cp /usr/local/src/ xcache-2.0.0 /xcache .ini /etc/php .d |
接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改爲如下行:
1 | zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache .so |
注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。
常見問題分析
錯誤:configure: error: xml2-config not found. Please check your libxml2installation.
解決:yum -y install libxml2-devel
錯誤:configure: error: Cannot find OpenSSL's <evp.h>
解決:yum install -y openssl openssl-devel
錯誤:configure: error: Please reinstall the BZip2 distribution
解決:yum install -y bzip2 bzip2-devel
錯誤:configure: error: jpeglib.h not found.
解決:yum install -y libjpeg-devel
錯誤:configure: error: png.h not found.
解決:yum install -y libpng libpng-devel
錯誤:configure: error:freetype.h not found.
解決:yum install -y freetype freetype-devel
錯誤:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決:rpm -ivh http://www.lishiming.net/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm
yum install -y libmcrypt-devel
錯誤:make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
這是由於內存小於1G所導致. 在./configure加上選項:
解決:--disable-fileinfo
錯誤:Disable fileinfo support 禁用 fileinfo 需重新編譯
錯誤:make: *** [libphp5.la] Error 1
解決:yum-y install libtool libtool-ltdl-devel
安裝完後直接make
錯誤:Cannot find autoconf. Please check your autoconf installation andthe
$PHP_AUTOCONFenvironment variable. Then, rerun this script.
解決:yum install autoconf
Apache支持PHP
修改配置文件
vim /usr/local/apache/conf/httpd.conf
1、添加如下二行
1 2 | AddType application /x-httpd-php .php AddType application /x-httpd-php-source .phps |
2、定位至DirectoryIndexindex.html
1 | DirectoryIndex index.phpindex.html |
3、去掉mod_proxy.so和mod_proxy_fcgi.so之前的註解,確保他們被apache加載。
4、如果php-fpm使用的是TCP socket,那麼在httpd.conf末尾加上:
1 2 3 | <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9000" < /FilesMatch > |
5、重啓apache以及php-fpm
1 2 | apachectl restart /etc/init .d /php-fpm start |
測試LAMP架構
1 2 3 4 5 6 7 8 | vim /usr/local/apache/htdocs/index.php #寫入PHP頁面測試php連接mysql是否成功 <? php $ conn = mysql_connect (‘localhost’,’root’,’Password’); if ($conn) echo“Success….”; else echo“Failure….”; Phpinfo(); ?> |
常見問題
如果訪問服務器上的php文件出現"file not found."十之八九是php-fpm.conf中的user和group沒有讀寫權限,修改成apache所使用的user和group即可