一、说明

  操作系统:centos 7.6(全新机器)

  安装nginx版本:nginx 1.15.2

二、准备工作

2.1、安装一系列需要用到的工具

yum groupinstall "Development Tools" -y
yum install expat-devel openssl-devel pcre-devel -y

    

2.2、下载所需要的依赖:apr、apr-util、pcre、zlib、openssl

  apr的下载地址:http://mirrors.hust.edu.cn/apache/apr/,选择最新的版本下载即可

cd /usr/local/src
wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz
tar -zxf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make
make install

  

  apr-util的下载地址:http://mirrors.hust.edu.cn/apache/apr/,选择最新的版本下载即可

cd /usr/local/src
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
tar -zxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-util
make
make install

  

  pcre的下载地址:https://ftp.pcre.org/pub/pcre/,建议下载1.x版本,pcre2可能在编译nginx时出现问题

cd /usr/local/src
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar -zxf pcre-8.42.tar.gz
cd pcre-8.42
./configure --prefix=/usr/local/pcre
make
make install

  

  zlib的下载地址:https://www.zlib.net/,下载tar.gz压缩格式即可

cd /usr/local/src
wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make install

  

  openssl的下载地址:https://www.openssl.org/source/,下载1.x版本,2.x版本可能在安装中出现确实头文件。

cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -zxf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
./config --prefix=/usr/local/openssl
make
make install

  

三、下载Nginx、安装

  在将工具和依赖的软件都安装后,就可以开始安装nginx了。

  nginx的下载地址:http://nginx.org/download/,我这里选择1.15.2版本

cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar -zxf nginx-1.15.2.tar.gz
cd nginx-1.15.2 # 注意下面的--with-pcre、--with-zlib、--wite-openssl都是源码路径
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.42 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.1d make
make install  

  注意,--with-pcre、--with-openssl、--with-zlib所指定的路径都是对应软件源码解压之后的目录,并不是安装目录。

  执行完毕后,如果没有出现错误,nginx就安装好了。

  

四、查看nginx文件目录

  前面在安装nginx时,我是将nginx的安装目录指定为/usr/local/nginx

[root@centos /usr/local/nginx]# ls
conf html logs sbin

  conf目录存放nginx的相关配置文件;

  html目录存放网站的源码文件;

  logs存放日志文件;

  sbin包含nginx可执行脚本,用于启动nginx。

  

五、修改nginx配置

  nginx的配置文件为conf/nginx.conf。

  先创建www用户和www组,只创建www用户即可(会自动创建一个www组)

useradd www

  修改/usr/local/nginx/conf/nginx.conf文件,修改user为www,然后取消pid前面的注释即可。

user  www;
pid logs/nginx.pid;

  

六、启动Nginx

  nginx的启动程序在nginx的安装目录下的sbin目录下

[root@centos /]# /usr/local/nginx/sbin/nginx

  检查端口信息:

[root@centos /]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 10456 root 6u IPv4 47721 0t0 TCP *:http (LISTEN)
nginx 10457 www 6u IPv4 47721 0t0 TCP *:http (LISTEN)
nginx 10458 www 6u IPv4 47721 0t0 TCP *:http (LISTEN)

  可以看到,nginx默认绑定到80端口。

  如果防火墙放行了80端口,那么访问机器ip,可以看到下面的页面:

  

七、Nginx的相关操作命令

[root@centos /]# nginx -h
nginx version: nginx/1.15.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

  上面虽然有很多命令,一般使用最多的是-s选项:

  nginx -s stop   立即停止

  nginx -s quit    平滑停止进程

  nginx -s reopen  重新打开日志

  nginx -s reload   重新加载配置文件(热加载)

  注意,注意,启动nginx,只需要一个命令就是nginx,不需要加任何选项和任何参数。

  nginx -t  检测配置文件的语法是否有错误。

八、立即停止进程(stop)和平滑停止进程(quit)的区别

  可以用这种场景来理解,有一个请求,需要服务器端运行20秒才能返回给客户端结果。

  某一时刻,客户端发起了请求,隔了10秒,还没有获得响应结果(因为前面说了要20秒才能收到响应),所以还要等10秒。

  但是如果这个时候,

  1、使用nginx -s stop命令停止nginx服务器,那么,客户端会立即收到响应,这个响应不是用户希望收到的请求响应,而是服务器崩溃的响应。那么用户也就不用再等10秒来等待服务器的响应了,因为服务器已经关闭了。

  2、使用nginx -s quit命令停止nginx服务器,会发现当前的请求并不会立即终止,客户端可以看到服务器还在运行(注意这个时候,服务器已经quit了),10秒中之后(这个10秒加上之前等的10秒,刚好20秒),客户端收到了服务器的响应。这个响应不是服务器崩溃的响应,而是对之前请求进行的响应。但是,如果客户端此时再次发起一个请求,那么就会立即收到服务器错误的响应。

  所以,他们的区别是:是否立即终止当前正在执行的进程。立即停止(stop)就会立即停止,平滑停止(quit)不会立即停止正在进行的进程,而是等他执行完毕之后,再终止。

九、热加载

  首先,nginx的运行模式是 主master + 多worker 模式,master监听http请求,然后将请求交给空闲的worker去处理。

  1、没有新配置的时候,所有的worker都使用旧配置。

  2、当配置有更新的时候,如果某个worker还没有执行结束,那么他就继续执行,仍使用旧的配置。

  3、如果worker结束后,该worker不再继续服务,立即终止。

  4、新创建的worker使用新的配置文件。

  就这样,逐步将所有的旧worker都停止,创建新的worker使用新配置文件,达到热加载的目的。

10、配置文件

  具体如下(有注意点都写了)

user www www;

#设置工作进程数量(worker)
worker_processes auto; #保存nginx进程id的文件路径
pid /usr/local/nginx/logs/nginx.pid; #设置一个进程最多可以打开多少个文件
worker_rlimit_nofile 51200; events {
#使用epoll模型
use epoll; #每一个worker支持的连接数
worker_connections 51200; multi_accept on;
} #全局设置
http{
include mime.types;
default_type application/octet-stream; server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m; sendfile on;
tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k; gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; #是否开启访问日志,如果开启的话,需要设置日志文件路径、日志格式
#access_log off
#默认的日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" $http_x
#自定义日志格式
log_format my_format '$remote_addr $request'; #配置上游服务器
upstream imageServer {
server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;
} #配置虚拟主机,每一个server { }都是配置一个虚拟主机,所以可以配置多个server{ }
server {
#监听的端口
listen 80 default_server; #指定监听的域名
server_name www.ganlixin.cn ganlixin.cn; #指定根路径
root /var/www; #指定默认首页
index index.html index.htm index.php; #关闭访问日志,每一个虚拟主机的访问日志单独设置
#access_log off
#开启访问日志,使用默认的日志格式
#access_log /var/www/nginx_access.log main;
#开启访问日志,使用自定义的日志格式
access_log /var/www/nginx_access.log my_format; #错误日志路径以及等级
error_log /var/www/ganlixin_error.log crit; #错误页面
#error_page 404 /404.html; #使用location来定义文件路径,相当于Apache的DocumentRoot,location后面跟~表示后面的是正则表达式匹配
#配置php解析
location ~ .*\.php(.*)$ {
#解决无法获取PATH_INFO参数
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
} include proxy-pass-php.conf; #对静态资源的请求单独处理
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
#设置反向代理(静态资源请求另外一台机器),注意前缀http://
proxy_pass http://192.168.1.2:80;
proxy_set_header X_Forwarded_For $remote_addr; #设置资源过期时间
expires 30d;
} location ~ (.*)$ {
#URL重写
rewrite (.)$ /index.php$1;
}
location ~ /.well-known {
allow all;
} location ~ /\.{
deny all;
}
} #单独配置虚拟机的配置文件
include vhost/*.conf;
}

  

nginx 编译安装以及简单配置的更多相关文章

  1. Nginx的安装及简单配置

    Nginx安装 1.下载相关组件 yum install -y gcc gcc-c++                                   #安装C/C++编译器 yum -y ins ...

  2. Linux下nginx编译安装教程和编译参数详解

    这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # y ...

  3. Nginx编译安装第三方模块http_substitutions_filter_module2222

    Nginx编译安装第三方模块http_substitutions_filter_module Rming -- 阅读 安装 Http 编译 module filter nginx 模块 >> ...

  4. Nginx编译安装第三方模块http_substitutions_filter_module

    Nginx编译安装第三方模块http_substitutions_filter_module 分类:服务器技术  作者:rming  时间:-- . >>ngx_http_substitu ...

  5. nginx编译安装支持lua脚本

    一.准备编译环境 1.操作系统:CentOS7.6 2.安装编译所需安装包 yum install gcc pcre pcre-devel zlib zlib-devel openssl openss ...

  6. LNMP平台搭建之一:nginx编译安装

    参考博客:https://www.cnblogs.com/zhang-shijie/p/5294162.html   jack.zhang 一.环境说明 系统环境:centos6.5 [root@lo ...

  7. Nginx编译安装lua-nginx-module

    lua-nginx-module 模块可以将Lua的强大功能嵌入NGINX服务器. 下载Nginx源码 如果已安装Nginx,需要查看当前安装版本的编译参数: $ /usr/local/nginx/s ...

  8. [nginx]编译安装及安全优化

    nginx配置-最后整理版 nginx_upstream_check_module nginx-module-vts nginx打补丁 nginx编译安装 - 下载 cd /usr/local/src ...

  9. 【转】编译安装PHP并配置PHP-FPM

    1.前言上一篇讲述了如何编译安装MySQL,虽然可以通过yum install 或者rpm来安装,但是yum install和rpm安装有一个特点,就是有些参数是别人根据大众需求定制的,如果需要进行自 ...

随机推荐

  1. C语言经典例题(菜鸟教程100例)

    学习c语言基础,怎么能少了菜鸟教程上的100道例题呢,这里整理一下每道题的链接,希望大家能享受学习的乐趣 1,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 2,企业发放 ...

  2. LeetCode算法题-Sum of Two Integers(Java实现)

    这是悦乐书的第210次更新,第222篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第78题(顺位题号是371).计算两个整数a和b的总和,但不允许使用运算符+和 - .例 ...

  3. java操作elasticsearch实现query String

    1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...

  4. 创建两个SAP系统之间的RFC信任关系

    一种常见的场景是企业运行着多个SAP系统(ERP/SRM/CRM),用户希望在AA1系统中使用BB1系统的事务.如果直接使用RFC调用另一系统的事务的话,则会弹出登陆框,让用户再次输入帐号密码... ...

  5. 《Java大学教程》—第16章 二维数组

    多维(Multi-dimensional)数组维数由索引个数决定.常用的数组:一维(one-dimensional)数组.二维(two-dimensional)数组 16.2    创建二维数组索引从 ...

  6. 精度 Precision

    柏拉图认为,尽管世间万物是不完美的,但存在一种永恒不变的形式,这个形式是完美的,而生命的意义就是让这个世界尽可能的接近这个完美的形式. 怎么理解这句话,和我们今天讲的精度有什么关系.我们先举一个例子, ...

  7. 5.04-requests_cookies

    import requests # 请求数据url member_url = 'https://www.yaozh.com/member/' headers = { 'User-Agent': 'Mo ...

  8. eclipse中添加server后,启动server,访问项目时,端口是怎么选择的。

    1   eclipse中添加了tomcat 2 设置端口时,可以在图2.1修改 也可以在图2.2修改 3 点击server的publish按钮,会将图2.2的配置文件和server中添加的项目同步到实 ...

  9. 【编辑器】sublime 标题栏中文乱码问题

    首选项--------设置-用户中添加"dpi_scale": 1.0,如下图所示 作者:smile.轉角 QQ:493177502

  10. 四、Oracle 序列、常用函数、多表连接

    一.序列定义:是oracle数据库专门用来产生连续且自动增长的数字的对象创建语法:create sequence 序列名(sq_表名) nocache(无缓存) create sequence sq_ ...