前言
本文主要大致介紹CentOS 7下編譯安裝Nginx、MariaDB、PHP。面向有Linux基礎且愛好鑽研的朋友。技藝不精,疏漏再所難免,還望指正。
環境簡介:
系統: | CentOS 7,最小化安裝 |
IP: | 192.168.170.128 |
Nginx: | 1.6.1 |
MariaDB: | 5.5.39 |
PHP: | 5.5.16 |
1、准備工作
1.1、系統硬件准備
盡管Linux能最大化發揮硬件資源,但RHEL/CentOS隨着版本增加對最低硬件的配置也越來越高[1]。RHEL7/CentOS最低要求內存最小 1GB,建議每個邏輯 CPU 1 GB。但如果系統為CentOS7,硬件內存小於1G,比如512M,而且沒有配置交換分區,建議先配置SWAP,以防內存不足導致數據庫啟動失敗或進程異常退出。配置添加SWAP方法參考《Linux下添加swap分區》。
1.2、系統軟件准備
如果是最小化安裝,比如VPS、雲主機一般就是最小化安裝。需要配置系統至方便易用。
更新系統至最新:
yum makecache && yum -y update
安裝必要實用基礎軟件:
yum -y install bash-completion vim net-tools bind-utils wget screen
建議重啟以應用最新補丁:
reboot
1.3、所需軟件及各軟件簡介
安裝需要用到的軟件有nginx、mysql、php、pcre、openssl、zlib、cmake、mcrypt、yasm、t1lib、libgd、libvpx、tiff、libpng、libjpeg、freetype。這些軟件的簡介如下:
(1)Nginx
Nginx(發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:新浪、網易、騰訊等。
官方下載地址:http://nginx.org/en/download.html
(2)PHP
PHP,這里略去十萬字…
官方下載網址:http://cn2.php.net/downloads.php
最新版下載地址:http://cn2.php.net/distributions/php-5.5.16.tar.bz2
(3)MariaDB
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,采用GPL授權許可。開發這個分支的原因之一是:甲骨文公司收購了MySQL后,有將MySQL閉源的潛在風險,因此社區采用分支的方式來避開這個風險。 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。在存儲引擎方面,使用XtraDB(英語:XtraDB)來代替MySQL的InnoDB。現在MariaDB官方主推最新版MariaDB 10,其對應MySQL 5.6,但它在文件結構、一些功能上已經和MySQL 5.6大為不同,查看官方博文《MariaDB vs. MySQL》、《MariaDB 10.0 and MySQL 5.6》。如果用於生產環境,建議使用穩定版5.5。
官方下載地址:https://downloads.mariadb.org/;MariaDB 5.5;下載頁面:https://mariadb.com/kb/en/mariadb/what-is-mariadb-55/
(4)PCRE
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。PCRE被廣泛使用在許多開源軟件之中,最著名的莫過於Apache HTTP服務器和PHP腳本語言。
官方網址:http://www.pcre.org/
(5)OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。該程序出現安全漏洞時比較危險,建議從官方網站下載最新版。
官方下載網址:http://www.openssl.org/source/
(6)zlib
zlib是提供數據壓縮用的函式庫,使用DEFLATE算法.zlib是一種事實上的業界標准,以至於在標准文檔中,zlib和DEFLATE常常互換使用。數以千計的應用程序直接或間接依靠zlib壓縮函式庫,包括: Linux核心:使用zlib以實作網絡協定的壓縮、檔案系統的壓縮以及開機時解壓縮自身的核心;libpng,用於PNG圖形格式的一個實現,對bitmap數據規定了DEFLATE作為流壓縮方法。Apache:使用zlib實作http 1.1;OpenSSH、OpenSSL:以zlib達到最佳化加密網絡傳輸。
官方下載網址:http://zlib.net/
(7)CMake
CMake是一個跨平台的安裝(編譯)工具,可以用簡單的語句來描述所有平台的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project文件,能測試編譯器所支持的C++特性,類似UNIX下的automake。CMake 可以編譯源代碼、制作程式庫、產生適配器(wrapper)、還可以用任意的順序建構執行檔。CMake 支持 in-place 建構(二進檔和源代碼在同一個目錄樹中)和 out-of-place 建構(二進檔在別的目錄里),因此可以很容易從同一個源代碼目錄樹中建構出多個二進檔。CMake 也支持靜態與動態程式庫的建構。這里用來編譯MySQL或MariaDB
官方網址:http://www.cmake.org/
(8)mcrypt
mcrypt 是 php 里面重要的加密支持擴展庫,Mcrypt庫支持20多種加密算法和8種加密模式,具體可以通過函數mcrypt_list_algorithms()和mcrypt_list_modes()來顯示。
下載地址:http://mcrypt.sourceforge.net/
(9)yasm
In computing, Yasm is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. Yasm is a full rewrite of Netwide Assembler (NASM). Yasm can generally be used interchangeably with NASM and supports the x86 and x86-64 architectures. It is licensed under a revision of the BSD licenses. As of 2011 it was developed by Peter Johnson and Michael Urman.
下載地址:http://yasm.tortall.net/Download.html
(10)libgd
GD 是一個開源的圖像處理庫,支持 PNG/JPEG/GIF以及其他格式的圖像。GD一般被用來創建圖表、圖形、縮略圖以及其他圖像相關的處理操作。
下載地址:http://libgd.bitbucket.org/
(11)libpng
libpng 軟件包包含 libpng 庫.這些庫被其他程式用於讀寫png文件. PNG 庫是用來創立和操作PNG 格式的圖像文件.PNG 格式是設計來替代GIF,他對於更小范圍的TIFF(標記圖象文件格式)來說, 有了非常多的進步和拓展並且減少了關於專利權的麻煩.
下載地址:http://www.libpng.org/pub/png/libpng.html
(12)libvpx
libvpx 是一個開源的VP8編碼解碼器。VP8:高質量的視頻編碼。
(13)FreeType
FreeType庫是一個完全免費(開源)的、高質量的且可移植的字體引擎,它提供統一的接口來訪問多種字體格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。FreeType 2被設計為一種占用空間小的、高效的、高度可定制的、並且可以產生可移植的高品質輸出(符號圖像)。可以被用在諸如圖像庫、展出服務器、字體轉換工具、圖像文字產生工具等多種其它產品上。
官方網址:http://www.freetype.org/
下載頁面網址:http://www.freetype.org/download.html
(14)LibJPEG
LibJPEG 是一個廣泛使用的 JPEG 圖像壓縮庫,采用 C 語言開發。
官方網址:http://www.ijg.org/
1.4、下載所需軟件
各軟件一鍵下載:
cd /usr/local/src/ && wget -ct 5 http://nginx.org/download/nginx-1.6.1.tar.gz && wget -ct 5 http://sfo1.mirrors.digitalocean.com/mariadb/mariadb-5.5.39/source/mariadb-5.5.39.tar.gz && wget -ct 5 http://cn2.php.net/distributions/php-5.5.16.tar.bz2 && wget -ct 5 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz && wget -ct 5 http://www.openssl.org/source/openssl-1.0.1i.tar.gz && wget -ct 5 http://zlib.net/zlib-1.2.8.tar.gz && wget -ct 5 http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz && wget -ct 5 http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz && wget -ct 5 http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz && wget -ct 5 ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz && wget -ct 5 https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz && wget -ct 5 https://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2 && wget -ct 5 http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz && wget -ct 5 ftp://ftp.simplesystems.org/pub/png/src/libpng16/libpng-1.6.13.tar.gz && wget -ct 5 http://download.savannah.gnu.org/releases/freetype/freetype-2.5.3.tar.bz2 && wget -ct 5 http://www.ijg.org/files/jpegsrc.v9a.tar.gz && echo "下載完畢!
注意:使用-c斷點續傳,-t參數表示重試次數。
2、安裝MariaDB
安裝依賴庫、編譯工具
yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* zlib-devel gd-devel
安裝CMake:
tar -zxvf cmake-2.8.11.2.tar.gz && cd cmake-2.8.11.2 && ./configure && make && make install
添加mysql運行組及用戶、創建數據庫數據文件文件夾並授予合適權限:
Trip:/bin/false是最嚴格的禁止login選項,一切服務都不能用,而/sbin/nologin只是不允許系統login,可以使用其他ftp等服務。如果想要用false在禁止login的同時允許ftp,則必須在/etc/shells里增加一行/bin/false。為了系統安全,這里建議使用/bin/false。
groupadd mysql && useradd -g mysql mysql -s /bin/false && mkdir -p /data/mysql && chown -R mysql:mysql /data/mysql
編譯、安裝MariaDB:
mkdir -p /usr/local/mysql && cd /usr/local/src && tar -zxvf mariadb-5.5.39.tar.gz && cd mariadb-5.5.39 && cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc && make && make install
由於系統自帶了MariaDB配置文件,備份或刪除它都可以:
mv /etc/my.cnf /etc/my.cnf.bak
初始化數據庫,配置啟動腳本:
cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql && ln -s /usr/local/mysql/my.cnf /etc/my.cnf && cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld && chmod 755 /etc/init.d/mysqld && chkconfig mysqld on
配置MariaDB啟動腳本:
#vim /etc/rc.d/init.d/mysqld 改動如下: basedir=/usr/local/mysql #MySQL程序安裝路徑 datadir=/data/mysql #MySQl數據庫存放目錄 ##重啟MySQL #service mysqld start
把MariaDB加入系統環境變量:
echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile && source /etc/profile
把MariaDB的庫文件鏈接到系統默認的位置,在編譯PHP等軟件時可以不用指定其庫文件地址:
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql && ln -s /usr/local/mysql/include/mysql /usr/include/mysql && mkdir /var/lib/mysql && ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
交互式設置數據庫:
[root@localhost mysql]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
3、安裝Nginx
創建nginx的用戶和用戶組:
groupadd www && useradd -g www www -s /bin/false
安裝PCRE:
cd /usr/local/src && mkdir /usr/local/pcre && tar -zxvf pcre-8.35.tar.gz && cd pcre-8.35 && ./configure --prefix=/usr/local/pcre && make && make install
安裝OpenSSL:
cd /usr/local/src && mkdir /usr/local/openssl && tar -zxvf openssl-1.0.1i.tar.gz && cd openssl-1.0.1i && ./config --prefix=/usr/local/openssl && make && make install && echo 'export PATH=$PATH:/usr/local/openssl/bin'>>/etc/profile && source /etc/profile
安裝zlib:
cd /usr/local/src && mkdir /usr/local/zlib && tar zxvf zlib-1.2.8.tar.gz && cd zlib-1.2.8 && ./configure --prefix=/usr/local/zlib && make && make install
安裝第三方模塊ngx_http_accesskey_module以實現Nginx防盜鏈
cd /usr/local/src && wget http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz && tar vxzf Nginx-accesskey-2.0.3.tar.gz && cd nginx-accesskey-2.0.3/
修改config文件,替換其中 的”$HTTP_ACCESSKEY_MODULE”為”ngx_http_accesskey_module”.
我們可以在官方文檔查看更多信息(http://nginx.org/en/docs/)。下面安裝Nginx:
cd /usr/local/src && tar -zxvf nginx-1.6.1.tar.gz && cd nginx-1.6.1 && ./configure --prefix=/usr/local/nginx \ --without-http_memcached_module \ --user=www \ --group=www \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-openssl=/usr/local/src/openssl-1.0.1i \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-pcre=/usr/local/src/pcre-8.35 \ --with-http_stub_status_module \ --with-http_sub_module \ --add-module=/usr/local/src/nginx-accesskey-2.0.3/ && make && make install && echo "安裝NGINX完畢!"
補充:–with-http_sub_module用於替換,非常有用(見Wiki)。–with-http_stub_status_module開啟Nginx狀態信息,非核心模塊,可不加入。
到此,Nginx軟件編譯並安裝完畢。
更多官方文檔:nginx documentation
修改Nginx配置文件,改為如下:
配置Nginx控制腳本或者從這里下載:
從本網站下載: cd /etc/rc.d/init.d/ && wget http://idoseek.com/dl/nx/nginx 或手動寫入: vim /etc/rc.d/init.d/nginx
寫入如下內容:

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { #configtest || return $? stop sleep 1 start } reload() { #configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
設置啟動腳本、啟動Nginx:
chmod 775 /etc/rc.d/init.d/nginx && chkconfig nginx on && /etc/rc.d/init.d/nginx start
把Nginx加入系統變量:
echo 'export PATH=$PATH:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile
打開地址,輸出如下內容說明安裝Nginx已經成功:
4、安裝PHP
4.1、編譯相關軟件安裝PHP
安裝yasm:
cd /usr/local/src && tar -zxvf yasm-1.2.0.tar.gz && cd yasm-1.2.0 && ./configure && make && make install
安裝mcrypt:
cd /usr/local/src && tar -zxvf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8 && ./configure && make && make install
安裝libvpx:
cd /usr/local/src && tar -jxvf libvpx-v1.3.0.tar.bz2 && cd libvpx-v1.3.0 && ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 && make && make install
安裝tiff:
cd /usr/local/src && tar -zxvf tiff-4.0.3.tar.gz && cd tiff-4.0.3 && ./configure --prefix=/usr/local/tiff --enable-shared && make && make install
安裝libpng:
cd /usr/local/src && tar -zxvf libpng-1.6.13.tar.gz && cd libpng-1.6.13 && ./configure --prefix=/usr/local/libpng --enable-shared --with-x=no && make && make install
安裝freetype:
cd /usr/local/src && tar -jxvf freetype-2.5.3.tar.bz2 && cd freetype-2.5.3 && ./configure --prefix=/usr/local/freetype --enable-shared --with-x=no && make && make install
安裝jpeg:
cd /usr/local/src && tar -zxvf jpegsrc.v9a.tar.gz && cd jpeg-9a && ./configure --prefix=/usr/local/jpeg --enable-shared && make && make install
安裝libgd:
cd /usr/local/src && tar -zxvf libgd-2.1.0.tar.gz && cd libgd-2.1.0 && ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx && make && make install
安裝t1lib:
cd /usr/local/src && tar -zxvf t1lib-5.1.2.tar.gz && cd t1lib-5.1.2 && ./configure --prefix=/usr/local/t1lib --enable-shared && make without_doc && make install
安裝php:
注意:如果系統是64位,安裝前請執行以下兩條命令:
ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so && \cp -frp /usr/lib64/libXpm.so* /usr/lib/
編譯並安裝PHP:
cd /usr/local/src && tar -jvxf php-5.5.16.tar.bz2 && cd php-5.5.16 && export LD_LIBRARY_PATH=/usr/local/libgd/lib && ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-mysql-sock=/tmp/mysql.sock \ --with-pdo-mysql=/usr/local/mysql \ --with-gd \ --with-png-dir=/usr/local/libpng \ --with-jpeg-dir=/usr/local/jpeg \ --with-freetype-dir=/usr/local/freetype \ --with-xpm-dir=/usr/ \ --with-vpx-dir=/usr/local/libvpx/ \ --with-zlib-dir=/usr/local/zlib \ --with-t1lib=/usr/local/t1lib \ --with-iconv \ --enable-libxml \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --enable-opcache \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --enable-ftp \ --enable-gd-native-ttf \ --with-openssl \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-mcrypt \ --with-curl \ --enable-exif \ --enable-ctype && make && make install
編譯配置說明:
–enable-opcache:PHP5.5內置了Zend Opcache,即Optimizer+,是 Zend 開發的閉源但可以免費使用的 PHP 優化加速組件。現在,Zend 科技公司將 Optimizer+ 在 PHP License 下開源成為 Zend Opcache。Zend OPcache 通過 opcode 緩存和優化提供更快的 PHP 執行過程。它將預編譯的腳本文件存儲在共享內存中供以后使用,從而避免了從磁盤讀取代碼並進行編譯的時間消耗。同時,它還應用了一些代碼優化模式,使得代碼執行更快。
更多編譯信息請查看
./configure --help
編譯、安裝完成后結尾輸出如下一段比較重要的信息:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/ Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/php/man/man1/ Installing PHP FPM binary: /usr/local/php/sbin/ Installing PHP FPM config: /usr/local/php/etc/ Installing PHP FPM man page: /usr/local/php/php/man/man8/ Installing PHP FPM status page: /usr/local/php/php/fpm/ Installing PHP CGI binary: /usr/local/php/bin/ Installing PHP CGI man page: /usr/local/php/php/man/man1/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/php/man/man1/ page: phpize.1 page: php-config.1 /usr/local/src/php-5.5.16/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/
安裝結束。
4.2、配置PHP運行環境
復制php配置文件到安裝目錄
cp php.ini-production /usr/local/php/etc/php.ini
備份系統自帶配置文件:
mv /etc/php.ini /etc/php.ini.bak
添加軟鏈接到 /etc目錄:
ln -s /usr/local/php/etc/php.ini /etc/php.ini
拷貝模板文件為php-fpm配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
添加軟連接到 /etc目錄:
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
以上幾步寫法合並如下:
cp php.ini-production /usr/local/php/etc/php.ini && mv /etc/php.ini /etc/php.ini.bak && ln -s /usr/local/php/etc/php.ini /etc/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
修改PHP配置文件:
# vim /usr/local/php/etc/php.ini ;找到"disable_functions =" (禁用掉某些比較“危險”函數,大概在305行),改為 disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname ;找到";date.timezone ="(大概在913行),修改為 date.timezone = Asia/Shanghai ;找到"expose_php = On"(禁止顯示php版本的信息,大概在366行),修改為 expose_php = Off ;找到"short_open_tag = Off"(支持php短標簽,大概在202行),修改為 short_open_tag = On ;找到";opcache.enable=0"(支持opcode緩存,大概在1838行),修改為 opcache.enable=1 ;找到";opcache.enable_cli=0"(支持opcode緩存,大概在1841行),修改為 opcache.enable_cli=0 ;並在下面加入'zend_extension = "opcache.so"'一行, 開啟opcode緩存功能 zend_extension = "opcache.so" opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1
4.3、配置php-fpm
#vim /usr/local/php/etc/php-fpm.conf 大概在分別25、147行,修改內容如下: ;取消pid前面的分號 pid = run/php-fpm.pid ;... ;設置php-fpm運行賬號為www user = www ;設置php-fpm運行組為www group = www
拷貝php-fpm腳本到啟動目錄,給予執行權限, 設置開機啟動:
cp /usr/local/src/php-5.5.16/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm && chmod +x /etc/rc.d/init.d/php-fpm && chkconfig php-fpm on
把php加入系統變量:
echo 'export PATH=$PATH:/usr/local/php/bin'>>/etc/profile && source /etc/profile
4.4、配置nginx支持php
修改/usr/local/nginx/conf/nginx.conf 配置文件,需做如下修改
user www www; index index.php index.html index.htm; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #取消FastCGI server部分location的注釋,注意fastcgi_param行的參數,改為$document_root$fastcgi_script_name,或者使用絕對路徑 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
5、測試
上述配置修改完畢,重啟nginx,並啟動php-fpm:
service nginx restart &&
service php-fpm start
創建phpinfo文件,設置適當的目錄權限。
cd /usr/local/nginx/html/ && echo '<?php phpinfo(); ?>'>index.php && chown -R www:www /usr/local/nginx/html/
瀏覽器查看,如果PHP文檔配置沒有錯誤,將輸出如下內容:
配置匯總
根目錄: /usr/local/nginx/html/
Nginx配置文件: /usr/local/nginx/conf/nginx.conf
PHP配置文件: /usr/local/php/etc/php.ini
PHP默認拓展文件夾:/usr/lib64/php/modules/
PHP拓展可放置於:/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212
MySQL配置文件: /etc/my.cnf
MySQL程序安裝路徑: basedir=/usr/local/mysql
MySQl數據庫存放目錄: datadir=/data/mysql
問題匯總
(1)NOTICE: PHP message: PHP Warning: Module ‘exif’ already loaded in Unknown on line 0
重復加載模塊了,可以使用以下命令查詢(參考):
#php -i | grep \.ini\$ Loaded Configuration File => /usr/local/etc/php.ini Additional .ini files parsed => /usr/local/etc/php/extensions.ini user_ini.filename => .user.ini => .user.ini
(2)[22-Oct-2014 09:07:25] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/imagick.so’ – /usr/lib64/php/modules/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
很明顯,沒有正確加載拓展模塊文件,請確認好php.ini的extensions_dir。
(3)PHP Warning: PHP Startup: mhash: Unable to initialize module
PHP版本升級后模塊沒有重新編譯,需要重新編譯。
(4)版本升級時注意哪些問題?
建議把php-fpm服務關掉,否則容易出現使用舊模塊,一些新模塊無法替換。建議備份后刪除舊模塊重新編譯。