nginx博客:基於nginx構建LNMP服務


一、nginx簡介


    Nginx是俄羅斯人編寫的十分輕量級的HTTP服務器,Nginx,它的發音爲“engine X”, 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器

專爲性能優化而開發,性能是其最重要的考量,實現上非常注重效率。

目前nginx在大陸主要有兩個分支:Tengine(淘寶)、Registry


    nginx工作時,先啓動一個master進程,然後由master進程生成一個或多個worker進程(自己設置),每個worker線程響應n個用戶請求。

   主進程主要完成如下工作:

      1. 讀取並驗正配置信息;

      2. 創建、綁定及關閉套接字;

      3. 啓動、終止及維護worker進程的個數;

      4. 無須中止服務而重新配置工作特性;

      5. 控制非中斷式程序升級,啓用新的二進制程序並在需要時回滾至老版本;

      6. 重新打開日誌文件,實現日誌滾動;

      7. 編譯嵌入式perl腳本;

    worker進程主要完成的任務包括:

      1. 接收、傳入並處理來自客戶端的連接;

      2. 提供反向代理及過濾功能;

      3. nginx任何能完成的其它任務;


    事件處理機制採用的是事件驅動機制:由於支持linux的epoll(邊緣觸發)、BSD的kqueue, solaris的/dev/poll三種事件觸發機制,複用器採用select、poll、 rt signal,支持sendfile, sendfile64,採用非阻塞異步IO,支持mmap,因此,nginx在處理高併發的靜態頁面時具有極高的處理性能,經過優化配置後能經受高負載的考驗,有報告表明能支持高達50,000個併發連接數,實際生產環境中支持30000個是不成問題的。保持10,000個沒有活動的連接,它只佔2.5M內存

    此外,nginx支持熱部署,在不停機的情況下而更新配置文件、日誌文件滾動、升級程序版本,從而爲實現7*24服務提供可能。


    nginx是個高度模塊化設計,編寫模塊相對簡單,模塊組成有下面幾類:

      ① 核心模塊

      ② Standard HTTP modules

      ③ Optional HTTP modules

      ④ Mail modules

      ⑤ 3rd party modules

    nginx自帶前4種,第五種模塊需要我們手動編譯使用。


    nginx現在最高的穩定版本是nginx-1.8.0,經典版最高的是nginx-1.6.3,可到nginx官網 nginx.org下載編譯安裝使用。


二、nginx的安裝使用


    本次試驗是目標是構建lnmp,並提供wordpress服務。

    lnmp構建順序爲:   編譯並配置好nginx --> 安裝並配置mariadb --> 編譯安裝php --> 整合nginx和php --> 安裝xcache加速器提升動態頁面處理性能 --> 安裝提供 wordpress 服務 。


(一)編譯並配置好nginx

     # uname -r
     2.6.32-504.el6.x86_64

     系統是64位的,以下所有程序包都需要下載64位的。

     主程序包:nginx-1.6.2.tar.gz


1、依賴關係

     編譯安裝事先都需要安裝開發包組"Development Tools"和 "Server Platform Development",同時,nginx的編譯安裝還需要安裝pcre-devel開發包

# yum groupinstall -y "Development Tools"  "Server Platform Development"
# yum -y install pcre-devel
pcre-devel.x86_64 0:7.8-6.el6


pcre-devel簡介:

    PCRE(Perl Compatible Regular Expressions,perl語言兼容正則表達式)是一個用C語言編寫的正則表達式函數庫,由菲利普.海澤(Philip Hazel)編寫。PCRE是一個輕量級的函數庫,比Boost之類的正則表達式庫小得多。PCRE十分易用,同時功能也很強大,性能超過了POSIX正則表達式庫和一些經典的正則表達式庫 。

    和另一個經典的Boost正則表達式庫的比較顯示,雙方的性能相差無幾,PCRE在匹配簡單字符串時更快,Boost則在匹配較長字符串時勝出。但兩者差距很小,考慮到PCRE的大小和易用性,我們可以認爲PCRE更值得考慮。

    PCRE被廣泛使用在許多開源軟件之中,最著名的莫過於Apache HTTP服務器和PHP腳本語言、R腳本語言,此外,正如從其名字所能看到的,同時也是perl語言的缺省正則庫。



2、編譯安裝nginx

     先添加用戶nginx,實現以之運行nginx服務進程

# id nginx
id: nginx: No such user
# groupadd -r nginx
# useradd -r -g nginx nginx


     從官網或安全途徑下載nginx主程序包,然後編譯安裝:

# tar xf nginx-1.6.2.tar.gz 
# cd nginx-1.6.2
# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --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 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_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/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi  --sbin-path=/usr/local/nginx/sbin/nginx  --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre


     正常編譯末尾出現的是:

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/tmp/nginx/client"
  nginx http proxy temporary files: "/var/tmp/nginx/proxy"
  nginx http fastcgi temporary files: "/var/tmp/nginx/fastcgi"
  nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
  nginx http scgi temporary files: "/var/tmp/nginx/scgi"
# make && make install

    爲上面編譯配置提供文件路徑(否則第一次啓動服務時會提示 “chown: missing operand after `/var/tmp/nginx/scgi'”等信息):

# mkdir -pv /var/tmp/nginx/{client,proxy,uwsgi}

wKiom1Vqt6SwbkY6AAQ5vrun3KQ891.jpg


3、提供SysV init服務腳本:


    添加如下內容:

# 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:      /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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
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/rc.d/init.d/nginx

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

# chkconfig --add nginx
# chkconfig nginx on 
# chkconfig  | grep nginx
nginx          0:off1:off2:on3:on4:on5:on6:off


    提供執行程序環境路徑:

# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
# . /etc/profile.d/nginx.sh


     啓動服務驗證:

# service nginx start
Starting nginx:                                            [  OK  ]
# ss -tnlp | grep nginx
LISTEN     0      128                       *:80                       *:*      users:(("nginx",35686,6),("nginx",35688,6))

     瀏覽器查看,nginx正常啓動:

wKiom1Vqt_Lj9XpZAAGQtbeMM7E396.jpg


(二)編譯安裝mariadb


     主程序包:mariadb-5.5.43-linux-x86_64.tar.gz


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


     新建一個邏輯卷(系統類型需要調整爲8e),並將其掛載至特定目錄即可。這裏不再給出過程。


     這裏假設其邏輯卷的掛載目錄爲/mydata,而後需要創建/mydata/data目錄做爲mysql數據的存放目錄。

# fdisk  /dev/sda
# partx -a /dev/sda
# partx -a /dev/sda
# mke2fs -t ext4 /dev/sda5
# mkdir /mydata
# mount /dev/sda5 /mydata
# mount
……
/dev/sda5 on /mydata type ext4 (rw)
# mkdir /mydata/data


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.43

# tar xf mariadb-5.5.43-linux-x86_64.tar.gz   -C /usr/local
# ln -sv /usr/local/mariadb-5.5.43-linux-x86_64/ /usr/local/mysql
`/usr/local/mysql' -> `/usr/local/mariadb-5.5.43-linux-x86_64/'
# cd /usr/local/mysql
# chown -R root:mysql .
# chown -R mysql:mysql /mydata/data
# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root .


wKiom1VquBeSJ_yKAAK1PPmex4g030.jpg

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

    修改或添加如下幾項:

thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = on
skip_name_resolve = on


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


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

MANPATH  /usr/local/mysql/man


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

# ln -sv /usr/local/mysql/include  /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'


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

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


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

# ldconfig


7、修改PATH環境變量,讓系統可以直接使用mysql的相關命令。

# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# . /etc/profile.d/mysql.sh


8、啓動服務測試使用

# service mysqld start
Starting MySQL...                                          [  OK  ]
[root@aunt-s mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.43-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>



但出現瞭如下問題:

[root@aunt-s ~]# /usr/local/mysql/bin/mysqld 
150530  9:40:29 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
150530  9:40:29 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.5.43-MariaDB-log) starting as process 2610 ...
150530  9:40:29 [ERROR] Fatal error: Please consult the Knowledge Base to find out how to run mysqld as root!
150530  9:40:29 [ERROR] Aborting
150530  9:40:29 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
[root@aunt-s ~]# vim /etc/my.cnf 
[root@aunt-s ~]# ll  /etc/rc.d/init.d/mysqld
ls: cannot access /etc/rc.d/init.d/mysqld: No such file or directory
[root@aunt-s ~]# ll  /etc/rc.d/init.d/mysql
ls: cannot access /etc/rc.d/init.d/mysql: No such file or directory
[root@aunt-s ~]# cp /usr/local/mysql/support-files/mysql
mysqld_multi.server  mysql-log-rotate     mysql.server         
[root@aunt-s ~]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@aunt-s ~]# vim /etc/rc.d/init.d/mysqld
[root@aunt-s ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@aunt-s ~]# ll  /etc/rc.d/init.d/mysql
ls: cannot access /etc/rc.d/init.d/mysql: No such file or directory
[root@aunt-s ~]# ll  /etc/rc.d/init.d/mysqld
-rwxr-xr-x 1 root root 12196 May 30 09:46 /etc/rc.d/init.d/mysqld
[root@aunt-s ~]# service mysqld start
Starting MySQL..                                           [  OK  ]
[root@aunt-s ~]#


    提供服務腳本就好了!!其他什麼都沒有修改!!!!


    但是過了半天時間重新接着做時,重新服務,又是failed,無法,用“ rm -rf ”刪除了/usr/local下的所有mariadb文件,從ftp上重新下載後在解壓初始化,然後好了。

推測,應該是以前安裝過mysql或者mariadb,文件沒有刪除乾淨導致。



(三)編譯安裝php


1、解決依賴關係


# yum install -y libxml2-devel
Installed:
  libxml2-devel.x86_64 0:2.7.6-14.el6_5.2                                                                                
Complete!

# yum install -y bzip2-devel
Installed:
  bzip2-devel.x86_64 0:1.0.5-7.el6_0                                                                                     
Complete!


# yum install -y libcurl-devel
Installed:
  libcurl-devel.x86_64 0:7.19.7-37.el6_5.3                                                                               
Dependency Installed:
  libidn-devel.x86_64 0:1.18-2.el6                                                                                       
Complete!

# yum install -y libmcrypt libmcrypt-devel
Installed:
  libmcrypt.x86_64 0:2.5.8-9.el6                           libmcrypt-devel.x86_64 0:2.5.8-9.el6                          
Complete!

# yum install -y net-snmp-devel
Installed:
  net-snmp-devel.x86_64 1:5.5-49.el6_5.3                                                                                 
Dependency Installed:
  elfutils-devel.x86_64 0:0.158-3.2.el6                    elfutils-libelf-devel.x86_64 0:0.158-3.2.el6                  
  file-devel.x86_64 0:5.04-21.el6                          lm_sensors-devel.x86_64 0:3.1.1-17.el6                        
  lm_sensors-libs.x86_64 0:3.1.1-17.el6                    net-snmp-libs.x86_64 1:5.5-49.el6_5.3                         
  popt-devel.x86_64 0:1.13-7.el6                           rpm-devel.x86_64 0:4.8.0-37.el6                               
  tcp_wrappers-devel.x86_64 0:7.6-57.el6                  
Complete!


    這些依賴關係都是在./configure 過程中一次次試出來的,只要根據提示一步一步完善安裝相應的開發包就可以了。


2、編譯安裝php-5.4.4

#  ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --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-bz2 --with-curl --with-snmp


    說明:如果php和mariadb不再統一主機上,則還需要添加“--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd”。

# make &&  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


wKiom1VquFTyuSDaAARbqcKkAlc224.jpg

    爲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
root      25441  0.0  0.6 190812  6088 ?        Ss   23:13   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)                                                                    
nobody    25442  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25443  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25444  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25445  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25446  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25447  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25448  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
nobody    25449  0.0  0.5 190812  5184 ?        S    23:13   0:00 php-fpm: pool www                                                                                                            
root      25463  0.0  0.0 103252   836 pts/1    S+   23:13   0:00 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;
        }


    如果有後面能正常打開html頁面時,而打開php頁面有問題時,tail日誌提示找不到路徑,則可以將倒數第二行改爲“fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;”。


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;



    最終/etc/nginx/nginx.conf的配置爲:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.cat.com;
        access_log  /var/log/nginx/cat.access.log  main;
error_log  /var/log/nginx/cat.error.log  notice;
        location / {
             root   html;
            index  index.php index.html index.htm;     #主頁面格式中添加php格式的主頁
        }
        location ~ \.php$ {
            root           /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
        }
    }
}




     而後重新載入nginx的配置文件:

     # service nginx reload


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

# mkdir /var/www/html -p
# cat > /var/www/html/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF
# cat /var/www/html/index.php 
<?php
phpinfo();
?>
# nginx -s reload



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

    測試連接htnl和php網頁是否正常


    先在主機上使用curl命令獲取網頁:

[root@aunt-s ~]# curl http://172.16.20.150/index.html

<h1>www.cat.com</h1>


wKioL1VquiuCSGsrAAFVVOylnyg275.jpg


    查看錯誤日誌:

# tail /var/log/nginx/cat.error.log 
……
2015/05/31 10:54:28 [error] 26185#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.16.250.248, server: www.cat.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "172.16.20.150"
2015/05/31 10:54:28 [error] 26185#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.16.250.248, server: www.cat.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "172.16.20.150"

     從錯誤日誌的說明及html能正常打開的情況來看,應該是識別php網頁中的腳本到php轉換是出了錯。


     所以先檢查兩個地址:

       ① 檢查/etc/nginx/fastcgi_params

             內容爲上面所寫,檢查結果沒問題

       ② 檢查/etc/nginx/nginx.conf

             根據參考網頁的內容做修改,還是無效的。

             參考網頁 :http://lovelace.blog.51cto.com/1028430/1314565

     重新檢查每一項的設置,發現是在定義網頁的根目錄對應本地文件系統目錄時除了錯,將root html 改爲 root /var/www/htnl就好了:

location / {
             root   html;
            index  index.php index.html index.htm;  
        }

             


4、 測試php連接mariadb是否正常:


    編輯  /var/www/html/index.php ,修改爲下面的內容:

# vim /var/www/html/index.php
<?php
      $link = mysql_connect('127.0.0.1','root','');
      if ($link)
        echo "Success...";
      else
        echo "Failure...";
      mysql_close();
?>

    重新刷新網頁:


wKioL1VqulawWtvlAACxAt7xJt8791.jpg


    注:一定要注意是否啓動了mariadb服務。


(五)安裝xcache,爲php加速


    程序包:xcache-3.2.0.tar.bz2


1、安裝

     xcache應該以php插件的形式運行,需要運行php中的phpize檢查環境並生成xcache的configure配置工具

# tar xf xcache-3.2.0.tar.bz2 
# 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


wKiom1VquRbT345WAARCEQrtxQs236.jpg


     結尾出現如下行,第2步需要用到 :

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


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


     首先將xcache的源碼目錄中提供的樣例配置導入php.ini,並在該文件中添加如下行:

# mkdir /etc/php.d
# cp /root/xcache-3.2.0/xcache.ini /etc/php.d/
# vim /etc/php.d/xcache.ini
extension =  /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so

   

    注:xcache3 已經不支持zend擴展了,直接extension擴展即可

3、測試效果:


    啓動xcache前可以用ab測試一下頁面:

    (測試時把php主頁設置成phpinfo()效果更明顯)

# ab -n 10000 -c 300 http://172.16.20.150/index.php
……
Time taken for tests:   14.202 seconds
Complete requests:      10000
Failed requests:        983
   (Connect: 0, Receive: 0, Length: 983, Exceptions: 0)
Write errors:           0
Total transferred:      683568917 bytes
HTML transferred:       681958917 bytes
Requests per second:    704.11 [#/sec] (mean)
Time per request:       426.069 [ms] (mean)
Time per request:       1.420 [ms] (mean, across all concurrent requests)
Transfer rate:          47002.80 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  196 515.0      0    7001
Processing:     9  210 141.1    185    3291
Waiting:        7  210 141.0    185    3291
Total:         31  406 536.2    189    7202
Percentage of the requests served within a certain time (ms)
  50%    189
  66%    199
  75%    207
  80%    223
  90%   1186
  95%   1203
  98%   1424
  99%   3180
 100%   7202 (longest request)


     啓動xcache後測試效果:


wKiom1VquT7xedbLAAFJfARh32c321.jpg

# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
# ab -n 10000 -c 300 http://172.16.20.150/index.php
……
Concurrency Level:      300
Time taken for tests:   15.397 seconds
Complete requests:      10000
Failed requests:        978
   (Connect: 0, Receive: 0, Length: 978, Exceptions: 0)
Write errors:           0
Total transferred:      721118913 bytes
HTML transferred:       719508913 bytes
Requests per second:    649.49 [#/sec] (mean)
Time per request:       461.899 [ms] (mean)
Time per request:       1.540 [ms] (mean, across all concurrent requests)
Transfer rate:          45738.42 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  190 540.0      0    7021
Processing:    10  244 311.8    197    8314
Waiting:        8  244 311.7    197    8314
Total:         34  434 663.8    200    9321
Percentage of the requests served within a certain time (ms)
  50%    200
  66%    211
  75%    228
  80%    276
  90%   1197
  95%   1217
  98%   3087
  99%   3212
 100%   9321 (longest request)

     從結果看出,速度沒有怎麼提升,反而有所下降,但xcache確實加載成功。這可能是我現行環境中,php頁面的編譯步驟不是瓶頸,而是磁盤IO或者數據庫查找讀取是瓶頸。如果是php編譯內容所佔時間是瓶頸,那麼安裝xcache後可以看到明顯提速效果。


(六)安裝使用wordpress


     程序包wordpress-3.3.1-zh_CN.zip


1、解壓縮程序包到網站根目錄下

# unzip -d /var/www/html/ wordpress-3.3.1-zh_CN.zip
# cd /var/www/html/wordpress/
# ls
index.php        wp-admin              wp-config-sample.php  wp-links-opml.php  wp-pass.php      wp-trackback.php
license.txt      wp-app.php            wp-content            wp-load.php        wp-register.php  xmlrpc.php
readme.html      wp-blog-header.php    wp-cron.php           wp-login.php       wp-settings.php
wp-activate.php  wp-comments-post.php  wp-includes           wp-mail.php        wp-signup.php

2、提供wordpress的配置文件

# cp wp-config-sample.php wp-config.php 
# vim wp-config.php
   修改下面幾項:
define('DB_NAME', 'wpdb');
/** MySQL 數據庫用戶名 */
define('DB_USER', 'wp');
/** MySQL 數據庫密碼 */
define('DB_PASSWORD', '123');
/** MySQL 主機 */
define('DB_HOST', 'localhost')


3、在本主機上給用戶wp授權使用數據庫的權限:

# mysql
……
MariaDB [(none)]> CREATE DATABASE wpdb;
Query OK, 1 row affected (0.09 sec)
MariaDB [(none)]> GRANT ALL ON wpdb.* TO wp@localhost IDENTIFIED BY '123';
Query OK, 0 rows affected (0.15 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> \q
Bye
[root@aunt-s wordpress]#


4、在瀏覽器上登錄後臺進行網站建設


wKioL1VquvaTFS_2AAHKidvDHok790.jpg

wKiom1VquWnyfBF9AAFC3jU2WPI893.jpg




























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章