Nginx环境搭建步骤

1 篇文章 0 订阅
1 篇文章 0 订阅

Nginx环境centos搭建步骤

支持库准备

安装步骤
安装make
yum -y install gcc automake autoconf libtool make

安装g++

yum install gcc gcc-c++

通常我们需要先装pcre和zlib,前者为了重写rewrite,后者为了gzip压缩

先选定源代码目录
cd /usr/local/src
安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make
make install

安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install

安装ssl(某些vps默认没装SSL)

cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1i.tar.gz
tar -zxvf openssl-1.0.1i.tar.gz

nginx安装

nginx编译安装

nginx一般有两个版本,分别是稳定版和开发版,可以根据目的选择其中一个,下面是把nginx安装到/usr/local/nginx目录下的详细步骤

cd /usr/local/src
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure –sbin-path=/usr/local/nginx/sbin/nginx \
–conf-path=/usr/local/nginx/conf/nginx.conf \
–pid-path=/usr/local/nginx/logs/nginx.pid \
–with-http_ssl_module \
–with-pcre=/usr/local/src/pcre-8.35 \
–with-zlib=/usr/local/src/zlib-1.2.8 \
–with-openssl=/usr/local/src/openssl-1.0.1i \
–with-http_stub_status_module
make
make install

–with-pcre=/usr/src/pcre-8.35 指的是pcre-8.35 的源码路径。
–with-zlib=/usr/src/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。

安装成功后 /usr/local/nginx 目录下如下(其他版本有差)
fastcgi.conf koi-win nginx.conf.default
fastcgi.conf.default logs scgi_params
fastcgi_params mime.types scgi_params.default
fastcgi_params.default mime.types.default uwsgi_params
html nginx uwsgi_params.default
koi-utf nginx.conf win-utf

nginx启动

确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,

netstat -ano|grep 80

如果查不到结果就执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,不然只能在前台运行)

sudo /usr/local/nginx/nginx
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。


到这里nginx就安装完成了,如果只是处理静态html就不用继续安装了

nginx编译选项说明

make是用来编译的,它从Makefile中读取指令,然后编译。
make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
configure命令是用来检测你的安装平台的目标特征的。它定义了系统的各个方面,包括nginx的被允许使用的连接处理的方法,比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本,执行结束时,它会创建一个Makefile文件。nginx的configure命令支持以下参数:

  • –prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/nginx。
  • –sbin-path=path 设置nginx的可执行文件的路径,默认为 prefix/sbin/nginx.
  • –conf-path=path 设置在nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf.
  • –pid-path=path 设置nginx.pid文件,将存储的主进程的进程号。安装完成后,可以随时改变的文件名 , 在nginx.conf配置文件中使用 PID指令。默认情况下,文件名 为prefix/logs/nginx.pid.
  • –error-log-path=path 设置主错误,警告,和诊断文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默认情况下,文件名 为prefix/logs/error.log.
  • –http-log-path=path 设置主请求的HTTP服务器的日志文件的名称。安装完成后,可以随时改变的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默认情况下,文件名 为prefix/logs/access.log.
  • –user=name 设置nginx工作进程的用户。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的用户名是nobody。
  • –group=name 设置nginx工作进程的用户组。安装完成后,可以随时更改的名称在nginx.conf配置文件中 使用的 user指令。默认的为非特权用户。
  • –with-select_module –without-select_module 启用或禁用构建一个模块来允许服务器使用select()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
  • –with-poll_module –without-poll_module 启用或禁用构建一个模块来允许服务器使用poll()方法。该模块将自动建立,如果平台不支持的kqueue,epoll,rtsig或/dev/poll。
  • –without-http_gzip_module — 不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib库。
  • –without-http_rewrite_module 不编译重写模块。编译并运行此模块需要PCRE库支持。
  • –without-http_proxy_module — 不编译http_proxy模块。
  • –with-http_ssl_module — 使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的。
  • –with-pcre=path — 设置PCRE库的源码路径。PCRE库的源码(版本4.4 - 8.30)需要从PCRE网站下载并解压。其余的工作是Nginx的./ configure和make来完成。正则表达式使用在location指令和 ngx_http_rewrite_module 模块中。
  • –with-pcre-jit —编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
  • –with-zlib=path —设置的zlib库的源码路径。要下载从 zlib(版本1.1.3 - 1.2.5)的并解压。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模块需要使用zlib 。
  • –with-cc-opt=parameters — 设置额外的参数将被添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:–with-cc-opt=”-I /usr/local/include。.如需要需要增加 select()支持的文件数量:–with-cc-opt=”-D FD_SETSIZE=2048”.
  • –with-ld-opt=parameters —设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:–with-ld-opt=”-L /usr/local/lib”.

php-fpm安装配置

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx。本文以php-fpm为例介绍如何使nginx支持PHP

编译安装php-fpm

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.
PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。
新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的。

安装前准备

yum -y install glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel \
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

新版php-fpm安装

wget http://cn2.php.net/distributions/php-5.6.1.tar.gz
tar zvxf php-5.6.1.tar.gz
cd php-5.6.1
./configure –prefix=/usr/local/php –enable-fpm –with-mcrypt \
–enable-mbstring –disable-pdo –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 –with-freetype-dir=/usr/include/freetype2/freetype/ –with-config-file-path=/usr/local/php/lib –with-openssl
make all install

安装后内容放在/usr/local/php目录下
完成了php-fpm的安装

php-fpm配置

下面是对php-fpm运行用户进行设置

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改

user = www-data
group = www-data

如果www-data用户不存在,那么先添加www-data用户

groupadd www-data
useradd -g www-data www-data

nginx配置

修改nginx配置文件以支持php-fpm
nginx安装完成后,修改nginx配置文件为,nginx.conf
其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
备选配置
1.http下增加如下内容
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;   
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;   
fastcgi_cache TEST;   
fastcgi_cache_valid 200 302 1h;   
fastcgi_cache_valid 301 1d;   
fastcgi_cache_valid any 1m; 

下面是对上述代码的含义进行介绍。

  • 第一行代码是为FastCGI缓存指定一个文件路径、目录结构等级、关键字区域存储时间和非活动删除时间。
  • fastcgi_connect_timeout:指定连接到后端FastCGI的超时时间。
  • fastcgi_send_timeout:指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间。
  • fastcgi_read_timeout:指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间。
  • fastcgi_buffer_size:用于指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为fastcgi_buffers选项指定的缓冲区大小。
  • fastcgi_buffers:指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“16 16k”、“4 64k”等。
  • fastcgi_busy_buffers_size:的默认值是fastcgi_buffers的两倍。
  • fastcgi_temp_file_write_size:表示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍。
  • fastcgi_cache:表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502错误的发生,但是开启缓存也会引起很多问题,要视具体情况而定。
  • fastcgi_cache_valid:fastcgi用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一个小时,将301应答缓存1天,其他应答均缓存1分钟。

: 2.server下配置如下内容

location ~* \.(php[3-9]?|phtm[l]?)(\/.*)*$ {
                fastcgi_index index.php;
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi_params;
                set $path_info "";
                set $real_script_name $fastcgi_script_name;
                if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                        set $real_script_name $1;
                        set $path_info $2;
                }
                fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                fastcgi_param SCRIPT_NAME $real_script_name;
                fastcgi_param PATH_INFO $path_info;
        }

这里的配置方式可以支持CI、ThinkPHP等基于PATH_INFO的请求方式。

还有,如果希望隐藏index.php,则在上面的配置下进行如下配置:

location /ICBBS {       
                if (!-e $request_filename){
                rewrite (.*) /ICBBS/index.php/$1;       
                }
        }

说明:ICBBS就是你的项目名称,这里将所有以ICBBS请求的不存在路径跳转到/ICBBS/index.php。

memcache

安装

执行

/usr/local/php/bin/pecl install memcache

当弹出

Enable memcache session handler support?[yes]:敲入yes

安装完毕后,记录生成的memcache.so的路径

php配置

将php源码目录下的php.ini-production复制到/usr/local/php/lib
注意修改

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226"

添加

extension=memcache.so

启动脚本配置

nginx

保存为/etc/init.d/nginx,修改权限为可执行

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
    start)
        echo -n "Starting $NAME... "

        if netstat -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if netstat -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
    ;;

    force-quit)
        echo -n "Terminating $NAME... "

        if ! netstat -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $SCRIPTNAME stop
        sleep 1
        $SCRIPTNAME start
    ;;

    reload)

        echo -n "Reload service $NAME... "

        if netstat -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can't reload."
            exit 1
        fi
    ;;

    configtest)

        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
    ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
    ;;

esac

php-fpm

保存为/etc/init.d/nginx,修改权限为可执行

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
    try=0

    while test $try -lt 35 ; do

        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;

            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac

        echo -n .
        try=`expr $try + 1`
        sleep 1

    done

}

case "$1" in
    start)
        echo -n "Starting php-fpm "

        $php_fpm_BIN --daemonize $php_opts

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi

        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -QUIT `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    reload)

        echo -n "Reload service php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -USR2 `cat $php_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload}"
        exit 1
    ;;

esac

脚本开启

chkconfig –add nginx
chkconfig nginx on
chkconfig –add php-fpm
chkconfig php-fpm on

php开启openssl的方法

php开启openssl的方法,大多数情况下openssl是没有开启的,要想启用需要进行下简单的设置:

windows下开启方法

  • 首先检查php.ini中;extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘;’, 如果不存在这行,那么添加extension=php_openssl.dll。
  • 将php文件夹下的: php_openssl.dll, ssleay32.dll, libeay32.dll 3个文件拷贝到 WINDOWS\system32\ 文件夹下。
  • 重启apache或者iis
    至此,openssl功能就开启了。

Linux下开启方法

cd /usr/local/src/php-5.6.1/ext/openssl/
/usr/local/php/bin/phpize

这里为你自己的phpize路径,如果找不到,使用whereis phpize查找
执行后,发现错误 无法找到config.m4 ,config0.m4就是config.m4。直接重命名

mv config0.m4 config.m4
/var/www/php5/bin/phpize
./configure –with-openssl –with-php-config=/var/www/php5/bin/php-config
make
make install

安装完成后,会返回一个.so文件(openssl.so)的目录。在此目录下把openssl.so 文件拷贝到你在php.ini 中指定的 extension_dir 下(在php.ini文件中查找:extension_dir =),我这里的目录是 /usr/local/php/lib/php/extensions
编辑php.ini文件,在文件最后添加

extension=openssl.so

重启nginx即可

目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值