砥砺前行,蜗牛也上网
VPS、独服专业分享站

手把手教你搭建nginx+php环境

本教程系统为centos7

nginx:

首先安装所需环境:

gcc:

yum install gcc-c++ -y

PCRE pcre-devel:

yum install -y pcre pcre-devel

zlib:

yum install -y zlib zlib-devel

OpenSSL:

yum install -y openssl openssl-devel

然后是nginx下载安装,你可以选择去官网https://nginx.org/en/download.html下载压缩包上传或直接

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

下载完后解压缩:

tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

再运行配置:

./configure

然后编译和安装

make & make install

无报错即表示安装成功,现在输入域名或IP即可看到nginx的界面了,对了,得先关闭下防火墙或设置规则

centOS6及以前版本使用命令:

systemctl stop iptables.service

centOS7关闭防火墙命令

systemctl stop firewalld.service

阻止防火墙自动启动:

systemctl disable firewalld.service

 

为了方便使用推荐把nginx注册为服务:

在/etc/init.d/下新建文件nginx,内容如下:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by caffreyxin at 2007.10.15.
# it is v.0.0.1 version.
# if you find any errors on this scripts, please contact caffreyxin.
# and send mail to xinyflove at sina dot com.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf</code>

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] &amp;&amp; exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {

if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] &amp;&amp; touch /var/lock/subsys/nginx
return $RETVAL
}

# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] &amp;&amp; rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}

# reload nginx service functions.
reload() {

echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo

}

# See how we were called.
case "$1" in
start)
start
;;

stop)
stop
;;

reload)
reload
;;

restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac

exit $RETVAL

保存退出

设置nginx权限:

chmod a+x /etc/init.d/nginx

注册为服务:

chkconfig --add nginx

设置开机启动:

chkconfig nginx on

重启系统测试nginx是否随机启动了。用

netstat -apn|grep nginx

查看。

为了能在任何路径直接执行nginx命令,可以把nginx的安装路径添加到环境变量中,我选择的是加入系统变量/etc/environment,命令如下:

PATH=/usr/local/nginx/sbin:$PATH

保存重启。
php安装:(我选的是5.6.35)

wget http://cn2.php.net/distributions/php-5.6.35.tar.gz
tar zvxf php-5.6.35.tar.gz
cd php-5.6.35

配置:

./configure –prefix=/usr/local/php –enable-fpm –with-mcrypt \
–enable-mbstring –enable-pdo –with-pdo-mysql –with-curl –disable-debug –disable-rpath \
–enable-inline-optimization –with-bz2 –with-zlib –enable-sockets \
–enable-sysvsem –enable-sysvshm –enable-pcntl –enable-mbregex \
–with-mhash –enable-zip –with-pcre-regex –with-mysql –with-mysqli \
–with-gd –with-jpeg-dir

无报错的话继续:

make all install

php配置:

cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

fpm测试php配置:

/usr/local/php/sbin/php-fpm -t

注册服务:

chkconfig --add php-fpm

设置开机启动:

chkconfig php-fpm on

启动php-fpm:

service php-fpm start
赞(0) 打赏
未经允许不得转载:上网的蜗牛 » 手把手教你搭建nginx+php环境

请不吝赐教 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址 (选填)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏