今天來教大家如何在CentOS7中來安裝Nginx + FastCGI
首先呢~YUM好像並沒有把 Nginx 跟 php-fpm收錄進去
今天來教大家如何在CentOS7中來安裝Nginx + FastCGI
首先呢~YUM好像並沒有把 Nginx 跟 php-fpm收錄進去
所以直接執行 yum -y install nginx 是找不到任何東西的...
這邊我們先手動創建一個repo檔案,來讓yum可以正常的下載到東西並安裝
PS 以下的動作會使用到很多次root權限(sudo),所以嫌麻煩的可以先 su root 切換到root身分
PS2 CentOS的編輯器為vi,不熟的朋友可以先到這個網站學習一下: http://phys.thu.edu.tw/~ctshih/teach/vi/
1.
鍵入 sudo vi /etc/yum.repos.d/nginx.repo
輸入以下內容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
以上這段是官網PO出來的資訊
網址在這邊: http://wiki.nginx.org/Install
儲存
2.
鍵入
sudo yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers patch zip bison wget
安裝相關套件
3.
鍵入
rpm -Uvh http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo yum --enablerepo=remi install php php-fpm php-common
安裝PHP及相關套件
4.
鍵入 sudo yum -y install nginx
安裝 nginx 套件
PS 預設網站目錄為: /usr/share/nginx/html
5.
調整 Nginx 設定檔
鍵入 sudo vi /etc/nginx/conf.d/default.conf
加入以下內容
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
儲存
鍵入 sudo vi /etc/nginx/nginx.conf
加入以下內容到 http 的大括號內
server {
listen 80;
access_log /var/logs/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
}
6.
創建init script,不然無法用 service指令 來對 Nginx 做(啟動、停止、重新啟動....)操作
鍵入 sudo vi /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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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
儲存
鍵入
chmod +x /etc/init.d/nginx
PS 以上腳本由官網抓取,其他系統的腳本可以參考: http://wiki.nginx.org/InitScripts
7.
鍵入
sudo service nginx stop
sudo service nginx start
sudo service php-fpm stop
sudo service php-fpm start
===============================================================================
以上就已完成安裝步驟
試試看在 /usr/share/nginx/html 創建一個PHP檔案用瀏覽器瀏覽
PS 如果發現無法連線的情況可以看看 iptables 是不是有把80PORT開放
祝大家玩得愉快
我經營的論壇: 台論之星
塵世中一位載浮載沉之小小工程師