阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx的安装及简单配置

134次阅读
没有评论

共计 5076 个字符,预计需要花费 13 分钟才能阅读完成。

Nginx 安装

1. 下载相关组件 yum install -y gcc gcc-c++                                  #安装 C /C++ 编译器 yum -y install gd-devel geoip-devel perl-ExtUtils-Embedwget http://125.39.35.133/files/40450000042A3380/zlib.net/zlib-1.2.8.tar.gzwget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gzwget https://www.openssl.org/source/openssl-1.0.2h.tar.gzwget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gzwget http://nginx.org/download/nginx-1.6.2.tar.gz 2. 顺次解压安装 zlib/pcre/openssl/nginx 并安装 zlib/pcre 这些库文件直接 ./configure &&make &&make install

tar xf openssl-1.0.2h.tar.gz -C /root
cd /root/openssl-1.0.2h
./config --prefix=/usr/local --openssldir=/usr/local/openssl
make
make install

 

 

 

 

cd /root/nginx-1.6.2./configure --add-module=/root/ngx_cache_purge-2.1 --prefix=/etc/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/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --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-mail --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'  –with-http_degradation_module –with-http_perl_module –with-ld-opt=-Wl,-E –with-mail_ssl_module –with-http_image_filter_module –with-http_geoip_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.2h
makemake install

    

 

 

 

 

 

 

错误信息
/usr/local/nginx/sbin/nginx: error whileloading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
cd /lib64
ln -s libpcre.so.0.0.libpcre.so.1
 
 
启动方法:
启动:nginx
停止:nginx -s stop
重载:nginx reload
 
3. 虚拟主机
基于域名的虚拟主机:
    server {
          listen 80;
          server_name www1.zyg.com;
          root /etc/nginx/www1;                            #创建目录
          index index.html;
    }
    server {
          listen 80;
          server_name www2.zyg.com;
          root /etc/nginx/www2;
          index index.html;
    }
基于 IP 的虚拟主机:
    server {
          listen 192.168.122.11:80;
          root /etc/nginx/www1;
          index index.html;
    }
    server {
          listen 2.2.2.1:80;
          root /etc/nginx/www2;
          index index.html;
    }
基于端口的虚拟主机:
    server {
          listen 8001;
          root /etc/nginx/www1;
          index index.html;
    }
    server {
          listen 8002;
          root /etc/nginx/www2;
          index index.html;
    }
 
4. 访问控制
    server {
        listen 8001;
        root www1;
        index index.html;
        auth_basic “test”;                #一个提示
        auth_basic_user_file /usr/local/nginx/passwd.db;
    }

    server {
        listen 8002;
        root www2;
        index index.html;
        allow 192.168.122.0/24;
        deny all;
    }

[root@node1 nginx]# htpasswd -c /usr/local/nginx/passwd.db power    #给 power 设置密码
 
5. 平滑升级
• 使用新的可执行程序替换旧的可执行程序, 对于编译安装的 Nginx, 可以将新版本编译安装到旧版本的 nginx 安装路径中. 替换之前, 最好备份一下旧的可执行程序
• 发送以下指令:
Kill USR2 < 旧版本的 nginx 主进程号 >
• 旧版本的主进程将重命名它的 pid 文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin), 然后执行新版本的 nginx 可执行程序, 依次启动新的主进程和新的工作进程.
• 此时, 新旧版本的 nginx 实例会同时运行, 共同处理输入的请求. 要逐步停止旧版本的 nginx 实例, 你必须发送 WINCH 信号给旧的主进程, 然后, 它的工作进程就将开始从容关闭:
   kill WINCH < 旧版本的 Nginx 主进程号 >
• 一段时间后, 旧的工作进程(worker process) 处理了所有已连接的请求后退出, 仅由新的工作进程来处理输入的请求了.
• 这时候, 我们可以决定是使用新版本, 还是 回复到旧的版本;
   Kill HUP < 旧的主进程号 >:Nginx 将在不重载配置文件的情况下启动它的工作进程;
   Kill QUIT < 新的主进程号 >: 正常关闭其它工作进程(woker process);
   如果此时报错,提示还有进程没有结束就用下面命令先关闭旧工作进程,再关闭主进程号:
   Kill TERM < 新的工作进程号 >: 强制退出工作进程;
   Kill < 新的主进程号或旧的主进程号 >: 如果因为某些原因新的工作进程不能退出, 则向其发送 kill 信号.
  新的主进程退出后, 旧的主进程会移除.oldbin 前缀, 恢复为他的.pid 文件, 这样, 一切就都恢复到升级之前了, 如果尝试升级成功, 而你也希望保留新的服务器时, 可发送 QUIT 信号给旧的主进程, 使其退出而只留下新的服务器运行
 
6. 日志切割
  mv /data1/logs/access.log /data1/logs/20160520.log
  kill -USR1 ‘cat /var/run/nginx/nginx.pid’  #这样的话就不用 ps 查找 Nginx 主进程号了.

  如果要让它每天定时切割日志, 可以编写 shell 脚本. 并且利用 crontab 来每天定时运行.

  #!/bin/bash
  #定义 Nginx 日志文件的存放路径
  logs_path = “/var/logs/nginx/”

  mkdir -p ${logs_path}$(date -d “yesterday” + “%Y”)/$(date -d “yesterday” +”%m”)/
  mv ${logs_path}access.log  ${logs_path}$(date -d “yesterday” +”%Y”)/$(date -d “yesterday”+”%m”)/access_$(date -d “yesterday” + “%Y%m%d”).log
  kill -USR1 ‘cat /var/run/nginx/nginx.pid’

  再配置 crontab,输入 crontab -e
  输入:
  00 00 * * * /bin/bash /usr/loca/webserver/nginx/sbin/cut_nginx_log.sh

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安���配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-05/131598.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5076字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中