搭建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();
 
?>

转载于:https://blog.51cto.com/weimouren/1731791

搭建Nginx+PHP环境相关推荐

  1. 使用docker快速搭建nginx+php环境

    2019独角兽企业重金招聘Python工程师标准>>> 简介: 经过了繁琐的docker环境安装,看了下镜像/容器的简单使用,开始进行nginx+php环境的搭建,本文记录一下在安装 ...

  2. docker php nginx,使用docker快速搭建nginx+php环境

    经过了繁琐的docker环境安装,看了下镜像/容器的简单使用,开始进行nginx+php环境的搭建,本文记录一下在安装过程中的笔记.

  3. nginx php 文件未找到,Linux下搭建nginx+php环境的file not found问题反省

    先总结下这个问题出现的提示 访问php页面显示file not found 查看error.log日志文件提示"Primary script unknown" while read ...

  4. (7)nginx: 搭建 nginx+php环境

    这里要先声明一下,针对Nginx的PHP安装和针对apache的php安装是有区别的,因为Nginx中的php是以fastcgi的方式结合nginx的,可以理解为nginx代理了php的fastcgi ...

  5. 使用 docker 搭建 nginx+php-fpm 环境 (两个独立镜像)

    获取 nginx 镜像 docker search nginx docker pull nginx 使用nginx镜像开启 nginx 应用容器 docker run -d --name nginx ...

  6. docker搭建nginx+php环境

    一.docker拉取nginx和php镜像 命令如下: docker pull nginx:latest docker pull php:7.4-fpm 1.创建本地挂载路径: mkdir -p /d ...

  7. win7下搭建nginx+php的开发环境

    win7下搭建nginx+php的开发环境,需要的朋友可以参考下 一.下载相关软件 nginx下载:http://nginx.org/en/download.html nginx常见异常:http:/ ...

  8. Nginx+Tomcat搭建集群环境

    Nginx+Tomcat搭建集群环境 ZeroOne01关注0人评论33534人阅读2018-05-05 14:15:39 集群概述与架构介绍 Tomcat集群能带来什么: 提高服务的性能,例如计算处 ...

  9. win7 nginx php 环境,win7下docker环境搭建nginx+php-fpm+easyswoole+lavarel开发环境

    本篇文章将叙述如何在上述基础上搭建laravel开发环境,这个其实安装跟easyswoole有点类似: 1.先配置nginx, 详细代码如下 server { listen80; listen [:: ...

最新文章

  1. 选择创业项目的基础——适合自己的才是最好的
  2. linux终端信息读取,linux系统 如何通过终端查看信息
  3. ML之XGBoost:XGBoost参数调优的优秀外文翻译—《XGBoost中的参数调优完整指南(带python中的代码)》(二)
  4. Android 2D Graphic Architecture
  5. 列出我所知道的图像处理库
  6. [MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现
  7. 机械臂中的四元素转为旋转矩阵_雅克比矩阵(上)雅克比推导
  8. mysql 跨域_解决go echo后端处理跨域的两种操作方式
  9. c语言如何求一个数学表达式的值,浅谈C语言中表达式的求值
  10. laravel连接mysql8_php – 如何使用laravel运行MySql 8?
  11. python使用pip
  12. 社区团购小程序+界面diy+分销+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
  13. 《鹿鼎记》中归家三侠击杀的是真太后还是假太后
  14. graphs菜单_Graphs Made Easy-统计图绘制软件下载 v4.1.0.0 官方版 - 安下载
  15. 如何创建微信公众号订阅号以及认证服务号
  16. 基于 8051单片机的线跟随小车
  17. 世界杯10大影帝你造么?演技派的天下
  18. 什么是SOLID原则(第1部分)
  19. 【linux进阶4】apache的服务使用(图文巨详细解释apache的正向和反向代理)
  20. 脉冲多普勒雷达设计附matlab代码

热门文章

  1. Python提示ModuleNotFoundError: No module named ‘PIL‘,已解决
  2. python的zip()函数,压缩多个可迭代对象
  3. 6.java中什么是类_类、对象(java基础知识六)
  4. (补)20200105:整数转罗马数字
  5. java正则表达式提取需要的字符并放入数组
  6. 安全随笔1:谨慎一次MD5值的可被穷举性
  7. 超强干货!AI、Python、机器学习课程免费学,请尽快领取!
  8. 苹果传出放弃研发自动驾驶,因iPhone销量不佳收紧支出
  9. IBM新创AI病毒:想打谁就打谁,看脸发作绝不误伤,隐蔽性极强
  10. 史上最严重数据车祸:100+车厂机密全曝光,通用丰田特斯拉统统中招