nignx的编译安装

直接开始安装

1、安装编译环境
[root@localhost ~]# yum -y install gcc gcc-c++
2、安装pcre软件包(使nginx支持http rewrite模块)
[root@localhost ~]# yum install -y pcre pcre-devel
3、安装 openssl-devel(使 nginx 支持 ssl)
[root@localhhost ~]# yum install -y openssl openssl-devel 
4、安装zlib
[root@localhhost ~]# yum install -y zlib zlib-devel gd gd-devel
5、创建用户 nginx
[root@localhhost ~]# useradd -s /sbin/nologin nginx 
6、编译安装 nginx
1、下载安装包
[root@localhhost ~]# wget http://nginx.org/download/nginx-1.19.0.tar.gz
[root@localhhost ~]# tar -zxvf nginx-1.19.0.tar.gz
[root@localhhost ~]# cd nginx-1.19.0
2、编译配置
[root@localhhost nginx-1.19.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module 
    
[root@localhhost nginx-1.19.0]# make && make install
[root@localhhost nginx-1.19.0]# cd /usr/local/nginx/sbin
[root@localhhost sbin]# ./nginx        		# 启动Nginx
[root@localhhost sbin]# ./nginx -t      		# 验证配置文件是正确
[root@localhhost sbin]# ./nginx -s reload  	# 重启Nginx
[root@localhhost sbin]# ./nginx -s stop   	# 停止Nginx
[root@localhhost sbin]# ./nginx -v            # 查看是否安装成功
nginx version: nginx/1.19.0
[root@localhhost sbin]# netstat -ntlp | grep nginx # 查看是否启动
tcp   0    0 0.0.0.0:80     0.0.0.0:*     LISTEN    20949/nginx: master
3、配置 Nginx 命令和服务并开机启动
1、创建服务配置文件
[root@localhhost sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/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=/usr/local/nginx/sbin/nginx -S reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
2、添加执行权限
[root@localhhost sbin]# chmod +x /usr/lib/systemd/system/nginx.service
3、启动服务(先停止nginx服务)
[root@localhhost sbin]# systemctl daemon-reload
[root@localhhost sbin]# systemctl start nginx.service   # 启动
[root@localhhost sbin]# systemctl stop nginx.service    # 停止
[root@localhhost sbin]# systemctl reload nginx.service  # 修改配置后重新加载生效 
[root@localhhost sbin]# systemctl restart nginx.service # 重启
[root@localhhost sbin]# systemctl status nginx   # 查看服务是否启动
4、添加开机启动
[root@localhhost sbin]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
4、防火墙默认开启,加入放行端口80
[root@localhhost sbin]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhhost sbin]# firewall-cmd --reload
success
7、Nginx 编译参数
# 查看 nginx 安装的模块
[root@localhhost ~]# nginx -V

# 模块参数具体功能 
--with-cc-opt='-g -O2 -fPIE -fstack-protector'   # 设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' 
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' 

--prefix=/usr/share/nginx                        # 指向安装目录
--conf-path=/etc/nginx/nginx.conf                # 指定配置文件
--http-log-path=/var/log/nginx/access.log        # 指定访问日志
--error-log-path=/var/log/nginx/error.log        # 指定错误日志
--lock-path=/var/lock/nginx.lock                 # 指定lock文件
--pid-path=/run/nginx.pid                        # 指定pid文件

--http-client-body-temp-path=/var/lib/nginx/body    # 设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     # 设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy         # 设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi           # 设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         # 设定http uwsgi临时文件路径

--with-debug                                        # 启用debug日志
--with-pcre-jit                                     # 编译PCRE包含“just-in-time compilation”
--with-ipv6                                         # 启用ipv6支持
--with-http_ssl_module                              # 启用ssl支持
--with-http_stub_status_module                      # 获取nginx自上次启动以来的状态
--with-http_realip_module                 # 允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module           # 实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module               # 作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module                    # 增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module                  # 使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module                 # 它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module            # 在线实时压缩输出数据流
--with-http_image_filter_module           # 传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
--with-http_spdy_module                   # SPDY可以缩短网页的加载时间
--with-http_sub_module                    # 允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module                   # 过滤转换XML请求
--with-mail                               # 启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module                    # 启用ngx_mail_ssl_module支持启用外部模块支持
8、Nginx 配置文件
[root@localhhost ~]# vim /usr/local/nginx/conf/nginx.conf
# 全局参数设置 
user  nginx/www;			   # 运行进程的用户
worker_processes  1;           # 设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 
error_log /var/log/nginx/error.log warn;     # 指定错误日志(建议绝对路劲) 
worker_rlimit_nofile 102400;   # 设置一个nginx进程能打开的最大文件数 
pid        /var/run/nginx.pid; 
events {                       # 事件配置
    worker_connections  10240; # 设置一个进程的最大并发连接数
    use epoll;                 # 事件驱动类型
} 
# http 服务相关设置 
http {  
    log_format  main  'remote_addr - remote_user [time_local] "request" '
                      'status body_bytes_sent "$http_referer" '
                      '"http_user_agent" "http_x_forwarded_for"'; 
    log_format  test                  
    access_log  /var/log/nginx/access.log  test;    #设置访问日志的位置和格式 
    sendfile          on;      # 用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
    tcp_nopush        on;      # 减少网络报文段数量,当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
    tcp_nodelay       on;      # 提高I/O性能,确保数据尽快发送, 提高可数据传输效率                           
    gzip              on;      # 是否开启 gzip 压缩 
    keepalive_timeout  65;     # 设置长连接的超时时间,请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系统带来的性能损耗,类似于线程池,数据库连接池
    types_hash_max_size 2048;  # 影响散列表的冲突率。越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。越小,消耗的内存就越小,但散列key的冲突率可能上升
    include             /etc/nginx/mime.types;     # 关联mime类型,关联资源的媒体类型(不同的媒体类型的打开方式)
    default_type        application/octet-stream;  # 根据文件的后缀来匹配相应的MIME类型,并写入Response header,导致浏览器播放文件而不是下载
# 虚拟服务器的相关设置 
    server { 
        listen      80;                # 设置监听的端口 
        server_name  localhost;        # 设置绑定的主机名、域名或ip地址 
        charset utf8;                  # 设置编码字符 
        location / { 
            root  /var/www/nginx;           # 设置服务器默认网站的根目录位置 
            index  index.html index.htm index.php;    # 设置默认打开的文档 
            } 
        error_page  500 502 503 504  /50x.html; # 设置错误信息返回页面 
            location = /50x.html { 
            root  html;        # 这里的绝对位置是/var/www/nginx/html 
        } 
    } 
 }
9、检测 nginx 配置文件是否正确
[root@localhhost ~]# usr/local/nginx/sbin/nginx -t
10、启动 nginx 服务
[root@localhhost ~]# /usr/local/nginx/sbin/nginx
11、Nginx 命令控制
nginx -c /path/to/nginx.conf  	 # 以特定目录下的配置文件启动nginx:
nginx -s reload            	 	 # 修改配置后重新加载生效
nginx -s reopen   			 	 # 重新打开日志文件
nginx -s stop  				 	 # 快速停止nginx
nginx -s quit  				  	 # 完整有序的停止nginx
nginx -t    					 # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf  # 测试特定的nginx配置文件是否正确
12、脚本实现 nginx 开机自启
1、添加启动脚本
[root@localhhost ~]#  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:      /usr/local/nginx/conf/nginx.conf 
# config:      /etc/sysconfig/nginx 
# 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"
  
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  
lockfile=/var/lock/subsys/nginx
  
make_dirs() { 
  # make required directories 
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
  for opt in $options; do
      if [ `echo $opt | grep '.*-temp-path'` ]; then
          value=`echo $opt | cut -d "=" -f 2` 
          if [ ! -d "$value" ]; then
              # echo "creating" $value 
              mkdir -p $value && chown -R $user $value 
          fi
      fi
  done
} 
  
start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    make_dirs 
    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
2、添加权限
[root@localhhost ~]# chmod +x /etc/init.d/nginx
3、重载系统启动文件
[root@localhhost ~]# systemctl daemon-reload
4、设置开机自启
[root@localhhost ~]# systemct start nginx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值