上一篇介紹
如何在 Ubuntu 底下安裝 Nginx + PHP FastCGI,這次來紀錄如何安裝在 Fedora 系統,目前環境是使用
Amazon Linux AMI,如果有在玩
AWS EC2 或是
Fedora Linux 的話,對這 OS 就不會很陌生了。
安裝 Nginx
用 yum 升級系統所有套件,再安裝
Nginx +
PHP 環境
$ yum update
$ yum install nginx php-cli php spawn-fcgi
將 Nginx 加入開機自動執行,並且啟動它
$ chkconfig --level 35 nginx on
$ service nginx start
設定 Nginx
Nginx 設定檔預設放在 /etc/nginx/nginx.conf,如果看過此設定檔你會發現跟 Ubuntu 安裝不同的地方在於這裡沒有
sites-available 跟
sites-enabled 目錄,當然我們可以自行去設定
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
接著修改
/etc/nginx/nginx.conf 加入底下設定
# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;
如果覺得這步驟很麻煩的話,可以直接修改
/etc/nginx/conf.d/default.conf,這檔案就是 Nginx 預設的 Virtual Host 設定,如果要增加網站,就可以直接建立新檔案
/etc/nginx/conf.d/www.example.com.conf,接下來設定單一網站,設定檔內容如下
server {
server_name www.example.com;
listen 8090;
access_log /var/log/nginx/example/logs/access.log;
error_log /var/log/nginx/example/logs/error.log;
root /home/www/appleboy;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/appleboy$fastcgi_script_name;
}
}
存檔後,記得建立 logs 目錄,Nginx 不會自動幫忙建立
mkdir -p /var/log/nginx/example/logs/
如果您想跟 Ubuntu 一樣的建立 Virtual Host,請執行底下步驟
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
最後重新啟動 Nginx
service nginx restart
建立 PHP Fast CGI 環境
我們透過 spawn-fcgi 來管理 CGI process,避免 PHP5-CGI 突然掛掉。首先建立 /usr/bin/php-fastcgi 檔案,內容如下
#!/bin/sh
/usr/bin/spawn-fcgi -P /var/run/php-cgi.pid -a 127.0.0.1 -p 9000 -C 15 -u nginx -g nginx -f /usr/bin/php-cgi
建立 /etc/rc.d/init.d/php-fastcgi 開機執行 php fastcgi
#!/bin/sh
#
# php-fastcgi - Use PHP as a FastCGI process via nginx.
#
# chkconfig: - 85 15
# description: Use PHP as a FastCGI process via nginx.
# processname: php-fastcgi
# pidfile: /var/run/php-cgi.pid
# modified: Bo-Yi Wu
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
phpfastcgi="/usr/bin/php-fastcgi"
prog=$(basename php-cgi)
pidfile="/var/run/${prog}.pid"
lockfile="/var/lock/subsys/${prog}"
STOP_TIMEOUT="10"
start() {
[ -x $phpfastcgi ] || exit 5
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} ${phpfastcgi}
retval=$?
echo
[ $retval -eq 0 ] && touch ${lockfile}
return $retval
}
stop() {
echo -n $"Stopping ${prog}: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} ${prog}
retval=$?
echo
[ $retval -eq 0 ] && rm -rf ${lockfile} ${pidfile}
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $prog -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
status)
status -p ${pidfile} $prog
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
exit 2
esac
更改
/etc/rc.d/init.d/php-fastcgi 檔案權限
chmod +x /etc/rc.d/init.d/php-fastcgi
加入開機自動執行
$ chkconfig --add php-fastcgi
$ chkconfig php-fastcgi on
$ /etc/init.d/php-fastcgi start
打開瀏覽器看有沒有正確看到
phpinfo();
Related