Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

LAMP 環境源碼編譯安裝實戰:Apache、MySQL、PHP 全流程

前言

Linux + Apache + MySQL/MariaDB + Perl/PHP/Python,這組開源軟體各自獨立,卻因常被放在一起使用而擁有極高的相容度,共同撐起一個強大的 Web 應用平台。開源浪潮下,LAMP 已與 J2EE、.Net 形成三足鼎立之勢,且開發項目的軟體投資成本較低,因此備受 IT 界關注。從流量來看,七成以上的網站訪問由 LAMP 承載。

環境說明

  • 系統:Red Hat Enterprise Linux Server release 6.4
  • 軟體:httpd-2.4.20 + mysql-5.7.12 + php-5.5.36
  • 前提:系統已具備編譯環境(gcc、make 等),若無請先安裝

一、編譯 Apache 前的依賴準備

httpd-2.4.20 之前必須先解決依賴:apr、apr-util 與 pcre。前兩者可用 RPM 升級或源碼編譯,此處統一走源碼編譯。

apr-1.5.2

tar zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make && make install

apr-util-1.5.4

tar zxvf 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

編譯 apr-util 時必須指定 apr 的安裝位置。

pcre-8.38

tar zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/usr/local/pcre
make && make install

二、編譯安裝 Apache

tar zxvf httpd-2.4.20.tar.gz
cd httpd-2.4.20
./configure \
  --prefix=/usr/local/apache \
  --sysconfdir=/etc/httpd \
  --enable-so --enable-ssl --enable-cgi --enable-rewrite \
  --with-zlib --with-pcre=/usr/local/pcre \
  --with-apr=/usr/local/apr \
  --with-apr-util=/usr/local/apr-util \
  --enable-mods-shared=most --enable-mpms-shared=all \
  --with-mpm=event
make && make install

選項說明:--prefix 指定安裝目錄;--sysconfdir 指定設定檔路徑;--enable-so 允許執行期載入 DSO 模組;--enable-ssl 啟動 SSL 加密;--enable-cgi 啟用 CGI;--enable-rewrite 啟用 URL 重寫;--with-pcre/--with-apr/--with-apr-util 指定依賴安裝路徑;--enable-mods-shared=most 啟用大多數共享模組;--with-mpm=event 指定 MPM 類型。

啟動並驗證

[root@server1 bin]# ./apachectl start
[root@server1 bin]# curl https://localhost

修改 PidFile 路徑

[root@server1 bin]# ./apachectl stop
[root@server1 bin]# vim /etc/httpd/httpd.conf
PidFile "/var/run/httpd.pid"
[root@server1 bin]# ./apachectl start

編寫服務腳本 /etc/init.d/httpd

#!/bin/bash
#
# httpd      Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# pidfile: /var/run/httpd.pid

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
    . /etc/sysconfig/httpd
fi

HTTPD_LANG=${HTTPD_LANG-"C"}
INITLOG_ARGS=""

apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
    echo -n $"Starting $prog: "
    LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} -d 10 $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=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

case "$1" in
    start) start ;;
    stop) stop ;;
    status) status -p ${pidfile} $httpd; RETVAL=$? ;;
    restart) stop; start ;;
    condrestart) [ -f ${pidfile} ] && { stop; start; } ;;
    reload) reload ;;
    graceful|help|configtest|fullstatus) $apachectl $@; RETVAL=$? ;;
    *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
       exit 1
esac
exit $RETVAL

給腳本執行權限並用 service 起停:

[root@server1 ~]# chmod +x /etc/init.d/httpd
[root@server1 ~]# service httpd start
Starting httpd: [  OK  ]
[root@server1 ~]# service httpd status
httpd (pid 3365) is running...

加入服務列表並設定開機自啟:

[root@server1 ~]# chkconfig --add httpd
[root@server1 ~]# chkconfig httpd --level 235 on
[root@server1 ~]# chkconfig --list httpd
httpd   0:off  1:off  2:on  3:on  4:off  5:on  6:off

把 Apache 的 bin 加進 PATH(腳檔名必須以 .sh 結尾):

[root@server1 ~]# vim /etc/profile.d/apache.sh
export PATH=$PATH:/usr/local/apache/bin

三、編譯安裝 MySQL 前的準備

MySQL 5.5 之後編譯工具改為 cmake,需先確認已安裝。建立 mysql 組與使用者:

[root@server1 src]# groupadd mysql
[root@server1 src]# useradd -g mysql mysql

cmake-3.5.2

tar zxvf cmake-3.5.2.tar.gz
cd cmake-3.5.2
./bootstrap --prefix=/usr/local/cmake
gmake & gmake install
[root@server1 ~]# vim /etc/profile.d/cmake.sh
export PATH=$PATH:/usr/local/cmake/bin

準備資料存放目錄

[root@server1 ~]# fdisk /dev/sdb            # 分一個 10G 分區
[root@server1 ~]# pvcreate /dev/sdb1
[root@server1 ~]# vgcreate myvg /dev/sdb1
[root@server1 ~]# lvcreate -L +10G -n mysql_data myvg
[root@server1 ~]# mke2fs -jv /dev/myvg/mysql_data
[root@server1 ~]# mkdir -p /mydata/mysql_data
[root@server1 ~]# vim /etc/fstab
/dev/myvg/mysql_data  /mydata  ext3  defaults  0 0
[root@server1 ~]# mount -a
[root@server1 /]# chown -R mysql.mysql /mydata/
[root@server1 /]# chmod o-rx /mydata/

四、編譯安裝 MySQL

tar zxvf mysql-5.7.12.tar.gz
cd mysql-5.7.12
cmake . \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/mysql_date/mydata \
 -DDOWNLOAD_BOOST=1 \
 -DWITH_BOOST=../boost_1_59_0 \
 -DSYSCONFDIR=/etc \
 -DMYSQL_USER=mysql \
 -DWITH_MYISAM_STORAGE_ENGINE=1 \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
 -DWITH_MEMORY_STORAGE_ENGINE=1 \
 -DWITH_READLINE=1 \
 -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \
 -DMYSQL_TCP_PORT=3306 \
 -DENABLED_LOCAL_INFILE=1 \
 -DENABLE_DOWNLOADS=1 \
 -DWITH_PARTITION_STORAGE_ENGINE=1 \
 -DEXTRA_CHARSETS=all \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DWITH_DEBUG=0 \
 -DMYSQL_MAINTAINER_MODE=0 \
 -DWITH_SSL:STRING=bundled \
 -DWITH_ZLIB:STRING=bundled
make && make install

參數重點:Boost 庫自 MySQL 5.7.5 起為必備;DEFAULT_CHARSET=utf8 設定預設字符集;WITH_SSL 與 WITH_ZLIB 使用內建版本;WITH_DEBUG=0 關閉除錯模式。

更改目錄屬主並加入環境變數:

[root@server1 mysql]# chown -R mysql.mysql .
[root@server1 mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin

加入服務列表並設定開機自啟:

[root@server1 ~]# cd /usr/local/mysql/support-files/
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld
[root@server1 support-files]# chmod +x /etc/init.d/mysqld
[root@server1 support-files]# chkconfig mysqld --level 2345 on
[root@server1 support-files]# chkconfig --list mysqld
mysqld   0:off  1:off  2:on  3:on  4:on  5:on  6:off

撰寫 /etc/my.cnf(以下為參考配置,可依實際情況調整):

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

初始化系統資料庫。--initialize 會產生一組隨機密碼(位於 ~/.mysql_secret),--initialize-insecure 則不設密碼:

[root@server1 ~]# mysqld --initialize-insecure \
  --user=mysql --basedir=/usr/local/mysql --datadir=/mysql_date/mydata/

啟動並確認:

[root@server1 ~]# mysqld_safe --user=mysql --datadir=/mysql_date/mydata/
[root@server1 ~]# service mysqld start
Starting MySQL [  OK  ]

[root@server1 ~]# ps -ef | grep mysqld
[root@server1 ~]# netstat -an | grep 3306
tcp   0  0  0.0.0.0:3306   0.0.0.0:*   LISTEN

設定 root 密碼。mysql_secure_installation 會詢問是否修改密碼,並提供三級密碼強度政策:LOW 僅要求長度 ≥ 8;MEDIUM 另需數字、大小寫與特殊字元;STRONG 還需通過字典檔檢查。

[root@server1 mysql]# mysql_secure_installation

把 MySQL 動態庫加入系統庫路徑:

[root@server1 ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@server1 ~]# ldconfig -v

測試登入:

[root@server1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Server version: 5.7.12-log Source distribution
mysql>

五、編譯 PHP 前的加密擴充依賴

若希望 PHP 支援 mcrypt 擴充,需先準備 libmcrypt、mhash、mcrypt 三個套件,安裝順序為 libmcrypt → mhash → mcrypt。

# libmcrypt-2.5.8
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make && make install
vim /etc/ld.so.conf.d/libmcrypt.conf
/usr/local/libmcrypt/lib
ldconfig -v
# mhash-0.9.9.9
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
vim /etc/ld.so.conf.d/mhash.conf
/usr/local/mhash/lib
ldconfig -v
# mcrypt-2.6.8
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure --with-libmcrypt-prefix=/usr/local/libmcrypt/
make && make install

把庫路徑寫進 /etc/ld.so.conf.d/ 並執行 ldconfig -v 是關鍵,否則編譯 mcrypt 會報錯。

六、編譯安裝 PHP

tar -zxvf php-5.5.36.tar.gz
cd php-5.5.36
./configure --prefix=/usr/local/php \
  --with-mysql=mysqlnd --with-openssl \
  --with-mysqli=mysqlnd \
  --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
make && make install

參數說明:--with-mysql=mysqlnd 與 --with-mysqli=mysqlnd 讓 PHP 與 MySQL 結合;--enable-mbstring 支援多字節中文;freetype、jpeg、png 相關選項支援不同字體與圖片格式;--with-zlib、--with-bz2 支援壓縮庫;--with-libxml-dir 與 --enable-xml 支援標記語言;--enable-sockets 支援套接字通信;--with-mcrypt 提供額外加密能力;--with-config-file-path=/etc 指定 php.ini 位置;--with-config-file-scan-dir=/etc/php.d 支援附加 ini 檔。

兩點提醒:
1. 為支援 Apache 的 worker 或 event MPM,編譯時需加 --enable-maintainer-zts(prefork 不需加)。可用 httpd -M 驗證目前的 MPM 模式。
2. PHP 5.3 以上可指定 mysqlnd,如此本機就不必先安裝 MySQL 或用戶端開發套件;mysqlnd 自 PHP 5.3 可用,5.4 起已是預設。寫法:./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd。

提供設定檔並讓 Apache 認識 PHP:

[root@server1 php-5.5.36]# cp php.ini-production /etc/php.ini
[root@server1 ~]# vim /etc/httpd/httpd.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

DirectoryIndex index.php index.html

重啟 Apache 並確認 80 埠已監聽:

[root@server1 ~]# service httpd restart
Stopping httpd: [  OK  ]
Starting httpd: [  OK  ]
[root@server1 ~]# netstat -an | grep :80
tcp   0  0  :::80  :::*  LISTEN

寫入測試頁:

[root@server1 htdocs]# vim index.php
<?php phpinfo(); ?>

瀏覽器訪問驗證:

LAMP 環境源碼編譯安裝實戰:Apache、MySQL、PHP 全流程

再測 PHP 能否連上 MySQL:

[root@server1 htdocs]# vim index.php
<?php
$conn = mysql_connect('localhost', 'root', '你的密碼');
if ($conn) echo "Success..."; else echo "Failure...";
?>

LAMP 環境源碼編譯安裝實戰:Apache、MySQL、PHP 全流程

把 MySQL 停掉再測一次,用來確認連線判斷是否有效:

[root@server1 htdocs]# service mysqld stop
Shutting down MySQL. [  OK  ]
[root@server1 htdocs]# service mysqld status
MySQL is not running [FAILED]
[root@server1 htdocs]# netstat -an | grep 3306

LAMP 環境源碼編譯安裝實戰:Apache、MySQL、PHP 全流程


至此,以源碼方式打造的 LAMP 環境就算完成了。過程中踩了不少坑,靠著搜尋與前人的經驗才得以順利收工。

Copyright © Linux教程網 All Rights Reserved