Nginx+php+mysql+xcache安裝配置

2019年1月10日14:02:34 發(fā)表評論 3,485 ℃

一、安裝Nginx:

1、解決依賴關系

編譯安裝nginx需要事先需要安裝開發(fā)包組"Development Tools"和 "Development Libraries"。同時,還需要專門安裝pcre-devel包:

# yum -y install pcre-devel

2、安裝

首先添加用戶nginx,實現(xiàn)以之運行nginx服務進程:

# groupadd -r nginx

# useradd -r -g nginx nginx

接著開始編譯和安裝:

# ./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 install

說明:如果想使用nginx的perl模塊,可以通過為configure腳本添加--with-http_perl_module選項來實現(xiàn),但目前此模塊仍處于實驗性使用階段,可能會在運行中出現(xiàn)意外,因此,其實現(xiàn)方式這里不再介紹。如果想使用基于nginx的cgi功能,也可以基于FCGI來實現(xiàn),具體實現(xiàn)方法請參照網(wǎng)上的文檔。

3、為nginx提供SysV init腳本:

新建文件/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' -`

  opti -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

而后為此腳本賦予執(zhí)行權限:

# chmod +x /etc/rc.d/init.d/nginx

添加至服務管理列表,并讓其開機自動啟動:

# chkconfig --add nginx

# chkconfig nginx on

而后就可以啟動服務并測試了:

# service nginx start

二、安裝mysql-5.5.28

1、準備數(shù)據(jù)存放的文件系統(tǒng)

新建一個邏輯卷,并將其掛載至特定目錄即可。這里不再給出過程。

這里假設其邏輯卷的掛載目錄為/mydata,而后需要創(chuàng)建/mydata/data目錄做為mysql數(shù)據(jù)的存放目錄。

2、新建用戶以安全方式運行進程:

# groupadd -r mysql

# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

# chown -R mysql:mysql /mydata/data

3、安裝并初始化mysql-5.5.28

首先下載平臺對應的mysql版本至本地,這里是32位平臺,因此,選擇的為mysql-5.5.28-linux2.6-i686.tar.gz,其下載位置為ftp://172.16.0.1/pub/Sources/mysql-5.5。

# tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local

# cd /usr/local/

# ln -sv mysql-5.5.24-linux2.6-i686 mysql

# cd mysql 

# chown -R mysql:mysql .

# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

# chown -R root .

4、為mysql提供主配置文件:

# cd /usr/local/mysql

# cp support-files/my-large.cnf /etc/my.cnf

并修改此文件中thread_concurrency的值為你的CPU個數(shù)乘以2,比如這里使用如下行:

thread_c 2

另外還需要添加如下行指定mysql數(shù)據(jù)文件的存放位置:

datadir = /mydata/data

5、為mysql提供sysv服務腳本:

# cd /usr/local/mysql

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

添加至服務列表:

# chkconfig --add mysqld

# chkconfig mysqld on

而后就可以啟動服務測試使用了。

為了使用mysql的安裝符合系統(tǒng)使用規(guī)范,并將其開發(fā)組件導出給系統(tǒng)使用,這里還需要進行如下步驟:

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

編輯/etc/man.config,添加如下行即可:

MANPATH /usr/local/mysql/man

7、輸出mysql的頭文件至系統(tǒng)頭文件路徑/usr/include:

這可以通過簡單的創(chuàng)建鏈接實現(xiàn):

# ln -sv /usr/local/mysql/include /usr/include/mysql

8、輸出mysql的庫文件給系統(tǒng)庫查找路徑:

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

而后讓系統(tǒng)重新載入系統(tǒng)庫:

# ldconfig

9、修改PATH環(huán)境變量,讓系統(tǒng)可以直接使用mysql的相關命令。具體實現(xiàn)過程這里不再給出。

三、編譯安裝php-5.4.4

1、解決依賴關系:

請配置好yum源(可以是本地系統(tǒng)光盤)后執(zhí)行如下命令:

# yum -y groupinstall "X Software Development" 

如果想讓編譯的php支持mcrypt、mhash擴展和libevent,此處還需要下載ftp://172.16.0.1/pub/Sources/ngnix目錄中的如下幾個rpm包并安裝之:

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 

另外,也可以根據(jù)需要安裝libevent,系統(tǒng)一般會自帶libevent,但版本有些低。因此可以升級安裝之,它包含如下兩個rpm包。

libevent-2.0.17-2.i386.rpm

libevent-devel-2.0.17-2.i386.rpm

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

2、編譯安裝php-5.4.4

首先下載源碼包至本地目錄,下載位置ftp://172.16.0.1/pub/Sources/new_lamp。

# tar xf php-5.4.4.tar.bz2

# cd php-5.4.4

# ./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選項則用于實現(xiàn)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

四、整合nginx和php5

1、編輯/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;

    }

2、編輯/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

3、在/usr/html新建index.php的測試頁面,測試php是否能正常工作:

# cat > /usr/html/index.php << EOF

<?php

phpinfo();

?>

接著就可以通過瀏覽器訪問此測試頁面了。

五、安裝xcache,為php加速:

1、安裝

# tar xf xcache-2.0.0.tar.gz

# cd xcache-2.0.0

# /usr/local/php/bin/phpize

# ./configure --enable-xcache --with-php-c># make && make install

安裝結束時,會出現(xiàn)類似如下行:

Installing shared extensions:  /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2、編輯php.ini,整合php和xcache:

首先將xcache提供的樣例配置導入php.ini

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

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

接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改為如下行:

zend_extensi /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。

3、重新啟動php-fpm

# service php-fpm restart

六、補充說明

如果要在SSL中使用php,需要在php的location中添加此選項:

fastcgi_param HTTPS on;

補充閱讀材料:

Events is one of paradigms to achieve asynchronous execution. But not all asynchronous systems use events. That is about semantic meaning of these two - one is super-entity of another.

epoll and aio use different metaphors:

epoll is a blocking operation (epoll_wait()) - you block the thread until some event happens and then you dispatch the event to different procedures/functions/branches in your code.

In AIO you pass address of you callback function (completion routine) to the system and the system calls your function when something happens.

Problem with AIO is that your callback function code runs from system thread and so on top of system stack. A few problems with that as you can imagine.

【騰訊云】云服務器、云數(shù)據(jù)庫、COS、CDN、短信等云產品特惠熱賣中

發(fā)表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: