Ubuntu下使用Nginx+PHP教程
Nginx收到了越来越多的欢迎,已经交广泛的取代了Apache的地位。
接下来已Ubuntu为例,写一个Nginx+php fastcgi指引。
1. Ubuntu 11.04
2. 首先卸载apache2,防止80端口冲突:
sudo apt-get remove apache2-mpm-prefork
3. 安装nginx, php-cgi, 顺便安装上php-apc
sudo apt-get install nginx php5-cgi php-apc
4. 建立/etc/default/php-fastcgi文件,设置fastcgi的启动属性 (若没有该文件,则建立)
vi /etc/default/php-fastcgi
START=yes
EXEC_AS_USER=www-data
FCGI_HOST=localhost
FCGI_PORT=9000
PHP_FCGI_CHILDREN=30
PHP_FCGI_MAX_REQUESTS=1000
参数说明:
本启动选线告知启动器,php执行用户为www-data(比较方便的是,保持其和nginx用户一致);监听本地9000端口,开启30个请求。每个cgi进程在响应1000次请求之后,释放其自身资源重新建立。
5. 建立fastcgi.conf文件。
luo@idooee:/etc/nginx$ sudo vi fastcgi.conf
#fastcgi.conf
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_connect_timeout 1800;
fastcgi_send_timeout 600;
fastcgi_read_timeout 1800;
#fastcgi_ignore_client_abort on;
# Vhosts fastcgi config
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
6. 建立cgi启动文件:
luo@idooee:/etc/init.d/$ sudo vi /etc/init.d/php-fastcgi
#!/bin/sh
### BEGIN INIT INFO
# Provides: php-fastcgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description: Start and stop php-cgi in external FASTCGI mode
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi in external FASTCGI mode"
NAME=php-fastcgi
DAEMON=/usr/bin/php-cgi
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
. /lib/lsb/init-functions
if [ "$START" != "yes" -a "$1" != "stop" ]; then
log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
exit 0
fi
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT"
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --background --make-pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- $DAEMON_ARGS || return 2
}
do_stop()
{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
rm -f $PIDFILE
return "$RETVAL"
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
7. 增加启动文件可执行权限。
luo@idooee:/etc/init.d/$ sudo chmod +x /etc/init.d/php-fastcgi
8. 启动fastcgi,并检查进程.
luo@idooee:/etc/default$ sudo /etc/init.d/php-fastcgi start
* Starting php-cgi in external FASTCGI mode php-fastcgi
...done.
luo@idooee:/etc/default$
luo@idooee:/etc/default$
luo@idooee:/etc/default$ ps aux | grep php
luo 3562 0.0 0.0 33540 3568 pts/2 S+ 14:50 0:00 vi php-fastcgi
www-data 3591 0.6 0.0 181368 8988 ? Ss 14:54 0:00 /usr/bin/php-cgi -q -b localhost:9000
www-data 3593 0.0 0.0 181368 3780 ? S 14:54 0:00 /usr/bin/php-cgi -q -b localhost:9000
www-data 3594 0.0 0.0 181368 3780 ? S 14:54 0:00 /usr/bin/php-cgi -q -b localhost:9000
www-data 3595 0.0 0.0 181368 3780 ? S 14:54 0:00 /usr/bin/php-cgi -q -b localhost:9000
9. 将php fastcgi作为开机自动启动
sudo update-rc.d php-fastcgi defaults
10. 设置站点
server {
listen 80;
server_name www.idooee.com;
root /data/idooee.com;
location / {
index index.html index.php; ## Allow a static html file to be shown first
#.....
}
location ~ .php$ { ## Execute PHP scripts
if ($request_uri ~ /(downloader|report)$){ # no trailing /, redirecting
rewrite ^(.*)$ $1/ permanent;
}
include /etc/nginx/fastcgi.conf;
}
}
11. 重启nginx, 完成。
sudo nginx -t && sudo /etc/init.d/nginx restart