CentOS7下安装nginx与php

        因为项目的需求需要在CentOS7的环境下安装nginx与php的组合。以前用的都是Apache+PHP组合,这次是个新的尝试。首先在网上搜了一些前辈的教程,照着一步一步做下来,可是安装的过程并不顺利,可能是因为每个人的系统多少会有些差异,我的系统始终无法运行起来。后来通过参考php官网的在线手册、章宴编写的《实战Nginx:取代Apache的高性能Web服务器》一书和网上的一些相关安装配置的帖子,终于成功运行。在这里将安装过程记录下来,以备以后查看。


一、软件下载

        1、nginx-1.10.2 Stable version

              http://nginx.org/download/nginx-1.10.2.tar.gz

        2、PHP 5.6.29 Stable version

              http://cn2.php.net/get/php-5.6.29.tar.gz/from/this/mirror

        因为系统将要部署在生产环境中,稳定最重要,所以用的都是 Stable 版的。

        将下载的文件放置到 /tmp 目录下


二、安装前的准备

        1、安装GCC编译器

              yum -y install gcc gcc-c++ autoconf automake

        2、安装第三方支持库

              yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

              yum -y install libxml2*

        3、创建供nginx和php使用的帐号“www”

              adduser www


三、nginx安装

        1、解压缩

              tar zxvf nginx-1.10.2.tar.gz

        2、编译安装

              cd /tmp/nginx-1.10.2    

       ./configure --user=www --group=www 
           --with-http_stub_status_module 
           --with-http_ssl_module
          --with-http_flv_module
          --with-http_mp4_module
          --add-module=../nginx-rtmp-module-master
           --with-debug
       (注1:默认安装路径为 /usr/local/nginx,如需自定义路径可将此行改为: ./configure --prefix=<目标路径>)
        (注2:--user=www --group=www 为nginx使用的帐号和用户组,可根据实际情况改变)
            (注3:nginx-rtmp-module-master 是流媒体服务第三方模块,需要先下载解压,在编译nginx时,要指出正确的路径)
 

              make

              make install   (注:需要有管理员权限)


四、PHP安装

            1、解压缩
                  tar zxvf php-5.6.29.tar.gz

            2、编译安装
                  cd /tmp/php-5.6.29
                  ./configure --prefix=/usr/local/php --enable-fpm --with-mysql --with-mysqli --enable-mbstring=all
 
                  make
                  make install (注:需要有管理员权限)

            3、复制配置文件
                  cp php.ini-development /usr/local/php/php.ini
                  cd /usr/local/php/etc
                  cp php-fpm.conf.default php-fpm.conf


五、编辑配置文件

         1、编辑php.ini
               vim /usr/local/php/php.ini
              定位到 cgi.fix_pathinfo= 并将其修改为: cgi.fix_pathinfo=0

          2、编辑php-fpm.conf,确保 php-fpm 模块使用www用户和 www组的身份运行。
              vim /usr/local/php/etc/php-fpm.conf
               找到以下内容并修改:

; Unix user/group of processes

; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www
group = www

              找到以下内容:
                      ;pid = run/php-fpm.pid
               将前面的“;”去掉


         3、配置 Nginx 使其支持 PHP 应用:

               vim /usr/local/nginx/conf/nginx.conf

               (1) 修改默认的 location 块,使其支持 .php 文件:

location / {
root /home/www;
index index.php index.html index.htm;
}

              (2) 下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~* \.(php|php5)?$ {
root /home/www;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fcgi.conf;
}
        
         4、创建fcgi.conf

               vim /usr/local/nginx/conf/fcgi.conf
                在此文件中添加如下内容:

       fastcgi_param GATEWAY_INTERFACE CGI/1.1;
       fastcgi_param SERVER_SOFTWARE   nginx;

       fastcgi_param QUERY_STRING      $query_string;
       fastcgi_param REQUEST_METHOD    $request_method;
       fastcgi_param CONTENT_TYPE      $content_type;
       fastcgi_param CONTENT_LENGTH    $content_length;

       fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
       fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
       fastcgi_param REQUEST_URI       $request_uri;
       fastcgi_param DOCUMENT_URI      $document_uri;
       fastcgi_param DOCUMENT_ROOT     $document_root;
       fastcgi_param SERVER_PROTOCOL   $server_protocol;

       fastcgi_param REMOTE_ADDR       $remote_addr;
       fastcgi_param REMOTE_PORT       $remote_port;
       fastcgi_param SERVER_ADDR       $server_addr;
       fastcgi_param SERVER_PORT       $server_port;
       fastcgi_param SERVER_NAME       $server_name;

       #PHP only, required if PHP was built with --enable-force-cgi-redirect
       fastcgi_param REDIRECT_STATUS   200;

 

六、其他              1、将 Nginx 添加到系统服务中,并设置开机启动                创建脚本文件                vim /etc/init.d/nginx                在脚本文件中添加如下内容

#!/bin/sh
# nginx - this script starts and stops the nginx daemin

# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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"

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
}

restart() {
configtest || return $?
stop
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 755 /etc/init.d/nginx

              脚本编辑完成后,可以能够启动、停止、重载了
                  /etc/init.d/nginx start 启动
                  /etc/init.d/nginx stop 停止
                  /etc/init.d/nginx reload 重载
              这个命令比较多,可以输入 /etc/init.d/nginx 进行查看

              设置开机启动
                  /sbin/chkconfig nginx on


         2、将 php-fpm 添加到系统服务中,并设置开机自启动

               拷贝文件
                  cp /tmp/php-5.6.29/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm

              设置权限
                 chmod 755 /etc/init.d/php-fpm

              确认/usr/local/php/etc/php-fpm.conf 文件中 pid=run/php-fpm.pid 这行前面的“;”已经删除

              打开脚本文件
                  vim /etc/init.d/php-fpm

              找到并编辑以下内容

prefix=
exec_prefix=
php_fpm_BIN=/usr/local/php/sbin/php-fpm
php_fpm_CONF=/usr/local/php/etc/php-fpm.conf
php_fpm_PID=/usr/local/php/var/run/php-fpm.pid

              脚本编辑完成后,可以能够启动、停止、重载了
                 /etc/init.d/php-fpm start 启动
                 /etc/init.d/php-fpm stop 停止
                 /etc/init.d/php-fpm reload 重载

             设置开机启动
                /sbin/chkconfig php-fpm on
        以上是基本的安装配置过程,在整理过程中难免有疏漏,发现问题及时改正吧。虽然运行环境
已经搭建起来了,但要投入实际使用,这些还远远不够,还有很多工作需要做。目前对这些配置文件
还有很多地方不明白,继续学习吧,加油!
------------------------------------------------------------------------------------
附上完整的nginx.conf配置文件
user www www;worker_processes 4;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;pid
logs/nginx.pid;worker_rlimit_nofile 65535;
events {
use epoll; worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_names_hash_bucket_size 128; client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
access_log /data/logs/nginx/access.log access;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
access_log /data/logs/nginx/host.access.log access;
location / {
root /home/www;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/www;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~* \.(php|php5)?$ {
root /home/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
include fcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht {
# deny all;
#}
location ~* \.(gif|jpg|jpeg|png|bmp|swf)$ {
root /home/www;
expires 30d;
}
location ~* /.(js|css)?$ {
root /home/www;
expires 1h;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration

#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / { # root html;
# index index.html index.htm;
# }
#}
# HTTPS server

#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
} 




 
 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值