2011年8月3日 星期三

[Unix] 使用 Tarball 安裝 Nginx + PHP (FastCGI) + HTTPS @ FreeBSD 7

已經拖了將三過月的筆記,終於抽空寫完了。看了幾篇關於 Nginx 的文章,例如使用的資源比 Apache Web Server 少,以至於可以提供比 Apache 更多連線服務等。這次就順手安裝一下,雖然我對它還是不熟悉,就連 FastCGI 也是,只依稀知道要在 Apache Web Server 上跑 PHP,一種是用 mod_php 的方式,另一種好像就是 fastcgi 模式。網路上的文章是說 FastCGI 將 PHP 與 Web server 切開不綁在一起,使得 Web server 不會受 PHP 影響過深,也能達到節省資源。


此篇在 FreeBSD 7 環境下,採用 PHP-5.2.17、PHP-PFM 和 Nginx-1.0.5 並透過 tarball 方式進行安裝。可在到 Nginx 下載 nginx-1.0.5 stable version(Unix), 另外使用 PHP 5.2.17。而 FastCGI 的部份,有兩套管理方式,一種是使用 lighttpd 中的 spawn-fcgi ,另一種則是用 PHP-FPM ,在此選用 PHP-FPM 囉。


安裝:


下載 PHP-5.2.17:


> wget http://www.php.net/get/php-5.2.17.tar.gz/from/tw.php.net/mirror
> tar -xvf php-5.2.17.tar.gz


下載 PHP-PFM patch for PHP-5.2.17:


> wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
> gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1


下載 Nginx-1.05:


> wget http://nginx.org/download/nginx-1.0.5.tar.gz
> tar -xvf nginx-1.0.5.tar.gz


安裝 PHP (依需求使用,但至少要有 --enable-fastcgi --enable-force-cgi-redirect --enable-fpm 資訊)


> cd php-5.2.17
>  ./configure --prefix=/path/tarball --without-pear --with-openssl --enable-mbstring --with-curl --with-imap --with-mcrypt --with-imap-ssl --with-gd --enable-pcntl --enable-fastcgi --enable-force-cgi-redirect --enable-fpm
> make install


安裝 Nginx:


> cd nginx-1.0.5
> ./configure --prefix=/path/tarball/ --with-http_ssl_module --with-pcre
> make install


設定:


PHP-FPM:


設定要跑 fastcgi 使用的 port number,預設在 9000:


> vim /path/tarball/etc/php-fpm.conf
<value name="listen_address">127.0.0.1:9000</value>


啟動 PHP-FPM:


> /path/tarball/sbin/php-fpm start
Starting php_fpm  done


停止:


> /path/tarball/sbin/php-fpm stop


Nginx :


設定 PHP FastCGI:


> vim /path/conf/nginx.conf
location ~ \.php$ {
#proxy_pass   http://127.0.0.1;
include conf/fastcgi.conf;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
}


設定 HTTPS:


建立憑證:


> cd /path/tarball/conf
> openssl genrsa -des3 -out server.key 2048
> openssl req -new -key server.key -out server.csr
> openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
> openssl rsa -in server.key -out server.key


設定 nginx.conf 之 HTTPS 部分:


> vim /path/conf/nginx.conf

在 http {} 中增加 client_max_body_size 30m; 來解決 413 Request Entity Too Large 訊息

# HTTPS server
#
server {
listen       443;
server_name  localhost;

ssl                  on;
ssl_certificate      server.crt;
ssl_certificate_key  server.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers   on;

location / {
root   html;
index  index.html index.htm index.php;
}
        location ~ \.php$ {
            #proxy_pass   http://127.0.0.1;
            include fastcgi.conf;
            fastcgi_param  HTTPS on; 
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
        }   
}


啟動:


> /path/tarball/sbin/nginx


停止:


> /path/tarball/sbin/nginx -s stop


如此一來,可以在 /path/tarball/html 目錄中,擺一個 index.php 檔案,用 <?php phpinfo(); ?> 來測試 PHP 是否正常工作囉。


沒有留言:

張貼留言