centos7安装php fastcgi,[CentOS]CentOS7安装Nginx+FastCGI教学

车峻熙
2023-12-01

今天来教大家如何在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.rpmrpm -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开放

祝大家玩得愉快

我经营的论坛: 台论之星

尘世中一位载浮载沉之小小工程师

 类似资料: