LNMP搭建,nginx整合php-fpm即fastCGI實現,以及nginx的多虛擬機器主機配置

來源:互聯網
上載者:User

PHP程式員一枚,此次環境也算是個人的一個筆記吧,貼出來了


環境:

      CentOS 5.5

鏡像地址就不貼了

php-5.4.20

地址:http://museum.php.net/php5/php-5.4.20.tar.gz

mysql-5..5.48(通用二進位格式) 

地址:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.48-linux2.6-i686.tar.gz

nginx-1.8.1

地址:http://nginx.org/download/nginx-1.8.1.tar.gz


一、編譯安裝Nginx:


1、解決依賴關係


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


2、安裝


首先添加使用者nginx,實現以之運行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選項來實現,但目前此模組仍處於實驗性使用階段,可能會在運行中出現意外,因此,其實現方式這裡不再介紹。如果想使用基於nginx的cgi功能,也可以基於FCGI來實現,具體實現方法請參照網上的文檔。


3、為nginx提供SysV init指令碼:


建立檔案/etc/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
esac


而後為此指令碼賦予執行許可權:


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


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


而後就可以啟動服務並測試了:

# service nginx start


如果看到nginx的歡迎頁面說明安裝成功。


二、通用二進位格式安裝mysql


注意:使用通用二進位格式安裝的mysql,其安裝目錄必須在/usr/local/下,並且目錄名必須為mysql


tar xf mysql-5.5.48-linux2.6-i686.tar.gz  -C /usr/local/ 解壓到/usr/local下

cd /usr/local/

ln -sv mysql-5.5.48 mysql [連結檔案,不用改變原名,而且遵循了mysql官方規定]

如果沒有mysql使用者和mysql組,需要建立:

groupadd -r -g 306 mysql

useradd -r -g 306 mysql

改變程式的屬主屬組:

chown -R mysql.mysql /usr/local/mysql/*

[注意]

如果mysql的預設資料目錄依然是程式目錄下的data,則必須為mysql使用者和mysql組
所以還需要改回來
其它的屬主操作完成之後則需要改成root,此方法為安全做法,防止有人惡意攻破mysql進程,而擷取惡意操作許可權

drwxr-xr-x  2 root  mysql   4096 Mar 13 07:01 bin
-rw-r--r--  1 root  mysql  17987 Mar 13 07:01 COPYING
drwxr-xr-x  5 mysql mysql   4096 Mar 13 07:20 data
drwxr-xr-x  2 root  mysql   4096 Mar 13 07:01 docs
drwxr-xr-x  3 root  mysql   4096 Mar 13 07:01 include
-rw-r--r--  1 root  mysql 151708 Mar 13 07:01 INSTALL-BINARY
drwxr-xr-x  3 root  mysql   4096 Mar 13 07:01 lib
drwxr-xr-x  4 root  mysql   4096 Mar 13 07:01 man
drwxr-xr-x 10 root  mysql   4096 Mar 13 07:02 mysql-test
-rw-r--r--  1 root  mysql   2496 Mar 13 07:00 README
drwxr-xr-x  2 root  mysql   4096 Mar 13 07:01 scripts
drwxr-xr-x 27 root  mysql   4096 Mar 13 07:01 share
drwxr-xr-x  4 root  mysql   4096 Mar 13 07:01 sql-bench
drwxr-xr-x  2 root  mysql   4096 Mar 13 07:00 support-files 

切換到程式目錄

cd /usr/local/mysql


scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql

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

# 加入sysV


chkconfig --add mysqld


# 開機啟動


[root@localhost mysql]# chkconfig mysqld on

# 拷貝設定檔


[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf


[注意]:如果資料目錄指定的是其它的目錄,則需要在/etc/my.cnf加上配置項


datadir=/mydata/data(例如) 不然mysql啟動不了,樓主用的是預設的程式目錄下的data,即不用修改

6、啟動服務


[root@localhost etc]# service mysqld start

7、添加本地環境變數

vim /etc/profile.d/mysqld

內容:export PATH=$PATH:/usr/local/mysql/bin


為了使用mysql的安裝符合系統使用規範,並將其開發組件匯出給系統使用,這裡還需要進行如下步驟:


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


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


7、輸出mysql的標頭檔至系統標頭檔路徑/usr/include:

這可以通過簡單的建立連結實現:
# ln -sv /usr/local/mysql/include  /usr/include/mysql

8、輸出mysql的庫檔案給系統庫尋找路徑:

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

而後讓系統重新載入系統庫:
# ldconfig

重新登入下:


運行成功。


二、編譯安裝PHP5.4


1、解決依賴關係:

請配置好yum源(可以是本地系統光碟片)後執行如下命令:


# yum -y groupinstall "X Software Development" 

如果想讓編譯的php支援mcrypt、mhash擴充和libevent,此處還需要如下幾個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 

另外,也可以根據需要安裝libevent,系統一般會內建libevent,但版本有些低。因此可以升級安裝之,它包含如下兩個rpm包。
libevent-2.0.17-2.i386.rpm
libevent-devel-2.0.17-2.i386.rpm

說明:libevent是一個非同步事件通知庫檔案,其API提供了在某檔案描述上發生某事件時或其逾時時執行回呼函數的機制,它主要用來替換事件驅動的網路伺服器上的event loop機制。目前來說, libevent支援/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。


2、編譯安裝php-5.4

# tar xf php-5.4.20.tar.gz
# cd php-5.4.20
#  ./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/init.d/php-fpm
# chmod +x /etc/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

3.整合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是否能正常工作:
# vim /usr/local/html/index.php
<?php
phpinfo();
?>
接著就可以通過瀏覽器訪問此測試頁面了。


四、配置nginx的虛擬機器主機,並且都支援PHP


①備份下主設定檔 

cp /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak

cd /etc/nginx

②建立額外的設定檔目錄

mkdir vohost

③修改配置項

vim nginx.conf


添加一行,讓其去讀取預設的設定檔

例如在vhost/下定義一個www_test_com.conf,作為新的基於網域名稱的www.test.com的虛擬機器主機

vim vhost/www_test_com.conf

內容如下:

server{
        listen  80;
        server_name     www.test.com;
        charset utf-8;
        index index.php index.html index.htm;
        autoindex       on;
        location ~ \.php$ {
                root /var/site/www.test.com;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
                include fastcgi_params;
        }

server_name:網站名稱

root : 網站根目錄


如果是將lnmp安裝到的虛擬機器中,別忘了ifconfig一下,然後將該ip和網域名稱(www.test.com)添加到主機的hosts檔案中,同時要檢查linux的防火牆是否是處於關閉狀態,OK。。。


第一次發,格式很亂。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.