Nginx(發(fā)音為 “engine x”)是一款免費(fèi)、開(kāi)源、高性能的HTTP服務(wù)器。同時(shí)Nginx以穩(wěn)定、功能豐富、配置簡(jiǎn)單、資源消耗少著稱。這篇教程將會(huì)為你展示如何在一臺(tái) Fedora 12中安裝Nginx+MySQL+PHP5(FastCGI模式)
我已經(jīng)測(cè)試無(wú)誤,這將保證為你工作!
1 前言備注
在這篇教程中我使用的用戶名是server1.example.com,IP地址是192.168.0.100.這些設(shè)置可能與你的有所不同,因此 你需要在適當(dāng)?shù)牡胤叫薷囊幌?
2.安裝MySQL5
我們通過(guò)執(zhí)行下面的命令來(lái)安裝MySQL:
yum install mysql mysql-server
然后我們?yōu)镸ySQL創(chuàng)建系統(tǒng)啟動(dòng)連接(這樣的話,MySQL就會(huì)在系統(tǒng)啟動(dòng)的時(shí)候自動(dòng)啟動(dòng))并且啟動(dòng)MySQL服務(wù)器:
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
現(xiàn)在檢查是否支持網(wǎng)絡(luò)訪問(wèn),運(yùn)行:
netstat -tap | grep mysql
應(yīng)該顯示如下信息:
[root@server1 ~]# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 1376/mysqld
[root@server1 ~]#
如果不顯示,編輯/etc/my.cnf文件,并注釋掉skip-networking參數(shù):
[...]
#skip-networking
[...]
并重啟 MySQL 服務(wù)器:
/etc/init.d/mysqld restart
運(yùn)行
mysqladmin -u root password yourrootsqlpassword
mysqladmin -h server1.example.com -u root password yourrootsqlpassword
來(lái)為root用戶設(shè)置一個(gè)密碼(否則的話任何人都可以訪問(wèn)你的MySQL數(shù)據(jù)庫(kù)。。
3安裝 Nginx
Nginx是Fedora12的默認(rèn)包,我們可以通過(guò)下列命令安裝它:
然后我們?yōu)閚ginx創(chuàng)建一個(gè)系統(tǒng)啟動(dòng)鏈接,并啟動(dòng)它:
chkconfig --levels 235 nginx on
/etc/init.d/nginx start
在你的瀏覽器 中輸入你的服務(wù)器IP地址或者主機(jī)名(例如http://192.168.0.100),然后你就可以看到nginx的歡迎頁(yè)面:

4 安裝PHP5
我們可以讓PHP5在nginx中以FastCGI的模式工作。默認(rèn)情況下Fedora中沒(méi)有獨(dú)立的FastCGI deamon包,因此我們使用lighttpd的FastCGI包(lighttpd-FastCGI),并同時(shí)安裝php-cli和其他的PHP5模 塊,例如php-mysql,它可以使你的PHP腳本支持MySQL:
yum install lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mapserver php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
然后打開(kāi)/etc/php.ini文件,并在文件的最后加入這一行l(wèi)ine cgi.fix_pathinfo = 1:
[...]
cgi.fix_pathinfo = 1
我們可以使用Lighttpd-fastcgi包中自帶的/usr/bin/spawn-fcgi,啟動(dòng)FastCGI進(jìn)程。請(qǐng)參考
來(lái)學(xué)習(xí)更多的東西.
我們運(yùn)行下列命令可以啟動(dòng)一個(gè)監(jiān)聽(tīng)本地9000端口,并以nginx用戶和組運(yùn)行的PHP FastCGI后臺(tái):
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
當(dāng)然,你并不像每次啟動(dòng)的時(shí)候都手動(dòng)的輸入這些命令,因此你可以讓系統(tǒng)在啟動(dòng)時(shí)自動(dòng)執(zhí)行這些命令,打開(kāi)/etc/rc.local…
… 然后在文件的結(jié)尾添加下列命令:
[...]
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
5 配置nginx
現(xiàn)在我們打開(kāi)nginx的配置文件/etc/nginx/nginx.conf:
配置文件簡(jiǎn)單易懂
(你可以在下列網(wǎng)站學(xué)習(xí)更多的配置方法http://wiki.codemongers.com/NginxFullExample和http://wiki.codemongers.com/NginxFullExample2)
首先你可以增加worker process的數(shù)量和設(shè)置keepalive_timeout為一個(gè)合理值:
[...]
worker_processes 5;
[...]
keepalive_timeout 2;
[...]
虛擬主機(jī)定義在server{}容器中.我們使用下列命令修改默認(rèn)的虛擬主機(jī):
[...]
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[...]
server_name www.unixbar.net; 你可以在這里通過(guò)修改www.unixbar.net來(lái)確 定你的域名
在location /部分,我在index行加入了index.php。root /usr/share/nginx/html 意思是文檔路徑為/usr/share/nginx/html。
對(duì)于PHP來(lái)說(shuō)最重要的部分就是 location ~ \.php$ {}。取消它的注釋。改變r(jià)oot這一行為網(wǎng)站的文檔路徑。例如root /usr/share/nginx/html。請(qǐng)確保把fastcgi-param行修改成了fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;;否則得花PHP解析器將不會(huì)找到瀏覽器中調(diào)用的PHP.
現(xiàn)在我們保存文件并重啟nginx:
/etc/init.d/nginx stop
/etc/init.d/nginx start
(我不能使用/etc/init.d/nginx restart,因?yàn)檫@個(gè)命令只停止了nginx,但是無(wú)法啟動(dòng)它-不知道為什么…)
現(xiàn)在在文檔路徑root /usr/share/nginx/html創(chuàng)建下列PHP文件:
vi /usr/share/nginx/html/info.php
現(xiàn)在我們就可以在瀏覽器中通過(guò)http://192.168.0.100/info.php訪 問(wèn)了。

正如你在Server API這一行中所看到的一樣,PHP5現(xiàn)在已經(jīng)以FastCGI模式運(yùn)行了。如果你繼續(xù)向下翻看,你就能過(guò)看到PHP5所支持的模塊,其中就包括 MySQL模塊:
