Linux課程筆記 Day09 課上內容總結 MySql,Php的安裝及Apache,Nginx,Php的優化

一 MySqljavascript

1.1    如何選擇MySql的版本php

 

 

1.2   MySql單實例安裝css

(1)       創建mysql用戶html

首先以root身份登錄到linux系統,而後執行以下命令建立mysql用戶及用戶組前端

[root@test3 ~]# groupadd mysqljava

[root@test3 ~]# useradd -s /sbin/nologin -g mysql -M mysqlmysql

[root@test3 ~]# tail -1 /etc/passwdlinux

mysql:x:1210:1210::/home/mysql:/sbin/nologinios

[root@test3 ~]# mkdir tools  #創建專門放置安裝包的目錄,養成規範的習慣nginx

[root@test3 ~]# cd tools/

[root@test3 tools]# wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.1/mysql-5.1.62.tar.gz

 

(2)       解壓安裝

[root@test3 mysql-5.1.62]#./configure  \

--prefix=/usr/local/mysql \

--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock \  #指定MySQL socket文件存放目錄

--localstatedir=/usr/local/mysql/data \  #設定mysql數據文件存放路徑

--enable-assembler \   #容許使用匯編模式(優化性能)

--with-mysqld-ldflags=-all-static \  #服務器使用靜態庫(優化性能)

--with-client-ldflags=-all-static  \  #客戶端使用靜態庫(優化性能)

--enable-thread-safe-client  \    #以線程方式編譯客戶端

--with-mysqld-user=mysql \      #指定mysql系統運行的用戶

--with-big-tables \

--without-debug \              #使用非debug模式

--with-pthread  \              #強制使用pthread線程序庫編譯

--with-extra-charsets=complex \

--with-readline  \

--with-ssl  \

--with-embedded-server \

--enable-local-infile \

--with-plugins=partition,innobase \

--with-plugin-PLUGIN

 

./configure --prefix=/usr/local/mysql --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock --localstatedir=/usr/local/mysql/data --enable-assembler --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --enable-thread-safe-client --with-mysqld-user=mysql --with-big-tables --without-debug --with-pthread --with-extra-charsets=complex --with-readline --with-ssl --with-embedded-server --enable-local-infile  --with-plugins=partition,innobase --with-plugin-PLUGIN

 

 

#在執行./configure的時候遇到:

configure: error: No curses/termcap library found

緣由:缺乏ncurses安裝包

解決:yum install  -y  ncurses-devel,而後再執行./configure

 

(3)       建立配置文件

[root@test3 mysql-5.1.62]# cd support-files/

[root@test3 support-files]# ll *.cnf

-rw-r--r-- 1 root root  4714 09-03 16:31 my-huge.cnf

-rw-r--r-- 1 root root 19763 09-03 16:31 my-innodb-heavy-4G.cnf

-rw-r--r-- 1 root root  4688 09-03 16:31 my-large.cnf

-rw-r--r-- 1 root root  4699 09-03 16:31 my-medium.cnf

-rw-r--r-- 1 root root  2467 09-03 16:31 my-small.cnf

[root@test3 support-files]# /bin/cp my-small.cnf /etc/my.cnf #用模板文件直接複製爲配置文件

 

(4)       建立數據文件

[root@test3 ~]# mkdir /usr/local/mysql/data       #建立數據文件目錄

[root@test3 ~]# chown -R mysql /usr/local/mysql/   #受權mysql用戶訪問mysql安裝目錄

[root@test3 ~]# /usr/local/mysql/bin/mysql_install_db --user=mysql   #安裝mysql數據文件

 

(5)       啓動數據庫

方法一:

[root@test3 ~]# cp tools/mysql-5.1.62/support-files/mysql.server /usr/local/mysql/bin/

[root@test3 ~]# chmod 700 /usr/local/mysql/bin/mysql.server  #使腳本可執行

[root@test3 ~]# /usr/local/mysql/bin/mysql.server start

Starting MySQL..                                           [肯定]

[root@test3 ~]# netstat -tupnl |grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      23990/mysqld

 

方法二:按照安裝mysql時候的提示

/usr/local/mysql/bin/mysqld_safe  --user=mysql &

 

(6)       配置mysql命令全局使用路徑

方法一:

[root@test3 ~]# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile

[root@test3 ~]# source /etc/profile

方法二:

把mysql的啓動命令拷貝到/usr/local/sbin/

 

(7)       配置/etc/init.d/mysqld start方式啓動數據庫

[root@test3 ~]# cp tools/mysql-5.1.62/support-files/mysql.server /etc/init.d/mysqld

[root@test3 ~]# chmod 700 /etc/init.d/mysqld

[root@test3 ~]# /etc/init.d/mysqld start

Starting MySQL.                                            [肯定]

 

(8)       設置mysql開機啓動

方法一:

[root@test3 ~]# chkconfig --add mysqld

[root@test3 ~]# chkconfig --list |grep mysqld

mysqld          0:關閉  1:關閉  2:啓用  3:啓用  4:啓用  5:啓用  6:關閉

方法二:

把啓動命令放到/etc/rc.local裏面

 

(9)       爲root設置密碼

[root@test3 ~]# mysqladmin -uroot password "123456"

[root@test3 ~]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 

(10)   清理多餘用戶

[root@test3 ~]# mysql -uroot -p"123456"

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.1.62 Source distribution

 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> select user,host from mysql.user;

+------+-------------------+

| user | host              |

+------+-------------------+

| root | 127.0.0.1         |

|      | localhost         |

| root | localhost         |

|      | test3.localdomain |

| root | test3.localdomain |

+------+-------------------+

5 rows in set (0.00 sec)

mysql> drop user ""@localhost;

Query OK, 0 rows affected (0.00 sec)

 

mysql> drop user ""@test3.localdomain;

Query OK, 0 rows affected (0.00 sec)

 

mysql> drop user "root"@test3.localdomain;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host from mysql.user;

+------+-----------+

| user | host      |

+------+-----------+

| root | 127.0.0.1 |

| root | localhost |

+------+-----------+

2 rows in set (0.00 sec)

遇到的問題:

 

鏈接數據庫的時候提示:mysql: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory

首先查看libmysqlclient.so.16文件是否存在,不存在要去下載一個

解決方案一:vi /etc/ld.so.conf 增長一行/usr/local/mysql/lib/mysql

解決方案二:ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16  /usr/lib/

可是仍是有問題,應該是由於我以前卸載了系統自帶的mysql,而/usr/bin下的mysql這個腳本仍是舊的緣由,因此須要/bin/cp /usr/local/mysql/bin/mysql /usr/bin/

 

二  Php安裝配置(結合的是apache)

2.1 查看如下軟件包是否安裝

[root@test3 ~]# rpm -qa zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel

zlib-1.2.3-7.el5

freetype-2.2.1-31.el5_8.1

libpng-1.2.10-17.el5_8

libpng-1.2.10-17.el5_8

zlib-1.2.3-7.el5

libjpeg-6b-37

gd-2.0.33-9.4.el5_4.2

curl-7.15.5-15.el5

zlib-devel-1.2.3-7.el5

libjpeg-6b-37

freetype-2.2.1-31.el5_8.1

 

若是沒有安裝可使用yum的方式安裝:

[root@test3 ~]# yum install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel

 

 

2.2  libiconv的安裝(可選擇的還有libmcrypt(與加密相關)庫,Mhash庫,mcrypt庫)

1. libiconv

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

tar zxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local/libiconv

make

make install

cd ../

 

2. libmcrypt

wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"

tar zxf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make

make install

sleep 2

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

cd ../../

 

3. Mhash

wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"

tar zxf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9/

./configure

make

make install

sleep 2

cd ../

rm -f /usr/lib/libmcrypt.*

rm -f /usr/lib/libmhash*

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la

ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8

ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

 

4.mcrypt

wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"

tar zxf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/

/sbin/ldconfig

./configure

make

make install

cd ../

sleep 2

 

2.3  php的安裝

[root@test3 php-5.3.10]#./configure \

> --prefix=/application/php5.3.10 \

> --with-apxs2=/application/apache/bin/apxs \  #這個須要指定bin/apxs的路徑,經過apxs來來增長php模塊到apache中

> --with-mysql=/usr/local/mysql \   #指定mysql的路徑

> --with-xmlrpc \

> --with-openssl \

> --with-zlib \

> --with-freetype-dir \

> --with-gd \

> --with-jpeg-dir \

> --with-png-dir \

> --with-iconv=/usr/local/libiconv \

> --enable-short-tags \

> --enable-sockets \

> --enable-zend-multibyte \

> --enable-soap \

> --enable-mbstring \

> --enable-static \

> --enable-gd-native-ttf \

> --with-curl \

> --with-xsl \

> --enable-ftp \

> --with-libxml-dir

#下面的參數適合Nagios的PHP環境

--enable-sigchild

--enable-pcntl

--enable-bcmath

 

[root@test3 php-5.3.10]#make && make install

 

[root@test3 php-5.3.10]# ls php.ini*

php.ini-development (開發版)  php.ini-production (產品版)

[root@test3 php-5.3.10]# diff php.ini-development  php.ini-production

521c521

< error_reporting = E_ALL | E_STRICT

---

> error_reporting = E_ALL & ~E_DEPRECATED

538c538

< display_errors = On

---

> display_errors = Off

549c549

< display_startup_errors = On

---

> display_startup_errors = Off

593c593

< track_errors = On

---

> track_errors = Off

611c611

< html_errors = On

---

> html_errors = Off

1318c1318

< mysqlnd.collect_memory_statistics = On

---

> mysqlnd.collect_memory_statistics = Off

1587c1587

< session.bug_compat_42 = On

---

> session.bug_compat_42 = Off

1596c1596

< session.bug_compat_warn = On

---

> session.bug_compat_warn = Off

 

#開發版與產品版相比,多開了一些方便調試的開關

#注意PHP5.2與PHP5.3安裝之間的差異。php5.2開啓fastcgi的參數是--enable-fastcgi,而php5.3開啓的參數則是--enable-fpm

 

2.4  Php的配置

[root@test3 php-5.3.10]#/bin/cp php.ini-production /application/php5.3.10/lib/php.ini

[root@test3 conf]# cp httpd.conf httpd.conf.qinbf.20131111-2

[root@test3 conf]# vi httpd.conf

#在大約311行,增長

AddType application/x-httpd-php .php .php3

AddType application/x-httpd-php-source .phps

 

#在大約166行,修改

<IfModule dir_module>

    DirectoryIndex  index.php index.html #增長了默認的首頁文件index.php

</IfModule>

 

 

2.5  php測試

測試:php和apache

 

若是不關閉虛擬主機的配置文件,那麼htdocs裏面的首頁文件不會生效,默認會顯示在第一個虛擬主機的站點。因此要測試的話,要麼關閉虛擬主機配置文件,要麼把默認站點的首頁文件移動到第一個虛擬主機的配置目錄中。

 

測試:php和mysql的鏈接 (在瀏覽器輸入ip/test_mysql.php便可看到)

 

[root@test3 www]# vi test_mysql.php

<?php

        //$link_id=mysql_connect('','?','');

        $link_id=mysql_connect('localhost','root','qbf19880328') or mysql_error();

        //$link_id=mysql_connect('localhost','test','');

        if($link_id){

                echo "mysql successful by qinbf !";

        }else{

                echo mysql_error();

        }

//php?

/* php? */

?>

 

 

 

三 創建屬於本身的博客網站

3.1 下載解壓

[root@test3 blog]# wget http://cn.wordpress.org/wordpress-3.7.1-zh_CN.zip

[root@test3 blog]# unzip wordpress-3.7.1-zh_CN.zip

在瀏覽器中其實已經能夠打開index.php文件,可是仍是缺乏wp-config.php文件

 

3.2 建立數據庫

[root@test3 blog]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 12

Server version: 5.1.62 Source distribution

 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> create database wordpress DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

Query OK, 1 row affected (0.19 sec)

#創建一個GBK數據庫(默認是拉丁文字符集)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

| wordpress          |

+--------------------+

4 rows in set (0.14 sec)

 

mysql> grant select,insert,delete,update,create on wordpress.* to wordpress@localhost identified by '123456';

Query OK, 0 rows affected (0.16 sec)

#安裝blog後,記得要撤銷create,保留select,insert,update,delete的受權

revoke create on wordpress.*  from wordpress@’localhot’;

 

mysql> flush privileges;   #刷新權限

Query OK, 0 rows affected (0.11 sec)

 

mysql> select user,host from mysql.user;

+-----------+-----------+

| user      | host      |

+-----------+-----------+

| root      | 127.0.0.1 |

| root      | localhost |

| wordpress | localhost |

+-----------+-----------+

3rows in set (0.04 sec)

mysql> show grants for wordpress@localhost;  #查看受權

+------------------------------------------------------------------------------------------------------------------+

| Grants for wordpress@localhost                                                                                   |

+------------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'wordpress'@'localhost' IDENTIFIED BY PASSWORD '*B492CDBF83DDE8AF7B2D2249ABFE0301E091503A' |

| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ON `wordpress`.* TO 'wordpress'@'localhost'                         |

+------------------------------------------------------------------------------------------------------------------+

2 rows in set (0.03 sec)

 

3.3 建立配置文件

 

 

當界面沒法下一步建立的時候,選擇使用直接建立wp-config.php文件

[root@test3 blog]# cp wp-config-sample.php wp-config.php

修改配置文件以下:

/** WordPress數據庫的名稱 */

define('DB_NAME', 'wordpress');

 

/** MySQL數據庫用戶名 */

define('DB_USER', 'wordpress');

 

/** MySQL數據庫密碼 */

define('DB_PASSWORD', '123456');

 

/** MySQL主機 */

define('DB_HOST', 'localhost');

 

/** 建立數據表時默認的文字編碼 */

define('DB_CHARSET', 'gbk');

 

/** 數據庫整理類型。如不肯定請勿更改 */

define('DB_COLLATE', '');

 

3.4 博客站點目錄的特殊設置

[root@test3 html]# chown root.root blog -R

[root@test3 html]# chmod 755 blog -R

#爲了防止apache出現了漏洞,僅給全部程序root屬主和組

 

[root@test3 blog]# mkdir -p wp-content/uploads

[root@test3 blog]# chown daemon.daemon wp-content/uploads/ -R

#受權文件上傳目錄,若是是集羣要放在公共存儲

#通過測試,wordpress前端禁止*.php,*.sh,*.bat等文件上傳

 

 

四 php擴展緩存加速等查件安裝配置

4.1 查看lamp版本

1. 查看linux系統版本

[root@test3 ~]# cat /etc/redhat-release

CentOS release 5.8 (Final)

 

2.查看apache版本

[root@test3 ~]# /application/apache/bin/apachectl -v

Server version: Apache/2.2.25 (Unix)

Server built:   Sep  3 2013 18:25:54

 

3. 查看php版本

[root@test3 ~]# /application/php/bin/php -v

PHP 5.3.10 (cli) (built: Nov 12 2013 00:24:52)

Copyright (c) 1997-2012 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

 

4. 查看mysql版本

[root@test3 tmp]# mysqladmin -uroot -p'123456' -S /usr/local/mysql/tmp/mysql.sock version

mysqladmin  Ver 8.41 Distrib 5.0.95, for redhat-linux-gnu on x86_64

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Server version          5.1.62

Protocol version        10

Connection              Localhost via UNIX socket

UNIX socket             /usr/local/mysql/tmp/mysql.sock

Uptime:                 8 min 23 sec

 

Threads: 1  Questions: 1  Slow queries: 0  Opens: 16  Flush tables: 1  Open tables: 4  Queries per second avg: 0.1

 

4.2   解決Perl編譯問題

[root@test3 ~]# echo 'LC_ALL=C' >> /etc/profile

[root@test3 ~]# tail -1 /etc/profile

LC_ALL=C

[root@test3 ~]# source /etc/profile

[root@test3 ~]# echo $LC_ALL

C

 

4.3   eACCelerator的安裝

eACCelerator是一個自由的開放源代碼的php加速、優化和動態內容緩存的擴展模塊,它能夠提高php程序的緩存性能,下降php程序在解析時對服務器的性能開銷。eACCelerator還有對PHP優化做用,加快其執行效率,使PHP程序代碼效率提升1-10倍。

phpize是什麼?

phpize是用來擴展php擴展模塊的,經過phpize能夠創建php的外掛模塊。好比想在原來編譯好的php中加入memcache等擴展模塊,可使用phpize。使用方式以下:

 

[root@test3 tools]# wget  http://sourceforge.net/projects/eaccelerator/files/latest/download

[root@test3 tools]# unzip eaccelerator-0.9.6.1.zip

[root@test3 tools]# cd eaccelerator-0.9.6.1

[root@test3 eaccelerator-0.9.6.1]# /application/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

Cannot find autoconf. Please check your autoconf installation and the

$PHP_AUTOCONF environment variable. Then, rerun this script

#出現這個錯誤是由於沒有安裝m4和autoconf包,能夠嘗試使用yum install或者源碼安裝這兩個包,而後從新執行/application/php/bin/phpize

[root@test3 eaccelerator-0.9.6.1]# yum install m4 autoconf

[root@test3 eaccelerator-0.9.6.1]# /application/php/bin/phpize

Configuring for:

PHP Api Version:         20090626

Zend Module Api No:      20090626

Zend Extension Api No:   220090626

[root@test3 eaccelerator-0.9.6.1]# ./configure --enable-eaccelerator=shared --with-php-config=/application/php/bin/php-config

[root@test3 eaccelerator-0.9.6.1]# make  && make  install

實踐測試:

提示:

1)  php5.3.10可用eaccelerator-0.9.6版本,若是使用eaccelerator-0.9.5.2版本會在make階段報以下錯誤:make: ***[optimize.lo] Error 1

2)  php5.2.6可用eaccelerator-0.9.5.2版本

 

4.4   安裝memcache緩存擴展模塊

軟件說明:

Memcache是一個高性能的分佈式的內存對象緩存系統,經過在內存裏維護一個統一的巨大的hash表,它可以用來存儲各類格式的數據,包括圖像、視頻、文件以及數據庫檢索的結果等。簡單的說就是將數據調用到內存中,而後從內存中讀取,從而大大提升讀取速度。

Memcache分爲服務端軟件(名字如:memcached-1.4.3.tar.gz)和客戶端插件(名字如:memcache-2.2.5.tar.gz)兩部分。本文是客戶端插件在php服務中的安裝。

[root@test3 tools]# wget  http://pecl.php.net/get/memcache-2.2.5.tgz

[root@test3 tools]# tar zxf memcache-2.2.5.tgz

[root@test3 tools]# cd memcache-2.2.5

[root@test3 memcache-2.2.5]# /application/php/bin/phpize

[root@test3 memcache-2.2.5]# ./configure --with-php-config=/application/php/bin/php-config [root@test3 memcache-2.2.5]# make  &&  make install

[root@test3 memcache-2.2.5]# ll /application/php5.3.10/lib/php/extensions/no-debug-zts-20090626/

總計 628

-rwxr-xr-x 1 root root 406459 11-14 03:22 eaccelerator.so

-rwxr-xr-x 1 root root 222246 11-14 03:44 memcache.so

提示:若是安裝memcache-2.2.4,可能報錯make:***[memcache.lo] Error 1 解決:cp memcache.loT memcache.lo

 

4.5   安裝PDO_MYSQL擴展模塊

PDO擴展爲PHP訪問數據庫定義了一個輕量級的、一致性的接口,它提供了一個數據訪問抽象層,這樣,不管是用什麼數據庫,均可以經過一致的函數執行查詢和獲取數據。

[root@test3 tools]# wget  http://lcmp.googlecode.com/files/PDO_MYSQL-1.0.2.tgz

[root@test3 tools]# tar zxf PDO_MYSQL-1.0.2.tgz

[root@test3 tools]# cd PDO_MYSQL-1.0.2

[root@test3 PDO_MYSQL-1.0.2]# /application/php/bin/phpize

[root@test3 PDO_MYSQL-1.0.2]# ./configure --with-php-config=/application/php/bin/php-config --with-pdo-mysql=/usr/local/mysql

[root@test3 PDO_MYSQL-1.0.2]#make && make install

 

4.6   安裝圖像處理程序及imageMagick軟件

4.6.1 安裝imageMagick軟件

[root@test3 tools]# wget  http://www.imagemagick.org/download/ImageMagick-6.8.7-5.tar.gz

[root@test3 tools]# tar zxf ImageMagick-6.8.7-5.tar.gz

[root@test3 tools]#cd  ImageMagick-6.8.7-5.tar.gz

[root@test3 tools]#./configure

[root@test3 tools]#make && make install

由於此步不是php的擴展,因此沒有.so文件

 

4.6.2 安裝image php擴展插件

image插件須要imageMagick的支持,不先安裝imageMagick會報錯。是一個供php調用imageMagick功能的擴展,進行圖片的建立與修改壓縮等操做,都集成在imagick這個擴展中。

[root@test3 tools]# wget http://pecl.php.net/get/imagick-3.0.1.tgz

[root@test3 tools]# tar zxf imagick-3.0.1.tgz

[root@test3 tools]# cd imagick-3.0.1

[root@test3 imagick-3.0.1]# ./configure --with-php-config=/application/php/bin/php-config

[root@test3 imagick-3.0.1]# make && make install

#make的過程當中遇到checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h的報錯,解決以下:(imagick-2.3.0.tgz沒有遇到這個問題)

今天在新服上安裝php imagick,

 

環境以下:

php 5.3.20

ImageMagick-6.8.3-8

imagick-3.1.0RC2

但是出錯了一個問題.就是死說找不到MagickWand.h:

 

checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h

 

但是我明明正確安裝了ImageMagick的呀.GOOGLE了半天,也解決不了這個問題.後面對照另外一個服務器上的ImageMagick才發現,原來ImageMagick 6.8這個版後的目錄結構變了,舊版本頭文件是放在/usr/local/include/ImageMagick目錄的,而ImageMagick 6.8則是放在/usr/local/include/ImageMagick-6

 

添加軟鏈接

 

命令以下:

 

ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick

 

後來發現imagick-3.1.0RC2編譯不經過,因而下降版本,使用imagick-3.01

 

make && make && install

 

搞掂!

 

 

 

更新:

 

最終解決方法:(如下是對於imagick操做的)

 

yum install pkgconfig

 

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

 

rm -rf Makefile config.status config.h

 

./configure

 

make

 

make install

 

 

4.7   安裝Zend緩存及加速擴展模塊

Zend Optimizer使用優化代碼的方法來提升PHP應用程序的執行速度,要比不使用的要快40%到100%。(對於PHP 5.2或更早的版本有效,對5.3版本無效)

 

4.8   安裝xcache緩存加速擴展模塊

Xcache是一個又快又穩定的PHP opcode緩存器,可以快速跟進PHP版本。

 

Xcache的2個特色:

將PHP代碼進行編譯,這樣能夠不用重複讀取PHP文件,加速整個程序的運行效率

    能夠緩存數據到內存中,直接能夠方便調用,例如你的一些不會常常改動的數據,只用一次從數據庫讀取出來後保存到Xcache中,就不用再去使用數據庫了。

wget http://xcache.lighttpd.net/pub/Releases/3.0.0/xcache-3.0.0.tar.gz

tar zxf xcache-3.0.0.tar.gz

cd xcache-3.0.0

/application/php/bin/phpize 

./configure --enable-xcache --with-php-config=/application/php/bin/php-config

make 

make install

ll /application/php5.3.10/lib/php/extensions/no-debug-zts-20090626/

 

4.9   配置相關php擴展插件模塊

4.9.1          修改php的配置文件

(1) 修改/application/php/lib/php.ini 查找;extension_dir=」./」,修改成/application/php5.3.10/lib/php/extension/no-debug-zts-20090626

[root@test3 ~]# cd /application/php/lib/

[root@test3 lib]# ls

php  php.ini

[root@test3 lib]# cp php.ini php.ini.20131115

 

[root@test3 lib]# sed -i 's#; extension_dir = "./"#extension_dir = "/application/php5.3.10/lib/php/extension/no-debug-zts-20090626/"#g' php.ini

 

[root@test3 lib]# grep "extension_dir =" php.ini

extension_dir = "/application/php5.3.10/lib/php/extensions/no-debug-zts-20090626/"

; extension_dir = "ext"

;sqlite3.extension_dir =

(2) 在php.ini的最後幾行添加:

cat >>/application/php/lib/php.ini << EOF

;--cache ext start by qinbf 2013-11-15--

extension = memcache.so

extension = pdo_mysql.so

extension = imagick.so

;--cache ext end by qinbf 2013-11-15--

EOF

[root@test3 lib]# tail -5 php.ini

;--cache ext start by qinbf 2013-11-15--

extension = memcache.so

extension = pdo_mysql.so

extension = imagick.so

;--cache ext end by qinbf 2013-11-15--

 

4.10  配置eAccelerator插件

4.10.1配置eaccelerator緩存目錄

[root@test3 lib]# mkdir -p /tmp/eaccelerator

[root@test3 lib]# egrep "User|Group" /application/apache/conf/httpd.conf

# User/Group: The name (or #number) of the user/group to run httpd as.

User daemon

Group daemon

[root@test3 lib]# chown daemon.daemon  -R /tmp/eaccelerator

 

4.10.2 配置eaccelerator參數

注意:[eaccelerator]要放在Zend配置內容以前

[root@oldboy lib]# cat >> /application/php/lib/php.ini<<EOF

[eaccelerator]

extension=eaccelerator.so

eaccelerator.shm_size="64"

eaccelerator.cache_dir="/tmp/eaccelerator"

eaccelerator.enable="1"

eaccelerator.optimizer="1"

eaccelerator.check_mtime="1"

eaccelerator.debug="0"

eaccelerator.filter=""

eaccelerator.shm_max="0"

eaccelerator.shm_ttl="3600"

eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

EOF

 

[root@test3 lib]# tail -20 php.ini

;--cache ext start by qinbf 2013-11-15--

extension = memcache.so

extension = pdo_mysql.so

extension = imagick.so

;--cache ext end by qinbf 2013-11-15--

[eaccelerator]

extension=eaccelerator.so

eaccelerator.shm_size="64"

eaccelerator.cache_dir="/tmp/eaccelerator"

eaccelerator.enable="1"

eaccelerator.optimizer="1"

eaccelerator.check_mtime="1"

eaccelerator.debug="0"

eaccelerator.filter=""

eaccelerator.shm_max="0"

eaccelerator.shm_ttl="3600"

eaccelerator.shm_prune_period="3600"

eaccelerator.shm_only="0"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

[root@test3 lib]# /application/php/bin/php -v

PHP 5.3.10 (cli) (built: Nov 12 2013 00:24:52)

Copyright (c) 1997-2012 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

 

[root@test3 lib]# ll /tmp/eaccelerator/

總計 64

drwxrwxrwx 18 root root 4096 11-14 07:10 0

drwxrwxrwx 18 root root 4096 11-14 07:10 1

drwxrwxrwx 18 root root 4096 11-14 07:10 2

drwxrwxrwx 18 root root 4096 11-14 07:10 3

drwxrwxrwx 18 root root 4096 11-14 07:10 4

drwxrwxrwx 18 root root 4096 11-14 07:10 5

drwxrwxrwx 18 root root 4096 11-14 07:10 6

drwxrwxrwx 18 root root 4096 11-14 07:10 7

drwxrwxrwx 18 root root 4096 11-14 07:10 8

drwxrwxrwx 18 root root 4096 11-14 07:10 9

drwxrwxrwx 18 root root 4096 11-14 07:10 a

drwxrwxrwx 18 root root 4096 11-14 07:10 b

drwxrwxrwx 18 root root 4096 11-14 07:10 c

drwxrwxrwx 18 root root 4096 11-14 07:10 d

drwxrwxrwx 18 root root 4096 11-14 07:10 e

drwxrwxrwx 18 root root 4096 11-14 07:10 f

 

4.11   修改php.ini配置xcache

通常認爲xcache和eaccelerator功能相近,安裝一個便可。若是想同時安裝,那麼eaccelerator的配置內容必須在xcache以後。

;xcache config by qinbf 20131115---------------------

[xcache-common]

;; install as zend extension (recommended), normally "$extension_dir/xcache.so"

;zend_extension = /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so

; zend_extension_ts = /usr/local/lib/php/extensions/non-debug-zts-xxx/xcache.so

;; For windows users, replace xcache.so with php_xcache.dll

;zend_extension_ts = c:/php/extensions/php_xcache.dll

;; or install as extension, make sure your extension_dir setting is correct

extension = xcache.so

;; or win32:

; extension = php_xcache.dll

 

;xcache config by qinbf 20131115----------------------

 

[xcache.admin]

[xcache.admin]

xcache.admin.enable_auth = On

 

xcache.admin.user = "mOo"

; xcache.admin.pass = md5($your_password)

xcache.admin.pass = ""

 

[xcache]

[xcache]

; ini only settings, all the values here is default unless explained

 

; select low level shm/allocator scheme implemenation

xcache.shm_scheme =        "mmap"

; to disable: xcache.size=0

; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows

xcache.size  =               128M

; set to cpu count (cat /proc/cpuinfo |grep -c processor)

xcache.count =                 2

; just a hash hints, you can always store count(items) > slots

xcache.slots =                8K

; ttl of the cache item, 0=forever

xcache.ttl   =                 86400

; interval of gc scanning expired items, 0=no scan, other values is in seconds

xcache.gc_interval =           3600

 

; same as aboves but for variable cache

xcache.var_size  =            0

xcache.var_count =             1

xcache.var_slots =            8K

; default ttl

xcache.var_ttl   =             0

xcache.var_maxttl   =          0

xcache.var_gc_interval =     300

 

不然會報錯:

[root@oldboy lib]# /application/php/bin/php -v

PHP Warning:  Cannot load module 'XCache' because conflicting module 'eAccelerator' is already loaded in Unknown on line 0

PHP 5.3.10 (cli) (built: Apr  7 2012 22:42:50)

Copyright (c) 1997-2012 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

with eAccelerator v0.9.6, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

 

 

正常狀況下:

 

 

 

 

五  Apache服務優化

5.1 配置Apache日誌輪詢

apache自帶的日誌輪詢工具也是可使用的,可是老師測試會丟日誌,因此採用cronolog來配置日誌輪詢

 

tar zxf cronolog-1.6.2.tar.gz

cd cronolog-1.6.2

./configure 

make 

make install

<VirtualHost *:80>

    ServerAdmin qinbf@etiantian.com

    DocumentRoot "/var/html/www"

    ServerName www.etiantian.com

    ServerAlias etiantian.com

    ErrorLog "logs/www.etiantiancom-error_log"

   # CustomLog "logs/www.etiantiancom-access_log" common

CustomLog "|/usr/local/sbin/cronolog /application/apache/logs/access_www_%w.log" combined

#注意這裏要寫全路徑,%w表示按周輪詢

</VirtualHost>

 

5.2 錯誤頁面優雅顯示

[root@test3 bin]# grep "ErrorDocument 404" /application/apache/conf/httpd.conf

#ErrorDocument 404 /missing.html

#ErrorDocument 404 "/cgi-bin/missing_handler.pl"

#能夠指定404錯誤提示的頁面,能夠是URL,也能夠是腳本或者文件

 

5.3 mod_deflate文件壓縮功能

5.3.1 mod_deflate模塊介紹

mod_deflate模塊提供了DEFLATE輸出過濾器,容許服務器在將輸出內容發送到客戶端以前進行壓縮,提高用戶體驗。

 

5.3.2 mod_deflate安裝檢查

(1)編譯安裝httpd狀況時是否已經安裝mod_deflate,命令爲:

[root@test3 ~]# /application/apache/bin/apachectl -l |grep mod_deflate

  mod_deflate.c

(2)若是是以DSO方式編譯的(--enable-so開啓DSO編譯方式),則查看方法爲:

[root@test3 ~]# ls /application/apache/modules/ |grep mod_deflate

mod_deflate.so

[root@test3 ~]# grep mod_deflate.so /application/apache/conf/httpd.conf

LoadModule  deflate_module  modules/mod_deflate.so

[root@test3 ~]# /application/apache/bin/apachectl -M |grep deflate

 deflate_module(dynamic)  #static是表示在編譯的時候就已經安裝了

Syntax OK

特別說明:以上兩種狀況不能同時存在,不然會有衝突

 

5.3.3 mod_deflate安裝

cd  /root/tools/httpd-2.2.25/modules/filters

/application/apache/bin/apxs -c -a -i mod_deflate.c

ll /application/apache/modules/mod_deflate.so

 

#若是提示衝突,要把/application/apache/conf/httpd.conf文件中,LoadModule  deflate_module  modules/mod_deflate.so這句命令註釋掉。

 

5.3.4 mod_deflate應用

<ifmodule mod_deflate.c>

DeflateCompressionLevel  9

SetOutputFilter DEFLATE

DeflateFilterNote  Input  instream

DeflateFilterNote  Output  outstream

DeflateFilterNote  Ratio  ratio

#Logformat  ‘  「%r」  %{outstream}n/%{instream}n  (%{ratio}n%%)’ deflate

#CustomLog  logs/deflate_log.log deflate

</ifmodule>

 

#把以上這段代碼嵌入到/application/apache/conf/extra/httpd-vhosts.conf的/var/html/www配置內容裏面(也能夠配置到全局目錄裏面)

 

<VirtualHost *:80>

    ServerAdmin qinbf@etiantian.com

    DocumentRoot "/var/html/www"

    ServerName www.etiantian.com

    ServerAlias etiantian.com

    ErrorLog "logs/www.etiantiancom-error_log"

    CustomLog "logs/www.etiantiancom-access_log" common

   <ifmodule mod_deflate.c>

        DeflateCompressionLevel  9

        SetOutputFilter DEFLATE

        DeflateFilterNote  Input  instream

        DeflateFilterNote  Output  outstream

        DeflateFilterNote  Ratio  ratio

        #Logformat  ‘  「%r」  %{outstream}n/%{instream}n  (%{ratio}n%%)’ deflate

        #CustomLog  logs/deflate_log.log deflate

   </ifmodule>

 

</VirtualHost>

 

 

測試:把老師的test_deflate.tar.gz壓縮包上傳到/var/html/www下,而後解壓,不須要作任何移動。打開Firefox開發專版,訪問192.168.1.4/test/deflate.html,打開firebug,在YSlow中能夠看到測試結果。

 

 

 

 

5.4 mod_expires緩存功能

Expire其實就是經過header報文來指定特定類型的文件在瀏覽器中的緩存時間。大多數的圖片,flash在發佈後都不是須要常常修改的,作了緩存之後這樣的瀏覽器之後就不須要再從服務器下載這些文件而是直接從緩存中讀取,這樣再訪問頁面的速度會大大加快。

 

在剛纔的mod_deflate模塊配置內容下面加入如下配置:

ExpiresActive on

ExpiresDefault "access plus 12 month"

ExpiresByType text/html "access plus 12 months"

ExpiresByType text/css "access plus 12 months"

ExpiresByType image/gif "access plus 12 months"

ExpiresByType image/jpeg "access plus12  12 months"

ExpiresByType image/jpg "access plus 12 months"

ExpiresByType image/png "access plus 12 months"

EXpiresByType application/x-shockwave-flash "access plus 12 months"

EXpiresByType application/x-javascript "access plus 12 months"

ExpiresByType video/x-flv "access plus 12 months"

[root@test3 www]# curl -I 192.168.1.4/01.jpg

HTTP/1.1 200 OK

Date: Sun, 17 Nov 2013 03:51:45 GMT

Server: Apache/2.2.25 (Unix) DAV/2 PHP/5.3.10

Last-Modified: Sun, 07 Nov 2010 12:20:20 GMT

ETag: "40904-486f3-4947587f66900"

Accept-Ranges: bytes

Content-Length: 296691

Cache-Control: max-age=31104000

Expires: Wed, 12 Nov 2014 03:51:45 GMT

Vary: Accept-Encoding

Content-Type: image/jpeg

 

5.5 更改Apache默認用戶

建立一個用戶例如Apache(最好起一個不經常使用的名字),用於子進程和子線程。

useradd  -M  -s /sbin/nologin  apache

vi  /application/apache/conf/httpd.conf

User  apache

Group  apache

5.6  worker模式提高併發數

 

5.7  屏蔽apache版本等敏感信息

(1) 修改httpd.conf文件,打開httpd-default.conf模塊

(2) 修改httpd-default.conf文件,ServerSignature Off以及ServerTokens Prod以後apachectl graceful使設置生效

 

5.8 apache目錄文件權限設置(root,目錄755,文件644)

[root@test3 html]# ll

總計 12

drwxr-xr-x 2 root root 4096 09-03 18:51 bbs

drwxr-xr-x 5 root root 4096 11-12 01:53 blog

drwxr-xr-x 3 root root 4096 11-17 11:43 www

提示:在網站架構中,應把資源文件,包括用戶上傳的圖片,福建等和程序分離,最好把上傳程序也分離,這樣就能夠從容受權了。

 

5.9 開啓httpd-mpm.conf增長鏈接數

 

5.10 apache防盜鏈功能

 

5.11 禁止目錄Index

Option  FollowSymLinks 這一個參數去掉Index選項

 

5.12 禁止用戶覆蓋(重載)

AllowOverride None  #禁止用戶覆蓋(重載)

#加快服務器速度,由於它再也不爲每一個請求尋找每一個目錄訪問控制問價(.htaccess)

 

5.13 關閉CGI

ScriptAlias /cgi-bin/ "/application/apache2.2.25/cgi-bin/"

<Directory "/application/apache2.2.25/cgi-bin">

    AllowOverride None

    Options None

    Order allow,deny

    Allow from all

</Directory>

刪除以上內容

 

5.14 避免使用.htaccess文件(分佈式配置文件)

首先從性能上,就應該避免使用.htaccess文件

 

5.15 apache安裝模塊

(mod_evasive20防DDOS、mod_limitipconn(針對單站點)配置、mod_security防SQL注入等

makejail   http://www.floc.net/makejail

是一個自動把創建jail所須要的程序放到jail內的軟件

mod_security   http://www.modsecurity.org

是apache的一個模塊,他有請求過濾,日誌審計等功能,能夠防止SQL Injection,跨站腳本攻擊,很不錯的一個模塊

 

16. 正確途徑取得源代碼,勤打apache補丁

 

17. apache日誌授予root700權限

chown  -R root.root logs

chmod  -R 700 logs 

 

18. 系統內核參數優化

 

19. 禁止PHP解析指定站點目錄

 

20. 使用tmps文件系統替代頻繁訪問的目錄

 

21儘量減小HTTP請求數

 

22. 使用CDN作網站加速

 

23. apache程序架構優化

1) 程序頁面服務器  2)圖片附件服務器  3)上傳服務器  三者的功能儘可能分離

a) 分離最佳方式是分別使用獨立的服務器(須要程序支持)

b) 次選方案在前端負載均衡器經過haproxy/nginx根據目錄或擴展名請求後面對應的服務器

 

 

 

六 apache服務Forbidden 403問題精彩講解

403 Forbidden資源不可用。服務器理解客戶的請求,但拒絕處理它。一般因爲服務器上文件或目錄的權限設置致使。

 

緣由1(重要)

apache配置文件中沒有對站點目錄的權限許可配置,這一般是在初始安裝apache後,更改了默認的apache站點目錄致使。如編譯安裝apache(假定安裝目錄爲/application/apache2.2.23/)後,將站點目錄更改成其餘的路徑:/var/html,則會報403錯誤

 

緣由2

站點目錄下無首頁文件,而apache的配置又禁止了目錄瀏覽,就會提示403錯誤

 

緣由3

Directory目錄權限問題: Order中allow,deny的順序以及限制

 

緣由4

站點目錄權限問題:站點目錄須要apache的用戶有訪問的權限(把/var/html的屬主和屬組設爲root,那麼就要給755權限,不然apache的用戶沒法讀取文件)

 

 

七 Nginx深刻應用實踐

7.1 關於Nginx模塊

Nginx使用不一樣的模塊實現不一樣的功能,主要有2組重要的模塊:

(1) Nginx core modules(必需的)

包括Main、Events

 

(2) Standard  HTTP modules(雖然不是必需的,可是缺省都會安裝,不建議改動)

典型的包括

Core、Access、FastCGI、Gzip、Log、Proxy、Rewrite、Upstream

 

7.2 Nginx目錄結構

/application/nginx

|-- client_body_temp

|-- conf

|   |-- fastcgi.conf   #fastcgi配置文件

|   |-- fastcgi.conf.default   #default文件均屬於備份文件

|   |-- fastcgi_params

|   |-- fastcgi_params.default

|   |-- koi-utf

|   |-- koi-win

|   |-- mime.types

|   |-- mime.types.default

|   |-- nginx.conf   #Nginx主配置文件

|   |-- nginx.conf.default

|   |-- nginx.conf.qinbf-20131101

|   |-- scgi_params

|   |-- scgi_params.default

|   |-- uwsgi_params

|   |-- uwsgi_params.default

|   `-- win-utf

|-- fastcgi_temp

|-- html

|   |-- 50x.html  #錯誤優雅顯示文件

|   `-- index.html

|-- logs

|   |-- access.log  #訪問日誌

|   |-- error.log   #錯誤日誌

|   `-- nginx.pid 

|-- proxy_temp

|-- sbin

|   `-- nginx

|-- scgi_temp

`-- uwsgi_temp

 

9 directories, 22 files

 

7.3 Nginx.conf配置文件

worker_processes  1;  #ps -ef |grep nginx能夠查看到nginx的子線程數

 

events {

    worker_connections  1024;  #能夠理解爲最大併發數

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 

    server {  #一個server至關於apache的一個vhost,能夠複製多個server模塊配置多個主機

        listen       80;

        server_name  localhost;

 

        location / {   #至關於htdocs

            root   html;

            index  index.html index.htm;

        }

 

        error_page   500 502 503 504  /50x.html;  #優雅顯示頁面

        location = /50x.html {

            root   html;

 

        }

 

    }

 

}

 

7.4 基於域名的虛擬主機配置

http {

 10     include       mime.types;

 11     default_type  application/octet-stream;

 12     sendfile        on;

 13     keepalive_timeout  65;

 14

 15     server {

 16         listen       80;

 17         server_name  www.etiantian.org;

 18

 19         location / {

 20             root   html;

 21             index  index.html index.htm;

 22         }

 23

 24         error_page   500 502 503 504  /50x.html;

 25         location = /50x.html {

 26             root   html;

 27

 28         }

 29

 30     }

 31

 32 }

 

而後檢查語法,優雅重啓

[root@test2 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.2.9/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.2.9/conf/nginx.conf test is successful

[root@test2 conf]# /application/nginx/sbin/nginx -s reload

[root@test2 conf]# netstat -tupnl |grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      21475/nginx  

 

配置虛擬主機流程:

1)  複製server標籤段,到結尾,注意放到http的結束大括號前

2)  更改server_name及對應網頁的根目錄

3)  建立對應網頁的根目錄,並創建測試文件

4)  檢查語法,重啓服務

5)  在host文件作解析

6)  瀏覽器訪問

 

7.5 禁止ip訪問

爲防止域名惡意解析到本身的服務器上,必需要配置禁止ip訪問

server  {

     listen  80  default;

     return 500;

}

#這段配置,是將訪問沒有配置爲本服務器虛擬主機的域名,默認返回500錯誤

 

#也能夠利用rewrite規則,把惡意解析到本服務器的域名訪問流量,導入到本身的站點

server  {

       listen  80  default;

       rewrite  ^(.*)  http://www.etiantian.com  permanent;

}

#域名解析到本地服務器,可是並未爲該域名配置本地服務器的虛擬主機,將跳轉到rewrite定義的站點上

 

#server_name  _;  這個的含義表明輸入ip地址直接訪問的結果

#listen  80  default_server;  default_server這個參數用於nginx 0.8版本以後

 

7.6 nginx日誌配置及切割

目前尚未比較好的Nginx日誌切割工具

日誌的格式定義:

log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                           '$status $body_bytes_sent "$http_referer" '

                           '"$http_user_agent" "$http_x_forwarded_for"';

 

    server {

        listen       80;

        server_name  www.etiantian.com;

 

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

        }

 

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

 

        }

 

        access_log /app/logs/www_access.log commonlog;

        #日誌格式的調用

     }

 

192.168.1.1 - - [22/Nov/2013:00:27:32 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" "-"

 

 

日誌切割腳本

#!/bin/bash

date=`date +%Y%m%d`

Nginx_Dir="/application/nginx"

Nginx_Logs="/app/logs"

Log_Name="www_access"

 

cd /tmp

[ -d $Nginx_Logs ] && cd $Nginx_Logs || exit 1

[ -f $Log_Name.log ] && /bin/mv $Log_Name.log  ${Log_Name}.${date}.log || exit 1

if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ]

   then

      kill -USR1 `cat  $Nginx_Dir/logs/nginx.pid` 

#把nginx的日誌重命名至關於刪除文件,須要重啓nginx服務

fi

 

 

而後天天晚上12點切割

crontab  -e

00  00  * * * /bin/sh /root/scripts/cut_nginx_log.sh >/dev/null 2>&1

 

統計IP並排序

awk '{print $1}' www_access.log  | sort | uniq -c | sort -rn -k 1 | head

 

 

7.7  Nginx配置文件優化

worker_processes  1;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                           '$status $body_bytes_sent "$http_referer" '

                           '"$http_user_agent" "$http_x_forwarded_for"';

   

    include extra/www.conf;

    include extra/bbs.conf;

    include extra/blog.conf;

    include extra/default.conf;

 

}

模仿apache配置文件,把虛擬主機的配置寫在extra目錄的配置文件內,而後用include的方式調用。

 

7.8  Nginx別名及鏈接狀態信息配置

#別名其實就是以相應的別名配置虛擬主機,而後利用rewrite規則,跳轉到主域名上。

專門寫一個配置文件內容以下:

server {

        listen       80;

        server_name  etiantian.com;

       

        rewrite ^(.*) http://www.etiantian.com permanent;

 

 }

 

而後在nginx文件將調用此文件:include  extra/www_alias.conf

 

 

便是配置一個虛擬主機文件,內容以下:

server {

        listen       80;

        server_name  status.etiantian.com;

 

        location / {

        stub_status on;

        access_log off;

        }

}

而後在nginx.conf文件中調用此配置文件

 

 

 

 

八 動態語言php應用(fastCGI應用)

8.1  FastCGI介紹

FastCGI是語言無關的,可伸縮架構的CGI開放擴展,其主要行爲是將CGI解釋器進程保持在內存中並所以得到較高的性能。它還支持分佈式的運算, 即 FastCGI 程序能夠在網站服務器之外的主機上執行而且接受來自其它網站服務器來的請求。即nginx和FastCGI是獨立的,而不像apache和php那樣是密不可分的。

FastCGI用於結合nginx和PHP,相似於apache和php結合的模塊。

 

8.2  FastCGI原理

一、Web Server啓動時載入FastCGI進程管理器(IIS ISAPI或Apache Module)

二、FastCGI進程管理器自身初始化,啓動多個CGI解釋器進程(可見多個php-cgi)並等待來自Web Server的鏈接。

三、當客戶端請求到達Web Server時,FastCGI進程管理器選擇並鏈接到一個CGI解釋器。Web server將CGI環境變量和標準輸入發送到FastCGI子進程php-cgi。

四、FastCGI子進程完成處理後將標準輸出和錯誤信息從同一鏈接返回Web Server。當FastCGI子進程關閉鏈接時,請求便告處理完成。FastCGI子進程接着等待並處理來自FastCGI進程管理器(運行在Web Server中)的下一個鏈接。 在CGI模式中,php-cgi在此便退出了。

 

在上述狀況中,你能夠想象CGI一般有多慢。每個Web請求PHP都必須從新解析php.ini、從新載入所有擴展並重初始化所有數據結構。使用FastCGI,全部這些都只在進程啓動時發生一次。一個額外的好處是,持續數據庫鏈接(Persistent database connection)能夠工做。

 

8.3 檢查如下軟件包是否安裝

[root@test3 ~]# rpm -qa zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel

zlib-1.2.3-7.el5

freetype-2.2.1-31.el5_8.1

libpng-1.2.10-17.el5_8

libpng-1.2.10-17.el5_8

zlib-1.2.3-7.el5

libjpeg-6b-37

gd-2.0.33-9.4.el5_4.2

curl-7.15.5-15.el5

zlib-devel-1.2.3-7.el5

libjpeg-6b-37

freetype-2.2.1-31.el5_8.1

 

若是沒有安裝可使用yum的方式安裝:

[root@test3 ~]# yum install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel  openldap-devel libxslt-devel

 

 

附上CentOS下常見編譯PHP的錯誤及解決辦法

configure: error: xml2-config not found. Please check your libxml2 installation.

yum install libxml2-devel

 

configure: error: Cannot find OpenSSL’s

yum install openssl-devel

 

configure: error: Please reinstall the libcurl distribution -

 

 

easy.h should be in /include/curl/

yum install curl-devel

configure: error: libjpeg.(a|so) not found

yum install libjpeg-devel

 

configure: error: libpng.(a|so) not found.

yum install libpng-devel

 

configure: error: libXpm.(a|so) not found.

yum install libXpm-devel

 

configure: error: freetype.h not found.

yum install freetype-devel

 

configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.

yum install libc-client-devel

 

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

yum install libmcrypt-devel

 

configure: error: Please reinstall libmhash – I cannot find mhash.h

yum install mhash-devel

 

configure: error: Cannot find MySQL header files under /usr.

Note that the MySQL client library is not bundled anymore!

yum install mysql-devel

 

configure: error: Please reinstall ming distribution. libming.(a|so) not found

- temp remove the config for ‘–with-ming=/opt/ming/’

 

configure: error: Cannot find pspell

yum install pspell-devel

 

configure: error: cannot find mm library

Download from http://www.ossp.org/pkg/lib/mm/

wget ftp://ftp.ossp.org/pkg/lib/mm/mm-1.4.2.tar.gz

Extract it:  tar -zxvf mm-1.4.2.tar.gz

./configure

make

make install

 

configure: error: Cannot find libtidy

yum install libtidy-devel

yum install libtidy

change path at configure: ‘–with-tidy=/usr’

 

configure: error: not found. Please reinstall the expat distribution.

yum install expat-devel

 

configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

yum install libxslt-devel

 

*Tips: To uninstall, just enter:

yum remove {package-name}

 

8.4  libiconv、libmcrypt、Mhash、mcrypt的安裝

1. libiconv

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

tar zxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local/libiconv

make

make install

cd ../

 

2. libmcrypt

wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"

tar zxf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make

make install

sleep 2

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

cd ../../

 

3. Mhash

wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"

tar zxf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9/

./configure

make

make install

sleep 2

cd ../

rm -f /usr/lib/libmcrypt.*

rm -f /usr/lib/libmhash*

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la

ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8

ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

 

4.mcrypt

wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"

tar zxf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/

/sbin/ldconfig

./configure

make

make install

cd ../

sleep 2

 

 

8.5  php5.3.27的編譯參數(結合的是nginx)

./configure \

--prefix=/application/php5.3.27 \

--with-mysql=/usr/local/mysql \

--with-iconv-dir=/usr/local/libiconv \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--disable-rpath \

--enable-safe-mode \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--with-curlwrappers \

--enable-mbregex \

--enable-fpm \

--enable-mbstring \

--with-mcrypt \

--with-gd \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-ldap \

--with-ldap-sasl \

--with-xmlrpc \

--enable-zip \

--enable-soap \

--enable-short-tags \

--enable-zend-multibyte \

--enable-static \

--with-xsl \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--enable-ftp

#--enable-discard-path

#--enable-fastcgi

#--enable-force-cgi-redirect

#--with-libevent-dir 這個參數在php5.3.10中是支持的

--enable-fpm這個參數時開啓fastcgi的,是nginx結合php用的參數,在apache編譯的參數中是沒有的

 

要記得先安裝8.2步驟中的那些軟件包,不然在安裝過程當中可能會遇到各類各樣的問題

 

8.6  PHP的配置

1. php.ini的配置

[root@test2 php-5.3.27]# pwd

/root/tools/php-5.3.27

[root@test2 php-5.3.27]# /bin/cp php.ini-production /application/php5.3.27/lib/php.ini

 

2. php-fpm

[root@test2 etc]# pwd

/application/php5.3.27/etc

[root@test2 etc]# ls php-fpm.conf.default

php-fpm.conf.default

[root@test2 tools]# cp php-fpm.conf.5.3.10 /application/php5.3.27/etc/php-fpm.conf

#用老師改好的文件複製爲/application/php5.3.27/etc/php-fpm.conf

 

[root@test2 tools]# /application/php/sbin/php-fpm -t

[29-Nov-2013 00:28:11] NOTICE: configuration file /application/php5.3.27/etc/php-fpm.conf test is successful

#檢查語法

 

[root@test2 tools]# pkill php-fpm

[root@test2 tools]# /application/php/sbin/php-fpm

#重啓服務

 

[root@test2 tools]# ps -ef |grep php-fpm

root     17875     1  1 00:29 ?        00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)

nginx    17876 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17877 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17878 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17879 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17880 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17881 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17882 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17883 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17884 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17885 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17886 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17887 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17888 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17889 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17890 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

nginx    17891 17875  0 00:29 ?        00:00:00 php-fpm: pool www           

root     17893  2685  0 00:29 pts/0    00:00:00 grep php-fpm

[root@test2 tools]# netstat -tupnl |grep 9000

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      17875/php-fpm      

[root@test2 tools]#

[root@test2 tools]# lsof -i :9000

COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

php-fpm 17875  root    7u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17876 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17877 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17878 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17879 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17880 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17881 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17882 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17883 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17884 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17885 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17886 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17887 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17888 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17889 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17890 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

php-fpm 17891 nginx    0u  IPv4 177862      0t0  TCP localhost.localdomain:cslistener (LISTEN)

#檢查服務的運行狀況

8.7 整合PHP和Nginx

server {

        listen       80;

        server_name  blog.etiantian.org;

        root   /data0/www/blog;

        index  index.php index.html index.htm;

    

  location ~ .*\.(php|php5)?$   #以php或者php5結尾的文件

     {     

       #fastcgi_pass  unix:/tmp/php-cgi.sock;

        fastcgi_pass  127.0.0.1:9000;   #交給本機的9000端口處理

        fastcgi_index index.php;        #首頁文件

        include fastcgi.conf;

     }

}

 

8.8 安裝wordpress博客

1.創建博客數據庫(默認是拉丁字符集)

(1)建立數據庫

create database wordpress;

也可按下面方式建立庫:

創建一個名爲wordpress的GBK數據庫

create database wordpress DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

 

(2)受權用戶

grant select,insert,delete,update,create on wordpress.* to wordpress@localhost identified by '19880328';

flush privileges;

 

  1. 若是是使用瀏覽器安裝,那麼blog目錄須要對nginx用戶受權,即把把屬主和組改成nginx

chown nginx.nginx blog -R

 

  1. 編輯wp-config.php配置文件

只須要編輯該文件的數據庫名、數據庫用戶名、密碼便可。

特別注意的是在編輯wp-config.php文件的時候,應該是數據root用戶編輯的,要改成nginx.nginx。在瀏覽器端配置完標題用戶以後,blog整個目錄下的文件均可以改root權限,可是wp-config.php文件必須是nginx.nginx權限。

 

  1. 在瀏覽器端輸入blog.etiantian.com建立用戶
  2. 把blog目錄下的文件改成root.root(wp-config.php除外)
  3. 在數據庫中把建立數據庫的權限回收:

mysql> use wordpress;

Database changed

mysql> revoke create on wordpress.* from wordpress@'localhost';

Query OK, 0 rows affected (0.00 sec)

 

做業:對wordpress的博客使用僞靜態

相關文章
相關標籤/搜索