Nginx和PHP-FPM编译安装详解

in 互联网技术 with 2 comments  访问: 18,089 次

一直以来我都通过网上的一些材料去搭建lnmp环境,通过直接yum安装nginx mysql php等软件。但是为了原生态的编译安装最新的软件版本,我决定自己亲手搭建lnmp环境,采用最新的nginx1.9.7(昨天出了1.9.8)和php7来研究如何搭建起nginx最新版本和php7的环境。

1. Nginx编译安装

1.1 编译环境准备

在linux使用make方式安装,需要保证linux已经具备比较OK的编译环境,例如gcc等编译工具。一般而言,服务器提供商在安装的系统中已经默认集成了这些软件,但是为了保险起见,我们还是通过一些较为基础的方式,把这些依赖包都跑一遍,以防在之后的编译中出差错。

$ yum -y install gcc gcc-c++ autoconf automake libtool make cmake
$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

zlib: 为nginx提供gzip模块,需要zlib库支持
openssl: 为nginx提供ssl功能
pcre: 为支持地址重写rewrite功能

1.2 创建用来运行nginx的用户及组

我们创建一个新的用户和用户组来运行nginx,这样可以把nginx和root分开,保证nginx不具备root权限。但是,我们并不希望nginx成为一个真实的可以登陆到远程进行操作的用户,所以,我们并不给它创建家目录,在useradd的时候,用-M参数:

groupadd nginx
useradd -g nginx -M nginx
usermod -s /sbin/nologin nginx

-g参数为nginx用户指定了一个组。-M参数保证其不自动生成home目录。
usermode -s 修改用户登录shell

1.3 编译安装Nginx

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1

编译参数:

./configure --prefix=/data/app/nginx \
--pid-path=/data/pid/nginx.pid \
--with-http_ssl_module \
--with-threads \
--with-stream --with-stream_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre \
--http-log-path=/data/logs/nginx/access.log \
--error-log-path=/data/logs/nginx/error.log 

make 
make install

make的地方有一个小技巧,如果服务器是双核,可以通过-j2来指定用双核进行编译,-j4代表4核编译。

安装到这里就结束了,但是,安装完可没完事儿,nginx还没有运行起来,你可以先去看看安装的结果,并且运行nginx服务器:

$ cd /data/app/nginx

$ vim /etc/init.d/nginx 
#!/bin/bash
#
# nginx     Start up the Nginx Web Server daemon
#
# chkconfig: 2345 55 25
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /data/app/nginx/conf/nginx.conf
# pidfile: /data/pid/nginx.pid

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

EXEC="/data/app/nginx/sbin/nginx"
CONFIG="/data/app/nginx/conf/nginx.conf"
PID_FILE="/data/pid/nginx.pid"

configtest ()
{
    $EXEC -t -c $CONFIG
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        exit $RETVAL
    fi
}

rh_status() {
    status -p $PID_FILE nginx
}

case $1 in 
    'stop'|'STOP')
        $EXEC -s stop
    ;;
    'start'|'START')
        configtest
        $EXEC
    ;; 
    'restart'|'RESTART')
                $EXEC -s stop
                usleep
                configtest
                $EXEC
    ;;
        'status'|'STATUS')
        rh_status
    ;;
    'reload'|'RELOAD')
        $EXEC -s reload
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|configtest|status}"
esac

$ /etc/init.d/nginx start

$ chmod +x /etc/init.d/nginx
$ chkconfig --add nginx && chkconfig nginx on

这样就运行起来了,访问你的服务器ip,看看能否看到ngin的欢迎页面吧。(不要让其他软件占用80端口哦)默认情况下网页文件放在/usr/local/nginx/html下,不符合我们的使用习惯,这个需要修改nginx的配置文件来修改,不过即使不修改,我们也是可以正常使用的,我们就不详细解释nginx的配置了。

2. PHP7编译安装

2.1 安装依赖环境

惯例是先解决一些编译的依赖包

yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

2.2 编译安装php7

wget https://www.php.net/distributions/php-7.2.26.tar.gz
tar zxf php-7.2.26.tar.gz 
cd php-7.2.26
./configure --prefix=/data/app/php7 \
--with-config-file-path=/data/app/php7/etc \
--with-config-file-scan-dir=/data/app/php7/etc/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp  \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache

配置无误后执行:

make
make install

同样可以使用-j2哦。如果安装成功,OK,那么php7的安装就OK了。

2.3 调整php配置

默认安装好之后,你会发现/data/app/php7/etc下面没有php.ini文件,这个去哪里要呢?在php7的源码安装包都有。

$ cd /usr/local/src/php-7.2.26
$ ls

可以看到有两个php.ini-xxx文件,我们可以分别vi打开来看下,一个是产品模式,一个是开发模式。

$ cp php.ini-production /data/app/php7/etc/php.ini
$ vi /data/app/php7/etc/php.ini

可以看到php的配置。本文就不做过多的配置解释了。

2.4 启用php-fpm服务

上面我们在编译php7的时候,已经将fpm模块编译了,那么接下来,我们要启用php-fpm。但是默认情况下它的配置文件和服务都没有启用,所以要我们自己来搞定。

搞定配置文件:

$ cd /data/app/php7/etc
$ mv php-fpm.conf.default php-fpm.conf
$ mv php-fpm.d/www.conf.default php-fpm.d/www.conf

php-fpm的具体配置我们也不做深入去详解,因为在编译之前./configure的时候,我们都已经确定了一些配置,比如运行fpm的用户和用户组之类的,所以默认配置应该不会存在路径问题和权限问题。

搞定php-fpm的服务载入:

就像上面的nginx一样,我们希望使用service php-fpm start|stop|restart这些操作来实现服务的重启,但没有像nginx那么复杂,php编译好之后,给我们提供了一个php-fpm的程序,不需要我再编写分享了。这个文件放在php编译源码目录中:

$ cd /usr/local/src/php-7.2.26/sapi/fpm/
$ cp init.d.php-fpm /etc/init.d/php-fpm
$ chmod +x /etc/init.d/php-fpm
$ chkconfig --add php-fpm
$ chkconfig php-fpm on

通过上面这个操作,我们就可以使用sevice php-fpm start来启用php-fpm了。用ps -ef | grep php-fpm看看进程吧。

3. nginx代理php实现typecho访问

通过上面的操作,nginx和php-fpm服务都被我们跑起来了,但是php-fpm走的是127.0.0.1:9000,外网是无法访问的,而且我们也不可能直接通过php-fpm给外网提供服务,我们用nginx去代理9000端口执行php。

实际上这个过程只需要对nginx进行配置即可,fpm已经在后台运行了,我们需要在nginx的配置文件中增加代理的规则,即可让用户在访问80端口,请求php的时候,交由后端的fpm去执行,并返回结果。

配置Nginx配置文件:

# vim /data/app/nginx/conf/nginx.conf

worker_processes  1;
worker_rlimit_nofile 1024;

events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    error_page 404 /404.html;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    sendfile        on;
    server_tokens  off;
    tcp_nopush     on;
    keepalive_timeout  65;

    limit_req_zone $binary_remote_addr zone=one:3m rate=1r/s;

    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  text/javascript;
    gzip_vary on;

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_temp_path /data/tmp/nginx/temp;
    proxy_cache_path /data/tmp/nginx/cache levels=1:2 keys_zone=cache_one:128m inactive=30m max_size=256m;
    proxy_read_timeout 1200;

    log_format access '$remote_addr $remote_user [$time_local] "$request" $status '
    'Upstream: $upstream_addr '
    'ups_resp_time: $upstream_response_time '
    'request_time: $request_time';

    server {
        error_page  404              /404.html;
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        server_name _;
        return 403;
    }

    include pools/*.conf;
    include vhost/*.conf;
}

配置VHOST:

server {
    listen 80;
    server_name fashengba.com www.fashengba.com;
    index index.html index.htm index.php;
    root /data/codes/fashengba;

    if ($request_method ~* TRACE|TRACK|BOGUS) {
        return 400;
    }

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

    location ~ .+\.php(/.*)?$ {
        fastcgi_split_path_info ^(.+\.php)(/.*)?$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
    }

    location ~.*\.(htm|html|js|css|gif|jpg|jpeg|png|bmp|ico|swf|flv)$ {
        proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_cache cache_one;
        proxy_cache_valid 200 304 15m;
        proxy_cache_valid 301 302 10m;
        proxy_cache_valid any 1m;
        proxy_cache_key $host$uri$is_args$args;
        expires 30m;
    }

    location ~ /\.ht {
        deny all;
    }

    access_log /data/logs/nginx/fsb_access.log;
    error_log /data/logs/nginx/fsb_error.log error;
}

4. 配置Nginx工作目录和重启

mkdir -pv /data/tmp/nginx/{temp,cache}
mkdir -pv /data/logs/nginx/
chown -R nginx.nginx /data/tmp/nginx/{temp,cache} /data/logs/nginx/
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart

typecho rewrite文章参考: https://www.jianshu.com/p/7968d1405836

WeZan