從以前到現在都是習慣用 Apache,我想滿多學 PHP 的人都是使用 Apache + Mysql + PHP

前陣子玩了一下 Nginx 也查了一下資料,深深的發現....... Nginx 才是王道呀!!

公司本身也是自學,自己在 Try 過 Sphinx (索引) 之後。自己也認為,一個 page 有許多的 SQL 真的是個很不優的做法。

註: Sphinx: http://sphinxsearch.com

漸漸的在公司首頁因為也是有大量 user 會連進來,在一次的改版中,將一些撈取資料的頁面改為靜態網頁。

有些人可能會問,靜態網頁這樣的話,資料就不即時了。

的確沒錯!! 但反過來問!?  試問一個網站難道東西會一直在 update 嗎?! 我相信也不是,就算是股市的那種高流量的頁面,我相信最優的做法也是,一個靜態 page 讓使用者來讀。

(若一個 query 來說,資料量少當然 ok 若你的資料量有成千上萬筆的時後呢?! 每秒有1萬的 query 我想 Mysql 就掛了吧!!)

原本是用 Apache 去跑這些靜態網頁,但漸漸讀到了 Nginx 的文章,發現!! 實際上就效能來看 Nginx 比 Apache 優太多了。

漸漸也放棄了 Aapche 這隻龐大的怪獸,投向 Nginx 的懷抱。

Nginx 與 Apache 的比較:  http://forums.knowledge.idv.tw/viewthread.php?tid=1453  (參考資料)

 

 

Nginx + MySQL + PHP + PHP-FPM + APC

前置:

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
libiconv-1.11.1.tar.gz
=> ./configure --prefix=/usr/local
=> make
=> make install 

wget http://linuxsoft.cern.ch/cern/slc43/i386/yum/updates/libpng-devel-1.2.7-1.el4.2.i386.rpm
libpng-1.2.7-1.el4.2.i386.rpm (預設)
=> rpm -Uvh libpng-devel-1.2.7-1.el4.2.i386.rpm

wget http://ftp.twaren.net/Linux/scientific/41/i386/SL/RPMS/libpng10-devel-1.0.16-1.i386.rpm
libpng10-1.0.16-1.i386.rpm (預設)
=> rpm -Uvh libpng10-devel-1.0.16-1.i386.rpm

wget http://linuxsoft.cern.ch/cern/slc4X/i386/yum/os/libjpeg-devel-6b-33.i386.rpm
libjpeg-6b-33.i386.rpm (預設)
=> rpm -Uvh libjpeg-devel-6b-33.i386.rpm

wget http://rpms.famillecollet.com/el4.i386/mhash-0.9.2-6.el4.remi.i386.rpm
wget http://rpms.famillecollet.com/el4.i386/mhash-devel-0.9.2-6.el4.remi.i386.rpm
=> rpm -Uvh mhash-0.9.2-6.el4.remi.i386.rpm
=> rpm -Uvh mhash-devel-0.9.2-6.el4.remi.i386.rpm


http://rpms.famillecollet.com/el4.i386/libmcrypt-2.5.7-3.el4.remi.i386.rpm
http://rpms.famillecollet.com/el4.i386/libmcrypt-devel-2.5.7-3.el4.remi.i386.rpm
=> rpm -Uvh libmcrypt-2.5.7-3.el4.remi.i386.rpm
=> rpm -Uvh libmcrypt-devel-2.5.7-3.el4.remi.i386.rp
m

wget http://rpms.famillecollet.com/el4.i386/mcrypt-2.6.4-3.el4.remi.i386.rpm
rpm -Uvh mcrypt-2.6.4-3.el4.remi.i386.rpm

wget ftp://mirror.switch.ch/pool/3/mirror/centos/4.8/os/i386/CentOS/RPMS/libc-client-devel-2002e-14.i386.rpm
libc-client-2002e-14.i386.rpm (預設)
rpm -Uvh libc-client-devel-2002e-14.i386.rpm

MySQL

wget http://mirror.provenscaling.com/mysql/community/source/5.1/mysql-5.1.28-rc.tar.gz

1. ./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-extra-charsets=all 
2. 配置
    groupadd mysql
    useradd -g mysql mysql
    chown mysql:mysql /usr/local/mysql -R
    cp /usr/local/mysql/share/mysql/my-medium.cnf /usr/local/mysql/my.cnf

    編輯 my.cnf

    [mysqld] 增加
    port = 3306
    socket  = /tmp/mysql.sock
    user = root
    pid-file = /usr/local/mysql/mysql.pid
    socket = /tmp/mysql.sock
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    tmpdir = /tmp

    [mysql] 裡增加
    socket = /tmp/mysql.sock

    槓掉
    #skip-networking

    ln -nfs /usr/local/mysql/my.cnf /etc/my.cnf
    mkdir /usr/local/mysql/data
    chown mysql:mysql /usr/local/mysql

3. 初始化
/usr/local/mysql/bin/mysql_install_db --defaults-file=/usr/local/mysql/my.cnf

 

4. 設定 root 密碼 (建議修改)
update mysql.user set password=PASSWORD('新密碼') where User='root';
flush privileges

# cp /usr/local/mysql/libexec/mysqld /etc/init.d/mysqld
# cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql.server

 

PHP + PHP-FPM

前置
yum install xorg-x11-devel 

wget http://museum.php.net/php5/php-5.2.8.tar.gz
wget http://php-fpm.org/downloads/php-5.2.8-fpm-0.5.10.diff.gz

gunzip php-5.2.8-fpm-0.5.10.diff.gz
patch -d php-5.2.8 -p1 < php-5.2.8-fpm-0.5.10.diff


'./configure' \
'--prefix=/usr/local/php5' \
'--with-mysql=/usr/local/mysql' \
'--with-iconv-dir=/usr/local' \
'--with-freetype-dir' \
'--with-jpeg-dir' \
'--with-png-dir' \
'--with-zlib' \
'--with-libxml-dir' \
'--with-gd' \
'--enable-xml' \
'--disable-debug' \
'--disable-rpath' \
'--enable-discard-path' \
'--enable-safe-mode' \
'--enable-bcmath' \
'--enable-shmop' \
'--enable-sysvsem' \
'--enable-inline-optimization' \
'--with-xmlrpc' \
'--with-mhash' \
'--with-curl' \
'--with-curlwrappers' \
'--enable-mbregex' \
'--enable-fpm' \
'--enable-fastcgi' \
'--enable-force-cgi-redirect' \
'--enable-mbstring' \
'--with-mcrypt' \
'--with-imap' \
'--with-imap-ssl' \
'--with-kerberos' \
'--with-openssl' \
'--with-xpm-dir=/usr/X11R6'

cp php.ini-dist /usr/local/php5/lib/php.ini

// 額外
wget http://mirror.trouble-free.net/sources/gd-2.0.33.tar.gz
gd-2.0.33.tar.gz
=>  ./configure --with-jpeg --with-png --with-zlib --with-freetype

wget http://image_magick.veidrodis.com/image_magick/ImageMagick-6.3.6-7.tar.gz
ImageMagick-6.3.6-7.tar.gz
=>  ./configure --prefix=/usr/local/ImageMagick

imagick-2.3.0.tar.gz
=>  /usr/local/php5/bin/phpize
=>  ./configure --with-php-config=/usr/local/php5/bin/php-config --with-imagick=/usr/local/ImageMagick


PHP-FPM

groupadd mysql
useradd -g mysql mysql

vi /usr/local/php5/etc/php-fpm.conf
63、66 需將 <!-- --> 刪除
51、52、63、66 add user and group

max_children => 100
rlimit_files => 4096

開啟 php-fpm
/usr/local/php5/sbin/php-fpm start


APC
文章來源: http://wiki.dreamhost.com/Pecl_APC

wget http://ftp.twaren.net/gnu/gnu/m4/m4-1.4.11.tar.gz
=> ./configure
=> make
=> make install 

cp apcinstall.sh /usr/local
/bin/sh apcinstall.sh

apc.so 會產生於 php5/lib/php/extensions/apc.so

vi php.ini
[apc]
extension="apc.so"

 

Nginx

前置
wget http://ftp.exim.llorien.org/pcre/pcre-8.00.tar.gz
=> ./configure
=> make
=> make install

wget http://nginx.org/download/nginx-0.9.5.tar.gz


'./configure' \
'--prefix=/usr/local/nginx' \
'--sbin-path=/usr/sbin/nginx' \
'--conf-path=/usr/local/nginx/conf/nginx.conf' \
'--user=www' \
'--group=www' \
'--with-http_ssl_module' \
'--with-http_stub_status_module' \
'--with-http_secure_link_module' \
'--with-http_realip_module' \
'--with-http_addition_module' \
'--with-http_sub_module' \
'--with-http_dav_module' \
'--without-http-cache' \
'--http-proxy-temp-path=/usr/local/nginx/proxy_temp' \
'--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp' \
'--with-sha1=/usr/lib'

vi /usr/local/nginx/conf/fastcgi_params
增加
astcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;


編輯 nginx.conf

nginx.conf 範例

user www;
worker_processes  1;

#error_log      /usr/local/nginx/logs/error.log debug;
pid             /var/run/nginx.pid;

events {
        worker_connections  1024;
        use epoll;
}

http {
        include         /usr/local/nginx/conf/mime.types;
        default_type    application/octet-stream;

        log_format      main    'remote_addr - remote_user [time_local] status '
                                '"request" body_bytes_sent "http_referer" '
                                '"http_user_agent" "http_x_forwarded_for"';
        #access_log     /usr/local/nginx/logs/access.log  main;

        sendfile        on;
        tcp_nopush      on;
        tcp_nodelay     on;

        gzip            on;
        gzip_types      text/css text/xml text/plain
                        image/gif image/jpeg image/png image/x-icon
                        application/x-javascript;

        server {
                listen          80;
                server_name     [domain];

                root            /usr/local/nginx/html;
                access_log      /usr/local/nginx/logs/access.log main;
                error_log       /usr/local/nginx/logs/error.log;


                index           index.php;
                #error_page      500 502 503 504 /50x.html;

                location / {


                }

                #location = /50x.html {
                #               root    /usr/local/nginx/50x_html;
                #}

                location /\.ht {
                                deny all;
                }
                location .*\.php? {
                                fastcgi_pass    127.0.0.1:9000;
                                fastcgi_index   index.php;
                                # modify to your server root or location that store php script
                                fastcgi_param   SCRIPT_FILENAME  /usr/local/nginx/htmlfastcgi_script_name;
                                fastcgi_param   PATH_INFO fastcgi_script_name;
                                include /usr/local/nginx/conf/fastcgi_params;
                }

                #location /NginxStatus {
                #               stub_status     on;
                #               access_log      /usr/local/nginx/logs/status.access.log;
                #               auth_basic      "NginxStatus";
                #}
        }
}

設定開機啟動
/usr/local/php5/sbin/php-fpm start
/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf