嘗試在CentOS 7 Minimal中編譯安裝LAMP,最初打算是依賴包也通過源碼編譯安裝,但過程太過痛苦,故轉而使用yum安裝相關依賴。Apache2.4、MariaDB10、PHP7通過源碼包編譯安裝。雖然這是一次嘗試,但期間進行了數十次的試驗,才勉強完成目標。現將操作過程記錄如下,以饗後來者。

源碼編譯安裝 * Apache httpd-2.4.18 * MariaDB-10.1.11 * PHP-7.0.3


Preparations

LAMP Official Website

Software Official Site Document Site
CentOS 7 https://www.centos.org/ https://wiki.centos.org/, https://access.redhat.com/documentation/en/red-hat-enterprise-linux/
Apache https://httpd.apache.org/ http://httpd.apache.org/docs/
MariaDB http://mariadb.org/ https://mariadb.com/kb/en/mariadb/documentation/
PHP http://php.net/ http://php.net/manual/en/

Required Build Enviroments

編譯安裝需要一些工具包 * GCC * GNU Make * bzip2: 用於解壓.tar.bz2格式文件 * OpenSSL * vim

CentOS 7 Minimal中預安裝targzipmakeopenssl(未安裝openssl-devel),但未安裝bzip2, vim-enhanced, gcc

CentOS 7 Minimal中未預裝gcc, bzip2,故須先安裝,此處使用yum安裝yum install gcc gcc-c++ bzip2

以下是各軟件需要的依賴包

Software Requirements Reference
Apache Apache Apr APR-util, PCRE, GCC Compiling and Installing
MariaDB GNU zip, GNU Tar, GCC GCC-C++, GNU Make, GNU Bison,CMake, Ncurses, zlib, jemalloc Build Environment Setup for Linux
PHP GNU Autoconf, GNU Automake, GNU Libtool, GNU Bison, re2c, GNU Flex Installation on Unix systems
Nginx PCRE, zlib ,OpenSSL, (zlib) Compiling and Installing From the Sources

Download Source Packages

在對應官網下載相關源碼包

可以使用ssh遠程傳輸,也可以使用wget下載

保存路徑以/usr/local/src/爲例,在CLI中可使用wget(假設已經安裝)命令下載 * 單個下載: wget -q -nc -P /usr/local/src http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.bz2 -a ~/log.txt * 多個下載: 將所有下載鏈接寫入文件如url.txt中,使用命令wget -q -nc -P /usr/local/src -i /PATH/FROM/url.txt -a /PATH/TO/log.txt

Verification

下載源碼包後,最好進行校驗,查看是否與官方提供的校驗碼一致,以確保下載的源碼包未被更改。

常使用到的命令 * openssl dgst -md5 /PATH/FROM/file * openssl dgst -sha1 /PATH/FROM/file * sha256sum /PATH/FROM/file * sha512sum /PATH/FROM/file * gpg --recv-keys KEYS * pgp --verify /PATH/FROM/file

當下載校驗成功後,可進行編譯安裝。

Apache httpd, MariaDB, PHP三者的安裝順序,按照彼此間的依賴關係,順序安裝。即按Apache httpd –> MariaDB –> PHP的次序。

安裝路徑以/usr/local/lamp爲例。


Apache httpd

Official Documentation

以下是 Apache HTTP Server Version 2.4 官方文檔 * Apache HTTP Server Version 2.4 Documentation * Compiling and Installing * suEXEC Support

Apache httpd依賴於aprapr-utilpcre(pcre-devel),而apr-util依賴於apr。故先安裝順序爲bzip2 –> apr –> apr-util –> pcre(pcre-devel)。

也可直接通過yum安裝yum install pcre pcre-devel apr apr-devel apr-util apr-util-devel openssl openssl-devel -y安裝相關依賴。

[root@CentOS7Minimal apache]# pwd
/usr/local/src/apache
[root@CentOS7Minimal apache]# ls -lh
total 11M
-rw-r--r-- 1 root root 1008K Feb 21 10:40 apr-1.5.2.tar.gz
-rw-r--r-- 1 root root  854K Feb 21 10:40 apr-util-1.5.4.tar.gz
-rw-r--r-- 1 root root  6.8M Feb 21 10:40 httpd-2.4.18.tar.gz
-rw-r--r-- 1 root root  1.5M Feb 21 10:41 pcre-8.37.tar.bz2

Apache Requirements

apr

安裝apr參考 Linux From Scratch-Apr-1.5.2

cd /usr/local/src/
rm -rf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/lamp/apr --disable-static --with-installbuilddir=/usr/share/apr-1/build
make -j 2
make install

apr-util

安裝apr-util參考 Linux From Scratch -Apr-Util-1.5.4

yum install openssl-devel -y
cd /usr/local/src/
tar xf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/lamp/apr-util --with-apr=/usr/local/lamp/apr --with-openssl=/usr
make -j 2
make install

pcre

安裝pcre參考 PCRE-8.38 - Linux From Scratch!

CentOS 7默認已安裝pcre,但沒有安裝pcre-devel。在rpm包中pcre是主包,pcre-develpcre的支包,此處直接通過yum安裝pcre-devel。另pcre依賴GCC-C++

yum install pcre-devel -y

Add user and group

出於安全考慮,創建沒有特權的用戶及用戶組

groupadd -r apache
mkdir -p /var/www/html
useradd -r -g apache -c "Apache Httpd Server" -d /var/www/html -s /bin/false apache
chown -R apache:apache /var/www/html

此處的/var/www/html與下方./configure中的--with-suexec-docroot

Apache httpd Installation

參考 Beyond Linux® From Scratch-Introduction to Apache HTTPD

如何升級可參看 Upgrading

  • 定義安裝路徑 /usr/local/lamp/apache
  • 配置文件安裝路徑 /etc/httpd24
  • 構建MPM爲動態模塊 --enable-mpms-shared=all
  • 指定默認MPM模塊 --with-mpm=event
cd /usr/local/src/
tar xf httpd-2.4.18.tar.gz
cd httpd-2.4.18

./configure --prefix=/usr/local/lamp/apache --sysconfdir=/etc/httpd24 --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/lamp/apr --with-apr-util=/usr/local/lamp/apr-util --enable-so --enable-ssl --enable-modules=most --enable-mpms-shared=all --with-mpm=event

make -j 2
make install

注: 使用--enable-ssl須確保已經安裝openssl-devel

  1. 導出頭文件

    ln -sv /usr/local/lamp/apache/include /usr/local/include/httpd24
    
  2. 導出庫文件

#httpd未生成庫文件,可不寫
echo -e "/usr/local/lamp/apr/lib\n/usr/local/lamp/apr-util/lib\n/usr/local/lamp/apache/lib" > /etc/ld.so.conf.d/httpd24.conf

#讓系統重新生成緩存
ldconfig -v
  1. 爲可執行程序添加PATH路徑
echo 'export PATH=$PATH:/usr/local/lamp/apache/bin' > /etc/profile.d/httpd24.sh
source /etc/profile.d/httpd24.sh

Customize Configuration

修改配置文件 /etc/httpd24/httpd.conf,相關指令參見 Directive Index

日誌默認在目錄/usr/local/lamp/apache/logs/

cd /etc/httpd24/
cp -p /etc/httpd24/httpd.conf{,.bak}
vim /etc/httpd24/httpd.conf
  1. 添加PidFile 設置PidFile記錄httpd守護進程(父進程)的進程號,PidFile Directive

在行ServerRoot "/usr/local/lamp/apache"下方添加如下參數

PidFile /var/run/httpd.pid
  1. 修改ServerName #ServerName www.example.com:80 ServerName localhost

如果不修改,測試時會報如下錯誤

[root@CentOS7Minimal apache]# /usr/local/lamp/apache/bin/apachectl -k start
[sudo] password for flying:
AH00557: httpd: apr_sockaddr_info_get() failed for LAMP7
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
[root@CentOS7Minimal apache]#
  1. 修改DcoumentRoot (可選)

    #DocumentRoot "/usr/local/lamp/apache/htdocs"
    DocumentRoot  "/var/www/html"
    
  2. 修改DirectoryIndex,支持PHP (可選) ``` #DirectoryIndex index.html DirectoryIndex index.php index.html

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


其餘配置修改,暫不修改。

#### Apache Testing
* 查看版本信息

[root@CentOS7Minimal apache]# httpd -V Server version: Apache/2.4.18 (Unix) Server built: Feb 21 2016 12:31:43 Server’s Module Magic Number: 20120211:52 Server loaded: APR 1.5.2, APR-UTIL 1.5.4 Compiled using: APR 1.5.2, APR-UTIL 1.5.4 Architecture: 64-bit Server MPM: event threaded: yes (fixed thread count) 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/lamp/apache” -D SUEXEC_BIN=“/usr/local/lamp/apache/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/httpd24/mime.types” -D SERVER_CONFIG_FILE=“/etc/httpd24/httpd.conf” [root@CentOS7Minimal apache]#


* 啓動服務測試

[root@CentOS7Minimal bin]# pwd /usr/local/lamp/apache/bin

#啓動httpd服務 [root@CentOS7Minimal bin]# /usr/local/lamp/apache/bin/apachectl -k start

#再次執行,提示已經啓動httpd服務 [root@CentOS7Minimal bin]# /usr/local/lamp/apache/bin/apachectl -k start httpd (pid 28991) already running

#關閉httpd服務 [root@CentOS7Minimal bin]# /usr/local/lamp/apache/bin/apachectl -k stop

#再次執行,提示httpd未運行 [root@CentOS7Minimal bin]# /usr/local/lamp/apache/bin/apachectl -k stop httpd (no pid file) not running [root@CentOS7Minimal bin]#


使用`/usr/local/lamp/apache/bin/apachectl -k start`啓動服務後,在瀏覽器ULR中輸入`localhost`或`127.0.0.1`,如果不是在本機,可輸入該Server的IP,即能看到 **It works!** 提示。

顯示的是`DocumentRoot "/usr/local/lamp/apache/htdocs"`目錄中的內容。


#### Systemd support
CentOS 7使用`systemd`作爲系統init程序,相關文件路徑

| Directory | Description |
| :--- | :--- |
| `/usr/lib/systemd/system/` | Systemd units distributed with installed RPM packages. |
| `/run/systemd/system/` | Systemd units created at run time. This directory takes precedence over the directory with installed service units. |
| `/etc/systemd/system/` | Systemd units created and managed by the system administrator. This directory takes precedence over the directory with runtime units. |

-- [CHAPTER 8. MANAGING SERVICES WITH SYSTEMD](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Managing_Services_with_systemd.html)

此處創建service實現通過`systemd`管理httpd服務。

使用yum安裝的httpd2.4,在目錄`/usr/lib/systemd/system`有文件`httpd.service`,其內容爲

[Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target Documentation=man:httpd(8) Documentation=man:apachectl(8)

[Service] Type=notify EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} KillSignal=SIGCONT PrivateTmp=true

[Install] WantedBy=multi-user.target


文件`/etc/sysconfig/httpd`內容爲(只有一句)

LANG=C


參考以上文件並結合[How Do I Compile Apache httpd 2.4.16 with systemd Support?](http://stackoverflow.com/questions/32977557/how-do-i-compile-apache-httpd-2-4-16-with-systemd-support)

執行

echo LANG=C > /etc/sysconfig/httpd


創建文件`/usr/lib/systemd/system/httpd24.service`

[Unit] Description=The Apache HTTP Server After=network.target remote-fs.target nss-lookup.target #Documentation=man:httpd(8) #Documentation=man:apachectl(8)

[Install] WantedBy=multi-user.target Alias=httpd.service

[Service] #Type=notify Type=forking #EnvironmentFile=/usr/local/lamp/apache/bin/envvars EnvironmentFile=/etc/sysconfig/httpd PIDFile=/var/run/httpd.pid ExecStart=/usr/local/lamp/apache/bin/apachectl start ExecReload=/usr/local/lamp/apache/bin/apachectl graceful ExecStop=/usr/local/lamp/apache/bin/apachectl stop KillSignal=SIGCONT PrivateTmp=true


如果更改此文件,須執行`systemctl daemon-reload`重新加載

需要重啓系統,不然無法正常生效。

* 啓動:`systemctl start httpd24.service`
* 關閉: `systemctl stop httpd24.service`
* 查看狀態: `systemctl status httpd24.service`
* 重載: `systemctl reload httpd24.service`

* 設置開啓啓動: `systemctl enable httpd24.service`
* 禁止開機啓動: `systemctl disable httpd24.service`

實現使用`systemd`管理httpd服務。

使用`systemctl status httpd24.service`提示報錯`systemd[1]: Ignoring invalid environment assignment ...rs`,與`EnvironmentFile`設置有關,執行`echo LANG=C > /etc/sysconfig/httpd`即可。


---
## MariaDB10
### Official Documentation
此爲MariaDB官方文檔
* [Compiling MariaDB From Source](https://mariadb.com/kb/en/mariadb/compiling-mariadb-from-source/)
* [Build Environment Setup for Linux](https://mariadb.com/kb/en/mariadb/Build_Environment_Setup_for_Linux/)
* [Building MariaDB on CentOS](https://mariadb.com/kb/en/mariadb/source-building-mariadb-on-centos/)

此爲Beyond Linux® From Scratch
* [MariaDB-10.1.11](http://www.linuxfromscratch.org/blfs/view/svn/server/mariadb.html)

### MariaDB Requirements
MariaDB使用`cmake`編譯,依賴`jemalloc`、`m4`、`bison`,順序安裝`cmake`, `jemalloc`, `m4`, `bison`。

也可直接通過yum安裝`yum install ncurses-devel openssl-devel libevent-devel jemalloc jemalloc-devel cmake m4 bison gcc-c++ -y`安裝相關依賴。

>`libgeoip` for the Geo IP module, `libgd` for the Image Filter module, `libxml2` , and `libxslt` for the XSLT module


#### cmake
依賴`gcc-c++`,參考[Beyond Linux® From Scratch-CMake-3.4.3](http://www.linuxfromscratch.org/blfs/view/svn/general/cmake.html)


### Add user and group

groupadd -r mysql useradd -r -g mysql -c “MariaDB Server” -d /data/mariadb -s /bin/false mysql


### MariaDB Installation
安裝參照 [Beyond Linux® From Scratch MariaDB-10.1.10](http://www.linuxfromscratch.org/blfs/view/svn/server/mariadb.html)

定義數據存儲路徑爲`/data/mariadb`

ssl(使支持加密),zlib(使支持compress, uncompress功能)

cd /usr/local/src tar xf mariadb-10.1.11.tar.gz

cmake -DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=/usr/local/lamp/mariadb
-DINSTALL_DOCDIR=share/doc/mariadb-10.1.11
-DINSTALL_DOCREADMEDIR=share/doc/mariadb-10.1.11
-DINSTALL_MANDIR=share/man
-DINSTALL_MYSQLSHAREDIR=share/mysql
-DINSTALL_MYSQLTESTDIR=share/mysql/test
-DINSTALL_PLUGINDIR=lib/mysql/plugin
-DINSTALL_SBINDIR=sbin
-DINSTALL_SCRIPTDIR=bin
-DINSTALL_SQLBENCHDIR=share/mysql/bench
-DINSTALL_SUPPORTFILESDIR=share/mysql
-DMYSQL_DATADIR=/data/mariadb
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock
-DWITH_EXTRA_CHARSETS=complex
-DWITH_EMBEDDED_SERVER=ON
-DWITH_SSL=system
-DWITH_ZLIB=system
-DTOKUDB_OK=0
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci make -j 2 make install


配置文件 `/etc/my.cnf`,在目錄`/usr/local/src/mariadb-10.1.11/support-files`中有配置文件示例


### Export Related Files
1. 導出頭文件

ln -sv /usr/local/lamp/mariadb/include /usr/local/include/mariadb


2. 導出庫文件

echo -e “/usr/local/lamp/mariadb/lib” > /etc/ld.so.conf.d/mariadb.conf

#讓系統重新生成緩存 ldconfig -v


3. 爲可執行程序添加PATH路徑

echo ‘export PATH=$PATH:/usr/local/lamp/mariadb/bin’ > /etc/profile.d/mariadb.sh source /etc/profile.d/mariadb.sh



### Configuration
在配置文件`/etc/my.cnf`中添加`skip_name_resolve=ON`,否則在執行`mysql_install_db`時會出現如`WARNING: The host 'CentOS7Minimal' could not be looked up with resolveip.`的報錯信息

#### my.cnf

[mysqld] port = 3306 socket = /tmp/mysqld.sock datadir=/data/mariadb

symbolic-links=0 skip_name_resolve=ON character_set_server=utf8 collation-server=utf8_general_ci

#log bin log_bin=mariadb-bin log_bin_index=mariadb-bin.index binlog_format=mixed sync-binlog = 1 expire-logs-days = 14

#slow log slow_query_log = 1 long_query_time = 1.5 slow_query_log_file = /var/lib/mariadb/mariadb_slow.log log_queries_not_using_indexes=1

CACHES AND LIMITS

tmp-table-size = 32M max-heap-table-size = 32M query-cache-type = 0 query-cache-size = 0 max-connections = 500 thread-cache-size = 50 open-files-limit = 65535 table-definition-cache = 1024 table-open-cache = 2048

INNODB

innodb-file-per-table = 1 innodb-flush-method = O_DIRECT innodb-log-files-in-group = 2 innodb-log-file-size = 128M innodb-flush-log-at-trx-commit = 1

innodb_buffer_pool_size = 400M #通常是內存的60%~80% innodb_buffer_pool_instances =1

#show status like ‘Threads_created’; #show status like ‘Connections’; #命中率計算 100 - ((Threads_created / Connections) * 100); 值越小,越應提高thread_cache_size

#thread thread_cache_size = 16 thread_pool_size = 16

[client] port = 3306 socket = /tmp/mysqld.sock

[mysqld_safe] log-error=/var/log/mariadb/mariadb_error.log

include all files from the config directory

!includedir /etc/my.cnf.d


註:此處將`socket`放置在`/tmp/`目錄,而非`/var/lib/`目錄下,是爲了解決每次重啓系統後MariaDB服務無法正常創建相關文件。

#### mysql_install_db
創建相關目錄並賦予權限

mkdir -p /var/log/mariadb chown -R mysql:mysql /var/log/mariadb

mkdir -p /data/mariadb chown -R mysql:mysql /data/mariadb

#安裝數據庫 mysql_install_db –basedir=/usr/local/lamp/mariadb –datadir=/data/mariadb –user=mysql



操作完成後,出現如下提示

To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER ! To do so, start the server, then issue the following commands:

’/usr/local/lamp/mariadb/bin/mysqladmin’ -u root password ‘new-password’ ‘/usr/local/lamp/mariadb/bin/mysqladmin’ -u root -h lampCentOS7 password ‘new-password’

Alternatively you can run: ‘/usr/local/lamp/mariadb/bin/mysql_secure_installation’

which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the MySQL manual for more instructions.

You can start the MariaDB daemon with: cd ‘/usr/local/lamp/mariadb’ ; /usr/local/lamp/mariadb/bin/mysqld_safe –datadir=‘/data/mariadb’

You can test the MariaDB daemon with mysql-test-run.pl cd ‘/usr/local/lamp/mariadb/mysql-test’ ; perl mysql-test-run.pl


使用service查看mysql狀態,報錯`MySQL is running but PID file could not be found`,參考[ERROR! MySQL manager or server PID file could not be found!](http://stackoverflow.com/questions/17424863/error-mysql-manager-or-server-pid-file-could-not-be-found-qnap)

ps aux | grep mysql kill -9 8965 9089 rm -rf /var/lock/subsys/mysql service mysqld start service mysqld status #查看正常


### Service Support

cp /usr/local/src/mariadb-10.1.11/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig mysqld on chkconfig –list

即可通過`service`控制mysqld服務狀態


---
### Systemd Support
使用yum安裝的MariaDB,在目錄`/usr/lib/systemd/system`有文件`mariadb.service`,其內容爲

[Unit] Description=MariaDB database server After=network.target After=syslog.target

[Install] WantedBy=multi-user.target Alias=mysql.service Alias=mysqld.service

[Service] Type=notify PrivateNetwork=false User=mysql Group=mysql CapabilityBoundingSet=CAP_IPC_LOCK PermissionsStartOnly=true ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS $_WSREP_NEW_CLUSTER

KillMode=process KillSignal=SIGTERM

Don’t want to see an automated SIGKILL ever

SendSIGKILL=no

Restart crashed server only, on-failure would also restart, for example, when

my.cnf contains unknown option

Restart=on-abort RestartSec=5s

UMask=007 PrivateTmp=false LimitNOFILE=16364



創建文件 `/usr/lib/systemd/system/mariadb.service`

[Unit] Description=MariaDB database server After=network.target After=syslog.target

[Install] WantedBy=multi-user.target Alias=mysql.service Alias=mysqld.service

[Service] #Type=notify Type=simple #PrivateNetwork=false User=mysql Group=mysql CapabilityBoundingSet=CAP_IPC_LOCK PermissionsStartOnly=true #ExecStart=/usr/local/lamp/mariadb/bin/mysqld_safe $MYSQLD_OPTS $_WSREP_NEW_CLUSTER ExecStart=/usr/local/lamp/mariadb/bin/mysqld_safe –basedir=/usr/local/lamp/mariadb –user= PIDFile=/data/mariadb/mariadb.pid #KillMode=process #KillSignal=SIGTERM

#SendSIGKILL=no

#Restart=on-abort #RestartSec=5s

UMask=007 PrivateTmp=false #LimitNOFILE=16364


注意:其中的別名`mysql.service`和`mysqld.service`只有在進行`systemctl enable mariadb.service`後才能使用,否則會提示

[root@CentOS7Minimal ~]# systemctl status mysqld.service ● mysqld.service Loaded: not-found (Reason: No such file or directory) Active: inactive (dead) [root@CentOS7Minimal ~]# systemctl status mysql.service ● mysql.service Loaded: not-found (Reason: No such file or directory) Active: inactive (dead) [root@CentOS7Minimal ~]#


執行`systemctl daemon-reload`重載systemd


### Secure Installation

mysql_secure_installation


出現報錯`ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")`,原因是MariaDB服務沒有啓動,啓動即可


---
## PHP7
### Official Documentation
* [Installation on Unix systems](http://php.net/manual/en/install.unix.php)
* [FastCGI Process Manager (FPM)](http://php.net/manual/en/install.fpm.php)
* [Apache 2.x on Unix systems](http://php.net/manual/en/install.unix.apache2.php)
* [Beyond Linux® From Scratch](http://www.linuxfromscratch.org/blfs/view/svn/general/php.html)

### PHP Requirements
`yum -y install autoconf automake libtool re2c flex bison bzip2-devel libmcrypt-devel libxml2-devel libxml2`

>To use PHP with MySQL, you may also have to enable MySQL with PHP by configuring PHP with the --with-mysql=/path_to_mysql option

PHP7中取消了`--with-mysql`,使用`mysqlnd`

### PHP Installation

cd /usr/local/src tar xf php-7.0.3.tar.xz cd php-7.0.3 ./buildconf –force ./configure –prefix=/usr/local/lamp/php –sysconfdir=/etc –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –localstatedir=/var –mandir=/usr/share/man –enable-fpm –enable-xml –enable-sockets –enable-mbstring –with-fpm-user=apache –with-fpm-group=apache –with-openssl –enable-mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd –with-mysql-sock=/tmp/mysqld.sock –with-zlib –with-bz2 –with-mcrypt –with-apxs2=/usr/local/lamp/apache/bin/apxs –enable-maintainer-zts make test make -j 2 make install


### Export Related Files
1. 導出頭文件

ln -sv /usr/local/lamp/php/include /usr/local/include/php7


2. 導出庫文件

echo -e “/usr/local/lamp/php/lib” > /etc/ld.so.conf.d/php7.conf

#讓系統重新生成緩存 ldconfig -v


3. 爲可執行程序添加PATH路徑

echo ‘export PATH=$PATH:/usr/local/lamp/php/bin’ > /etc/profile.d/php7.sh source /etc/profile.d/php7.sh


### PHP Configuration

* `php.int`

cp /usr/local/src/php-7.0.3/php.ini-production /etc/php.ini cp -p /etc/php.ini{,.bak}


#vim /etc/phpini

;date.timezone = date.timezone = Asia/Shanghai

;expose_php = On expose_php = Off


* `php-fpm`

cp -p /etc/php-fpm.conf.default /etc/php-fpm.conf


#vim /etc/php-fpm.conf

;pid = run/php-fpm.pid pid = run/php-fpm.pid

;error_log = log/php-fpm.log error_log = log/php-fpm.log


* `www.conf`

cp -p /etc/php-fpm.d/www.conf.default /etc/php-fpm.d/www.conf


#vim /etc/php-fpm.d/www.conf

;listen = 127.0.0.1:9000 listen = 127.0.0.1:9000


### systemd support
創建文件`/usr/lib/systemd/system/php-fpm.service`

[Unit] Description=The PHP FastCGI Process Manager After=syslog.target network.target

[Install] WantedBy=multi-user.target Alias=php.service

[Service] Type=simple PIDFile=/run/php-fpm.pid ExecStart=/usr/local/lamp/php/sbin/php-fpm –nodaemonize –fpm-config /etc/php-fpm.conf ExecReload=/usr/bin/kill -USR2 $MAINPID ExecStop=/usr/bin/kill -SIGINT $MAINPID


執行`systemctl daemon-reload`重載systemd


---
## Final Testing
測試整個LAMP能否正常工作

創建`/var/www/html/index.php`

```php
<?php
#   phpinfo()

$db = new PDO('mysql:host=127.0.0.1;port=3306','flying','12345');

$sql = "select user,host from mysql.user";
$stmt = $db->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);

可正常輸出數據

Array ( [0] => Array ( [user] => flying [host] => % ) [1] => Array ( [user] => root [host] => 127.0.0.1 ) [2] => Array ( [user] => root [host] => ::1 ) [3] => Array ( [user] => root [host] => localhost )

Problems Left

  • 重啓系統後無法啓動MariaDB服務,提示沒有權限在目錄/var/run/var/lib/下創建文件(pid和sock文件),此問題暫時未解決。pid文件暫放在/data/mariadb路徑下,sock文件贊放在/tmp路徑下
  • PHP7通過sock與apache通信,暫未實現。

References


Change Log

  • 2016.02.23 14:43 Tue Asia/Beijing
    • 完成初稿,上傳至lempstacker部落格

  • Note Time: 2016.02.23 14:43 Tue
  • Note Location: Asia/Beijing
  • Writer: lempstacker