搭建Nginx+PHP環境
一. 源碼包編譯安裝部署web服務器
1.安裝nginx必須的依賴包
[root@test01 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel
2.安裝編譯nginx,目前系統測試環境爲CentOS6.3 軟件版本爲nginx-1.2.6
[root@test01 ~]# useradd nginx -s /sbin/nologin
[root@test01 ~]# tar zxvf nginx-1.2.6.tar.gz
[root@test01 ~]# cd nginx-1.2.6
[root@test01 nginx-1.2.6]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
// --with-http_stub_status_module 安裝允許狀態模塊
// --with-http_ssl_module 安裝ssl模塊
[root@test01 ~]# /usr/local/nginx/sbin/nginx -v //查看Nginx的相關環境配置信息是否正確
nginx version: nginx/1.2.6
[root@test01 ~]#
[root@test01 nginx-1.2.6]# make & make install //編譯安裝過程略
3.通過nginx自身腳本機器nginx服務器,並通過各種命令查看是否啓動成功
[root@test01 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf // -c指向nginx主配置文件
[root@test01 ~]# lsof -i :80 //查看nginx進程號及運行情況
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 21910 root 9u IPv4 262148 0t0 TCP *:http (LISTEN)
nginx 21911 nginx 9u IPv4 262148 0t0 TCP *:http (LISTEN)
[root@test01 ~]# ps -ef | grep nginx //查看nginx進程號及運行情況
root 21910 1 0 10:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 21911 21910 0 10:41 ? 00:00:00 nginx: worker process
root 21957 21755 0 10:42 pts/0 00:00:00 grep nginx
[root@test01 ~]# netstat -nltp | grep 80 //查看nginx進程監聽端口
[root@test01 ~]# netstat -an |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
4.通過windows服務器IE瀏覽器測試
擴展知識:
1)設置nginx開機自動啓動,將nginx啓動命令添加到/etc/rc.local
[root@test01 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local
[root@test01 ~]# cat /etc/rc.local | grep nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2)通過設置System V 腳本,使用server命令啓動nginx服務
[root@test01 init.d]# vim nginx //在/etc/init.d/下創建nginx啓動腳本文件
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@test01 init.d]# chmod 755 nginx //修改腳本文件nginx的權限
[root@test01 init.d]# chkconfig --add nginx //將腳本文件加入chkconfig中
[root@test01 init.d]# chkconfig --level 35 nginx on //設置nginx開機在3和5級別自動啓動
[root@test01 init.d]# chkconfig --list | grep nginx
nginx 0:off 1:off 2:off 3:on 4:off 5:on 6:off
測試nginx腳本文件是否能夠正常使用
[root@test01 init.d]# /etc/init.d/nginx restart //重新啓動
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@test01 init.d]# /etc/init.d/nginx reload //不間斷服務平滑重啓
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reloading nginx: [ OK ]
[root@test01 ~]# cat /usr/local/nginx/logs/nginx.pid //存儲了nginx運行的進程號
15799
[root@test01 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //不間斷服務重新啓動nginx
[root@test01 init.d]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
[root@test01 init.d]# killall -9 nginx //結束nginx的全部經常
[root@test01 html]# pwd //安裝nginx完成後,默認網站的路徑
/usr/local/nginx/html
[root@test01 html]# ll //可以查看到默認網站爲index.html,內容爲以上測試內容。
total 8
-rw-r--r--. 1 root root 537 Feb 27 11:40 50x.html
-rw-r--r--. 1 root root 612 Feb 27 11:40 index.html
二.安裝php環境
nginx目前還不能直接支持php,必須先藉助於fastcgi來驅動php。現在fastcgi較好的辦法有2種,一個是spawn-fcgi,另外一個就是php-fpm,一般來說可能php-fpm更強大一點.
由於PHP5.3版本以後就自帶php-fpm了,所以如果你用源碼安裝的話只需要enable fpm就可以了,下面來說說通過yum安裝php-fpm
開始安裝Nginx和PHP-FPM之前,你必須卸載系統中以前安裝的Apache和PHP。用root登錄輸入下面的命令:
yum remove httpd* php*
增加額外資源庫:
默認情況下,CentOS的官方資源是沒有php-fpm的, 但我們可以從Remi的RPM資源中獲得,它依賴於EPEL資源。我們可以這樣增加兩個資源庫:
yum install yum-priorities –y
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh epel-release-6-8.noarch.rpm
rpm -Uvh remi-release-6.rpm
安裝php,php-ftpm
yum --enablerepo=remi install php php-fpm
添加到系統自動運行
chkconfig --level 345 php-fpm on
PHP僅安裝了核心模塊,你很可能需要安裝其他的模塊,比如MySQL、 XML、 GD等等,你可以輸入下列命令:
yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt
第一次啓動php-fpm,輸入下列命令:
/etc/init.d/php-fpm start
三.配置PHP-FPM和Nginx
編輯Nginx的配置文件
vi /usr/local/nginx/conf/nginx.conf
修改如下:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
配置fastcgi
vi /usr/local/nginx/conf/fastcgi_params
添加如下行:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
添加php測試文件
vi /usr/local/nginx/html/index.php
添加以下內容:
<?php
phpinfo();
?>
測試結果: