lamp編譯詳解

wadeson發表於2017-07-07

首先確認系統環境:centos6.4 min版本

1、安裝需要的開發環境
yum groupinstall "Development Tools" "Server Platform Development" -y
2、下載並安裝依賴的包(由於編譯的是httpd2.4的版本對apr有更高要求)
3、開始編譯安裝依賴包
同步時間:ntpdate time.nist.gov
tar -xf apr-1.6.2.tar.gz
cd apr-1.6.2
./configure --prefix=/usr/local/apr
make && make install
 
tar xf apr-util-1.6.0.tar.gz
cd apr-util-1.6.0
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
如果在apr-util編譯時報錯:
解決辦法:yum install expat-devel
4、編譯httpd2.4
tar xf httpd-2.4.25.tar.gz
cd httpd-2.4.25
由於系統上已經安裝了rpm包的httpd所以存在使用者apache
[root@zabbix httpd-2.4.25]# id apache
uid=48(apache) gid=48(apache) groups=48(apache)
[root@zabbix httpd-2.4.25]# rpm -qa pcre*
pcre-7.8-6.el6.x86_64
安裝依賴包pcre-devel
yum -y install pcre-devel
./configure --prefix=/usr/local/apache2.4 --sysconf=/etc/httpd2.4 --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=prefork
make && make install
 
將2.4啟動的指令碼新增到環境變數:
[root@zabbix local]# vim /etc/profile.d/apache2.sh
export PATH=/usr/local/apache2.4/bin:$PATH
 
[root@zabbix local]# . /etc/profile.d/apache2.sh
[root@zabbix local]# echo $PATH
/usr/local/apache2.4/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
 
啟動httpd2.4服務:
apachectl start
 
編譯安裝mysql:
1、建立相應的賬戶和組
cat /etc/passwd|grep mysql 檢視是否存在mysql賬號
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin -M mysql 建立mysql組和mysql賬號
2、建立mysql資料目錄,並授權
mkdir /data/mydata -pv 建立mysql資料目錄
chown -R mysql:mysql /data/mydata 將資料目錄的所屬組所屬使用者修改
由於編譯的是mysql5.6的版本,所以需要使用cmake
3、下載並安裝cmake
下載cmake:
安裝cmake:
tar xf cmake-3.7.2.tar.gz
cd cmake-3.7.2
./bootstrap
gmake
make install
4、編譯安裝mysql
下載mysql原始檔:
tar xf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mydata \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DWITH_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
5、初始化mysql
chown -R mysql:mysql /usr/local/mysql 修改屬組屬主
cd /usr/local/mysql/scripts 執行資料初始化
./mysql_install_db --user=mysql --datadir=/data/mydata 執行此步報錯:
[root@wadeson scripts]# ./mysql_install_db --user=mysql --datadir=/data/mydata
FATAL ERROR: Could not find ./bin/my_print_defaults
 
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
 
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.
解決辦法:
解決辦法:加上--basedir=/usr/local/mysql
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mydata
 
cp support-files/my-default.cnf /etc/my.cnf 複製配置檔案
增加到配置檔案:
【mysqld】
basedir = /usr/local/mysql
datadir = /data/mydata
socket = /tmp/mysql.sock 增加套接字檔案
cp support-files/mysql.server /etc/init.d/mysqld 複製啟動指令碼
vim /etc/profile.d/mysqld.sh      
export PATH=$PATH:/usr/local/mysql/bin
. /etc/profile.d/mysqld.sh
chkconfig --add mysqld
chkconfig --list|grep mysqld
[root@wadeson mysql]# service mysqld start
Starting MySQL.Logging to '/data/mydata/wadeson.err'.
. SUCCESS!
[root@wadeson mysql]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4086/sshd
tcp 0 0 :::22 :::* LISTEN 4086/sshd
tcp 0 0 :::3306 :::* LISTEN 87380/mysqld
 
編譯安裝php5.6版本:
1、下載php原始碼包:http://php.net/releases/

tar xf php-5.6.30.tar.bz2 

cd php-5.6.30

編譯前yum安裝必要的環境

yum install -y libxml2 libxml2-devel bzip2-devel
然後執行編譯:
./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-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-gd
報錯如下:
configure: error: jpeglib.h not found.
解決辦法:
yum -y install libjpeg-devel
繼續執行,依然報錯:
configure: error: png.h not found.
解決辦法:
yum -y install libpng-devel
再次執行,還是報錯:
configure: error: freetype-config not found
解決辦法:
yum install freetype-devel
依然執行./configure,報錯:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決辦法:
tar xf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install
最後再次執行編譯引數,成功
make && make install
 
上述部分編譯引數解釋:
--prefix=/usr/local/php:定義安裝的路徑
--with-mysql=/usr/local/mysql :指定mysql安裝目錄
--with-mysqli=/usr/local/mysql/bin/mysql_config
--enable-xml :支援擴充套件標記語言
--enable-sockets :支援sockets
--with-mcrypt :提供加密支援
--with-config-file-path=/etc :在/etc目錄下生成配置檔案/etc/php.ini
--with-config-file-scan-dir=/etc/php.d :在/etc/php.d目錄下有分配置檔案
--with-bz2 :支援bz2壓縮
--enable-maintainer-zts:支援執行緒的模組,如果使用prefork請不要加入該選項,可通過命令httpd -V顯示當前載入的模組
檢視當前編譯的httpd2.4支援的mpm:
[root@wadeson ~]# /usr/local/apache2.4/bin/httpd -V
AH00557: httpd: apr_sockaddr_info_get() failed for wadeson
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.25 (Unix)
Server built: Jul 7 2017 00:38:29
Server's Module Magic Number: 20120211:67
Server loaded: APR 1.6.2, APR-UTIL 1.6.0
Compiled using: APR 1.6.2, APR-UTIL 1.6.0
Architecture: 64-bit
Server MPM: prefork 支援的prefork模組,於是php沒有上述選項
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr/local/apache2.4"
-D SUEXEC_BIN="/usr/local/apache2.4/bin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/etc/httpd2.4/mime.types"
-D SERVER_CONFIG_FILE="/etc/httpd2.4/httpd.conf"
 
複製php.ini主配置檔案:
cp php.ini-production  /etc/php.ini
然後將httpd與php進行連線,因為httpd是編譯安裝的2.4的版本:
[root@wadeson httpd2.4]# pwd
/etc/httpd2.4
[root@wadeson httpd2.4]# ll
total 124
drwxr-xr-x. 2 root root 4096 Jul 7 00:40 extra
-rw-r--r--. 1 root root 18991 Jul 7 16:22 httpd.conf
-rw-r--r--. 1 root root 18759 Jul 7 15:46 httpd.conf.bak
-rw-r--r--. 1 root root 13077 Jul 7 00:40 magic
-rw-r--r--. 1 root root 60855 Jul 7 00:40 mime.types
drwxr-xr-x. 3 root root 4096 Jul 7 00:40 original
在http.conf配置檔案中加入以下:
DirectoryIndex index.html index.php
AddType application/x-httpd-php-source .phps
AddHandler php5-script .php
AddType text/html .php
並且確認php的模組已經被啟用:(因為這種模式是和httpd模組的方式)
LoadModule php5_module modules/libphp5.so (這是針對prefork模式)
區分:
<IfModule prefork.c>
LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
LoadModule php5_module modules/libphp5-zts.so
 
然後在網站目錄編寫index.php測試頁面:
[root@wadeson htdocs]# cat index.php
<?php
phpinfo();
?>
然後測試是否與資料庫mysql連線:
[root@wadeson htdocs]# vim index.php
<?php
phpinfo();
$link = mysql_connect('127.0.0.1','root','');
if ($link)
echo "successful";
else
echo "failure";
mysql_close()
?>
/usr/local/php/bin/php -i:檢視所有配置資訊
 
為php5.6增加xcache模組:
tar xf xcache-3.0.1.tar.gz
cd xcache-3.0.1
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
make && make install 後執行報錯
報錯如下:
.0.1/xcache.c -fPIC -DPIC -o .libs/xcache.o
/root/tools/xcache-3.0.1/xcache.c: In function ¡®zif_xcache_get_special_value¡¯:
/root/tools/xcache-3.0.1/xcache.c:324: error: ¡®IS_CONSTANT_ARRAY¡¯ undeclared (first use in this function)
/root/tools/xcache-3.0.1/xcache.c:324: error: (Each undeclared identifier is reported only once
/root/tools/xcache-3.0.1/xcache.c:324: error: for each function it appears in.)
make: *** [xcache.lo] Error 1
上面的內容會報錯,原因是php5.6  xcache3.0並不支援,所以官網下載最新版,所以還是多去官網或權威網站比較好
於是下載新版本:
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-non-zts-20131226/
[root@wadeson xcache-3.2.0]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
total 1264
-rwxr-xr-x. 1 root root 589332 Jul 7 15:46 opcache.so
-rwxr-xr-x. 1 root root 701379 Jul 7 16:48 xcache.so
 
把主配置檔案放到php的碎片配置小分隊
[root@wadeson xcache-3.2.0]# mkdir /etc/php.d
[root@wadeson xcache-3.2.0]# cp xcache.ini /etc/php.d/
修改/etc/php.d/xcache.ini檔案中的指定地方
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
重啟服務:
[root@wadeson httpd2.4]# /usr/local/apache2.4/bin/apachectl restart

於是整個lamp環境已經編譯成功

note:

如果在編譯php遇見了其他錯誤可以參考如下內容:

http://www.poluoluo.com/jzxy/201505/364819.html

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章