編譯安裝LNMP架構

1、安裝Nginx:php

一、解決依賴關係html

編譯安裝nginx須要事先須要安裝開發包組"Development Tools"和 "Development Libraries"。同時,還須要專門安裝pcre-devel包:
# yum -y install pcre-develmysql

 

二、安裝nginx

首先添加非特權用戶nginx,實現以非特權用戶身份運行nginx服務進程:
# groupadd -r nginx
# useradd -r -g nginx nginxsql

接着開始編譯和安裝:
# ./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
# make && make installbootstrap

說明:若是想使用nginx的perl模塊,能夠經過爲configure腳本添加--with-http_perl_module選項來實現,但目前此模塊仍處於實驗性使用階段,可能會在運行中出現意外,所以,其實現方式這裏再也不介紹。若是想使用基於nginx的cgi功能,也能夠基於FCGI來實現,具體實現方法請參照網上的文檔。vim

 

三、爲nginx提供SysV init腳本:centos

新建文件/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:      /etc/sysconfig/nginx
# pidfile:     /var/run/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/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/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' -`
   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
esacapi

然後爲此腳本賦予執行權限:
# chmod +x /etc/rc.d/init.d/nginx瀏覽器

添加至服務管理列表,並讓其開機自動啓動:
# chkconfig --add nginx
# chkconfig nginx on

然後就能夠啓動服務並測試了:
# service nginx start


2、安裝mysql-5.6.10

一、準備數據存放的文件系統

新建一個邏輯卷,並將其掛載至特定目錄便可。這裏再也不給出過程。

這裏假設其邏輯卷的掛載目錄爲/mydata,然後須要建立/mydata/data目錄作爲mysql數據的存放目錄。

 

二、安裝cmake
在mysql-5.6之後安裝方式不在使用./configure的方式進行安裝,而是有cmake來安裝,所以,在安裝mysql以前,還須要安裝cmake程序。
下載cmake軟件
# wget http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz
# tar xf  cmake-2.8.11.2.tar.gz
# cd cmake-2.8.11.2
# ./bootstrap
# make && make install


三、新建用戶以安全方式運行進程:
# mkdir /mydata/data -pv
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data

 


四、安裝並初始化mysql-5.6.10

首先下載平臺對應的mysql版本至本地,這裏是64位平臺,所以,選擇的爲mysql-5.6.10.tar.gz。

# tar xf mysql-5.6.10.tar.gz
# cd mysql-5.6.10/
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/mydata/data/ -DSYSCONFDIR=/etc

使用cmake編譯安裝時,出現以下錯誤:
CMake Error at cmake/readline.cmake:83 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:126 (FIND_CURSES)
  cmake/readline.cmake:193 (MYSQL_USE_BUNDLED_LIBEDIT)
  CMakeLists.txt:325 (MYSQL_CHECK_READLINE)
 
出現錯誤緣由是沒有安裝ncurses-devel軟件包,解決辦法:
第一步:安裝ncurses-devel
# yum -y install ncurses-devel

第二步:刪除全部的CMakeCache.txt文件
# find / -name CMakeCache.txt
# rm -f /root/cmake-2.8.10/Tests/CMakeFiles/CheckFortran/CMakeCache.txt
# rm -f /root/cmake-2.8.10/Tests/ComplexOneConfig/Cache/CMakeCache.txt
# rm -f /root/cmake-2.8.10/Tests/Complex/Cache/CMakeCache.txt
# rm -f /root/cmake-2.8.10/CMakeCache.txt

而後再從新執行cmake命令
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/mydata/data/ -DSYSCONFDIR=/etc
# make  && make install

接着初始化mysql
# cd /usr/local/mysql
# chown -R mysql:mysql  .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root  .

 

五、爲mysql提供主配置文件:

# cd /usr/local/mysql
# cp my.cnf  /etc/my.cnf

並在配置文件中添加以下幾行信息:

thread_concurrency = 2

另外還須要添加以下行指定mysql數據文件的存放位置:
datadir=/mydata/data
socket=/tmp/mysql.sock
innodb_file_per_table  = 1
thread_concurrency = 2


六、爲mysql提供sysv服務腳本:

# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

添加至服務列表:
# chkconfig --add mysqld
# chkconfig mysqld on

然後就能夠啓動服務測試使用了。


爲了使用mysql的安裝符合系統使用規範,並將其開發組件導出給系統使用,這裏還須要進行以下步驟:

七、輸出mysql的man手冊至man命令的查找路徑:

編輯/etc/man.config,添加以下行便可:
MANPATH  /usr/local/mysql/man

 

八、輸出mysql的頭文件至系統頭文件路徑/usr/include:

這能夠經過簡單的建立連接實現:
# ln -sv /usr/local/mysql/include  /usr/include/mysql

 

九、輸出mysql的庫文件給系統庫查找路徑:

# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

然後讓系統從新載入系統庫:
# ldconfig

 

十、修改PATH環境變量,讓系統能夠直接使用mysql的相關命令。
建立/etc/profile.d/mysql.sh文件,並添加以下信息:
export PATH=$PATH:/usr/local/mysql/bin

.  /etc/profile.d/mysql.sh

 

 

3、編譯安裝php-5.6.7

一、解決依賴關係:

請配置好yum源(能夠是本地系統光盤)後執行以下命令:
# yum -y groupinstall "X Software Development"

若是想讓編譯的php支持mcrypt、mhash擴展和libevent,此處還須要下載以下軟件包。:
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm

最好使用升級的方式安裝上面的rpm包,命令格式以下:
# rpm -Uvh


另外,也能夠根據須要安裝libevent,系統通常會自帶libevent,但版本有些低。所以能夠升級安裝之,它包含以下兩個rpm包。
libevent-2.0.17-2.i386.rpm
libevent-devel-2.0.17-2.i386.rpm

# yum -y install libevent
# yum -y install libevent-devel

說明:libevent是一個異步事件通知庫文件,其API提供了在某文件描述上發生某事件時或其超時時執行回調函數的機制,它主要用來替換事件驅動的網絡服務器上的event loop機制。目前來講, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。

 

二、編譯安裝php-5.6.7
# tar xf php-5.6.7.tar.bz2
# cd  php-5.6.7
#  ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl


安裝過程當中出現以下錯誤:
configure: error: xml2-config not found. Please check your libxml2 installation.
# yum install libxml2
# yum install libxml2-devel

解決辦法:安裝libxml2,libxml2-devel

而後再從新編譯安裝php-5.6.7
#  ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl

 

說明:若是前面第1步解決依賴關係時安裝mcrypt相關的兩個rpm包,此./configure命令還能夠帶上--with-mcrypt選項以讓php支持mycrpt擴展。--with-snmp選項則用於實現php的SNMP擴展,但此功能要求提早安裝net-snmp相關軟件包。

# make
# make test
# make intall

爲php提供配置文件:
# cp php.ini-production /etc/php.ini

爲php-fpm提供Sysv init腳本,並將其添加至服務列表:
# cp 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提供配置文件:
# 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
配置fpm的相關選項爲你所須要的值,並啓用pid文件(以下最後一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid

 

接下來就能夠啓動php-fpm了:
# service php-fpm start

使用以下命令來驗正(若是此命令輸出有中幾個php-fpm進程就說明啓動成功了):
# ps aux | grep php-fpm

wKioL1Ubv8HSBUvwAAJW81BSlgw320.jpg

 

4、整合nginx和php5

一、編輯/etc/nginx/nginx.conf,啓用以下選項:
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

 

二、編輯/etc/nginx/fastcgi_params,將其內容更改成以下內容:
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

並在所支持的主頁面格式中添加php格式的主頁,相似以下:
location / {
            root   html;
            index  index.php index.html index.htm;
        }
       
然後從新載入nginx的配置文件:
# service nginx reload

 

三、在/usr/html新建index.php的測試頁面,測試php是否能正常工做:
# cat > /usr/html/index.php << EOF
<?php
phpinfo();
?>

接着就能夠經過瀏覽器訪問此測試頁面了。

 

 

5、安裝xcache,爲php加速:
xcache簡介
XCache 是一個開源的 opcode 緩存器/優化器, 這意味着他可以提升您服務器上的 PHP 性能. 他經過把編譯 PHP 後的數據緩衝到共享內存從而避免重複的編譯過程, 當下一次訪問相同的頁面時,可以直接使用緩衝區已編譯的代碼從而提升速度. 一般可以提升您的頁面生成速率 2 到5 倍, 下降服務器負載。

 

一、安裝
# 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/

 

二、編輯xcache.ini,將xcache整合到php中

首先將xcache提供的樣例配置xcache.ini導入到/etc/php.d/目錄下
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d

說明:xcache.ini文件在xcache的源碼目錄中。

接下來編輯/etc/php.d/xcache.ini,找到extension = xcache.so開頭的行,修改成以下行:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so

注意:若是xcache.ini文件中有多條extension指令行,要確保此新增的行排在第一位。

 

三、從新啓動php-fpm
# service php-fpm restart

 

四、測試xcache功能是否生效
在瀏覽器上輸入http://192.168.108.230/,測試結果以下:

wKioL1UbwCOzU__-AAERX1gdo3g286.jpg

說明xcache功能已經生效了。

相關文章
相關標籤/搜索