在Ubuntu 12.04 安裝Nginx
發表於 : 2012-10-17 10:35:18
http://blog.tenyi.com/2012/10/ubuntu-12 ... %84Blog%29
昨天有個新聞,房價實價登錄網站一上線就爆了。雖然我很久沒有玩高流量網站,不過相信一般的IIS或Apache肯定是撐不住的,所以就來玩玩目前最夯的Nginx。
因為FreeBSD安裝實在太久了,所以我就用Ubuntu 12.04 LTS來做Server,安裝就不寫了,不會就甭玩。
因為Ubuntu預設的 /bin/sh 是 dash ,有時候會出問題,所以我都習慣換成 bash:
$sudo dpkg-reconfigure dash
如果有裝Apache 要先移掉:
$sudo apt-get remove apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapache2-mod-fastcgi
如果不想移Apache就先停用:
$sudo /etc/init.d/apache2 stop
$sudo update-rc.d -f apache2 remove
再來安裝Nginx,好習慣是先更新repository,省得某些套件會找不到:
$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install nginx
接下來裝PHP及FastCGI,參考資料建議使用PHP-FPM及XCache,接下來我就一次裝完。
$sudo apt-get install fcgiwrap php5-fpm php5-xcache php5-mysql php5-pgsql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
至此套件都裝好了,可以設定Nginx,若有要支援https,得先改 /etc/nginx/nginx.conf
在 http {...} 區域內加上
## Detect when HTTPS is used
map $scheme $fastcgi_https {
default off;
https on;
}
再改 /etc/nginx/sites-available/default
在 server { ... } 區域內加上
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param HTTPS $fastcgi_https; # <-- 要用SSL得加上這行,並得改 nginx.conf
fastcgi_index index.php;
include fastcgi_params;
}
較複雜的版本
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTPS $fastcgi_https; # <-- 要用SSL得加上這行,並得改 nginx.conf
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
並同樣在 server {...} 內把 root /usr/share/nginx/www; 改成 root /var/www; 才會和原本Apache的目錄一致。再把 index index.html index.htm; 改成 index index.html index.htm index.php; 。server_name 也可改成你的server名稱。
最後重新啟動服務即可
$sudo /etc/init.d/php5-fpm restart
$sudo /etc/init.d/nginx restart
參考資料:
Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS
The Perfect Server - Ubuntu 12.04 LTS (nginx, BIND, Dovecot, ISPConfig 3)
昨天有個新聞,房價實價登錄網站一上線就爆了。雖然我很久沒有玩高流量網站,不過相信一般的IIS或Apache肯定是撐不住的,所以就來玩玩目前最夯的Nginx。
因為FreeBSD安裝實在太久了,所以我就用Ubuntu 12.04 LTS來做Server,安裝就不寫了,不會就甭玩。
因為Ubuntu預設的 /bin/sh 是 dash ,有時候會出問題,所以我都習慣換成 bash:
$sudo dpkg-reconfigure dash
如果有裝Apache 要先移掉:
$sudo apt-get remove apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapache2-mod-fastcgi
如果不想移Apache就先停用:
$sudo /etc/init.d/apache2 stop
$sudo update-rc.d -f apache2 remove
再來安裝Nginx,好習慣是先更新repository,省得某些套件會找不到:
$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install nginx
接下來裝PHP及FastCGI,參考資料建議使用PHP-FPM及XCache,接下來我就一次裝完。
$sudo apt-get install fcgiwrap php5-fpm php5-xcache php5-mysql php5-pgsql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
至此套件都裝好了,可以設定Nginx,若有要支援https,得先改 /etc/nginx/nginx.conf
在 http {...} 區域內加上
## Detect when HTTPS is used
map $scheme $fastcgi_https {
default off;
https on;
}
再改 /etc/nginx/sites-available/default
在 server { ... } 區域內加上
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param HTTPS $fastcgi_https; # <-- 要用SSL得加上這行,並得改 nginx.conf
fastcgi_index index.php;
include fastcgi_params;
}
較複雜的版本
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTPS $fastcgi_https; # <-- 要用SSL得加上這行,並得改 nginx.conf
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
並同樣在 server {...} 內把 root /usr/share/nginx/www; 改成 root /var/www; 才會和原本Apache的目錄一致。再把 index index.html index.htm; 改成 index index.html index.htm index.php; 。server_name 也可改成你的server名稱。
最後重新啟動服務即可
$sudo /etc/init.d/php5-fpm restart
$sudo /etc/init.d/nginx restart
參考資料:
Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS
The Perfect Server - Ubuntu 12.04 LTS (nginx, BIND, Dovecot, ISPConfig 3)