CentOS 7.5 编译安装 Nginx 1.15.3

本文主要记录如何在CentOS 7.5中编译安装Nginx官方最新的1.15.3版本。由于像NginxMysqlPHP的的源码都是用C/C++写的,所以自己的CentOS 7.5服务器上必须要安装gccg++软件。

搭建LNMP环境一般是先安装Mysql/MariaDB, 再安装Nginx, 其次是安装PHP

安装环境
  • 系统:CentOS 7.5.1804
  • 软件:Nginx 1.15.3
  • 依赖软件:PcreZlibOpenssl
创建用户及用户组

先创建一个名为nginx没有登录权限的用户和一个名为nginx的用户组,然后安装nginx所需的依赖库和依赖包,最后通过.configure进行安装的详细配置。

  • 创建Nginx用户组
> 创建`nginx`用户组(`-r`选项是创建一个系统用户组的意思)

[root@lightserver ~]$ groupadd -r nginx
  • 创建用户并加入到nginx系统用户组
> 添加新用户

> -r: 添加系统用户( 这里指将要被创建的系统用户`nginx`)

> -g: 指定要创建的用户所属组( 这里指添加到新系统用户`nginx`到`nginx`系统用户组 )

> -s: 新帐户的登录`shell`( `/sbin/nologin` 这里设置为将要被创建系统用户`nginx`不能用来登录系统 )

> -d: 新帐户的主目录( 这里指定将要被创建的系统用户`nginx`的家目录为 `/usr/local/nginx` )

> -M: 不要创建用户的主目录( 也就是说将要被创建的系统用户`nginx`不会在 `/home` 目录下创建 `nginx` 家目录 )

[root@lightserver ~]$ useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx -M nginx
创建相关目录
>`/var/tmp/nginx/`创建缓存目录

[root@lightserver ~]$ mkdir -pv /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}

> 赋予`nginx`用户权限

[root@lightserver ~]$ chown -R nginx:nginx /var/tmp/nginx/

>`/usr/local/nginx/`创建`logs`目录

[root@lightserver ~]$ mkdir -pv /usr/local/nginx/logs/

> 赋予`nginx`用户权限

[root@lightserver ~]$ chown -R nginx:nginx /usr/local/nginx/
安装依赖库
> 安装`gcc``gcc-c++``C/C++`语言编译环境

[root@lightserver ~]$  yum -y install gcc gcc-c++ autoconf automake make 
 
> `yum`安装`nginx`必须的依赖库

[root@lightserver ~]$  yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch
创建家目录存放软件包目录
> 创建目录存放软件包目录

[root@lightserver ~]$ mkdir soft && cd soft
下载解压源码包
> 官网下载`Nginx1.15.3``tar`包,然后解压到服务器上

[root@lightserver soft]$  wget http://nginx.org/download/nginx-1.15.3.tar.gz

[root@lightserver soft]$  tar -zxvf nginx-1.15.3.tar.gz
  • pcre: NginxRewrite功能
> 下载

[root@lightserver soft]$   wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz

> 解压

# tar -zxvf pcre-8.42.tar.gz

> 进入解压后的源码目录编译并安装

# cd pcre-8.42/

# ./configure

# make && make install

# cd ~/soft/

  • zlib: NginxGzip压缩功能
> 下载

[root@lightserver soft]$  wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz

> 解压

# tar -zxvf zlib-1.2.11.tar.gz

> 进入解压后的源码目录编译并安装

# cd zlib-1.2.11

# ./configure

# make && make install

# cd ~/soft/
  • openssl: nginx第三方模块—nginx-sticky-module的使用(基于cookie的会话保持)
> 下载

[root@lightserver soft]$  wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz

> 解压

# tar -zxvf openssl-1.1.1-pre8.tar.gz

> 进入解压后的源码目录编译并安装

# cd openssl-1.1.1

# ./configure

# make && make install

# cd ~/soft/

  • nginx-sticky-module
> 下载

[root@lightserver soft]$ wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz

> 解压

# tar -zxvf /master.tar.gz 

# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module/
nginx编译参数
> 进入`nginx`目录

[root@lightserver ~]$  cd ~/soft/nginx-1.15.3

> 编译安装
> 
> 编译时,复制一下代码,末尾不能有空格,不能有注释

[root@lightserver nginx-1.15.3]$  
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--add-module=../nginx-sticky-module \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1-pre8 \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"

编译选项说明

  • --prefix=PATH: 指定nginx的安装目录。默认 /usr/local/nginx
  • --sbin-path=PATH:设置nginx可执行文件的名称。默认情况下,文件指向 安装目录/sbin/nginx
  • --conf-path=PATH:设置nginx.conf配置文件的名称。nginx允许使用不同的配置文件启动,通过在命令行参数中指定它 。默认情况下,文件指向 安装目录/conf/nginx.conf
  • --pid-path=PATH:设置存储主进程ID文件nginx.pid的名称。默认情况下,文件指向 安装目录/logs/nginx.pid
  • --error-log-path=PATH:设置错误,警告和诊断文件的名称。默认情况下,文件指向 安装目录/logs/error.log
  • --http-log-path=PATH:设置HTTP服务器的请求日志文件的名称。默认情况下,文件指向 安装目录/logs/access.log
  • --lock-path=PATH:安装文件锁定,防止安装文件被别人利用,或自己误操作。
  • --user=nginx:指定程序运行时的非特权用户。安装完成后,可以随时在nginx.conf配置文件更改user。默认用户名为nobody
  • --group=nginx:指定程序运行时的非特权用户所在组名称。默认情况下,组名称设置为非root用户的名称。
  • --with-http_realip_module 启用ngx_http_realip_module支持(这个模块允许从请求标头更改客户端的IP地址值,默认为关)
  • --with-http_ssl_module :启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
  • --with-http_stub_status_module :启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
  • --with-http_gzip_static_module :启用ngx_http_gzip_module支持(该模块同–without-http_gzip_module功能一样)
  • --http-client-body-temp-path=PATH :设定http客户端请求临时文件路径
  • --http-proxy-temp-path=PATH:设定http代理临时文件路径
  • --http-fastcgi-temp-path=PATH:设定http fastcgi临时文件路径
  • --http-uwsgi-temp-path=PATH:设定http scgi临时文件路径
  • --with-pcre:设置pcre库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本8.4)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
  • --with-zlib=PATH:指定 zlib(版本1.2.11)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib
  • --with-http_ssl_module:使用https协议模块。默认情况下,该模块没有被构建。前提是openssl已安装
  • --add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)

注释版

./configure \
--prefix=/usr/local/nginx \                     [Nginx安装目录]
--sbin-path=/usr/sbin/nginx \.                  [Nginx的sbin目录]
--conf-path=/etc/nginx/nginx.conf \             [Nginx的配置文件]
--error-log-path=/var/log/nginx/error.log \     [Nginx的错误日志]
--http-log-path=/var/log/nginx/access.log \     [Nginx的访问日志]
--pid-path=/var/run/nginx.pid  \                [Nginx的进程ID]
--lock-path=/var/lock/nginx.lock \              [Nginx的进程锁]
--user=nginx \                                  [Nginx所属用户]
--group=nginx \                                 [Nginx所属用户组]
--with-http_ssl_module \                        [Nginx的ssl模块]
--with-http_v2_module \ 
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \                     [记录原始客户端的IP地址]
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \                [监控 Nginx 的当前状态]
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \                [Nginx的gzip压缩模块]
--with-http_perl_module \          
--with-pcre=../pcre-8.42 \                      [pcre的安装目录]
--with-zlib=../zlib-1.2.11 \                    [zlib的安装目录]
--with-openssl=../openssl-1.1.1-pre8 \          [openssl的安装目录]
--with-debug \                                  [允许DEBUG]
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \                                 [Nginx特有的stream模块]
--with-ld-opt="-Wl,-E"                          [gcc的编译优化]

配置过程大概需要5分钟左右

配置完后,编译安装
[root@lightserver ~]$  make && make install
配置Nginx
[root@lightserver ~]$  vim /etc/init.d/nginx

> 编写脚本文件

#! /bin/bash
#
# 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
# 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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock

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
    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
配置Nginx启动脚本
> 改变文件权限
[root@lightserver ~]$ chmod +x /etc/init.d/nginx

> 添加到系统服务
[root@lightserver.cn ~]$ chkconfig --add nginx

> 设置系统为开机启动
[root@lightserver ~]$ chkconfig nginx on
启动Nginx服务器
[root@lightserver ~]$ systemctl start nginx
查看Nginx服务启动状态
[root@lightserver ~]$ systemctl status nginx
修改Nginx配置文件
> 修改nginx配置文件

[root@lightserver ~]$ vi /etc/nginx/nginx.conf

# 运行用户
#user  nobody;
# 启动进程, 通常设置成和cpu的数据相等
worker_processes  1;

# 全局错误日志及PID文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

# 工作模式及连接数上限
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"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;

    gzip  on;
    gzip_disable "MSIE [1-6]";

    client_header_buffer_size   128k;
    large_client_header_buffers  4 128k;

    server {
        listen       80;
        server_name  www.nginx.dev;


        #charset koi8-r;

        access_log  logs/nginx.dev.access.log  main;

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

        #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   /data/www/html;
        }

    location ~ ^/(images|javascript|js|css|flash|media|static)/ {

        expires 30d;
    }

        # 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$ {
        #    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;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


    # 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;
    #    }
    #}

}
CentOS 添加开放TCP 80端口
> 加入开放端口到配置文件

[root@lightserver ~]$ firewall-cmd --zone=public --add-port=80/tcp --permanent

> 添加时区

--zone=public 

> 添加端口

--add-port=80/tcp
 
> 永久生效

--permanent 

> 加载防火墙新配置文件(`root`身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息. )

[root@lightserver.cn ~]$ firewall-cmd --reload
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值