Linux ·

编译LNMP之Nginx+php-fpm

本文目录:
1 编译nginx
2 编译php
3 配置nginx和php-fpm交互

 

1. 编译nginx

rpm包格式的nginx地址:http://nginx.org/packages/
源码包下载地址:http://nginx.org/en/download.html 。本文下载的是最新稳定版nginx-1.12.1。

shell> groupadd -r nginx
shell> useradd -r -g nginx nginx
shell> wget http://nginx.org/download/nginx-1.12.1.tar.gz
shell> tar xf nginx-1.12.1.tar.gz
shell> cd nginx-1.12.1
shell> ./configure \
  --user=nginx \
  --group=nginx \
  --prefix=/usr/local/nginx-1.12.1 \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/subsys/nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --with-pcre \
  --with-threads 

shell> make && make install
shell> ln -s /usr/local/nginx-1.12.0 /usr/local/nginx

./configure过程中,--with-XX_module的表示启用某模块即功能,--without-XX_module表示禁用模块即功能。在./configure --help的结果中,出现--with-XX_module的表示XX模块默认是禁用的,需要手动启用,出现--without-XX_module表示XX模块默认是启用的,需要手动禁用。以下是一些常见选项的说明:

--prefix=/usr/local/nginx-1.12.0             # 定义安装路径,不写时默认为/usr/local/nginx
--sbin-path=                                 # 定义应用程序存放路径,不写时默认为<prefix>/sbin/nginx
--conf-path=                                 # 定义配置文件路径,不写时默认为<prefix>/conf/nginx.conf
--error-log-path=/var/log/nginx/error.log    # 在配置文件中没有指定error log时的错误日志路径,不写时默认为<prefix>/logs/error.log
--http-log-path=/var/log/nginx/access.log    # 在配置文件中没有指定access log时的访问日志路径, 不写时默认为<prefix>/logs/access.log
--pid-path=/var/run/nginx/nginx.pid          # pid文件路径,没指定时默认为<prefix>/logs/nginx.pid
--lock-path=/var/lock/subsys/nginx           # 锁文件路径
--user=nginx                                 # 在配置文件中没有指定user指定时,worker进程的运行身份,不写时默认为nobody
--group=nginx                                # 在配置文件中没有指定user(不是group,配置文件中没有group指令)指定时,worker进程的运行组

--with-select_module       # 启用select方法模型,当找不到epoll时自动启用select
--without-select_module   
--with-poll_module         # 启用poll方法模型,当找不到epoll时自动启用poll
--without-poll_module  

--with-http_ssl_module            # 启用ssl功能
--with-http_flv_module            # 启用flv视频流功能
--with-http_stub_status_module    # 启用nginx状态监控功能,在启动后在浏览器使用root/status显示状态信息
--with-http_gzip_static_module    # 启用gzip压缩功能压缩web服务器响应客户端的响应报文
--http-client-body-temp-path=/var/tmp/nginx/client   # 定义客户端请求报文主体的临时文件存放路径,不写为<prefix>/client_body_temp
--http-proxy-temp-path=/var/tmp/nginx/proxy          # 定义从代理服务器收到的临时文件存放路径,不写为<prefix>/proxy_temp
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi         # 定义从fastcgi服务器收到的临时文件存放路径,不写为<prefix>/fastcgi_temp
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi          # 定义从uwsgi服务器收到的临时文件存放路径,不写为<prefix>/uwsgi_temp
--http-scgi-temp-path=/var/tmp/nginx/scgi            # 定义从scgi服务器收到的临时文件存放路径,不写为<prefix>/scgi_temp
--with-pcre                                          # 设置pcre库的路径,yum安装的pcre-devel可以不写路径
--with-threads                                       # 设置nginx支持多线程

在前面的编译选项中,安装路径使用了版本号,且未指定程序目录和配置文件路径,虽说提供了它们很方便,但是在升级nginx版本有些麻烦。所以,通过最后一步建立软链接的方式,让一切都变得简单,可以将新旧版本的nginx分离开来。这种方式安装nginx,配置文件默认为/conf/nginx.conf,应用程序路径为/sbin/nginx。

提供服务管理脚本/etc/rc.d/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

# 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)

sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run/nginx/nginx.pid"

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f $sysconfig ] && . $sysconfig


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 -p $pidfile $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest_q || return 6
    stop
    start
}

reload() {
    configtest_q || return 6
    echo -n $"Reloading $prog: "
    killproc -p $pidfile $prog -HUP
    echo
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}

configtest_q() {
    $nginx -t -q -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
    local oldbin_pidfile="${pidfile}.oldbin"

    configtest_q || return 6
    echo -n $"Upgrading $prog: "
    killproc -p $pidfile $prog -USR2
    retval=$?
    sleep 1
    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
        killproc -p $oldbin_pidfile $prog -QUIT
        success $"$prog online upgrade"
        echo 
        return 0
    else
        failure $"$prog online upgrade"
        echo
        return 1
    fi
}

# Tell nginx to reopen logs
reopen_logs() {
    configtest_q || return 6
    echo -n $"Reopening $prog logs: "
    killproc -p $pidfile $prog -USR1
    retval=$?
    echo
    return $retval
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest|reopen_logs)
        $1
        ;;
    force-reload|upgrade) 
        rh_status_q || exit 7
        upgrade
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status|status_q)
        rh_$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
        exit 2
esac

如果是systemd管理,则提供/usr/lib/systemd/system/nginx.service。

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.RedHat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /var/run/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

最后,将nginx命令加入环境变量。

shell> echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
shell> . /etc/profile.d/nginx.sh

 

2. 编译PHP

此处只给编译步骤,具体编译细节和编译选项说明见:编译php

yum install -y bzip2-level libmcrypt-devel openssl-devel libxml2-devel

tar xf php-5.5.38.tar.bz2 -C /tmp
cd /tmp/php-5.5.38
./configure --prefix=/usr/local/php --with-openssl --enable-mbstring --enable-sockets --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --with-zlib --with-mcrypt --with-bz2 --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-fpm

make
make install

# 提供php配置文件
cp php.ini-production /etc/php.ini

# 提供php-fpm服务管理脚本
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd
chmod +x /etc/init.d/phpfpmd

# 提供php-fpm配置文件
cd /usr/local/php/
cp etc/php-fpm.conf.default etc/php-fpm.conf

# 修改php-fpm配置文件(做实验的话改不改随意)
vim etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

# 启动php-fpm
service php-fpmd start

 

3. 配置nginx和php-fpm交互

在nginx配置文件中加入类似如下location容器。

location ~ \.php$ {
    root           /php/;
    fastcgi_pass   192.168.100.16:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

该location表示当请求的url能匹配以.php结尾时,将以该容器的指令进行处理。root指令将此容器的document_root设置为/php/,fastcgi_pass指令表示将该url请求代理至192.168.100.16主机上运行的php-fpm,由于此处设置了SCRIPT_FILENAME,所以当请求的uri为/a/a.php时,document_root=/php/,fastcgi_script_name=/a/a.php,这表示转发请求至192.168.100.16主机上的/php/a/a.php。所以,

参与评论